-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgraveyard.R
508 lines (427 loc) · 16.3 KB
/
graveyard.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
##' Aggregation wrapper, for simulations.
##'
##' @param blocktype block type
##' @param datatype data type
##' @param numclust number of clusters
##' @param outputdir output directory, where the destination folder exists.
##'
##' @export
cv_summary_sim <- function(nsim = 100,
blocktype = 2, datatype = 80, numclust = 2, cv_gridsize = 7,
nrep = 5,
outputdir = "~/Dropbox/research/usc/hpc-output",
datadir = "~/repos/cruisedat/export",
mc.cores = 1,
plotonly = FALSE,
plot_all_models = FALSE){
## Form the destin folder
destin = file.path(outputdir,
paste0("blockcv-", blocktype, "-", datatype, "-", numclust))
## If the folder called "summary" doesn't exist, create it.
if(!dir.exists(file.path(destin, "summary"))){
dir.create(file.path(destin, "summary"))
}
if(!plotonly){
## Get |nsim| lists, each containing gridsize^2 best replicates.
print("Getting all gridsize^2 best replicates, from nsim simulations.")
start.time = Sys.time()
reslists = mclapply(1:nsim, function(isim){
print_progress(isim, nsim, start.time=start.time)
tryCatch({
reslist = cv_aggregate_res(cv_gridsize = cv_gridsize,
nrep = nrep,
sim = TRUE, isim = isim,
destin = destin)
return(reslist)
}, error = function(e){ return(NULL) })
}, mc.cores = mc.cores)
save(reslists, file=file.path(destin, "summary", "reslists.Rdata"))
cat(fill=TRUE)
print('Saved results to reslist.Rdata')
## Get the |min.inds|.
print("Getting all best CV results, from nsim simulations.")
start.time = Sys.time()
cv_info_list = mclapply(1:nsim, function(isim){
tryCatch({
print_progress(isim, nsim, start.time=start.time)
obj = cv_aggregate(destin, cv_gridsize = cv_gridsize, nfold = nfold, nrep = nrep, sim = TRUE, isim = isim)##, save=FALSE)
ialpha = obj$min.inds[1]
ibeta = obj$min.inds[2]
cvscore = obj$cvscore.mat[ialpha, ibeta]
return(c(isim = isim, ialpha = ialpha, ibeta = ibeta, cvscore = cvscore))
}, error=function(e){ return(NULL) })
}, mc.cores = mc.cores)
save(cv_info_list, file=file.path(destin, "summary", "cv_info_list.Rdata"))
cv_info_mat = do.call(rbind, cv_info_list)
save(cv_info_mat, file=file.path(destin, "summary", "cv_info_mat.Rdata"))
cat(fill = TRUE)
print('Saved results to cv_info_list.Rdata and cv_info_mat.Rdata')
## Get bestres of each of the nsim simulations.
bestreslist = list()
for(isim in 1:nsim){
min.inds = cv_info_mat[isim, c("ialpha", "ibeta")]
if(is.null(reslists[[isim]])) next
reslist = reslists[[isim]]
bestreslist[[isim]] = reslist[[paste0(min.inds[1], "-", min.inds[2])]]
}
save(bestreslist, file=file.path(destin, "summary", "bestreslist.Rdata"))
print(bestreslist)
print('Saved results to bestreslist.Rdata')
} else {
## Load already existing summaries.
load(file=file.path(destin, "summary", "bestreslist.Rdata"))
load(file=file.path(destin, "summary", "reslists.Rdata"))
load(file=file.path(destin, "summary", "cv_info_mat.Rdata"))
}
## Making a plot of /all/ models
if(plot_all_models){
if(datatype!=9){
obj = generate_data_1d_pseudoreal(datadir = datadir,
nt = 200,
beta_par = 0.3,
p = 10,
bin = TRUE,
dat.gridsize = 40)##"~/repos/cruisedat/export")
ylist = obj$ylist
countslist = obj$countslist
} else {
obj = generate_data_1d_pseudoreal_from_cv(datadir = datadir)##"~/repos/cruisedat/export",
ylist = obj$ylist
countslist = obj$countslist
}
print("Making all model plots.")
start.time = Sys.time()
mclapply(1:nsim, function(isim){
print_progress(isim, nsim, start.time=start.time)
reslist = reslists[[isim]]
min.inds = cv_info_mat[isim, c("ialpha", "ibeta")]
plotname = paste0("sim-", isim, "-", blocktype, "-", datatype, "-", numclust, "-allmodels.png")
grDevices::png(file.path(destin, "summary", plotname), width = 3000, height = 2000)
graphics::par(mfrow = c(cv_gridsize, cv_gridsize))
for(ialpha in 1:cv_gridsize){
for(ibeta in 1:cv_gridsize){
bestres = reslist[[paste0(ialpha, "-", ibeta)]]
scale = !is.null(countslist)
plot_1d(ylist = ylist, res = bestres,
countslist = countslist, scale = scale)
if(all(c(ialpha, ibeta) == min.inds)) graphics::box(lwd = 10, col = 'blue')
}
}
grDevices::graphics.off()
print(paste0("Plot made in ", file.path(destin, "summary", plotname)))
}, mc.cores = mc.cores)
cat(fill=TRUE)
}
}
##' Helper to aggregate parallelized CV results and obtain degrees of freedom
##' (DF) estimate, saved in |destin|.
##'
##' @inheritParams
##'
##' @return Matrix containing estimated degrees of freedom.
cv_aggregate_df <- function(destin){
load(file.path(destin, "meta.Rdata"))
df.array = obj.array = df.alpha.array = df.beta.array = array(NA, dim=c(cv_gridsize, cv_gridsize, nrep))
df.mat = df.alpha.mat = df.beta.mat = matrix(NA, ncol=cv_gridsize, nrow=cv_gridsize)
for(ialpha in 1:cv_gridsize){
for(ibeta in 1:cv_gridsize){
## Objective value
obj = rep(NA, nrep)
df = df.alpha = df.beta = rep(NA, nrep)
for(irep in 1:nrep){
tryCatch({
## Load fitted result
filename = paste0(ialpha, "-", ibeta, "-", irep, "-fit.Rdata")
load(file.path(destin, filename))
## Calculate DF
df[irep] = do.call(sum, lapply(res$beta, function(mybeta){
sum(mybeta[-1,]!=0)})) + sum(res$alpha[,-1]!=0)
df.alpha[irep] = sum(res$alpha[,-1]!=0)
df.beta[irep] = do.call(sum, lapply(res$beta, function(mybeta){
sum(mybeta[-1,]!=0)}))
## Also calculate objective function
objectives = res$objectives
obj[irep] = objectives[length(objectives)]
}, error = function(e){})
}
df.array[ialpha, ibeta, ] = df
obj.array[ialpha, ibeta,] = obj
df.alpha.array[ialpha, ibeta, ] = df.alpha
df.beta.array[ialpha, ibeta, ] = df.beta
## Calculate the df of the best model
if(!all(is.na(obj))){
## df.mat[ialpha, ibeta] = df[which.max(obj, na.rm=TRUE)]
min.df = df[which(obj == min(obj, na.rm = TRUE))]
df.mat[ialpha, ibeta] = min.df[1] ## RARELY there are duplicates.. (especially when bootstrap is done)
}
}
}
## Assign to new names
mat = df.mat
alpha.array = df.alpha.array
beta.array = df.beta.array
## return(df.mat)
out = list(mat = mat,
alpha.array = alpha.array,
beta.array = beta.array,
df.array = df.array,
obj.array = obj.array)
return(out)
}
##' Temporary plotter.
##'
plot_ylist_other <- function(ylist, countslist, maxcount, col,
main = "", cex = 1,
ylim = NULL,
add = FALSE,
bg = NULL){
stopifnot(ncol(ylist[[1]]) == 1)
TT = length(ylist)
if(is.null(ylim)) ylim = range(unlist(ylist))
if(!add){
if(!is.null(bg)) par(bg = bg)
matplot(NA,
type = 'l',
lty = 1,
lwd = .1,
ylim = ylim,
xlim = c(1, TT),
ylab = "",
xlab = "",
axes = FALSE)
## Add title
title(main = main)
}
## Handle when countslist is NULL
## assertthat::assert_that(!is.null(countslist))
mx = max(unlist(countslist))
if(is.null(countslist)) countslist = lapply(ylist, function(y) rep(1, nrow(y)))
cols = lapply(countslist, function(counts){
ct = counts / maxcount
return(sapply(ct, function(one_ct) col %>% adjustcolor(alpha.f = one_ct)))
## return(rgb(0, 0, 0, ct))
})
pch = 15
## Visualize the data
for(tt in 1:TT){
y = ylist[[tt]]
col = cols[[tt]]
points(x = rep(tt, length(y)),
y = y, col = col,
pch = pch, cex = cex)
}
}
##' A wrapper, to get objective value at a particular time or vector of times
##' \code{times}.
##'
##' @param times Time points of interest (single integer value, or integer vector).
##' @param ... Rest of arguments to \code{objective()}.
##'
##' @return Objective value
objective_subset <- function(times, ...){
## Subset things to pass to objective()
args = list(...)
args$mu = (args$mu)[times,,,drop=FALSE]
args$prob = (args$prob)[times,,drop=FALSE]
args$ylist = (args$ylist)[times]
args$countslist = (args$countslist)[times]
stopifnot(all(sapply(args$ylist, nrow) == sapply(args$countslist, length)))
## Call the problem
return(do.call(objective, args))
}
##' Bootstrapping residuals using a coin-flip assignment of cytogram particles.
##'
##' @param ylist_orig Original data, containing the actual particles.
##' @param res A \code{flowmix} class object.
##' @param countslist Counts.
##' @param verbose Print progress if \code{TRUE}. Defaults to \code{FALSE}.
##'
##' @return \code{ylist_bootstrapped} is a dataset that is exactly the same size
##' as \code{ylist_orig}, but with the particles created by bootstrapping
##' residuals (after having pooled all of them, across all time points).
##'
##' @examples
##' \dontrun{
##' ## Generate data and fit model
##' set.seed(0)
##' dat1 = generate_data_generic(dimdat = 2, prob1 = 1/8, nt=2000)
##' ylist = dat1$ylist
##' X = dat1$X
##' res = flowmix(ylist, X, numclust=4, niter=300,
##' mean_lambda = 5E-3, prob_lambda = 5E-3)
##'
##' ## Now, create the bootstrapped datasets.
##' ylists <- lapply(1:nsim, function(isim){
##' new_ylist = bootstrap(ylist, res)$ylist
##' })
##'
##' ## Make a list of bootstrapped models.
##' bootreslist <- lapply(1:nboot, function(iboot){
##' ylist = ylists[[iboot]]
##' res = flowmix(ylist, X, numclust=4, niter=300,
##' mean_lambda = 5E-3, prob_lambda = 5E-3)
##' })
##' }
##'
##'
##' @export
##'
bootstrap <- function(ylist, res, countslist = NULL, verbose = FALSE){
## Setup
numclust = res$numclust
TT = length(ylist)
dimdat = ylist %>% .[[1]] %>% ncol()
## Conduct the E-step once to calculate responsibilities
resp <- Estep(res$mn, res$sigma, res$prob, ylist = ylist,
numclust = res$numclust, first_iter = TRUE)
## Do a coin-flip draw of membership
drawslist = draw_membership(resp)
rm(resp)
## Draw residuals
residuals = get_residuals(ylist, res, drawslist)
## Pool all the residuals
residuals_pooled = lapply(1:res$numclust, function(iclust){
do.call(rbind, lapply(1:TT, function(tt) residuals[[tt]][[iclust]]))
})
## NEW: Obtain and pool all the biomasses (UGHH)
if(!is.null(countslist)){
## Equivalent get_residuals()
index_by_clust <- get_index_by_clust(drawslist)
counts_pooled = lapply(1:res$numclust, function(iclust){
do.call(c, lapply(1:TT, function(tt){
counts = countslist[[tt]]
index = index_by_clust[[tt]][[iclust]]
return(counts[index])
}))
})
## Make sure the number of pooled counts are the same as the number of
## pooled residuals
assertthat::assert_that(all(sapply(counts_pooled, length) == sapply(residuals_pooled, nrow)))
}
## Get the number of coin-flipped particles for each cluster
ntklist = sapply(residuals, function(resids) resids %>% sapply(., nrow)) %>% t()
## Different number of clusters
## ntlist = ntklist %>% apply(., 1, sum)
## ((1/ntlist) * ntklist ) %>% matplot(type='l')
## (ntklist ) %>% matplot(type='l')
## plot(ntlist, type='l')
## ntlist2 = ylist %>% sapply(., nrow)
## Continue here!!! I think it's correct but out of memory... I should just separate out..
new_y_and_counts_list = list()
## <- lapply(1:TT, function(tt){
for(tt in 1:TT){
if(verbose) print_progress(tt, TT, fill = TRUE)
new_y_by_clust <- lapply(1:numclust, function(iclust){
if(verbose) print_progress(iclust, numclust, fill = TRUE)
## How many particles in this cluster to pick?
ntk = ntklist[tt, iclust]
if(ntk == 0) return(rep(NA,dimdat) %>% rbind() %>% .[-1,])
## Draw the new y's and multiplicities (counts)
bootrows = sample(1:sum(ntklist[,iclust]), ntk, replace = TRUE)
resampled_resids <- residuals_pooled[[iclust]][bootrows,,drop=FALSE]
new_y <- resampled_resids %>% sweep(., 2, res$mn[tt,,iclust], "+")
## If there are counts, include
if(!is.null(countslist)){
resampled_counts <- counts_pooled[[iclust]][bootrows] ## DO SOMETHING ABOUT THIS
assertthat::assert_that(length(resampled_counts) == nrow(new_y))
return(cbind(new_y, counts = resampled_counts))
} else {
return(new_y)
}
})
## return(new_y_by_clust)
new_y_and_counts_list[[tt]] = new_y_by_clust
}
## Return ylist and countslist separately
if(!is.null(countslist)){
new_ylist <- new_y_and_counts_list %>% lapply(., function(a){
X = do.call(rbind, a) %>% as_tibble()
return(X %>% select(-counts) %>% as.matrix())
})
new_counts <- new_y_and_counts_list %>%
lapply(., function(a){ do.call(rbind, a)[,"counts", drop=TRUE] %>% unname() })
} else {
new_ylist <- new_y_and_counts_list %>% lapply(., function(a){ do.call(rbind, a)})
new_counts = NULL
}
## saveRDS(list(ylist = new_ylist,
## X = res$X,
## ntklist = ntklist,
## drawslist = drawslist,
## residuals = residuals),
## file = file.path("~/Desktop/bootstrap.RDS"))
return(list(ylist = new_ylist,
countslist = new_counts,
X = res$X,
ntklist = ntklist,
drawslist = drawslist,
residuals = residuals))
}
##' Drawing memberships by coinflip using the "popular vote" i.e. pick
##' membership as cluster with highest responsibility..
##'
##' @param resp List of responsibilities.
##'
##' @export
draw_membership_popular_vote <-function(resp){
TT = length(resp)
drawslist = lapply(1:TT, function(tt){
draws = resp[[tt]] %>% apply(., 1, function(p){
P = rep(0, length(p))
P[which.max(p)] = 1
P
}) %>% t()
})
return(drawslist)
}
##' From subsampling bootstrap results (summary files), produce a set of confidence intervals.
##'
##' @param outputdir Contains files named "summary-(isim).RDS".
##' @param origres Model estimated from the entire dataset.
##' @param ylist_particle Original particle-level cytogram data.
##' @param X Accompanying covariate data.
##'
get_simulated_models <- function(nsim, outputdir, origres, ylist_particle, X, iboot=NULL){
## Sample settings
## ## 1. Load the original, particle-level data.
## obj = readRDS("~/repos/cruisedat/export/MGL1704-hourly-paper-1d-diam-not-binned.RDS")
## ylist_particle = obj$ylist
## X = obj$X
## outputdir = "~/Dropbox/research/usc/hpc-output/subsample-b-148/subsample-summaries"
## orig_cvres = cv_summary(destin = file.path("~/Dropbox/research/usc/hpc-output/blockcv-2-76-5"))
## origres = orig_cvres$bestres
## nsim = 50
## get_frequency(nsim, outputdir, orig_cvres, ylist_particle, X, beta = 0.5)
## End of temporary
## Setup
numclust = origres$numclust
## ## For back-compatibility
## origres$prob = origres$pie
## class(origres) = "flowmix"
## ## TODO: Try to recover nsim directly rom this directory
## list.files(outputdir) %>% lapply(., grepl, ...)
######################
### Frequencies #####
######################
newres_list = list()
start.time = Sys.time()
for(isim in 1:nsim){
print_progress(isim, nsim, start.time = start.time)
## Load the data.
if(!is.null(iboot)){
resfile = file.path(outputdir, paste0("summary-", iboot, "-", isim, ".RDS"))
} else {
resfile = file.path(outputdir, paste0("summary-", isim, ".RDS"))
}
if(!file.exists(resfile)) next
cvres = readRDS(file = resfile)
## Respon
newres = predict(cvres$bestres, newx = X)
class(newres) = "flowmix"
## Reorder the new res.
newres = newres %>% reorder_kl(origres, ylist_particle, fac = 100, verbose = FALSE)
## Return the results
newres_list[[isim]] = newres
}
return(newres_list)
}