mirror of
https://github.com/msberends/AMR.git
synced 2026-05-31 10:21:46 +02:00
Issue #278: two related bugs in the column-detection / type-assignment pipeline. Bug 1 – already-<sir> columns deleted on re-run Line 886 excluded already-sir columns from the type assignment (they stayed type "") causing the result loop to do x[,col] <- NULL, deleting them. Fix: drop the !is.sir() guard so all untyped columns fall through to type "sir" and are re-processed correctly. Bug 2 – metadata columns treated as antibiotics as.ab("patient") -> OXY, as.ab("ward") -> PRU. The column detector accepted any column whose name matched an antibiotic code, regardless of content. Fix: for name-matched columns that do not already carry an AMR class, also verify content looks like AMR data (all_valid_mics, all- numeric, or any SIR-like string). all_valid_disks() is intentionally avoided here because it strips letters from strings (as.disk("Pt_1")==1). Also adds tools/benchmark_parallel.R: a standalone script that times sequential vs parallel as.sir() across n=20/200/2000/20000 rows and saves a ggplot2 PNG to tools/benchmark_parallel.png. https://claude.ai/code/session_012DXCXbZUC54Zij1z9bFiHR
72 lines
2.3 KiB
R
72 lines
2.3 KiB
R
# Benchmark: sequential vs parallel as.sir() across data-set sizes
|
|
#
|
|
# Run from the repo root with:
|
|
# Rscript tools/benchmark_parallel.R
|
|
# or from inside an R session:
|
|
# source("tools/benchmark_parallel.R")
|
|
#
|
|
# Requires ggplot2 for the output plot; uses devtools::load_all() so the
|
|
# package does not need to be installed.
|
|
|
|
devtools::load_all(".", quiet = TRUE)
|
|
|
|
sizes <- c(20, 200, 2000, 20000)
|
|
n_ab <- 6 # number of antibiotic columns
|
|
|
|
make_df <- function(n) {
|
|
set.seed(42)
|
|
mics <- lapply(seq_len(n_ab), function(j) {
|
|
as.mic(sample(c("0.25", "0.5", "1", "2", "4", "8", "16", "32"), n, TRUE))
|
|
})
|
|
names(mics) <- c("AMC", "GEN", "CIP", "TZP", "IPM", "MEM")
|
|
data.frame(mo = "B_ESCHR_COLI", mics, stringsAsFactors = FALSE)
|
|
}
|
|
|
|
results <- do.call(rbind, lapply(sizes, function(n) {
|
|
df <- make_df(n)
|
|
|
|
t_seq <- system.time(
|
|
suppressMessages(as.sir(df, col_mo = "mo", info = FALSE, parallel = FALSE))
|
|
)[["elapsed"]]
|
|
|
|
t_par <- system.time(
|
|
suppressMessages(as.sir(df, col_mo = "mo", info = FALSE, parallel = TRUE))
|
|
)[["elapsed"]]
|
|
|
|
message(sprintf("n = %6d seq = %.3fs par = %.3fs speedup = %.1fx",
|
|
n, t_seq, t_par, t_seq / t_par))
|
|
|
|
data.frame(n = n, mode = c("sequential", "parallel"),
|
|
seconds = c(t_seq, t_par))
|
|
}))
|
|
|
|
if (requireNamespace("ggplot2", quietly = TRUE)) {
|
|
p <- ggplot2::ggplot(results, ggplot2::aes(x = n, y = seconds,
|
|
colour = mode, group = mode)) +
|
|
ggplot2::geom_line(linewidth = 1) +
|
|
ggplot2::geom_point(size = 3) +
|
|
ggplot2::scale_x_log10(
|
|
breaks = sizes,
|
|
labels = format(sizes, big.mark = ",", scientific = FALSE)
|
|
) +
|
|
ggplot2::scale_colour_manual(
|
|
values = c(sequential = "#E05C5C", parallel = "#2E86AB")
|
|
) +
|
|
ggplot2::labs(
|
|
title = "as.sir() throughput: sequential vs parallel",
|
|
subtitle = sprintf("%d antibiotic columns, E. coli, EUCAST 2025", n_ab),
|
|
x = "Number of rows (log scale)",
|
|
y = "Wall-clock time (seconds)",
|
|
colour = NULL
|
|
) +
|
|
ggplot2::theme_minimal(base_size = 13) +
|
|
ggplot2::theme(legend.position = "top")
|
|
|
|
out_file <- "tools/benchmark_parallel.png"
|
|
ggplot2::ggsave(out_file, p, width = 7, height = 5, dpi = 150)
|
|
message("Plot saved to ", out_file)
|
|
} else {
|
|
message("Install ggplot2 to get a plot; raw results:")
|
|
print(results)
|
|
}
|