-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy path2-retrieve_results.R
344 lines (274 loc) · 11.9 KB
/
2-retrieve_results.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#' Retrieve and process the stability results
library(dynbenchmark)
library(tidyverse)
experiment("07-stability")
metric_ids <- c("correlation", "him", "featureimp_wcor", "F1_branches")
##############################################################
### PART TWO: RETRIEVE RESULTS ###
##############################################################
# If you are the one who submitted the jobs, run:
benchmark_fetch_results(TRUE)
# qsub::rsync_remote(
# remote_src = FALSE,
# path_src = derived_file(remote = FALSE, experiment = "07-stability"),
# remote_dest = TRUE,
# path_dest = derived_file(remote = TRUE, experiment = "07-stability"),
# verbose = TRUE,
# exclude = "*/r2gridengine/*"
# )
# If you want to download the output from prism
# qsub::rsync_remote(
# remote_src = TRUE,
# path_src = derived_file(remote = TRUE, experiment = "07-stability"),
# remote_dest = FALSE,
# path_dest = derived_file(remote = FALSE, experiment = "07-stability"),
# verbose = TRUE,
# exclude = "*/r2gridengine/*"
# )
##############################################################
### SUBMIT PAIRWISE COMPARISON JOBS ###
##############################################################
pairwise_submit <- function(memory = "10G", max_wall_time = "12:00:00") {
requireNamespace("qsub")
# load stability dataset info
datasets <- read_rds(derived_file("datasets.rds", "07-stability"))
orig_dataset_ids <- unique(datasets$orig_dataset_id)
# determine output folders
local_output_folder <- derived_file("suite")
remote_output_folder <- derived_file("suite", remote = TRUE)
# find all finished jobs
metric_files <- list.files(local_output_folder, pattern = "output_metrics.rds", recursive = TRUE, full.names = TRUE)
# make sure the remote is rsynced with the host, otherwise this job will fail
walk(metric_files, function(metric_file) {
folder <- gsub("/output_metrics\\.rds$", "", metric_file)
name <- gsub(paste0(local_output_folder, "/"), "", folder, fixed = TRUE) %>% gsub("/", "_", .)
subdir <- gsub(derived_file(""), "", folder, fixed = TRUE)
pairwise_file <- derived_file(c(subdir, "/output_pairwise.rds"))
qsubhandle_file <- derived_file(c(subdir, "/qsubhandle_pairwise.rds"))
if (file.exists(pairwise_file)) {
cat(name, ": Already calculated\n", sep = "")
return()
}
if (file.exists(qsubhandle_file)) {
cat(name, ": Job still running\n", sep = "")
return()
}
if (!file.exists(pairwise_file) && !file.exists(qsubhandle_file)) {
tmp_path <- paste0(folder, "/r2gridengine_pairwise")
tmp_path_rem <- gsub(local_output_folder, remote_output_folder, tmp_path, fixed = TRUE)
cat(name, ": Submitting job\n", sep = "")
qsub_config <- qsub::override_qsub_config(
name = name,
memory = memory,
max_wall_time = max_wall_time,
wait = FALSE,
local_tmp_path = tmp_path,
remote_tmp_path = tmp_path_rem,
remove_tmp_folder = FALSE,
stop_on_error = FALSE
)
qsub_fun <- function(orig_dataset_id) {
experiment("07-stability")
metric_file <- derived_file(c(subdir, "/output_metrics.rds"))
models_file <- derived_file(c(subdir, "/output_models.rds"))
# check which datasets were generated from the original dataset
datasets <-
read_rds(derived_file("datasets.rds")) %>%
filter(orig_dataset_id == !!orig_dataset_id)
# load metrics and models
metrics <- read_rds(metric_file)
models <- read_rds(models_file)
# fetch method_id
method_id <- metrics$method_id[[1]]
# retain only relevant metrics and models
ix <- which(metrics$dataset_id %in% datasets$id)
metrics <- metrics %>% slice(ix)
models <- models[ix]
# load original dataset
orig_dataset <- load_datasets(ids = orig_dataset_id) %>% extract_row_to_list(1)
crossing(
i = seq_along(models),
j = seq_along(models)
) %>%
# filter(i != j) %>% # runs longer
filter(i + 1 == j) %>% # compare less models, for now
pmap_dfr(function(i, j) {
did_bs_i <- metrics$dataset_id[[i]]
did_bs_j <- metrics$dataset_id[[j]]
modeli <- models[[i]]
modelj <- models[[j]]
eval_out <- tryCatch({
if (is.null(modeli)) stop("model i is null")
if (is.null(modelj)) stop("model j is null")
dataseti <- datasets %>% inner_join(tibble(id = did_bs_i), by = "id") %>% pull(fun) %>% first() %>% invoke()
datasetj <- datasets %>% inner_join(tibble(id = did_bs_j), by = "id") %>% pull(fun) %>% first() %>% invoke()
# join cell ids and feature ids
cell_map <-
inner_join(
dataseti$cell_id_map %>% rename(cell_id_i = cell_id),
datasetj$cell_id_map %>% rename(cell_id_j = cell_id),
by = "old_id"
)
feature_map <-
inner_join(
dataseti$feature_id_map %>% rename(feature_id_i = feature_id),
datasetj$feature_id_map %>% rename(feature_id_j = feature_id),
by = "old_id"
)
# revert ids for modeli
modeli$milestone_percentages <-
modeli$milestone_percentages %>%
rename(cell_id_i = cell_id) %>%
inner_join(cell_map %>% select(cell_id = old_id, cell_id_i), by = "cell_id_i") %>%
select(-cell_id_i)
modeli$progressions <-
modeli$progressions %>%
rename(cell_id_i = cell_id) %>%
inner_join(cell_map %>% select(cell_id = old_id, cell_id_i), by = "cell_id_i") %>%
select(-cell_id_i)
modeli$cell_ids <- cell_map$old_id
modeli <- modeli %>% add_cell_waypoints()
# revert ids for modeli
modelj$milestone_percentages <-
modelj$milestone_percentages %>%
rename(cell_id_j = cell_id) %>%
inner_join(cell_map %>% select(cell_id = old_id, cell_id_j), by = "cell_id_j") %>%
select(-cell_id_j)
modelj$progressions <-
modelj$progressions %>%
rename(cell_id_j = cell_id) %>%
inner_join(cell_map %>% select(cell_id = old_id, cell_id_j), by = "cell_id_j") %>%
select(-cell_id_j)
modelj$cell_ids <- cell_map$old_id
modelj <- modelj %>% add_cell_waypoints()
# get expression
expr <- get_expression(orig_dataset)
expr <- expr[cell_map$old_id, feature_map$old_id]
# calculate metrics
dyneval::calculate_metrics(modeli, modelj, expression_source = expr, metrics = metric_ids)
}, error = function(e) {
dyneval::metrics %>% select(metric_id, worst) %>% filter(metric_id %in% !!metric_ids) %>% deframe() %>% t() %>% as_data_frame()
})
# calculate geom mean
eval_out$geom_mean <-
eval_out %>%
select(one_of(metric_ids)) %>%
dynutils::calculate_geometric_mean()
# add information and reorder columns
eval_out %>%
mutate(dataset_id = orig_dataset_id, method_id, indexi = i, indexj = j) %>%
select(method_id, dataset_id, indexi, indexj, everything())
})
}
qsub_handle <-
qsub::qsub_lapply(
X = orig_dataset_ids,
qsub_config = qsub_config,
qsub_environment = c("subdir", "metric_ids"),
qsub_packages = c("dynbenchmark", "tidyverse"),
FUN = qsub_fun
)
write_rds(qsub_handle, qsubhandle_file)
}
})
invisible()
}
# pairwise_submit(memory = "10G")
# pairwise_submit(memory = "20G") # rerun those that crashed with 10G
##############################################################
### FETCH PAIRWISE RESULTS ###
##############################################################
pairwise_fetch_results <- function(remote = NULL) {
requireNamespace("qsub")
local_output_folder <- derived_file("suite")
# find all 2nd level folders with individual tasks
handles <- list.files(local_output_folder, pattern = "qsubhandle_pairwise.rds", recursive = TRUE, full.names = TRUE)
# check for running job ids
running_job_ids <-
if (!is.null(remote)) {
qsub::qstat_remote(remote = remote) %>%
gsub("^ *([0-9]*).*", "\\1", .) %>%
unique() %>%
keep(~ . != "")
} else {
c()
}
# process each method separately
map(handles, function(handle) {
name <- handle %>% gsub(paste0(local_output_folder, "/"), "", ., fixed = TRUE) %>% gsub("/qsubhandle_pairwise.rds", "", ., fixed = TRUE)
output_pairwise_file <- gsub("qsubhandle_pairwise.rds", "output_pairwise.rds", handle, fixed = TRUE)
if (!file.exists(handle)) {
cat(name, ": No qsub file was found.\n", sep = "")
return(FALSE)
}
if (file.exists(output_pairwise_file)) {
cat(name, ": Output already present.\n", sep = "")
return(FALSE)
}
cat(name, ": Attempting to retrieve output from cluster. ", sep = "")
qsub_handle <- readr::read_rds(handle)
if (qsub_handle$job_id %in% running_job_ids) {
cat("Job is still running.\n")
return(FALSE)
}
# attempt to retrieve results; return NULL if job is still busy or has failed
output <- qsub::qsub_retrieve(
qsub_handle,
wait = FALSE
)
if (is.null(output)) {
qstat_out <-
tryCatch({
qsub::qstat_j(qsub_handle)
}, error = function(e) {
NULL
}, warning = function(w) {})
if (is.null(qstat_out)) {
cat("The job had finished but no output was found.\n")
} else {
cat("The job is still running.\n")
}
return(FALSE)
}
cat("Output found! Saving results.\n")
outputs <- bind_rows(output)
# save output
readr::write_rds(outputs, output_pairwise_file)
return(TRUE)
})
# return nothing
invisible()
}
pairwise_fetch_results(remote = TRUE)
##############################################################
### FETCH PAIRWISE RESULTS ###
##############################################################
pairwise_bind_results <- function() {
handles <- list.files(derived_file("suite"), pattern = "output_pairwise.rds", recursive = TRUE, full.names = TRUE)
map_dfr(handles, readr::read_rds)
}
df <- pairwise_bind_results() %>%
group_by(method_id, dataset_id) %>%
filter((all(is.na(time_correlation)) & n() == 1) | !is.na(time_correlation)) %>%
mutate_if(is.numeric, function(x) ifelse(!is.finite(x), 0, x)) %>%
ungroup()
df %>% group_by(method_id) %>% summarise(error = mean(is.na(time_him))) %>% filter(error > 0) %>% arrange(desc(error))
df %>% group_by(dataset_id) %>% summarise(error = mean(is.na(time_him))) %>% filter(error > 0) %>% arrange(desc(error))
##############################################################
### SAVE DATA ###
##############################################################
df_g <-
df %>%
select(method_id, dataset_id, one_of(c("geom_mean", metric_ids))) %>%
gather(metric, value, -method_id, -dataset_id)
summ <- df %>% group_by(method_id) %>% summarise_at(c("geom_mean", metric_ids), mean)
summ %>% arrange(desc(geom_mean)) %>% as.data.frame
g <-
ggplot(df_g) +
geom_histogram(aes(value, fill = metric), binwidth = .05) +
facet_wrap(method_id~metric, ncol = length(metric_ids) + 1, scales = "free_y") +
theme_bw() +
scale_fill_brewer(palette = "Dark2")
g
ggsave(result_file("score_histogram.pdf"), g, width = 15, height = nrow(summ) * 2.5, limitsize = FALSE)
write_rds(lst(df, summ), result_file("stability_results.rds"), compress = "xz")