Merge remote-tracking branch 'origin/main' into breakpoints

This commit is contained in:
dr. M.S. (Matthijs) Berends committed 2026-11-02 18:26:08 +01:00
commit d904ea5afb
23 files changed
+288 -83

No files matched your search

+51 -1
View File
@@ -47,6 +47,44 @@ test_that("test-antibiogram.R", {
wisca_known <- suppressMessages(wisca(example_isolates[example_isolates$mo != "UNKNOWN", ], antimicrobials = "GEN", simulations = 100, info = FALSE, guideline = "EUCAST"))
expect_true("UNKNOWN" %in% attributes(wisca_all)$wisca_parameters$mo)
expect_false(identical(as.data.frame(wisca_all), as.data.frame(wisca_known)))
# WISCA only includes pathogens that occur in a group and were tested for the regimen (#337)
set.seed(337)
wisca_data <- example_isolates[mo_domain(example_isolates$mo) == "Bacteria", ]
wisca_data$grp <- sample(c("A", "B", "C"), nrow(wisca_data), replace = TRUE)
wisca_strat <- suppressWarnings(wisca(wisca_data, antimicrobials = "MEM", syndromic_group = "grp", simulations = 5000, info = FALSE))
wisca_params <- retrieve_wisca_parameters(wisca_strat)
# no absent or untested pathogens
expect_true(all(wisca_params$n_total > 0 & wisca_params$n_tested > 0))
# each stratum equals the unstratified WISCA of its subset
for (g in c("A", "B", "C")) {
wisca_subset <- suppressWarnings(wisca(wisca_data[wisca_data$grp == g, ], antimicrobials = "MEM", simulations = 5000, info = FALSE))
cov_strat <- attributes(wisca_strat)$long_numeric$coverage[attributes(wisca_strat)$long_numeric$syndromic_group == g]
expect_true(abs(cov_strat - attributes(wisca_subset)$long_numeric$coverage) < 0.01)
expect_identical(attributes(wisca_strat)$long_numeric$n_total[attributes(wisca_strat)$long_numeric$syndromic_group == g], attributes(wisca_subset)$long_numeric$n_total)
}
# untested pathogens have no influence
wisca_untested <- wisca_data[1:20, ]
wisca_untested$mo <- as.mo("Brucella melitensis")
wisca_untested$MEM <- NA_sir_
set.seed(337)
wisca_without <- suppressWarnings(wisca(wisca_data, antimicrobials = "MEM", simulations = 1000, info = FALSE))
set.seed(337)
wisca_with <- suppressWarnings(wisca(rbind(wisca_data, wisca_untested), antimicrobials = "MEM", simulations = 1000, info = FALSE))
expect_identical(attributes(wisca_with)$long_numeric, attributes(wisca_without)$long_numeric)
expect_false("B_BRCLL_MLTN" %in% retrieve_wisca_parameters(wisca_with)$mo)
# warning for a large prior weight of rare pathogens
expect_warning(wisca(wisca_data, antimicrobials = "MEM", syndromic_group = "grp", simulations = 100, info = FALSE), "prior weight")
# (expect_warning(..., NA) is not supported by tinytest)
wisca_warnings <- character(0)
withCallingHandlers(
wisca(example_isolates, antimicrobials = "MEM", simulations = 100, info = FALSE),
warning = function(w) {
wisca_warnings <<- c(wisca_warnings, conditionMessage(w))
invokeRestart("muffleWarning")
}
)
expect_false(any(grepl("prior weight", wisca_warnings)))
if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0", also_load = TRUE)) {
ab_grouped <- tryCatch(
# pathogen names are translated, so test in English, which must also hold on systems in another language
@@ -230,7 +268,19 @@ test_that("test-antibiogram.R", {
expect_equal(nrow(ab_grp_seq), nrow(ab_grp_par))
}
# 6. parallel = TRUE without a plan raises an informative error
# 6. parallel and sequential use the same WISCA parameters and give the same coverage (#337)
set.seed(337)
wisca_data <- example_isolates[mo_domain(example_isolates$mo) == "Bacteria", ]
wisca_data$grp <- sample(c("A", "B", "C"), nrow(wisca_data), replace = TRUE)
future::plan(future::sequential)
wisca_strat_seq <- suppressWarnings(wisca(wisca_data, antimicrobials = "MEM", syndromic_group = "grp", simulations = 5000, info = FALSE))
future::plan(future::multicore)
wisca_strat_par <- suppressWarnings(wisca(wisca_data, antimicrobials = "MEM", syndromic_group = "grp", simulations = 5000, parallel = TRUE, info = FALSE))
expect_identical(retrieve_wisca_parameters(wisca_strat_par), retrieve_wisca_parameters(wisca_strat_seq))
expect_true(all(retrieve_wisca_parameters(wisca_strat_par)$n_tested > 0))
expect_true(all(abs(attributes(wisca_strat_par)$long_numeric$coverage - attributes(wisca_strat_seq)$long_numeric$coverage) < 0.01))
# 7. parallel = TRUE without a plan raises an informative error
future::plan(future::sequential)
expect_error(
suppressWarnings(wisca(example_isolates, antimicrobials = "TZP", parallel = TRUE, info = FALSE)),
+19
View File
@@ -547,6 +547,25 @@ test_that("intrinsic_resistant: synonyms have exactly the resistances of their c
expect_true(length(absent) == 0, info = paste("Current names missing while their synonym is listed:", describe_mo(absent)))
})
test_that("intrinsic_resistant: species groups have the resistances shared by all their species", {
skip_on_cran()
ir_keys <- paste(intrinsic_resistant$mo, intrinsic_resistant$ab)
groups <- microorganisms.groups[which(microorganisms$rank[match(microorganisms.groups$mo, microorganisms$mo)] == "species"), , drop = FALSE]
groups_species <- split(as.character(groups$mo), as.character(groups$mo_group))
missing <- unlist(Map(function(group, species) {
abx <- unique(as.character(intrinsic_resistant$ab[intrinsic_resistant$mo %in% species]))
abx <- abx[vapply(abx, function(ab) all(paste(species, ab) %in% ir_keys), logical(1))]
if (length(abx) == 0) {
return(NULL)
}
abx <- abx[!paste(group, abx) %in% ir_keys]
if (length(abx) > 0) paste0(group, " (", paste(abx, collapse = ", "), ")")
}, names(groups_species), groups_species), use.names = FALSE)
expect_true(length(missing) == 0, info = paste("Species groups missing resistances shared by all their species:", paste(missing, collapse = "; ")))
# Enterobacter cloacae complex (#338)
expect_true(all(mo_is_intrinsic_resistant(rep("B_ENTRBC_CLOC-C", 2), ab = c("AMX", "AMC"))))
})
test_that("interpretive rules name organisms that exist", {
skip_on_cran()
r <- AMR:::INTERPRETIVE_RULES_DF