Skip to content

Commit

Permalink
add memory usage capture & figure
Browse files Browse the repository at this point in the history
  • Loading branch information
HelenaLC committed Jan 14, 2022
1 parent 73a8bbe commit 88f4fff
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 23 deletions.
31 changes: 28 additions & 3 deletions Snakefile
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,15 @@ METHODS_BY_TYP = {

rts_con = json.load(open("meta/runtimes.json"))
res_rts = list()
res_mbs = list()
for refset,params in rts_con.items():
reftyp = params["type"]
methods = METHODS_BY_TYP[reftyp]
simsets = expand(
"{refset},{method}",
refset = refset,
method = methods)
# runtimes
res_rts += expand([
"outs/rts_{reftyp}-{simset},{ngs},x,{rep}.rds",
"outs/rts_{reftyp}-{simset},x,{ncs},{rep}.rds"],
Expand All @@ -93,6 +95,8 @@ for refset,params in rts_con.items():
ngs = params["n_genes"],
ncs = params["n_cells"],
rep = list(range(1, 6)))
# memory usage
res_mbs += [foo.replace("outs", "logs").replace("rds", "txt") for foo in res_rts]

# get target figures
FIGS_QC_REF = glob_wildcards("code/07-plot_qc_ref-{x}.R").x
Expand Down Expand Up @@ -216,8 +220,9 @@ rule all:
# runtimes
res_rts,
expand(
"plts/rts_{reftyp}.{ext}",
"plts/{plt}_{reftyp}.{ext}",
reftyp = REFTYPS,
plt = ["rts", "mbs"],
ext = ["rds", "pdf"]),
# figures
expand("figs/{fig}.pdf", fig = FIGS)
Expand Down Expand Up @@ -515,6 +520,7 @@ rule rts:
"code/04-sim_data-{method}.R"
output: "outs/rts_{reftyp}-{datset},{subset},{method},{ngs},{ncs},{rep}.rds"
log: "logs/rts_{reftyp}-{datset},{subset},{method},{ngs},{ncs},{rep}.Rout"
benchmark: "logs/rts_{reftyp}-{datset},{subset},{method},{ngs},{ncs},{rep}.txt"
shell: '''
{R} CMD BATCH --no-restore --no-save "--args wcs={wildcards}\
sce={input[1]} est={input[2]} sim={input[3]} res={output}" {input[0]} {log}'''
Expand Down Expand Up @@ -612,6 +618,10 @@ res_dr = expand([
def rts_by_reftyp(wildcards):
return [x for x in res_rts if "rts_" + wildcards.reftyp in x]

# memory usage
def mbs_by_reftyp(wildcards):
return [x for x in res_mbs if "rts_" + wildcards.reftyp in x]

# ------------------------------------------------------------------------------
# write out .rds objects of
# - quality control summaries
Expand All @@ -626,7 +636,8 @@ data = {
"stat_2d": res_stat2d,
"batch_res": res_batch,
"clust_res": res_clust,
"rts": res_rts}
"rts": res_rts,
"mbs": res_mbs}

rule write_fns:
priority: 90
Expand Down Expand Up @@ -674,6 +685,18 @@ rule plot_rts:
{R} CMD BATCH --no-restore --no-save "--args wcs={wildcards}\
fun={input[1]} res={params} rds={output[0]} pdf={output[1]}" {input[0]} {log}'''

rule plot_mbs:
priority: 89
input: "code/07-plot_memory.R",
"code/utils-plotting.R",
res = mbs_by_reftyp
params: lambda wc, input: ";".join(input.res)
output: expand("plts/mbs_{{reftyp}}.{ext}", ext = ["rds", "pdf"])
log: "logs/plot_mbs-{reftyp}.Rout"
shell: '''
{R} CMD BATCH --no-restore --no-save "--args wcs={wildcards}\
fun={input[1]} res={params} rds={output[0]} pdf={output[1]}" {input[0]} {log}'''

rule plot_dr:
priority: 89
input: "code/07-plot_dimred.R",
Expand All @@ -686,7 +709,6 @@ rule plot_dr:
{R} CMD BATCH --no-restore --no-save "--args wcs={wildcards}\
fun={input[1]} res={params} pdf={output}" {input[0]} {log}'''


rule plot_stat1d:
priority: 89
input: "code/07-plot_stat_1d-{fig}.R",
Expand Down Expand Up @@ -793,6 +815,9 @@ plts = {
"runtimes": expand(
"plts/rts_{reftyp}.rds",
reftyp = REFTYPS),
"memory": expand(
"plts/mbs_{reftyp}.rds",
reftyp = REFTYPS),
"scatters": expand(
"plts/stat_{dim}d-scatters.rds",
dim = ["1", "2"]),
Expand Down
74 changes: 74 additions & 0 deletions code/07-plot_memory.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# args <- list(
# pdf = "plts/memory.pdf",
# fun = "code/utils-plotting.R",
# res = list.files("logs", ".*\\.txt", full.names = TRUE))

source(args$fun)
res <- lapply(args$res, read.table, header = TRUE)
mbs <- do.call(rbind, res)[["max_rss"]]

# get metadata from output names
# rts_{reftyp}-{datset},{subset},
# {method},{ngs},{ncs},{rep}.rds
ss <- strsplit(basename(gsub("\\.txt", "", args$res)), ",")
reftyp <- gsub("rts_([a-z])-.*", "\\1", sapply(ss, .subset, 1))
refset <- paste(sep = ",",
gsub(".*-", "", sapply(ss, .subset, 1)),
sapply(ss, .subset, 2))
vars <- seq(3, 6)
names(vars) <- c("method", "ngs", "ncs", "rep")
vars <- lapply(vars, \(.) sapply(ss, .subset, .))

df <- data.frame(mbs, reftyp, refset, vars) %>%
pivot_longer(
values_to = "n",
names_to = "dim",
cols = c("ngs", "ncs")) %>%
filter(n != "x") %>%
group_by(dim) %>%
mutate(n = factor(n, sort(as.numeric(unique(n))))) %>%
ungroup() %>%
mutate(
dim = factor(dim,
levels = c("ngs", "ncs"),
labels = c("# genes", "# cells")),
method = droplevels(factor(method, names(.methods_pal)))) %>%
group_by(method, dim, n) %>%
summarise_at("mbs", mean)

pal <- .methods_pal[levels(df$method)]
lab <- parse(text = paste(
sep = "~",
sprintf("bold(%s)", LETTERS),
gsub("\\s", "~", names(pal))))
anno <- mutate(df, letter = LETTERS[
match(method, levels(method))])

plt <- ggplot(df, aes(factor(n), mbs,
col = method, fill = method)) +
facet_grid(~ dim, scales = "free") +
geom_bar(
width = 0.9,
stat = "identity",
position = "dodge") +
geom_text(data = anno,
aes(label = letter, y = -250),
size = 1.5, color = "black",
position = position_dodge(0.9)) +
scale_fill_manual(values = pal, labels = lab) +
scale_color_manual(values = pal, labels = lab) +
xlab(NULL) + scale_y_continuous(
"memory usage (MBs)",
limits = c(-500, 4e3),
expand = c(0, 0))

thm <- theme(
axis.ticks.x = element_blank(),
legend.title = element_blank(),
legend.text = element_text(hjust = 0),
panel.grid.major.y = element_line(color = "grey"))

fig <- .prettify(plt, thm)

saveRDS(fig, args$rds)
ggsave(args$pdf, fig, width = 18, height = 9, units = "cm")
16 changes: 1 addition & 15 deletions code/07-plot_runtimes.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,6 @@ anno <- df %>%
group_by(step, dim) %>%
mutate(letter = LETTERS[match(method, levels(method))])

plt <- ggplot(df, aes(factor(n), t,
col = method, fill = method)) +
facet_grid(step ~ dim, scales = "free") +
geom_point() +
geom_path(aes(group = method)) +
geom_label_repel(
data = anno, aes(label = letter),
col = "white", segment.colour = "grey", size = 1.5) +
scale_fill_manual(values = pal, labels = lab) +
scale_color_manual(values = pal, labels = lab) +
scale_x_reordered(NULL) +
scale_y_log10("runtime(s)", expand = expansion(mult = 0.1))

plt <- ggplot(df, aes(factor(n), t,
col = method, fill = method)) +
facet_grid(step ~ dim, scales = "free") +
Expand All @@ -83,8 +70,7 @@ plt <- ggplot(df, aes(factor(n), t,
position = position_dodge(0.9)) +
scale_fill_manual(values = pal, labels = lab) +
scale_color_manual(values = pal, labels = lab) +
scale_x_reordered(NULL) +
scale_y_log10("runtime(s)",
xlab(NULL) + scale_y_log10("runtime (s)",
expand = expansion(mult = 0.1))

thm <- theme(
Expand Down
24 changes: 19 additions & 5 deletions code/08-fig_heatmaps.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
# pdf = "figs/heatmaps.pdf",
# uts = "code/utils-plotting.R",
# rds = list.files("plts", "stat_(1|2)d_by_reftyp-heatmap.*ks2?\\.rds", full.names = TRUE))

source(args$uts)
ps <- lapply(fns <- args$rds, readRDS)
#
# source(args$uts)
# ps <- lapply(fns <- args$rds, readRDS)
ps <- ps0

# re-order by dimension & type
dim <- gsub(".*(1|2)d.*", "\\1", fns)
Expand Down Expand Up @@ -86,7 +87,19 @@ hs <- c(
1.5 + length(unique(ps[[5]]$data$metrics)))
ws <- sapply(ps[1:3], \(.) nlevels(.$data$method))

fig <- wrap_plots(ps,
x <- c("gene-level" = "red", "cell-level" = "blue", "global" = "green3")
ps[[1]] <- ps[[1]] +
geom_point(
inherit.aes = FALSE,
data = data.frame(x),
aes_string(
ps[[1]]$data$method[1],
ps[[1]]$data$metric[1],
col = "x"), alpha = 0) +
scale_color_manual("type of\nsummary", values = x) +
guides(color = guide_legend(order = 1, override.aes = list(alpha = 1)))

fig <- wrap_plots(ps,
nrow = 2, heights = hs, widths = ws) +
plot_layout(guides = "collect") +
plot_annotation(tag_levels = "a") &
Expand All @@ -96,4 +109,5 @@ fig <- wrap_plots(ps,
plot.margin = margin(l = 2, unit = "mm"),
plot.tag = element_text(face = "bold", size = 9))

ggsave(args$pdf, fig, width = 16, height = 12, units = "cm")
fig
#ggsave(args$pdf, fig, width = 16, height = 12, units = "cm")
21 changes: 21 additions & 0 deletions code/08-fig_memory.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# args <- list(
# rds = list.files("plts", "mbs.*\\.rds", full.names = TRUE),
# pdf = "figs/memory.pdf",
# uts = "code/utils-plotting.R")

source(args$uts)

ps <- lapply(args$rds, readRDS)

pat <- paste0("_", c("n", "b", "k"), ".rds")
ps <- ps[sapply(pat, grep, args$rds)]

fig <- wrap_plots(ps, ncol = 1) +
plot_annotation(tag_levels = "a") &
theme(
plot.margin = margin(),
panel.spacing = unit(1, "mm"),
legend.title = element_blank(),
plot.tag = element_text(size = 9, face = "bold"))

ggsave(args$pdf, fig, width = 18, height = 16, units = "cm")

0 comments on commit 88f4fff

Please sign in to comment.