From f340e257fa6f36d2a937f779202a692d793ce88e Mon Sep 17 00:00:00 2001 From: Matthijs Berends Date: Sat, 26 Apr 2025 21:29:50 +0200 Subject: [PATCH] (v2.1.1.9256) unit tests --- DESCRIPTION | 2 +- NEWS.md | 2 +- R/ab.R | 1 - R/sir.R | 11 +- man/as.ab.Rd | 1 - man/as.sir.Rd | 4 +- tests/testthat/test-ab.R | 5 +- tests/testthat/test-antibiogram.R | 3 +- tests/testthat/test-resistance_predict.R | 124 ----------------------- tests/testthat/test-sir.R | 6 +- 10 files changed, 17 insertions(+), 142 deletions(-) delete mode 100644 tests/testthat/test-resistance_predict.R diff --git a/DESCRIPTION b/DESCRIPTION index ba4ad1601..6194b806b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,5 +1,5 @@ Package: AMR -Version: 2.1.1.9255 +Version: 2.1.1.9256 Date: 2025-04-26 Title: Antimicrobial Resistance Data Analysis Description: Functions to simplify and standardise antimicrobial resistance (AMR) diff --git a/NEWS.md b/NEWS.md index 11c93d263..963b3176c 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -# AMR 2.1.1.9255 +# AMR 2.1.1.9256 *(this beta version will eventually become v3.0. We're happy to reach a new major milestone soon, which will be all about the new One Health support! Install this beta using [the instructions here](https://amr-for-r.org/#get-this-package).)* diff --git a/R/ab.R b/R/ab.R index a12c5ddbb..65501e65d 100755 --- a/R/ab.R +++ b/R/ab.R @@ -72,7 +72,6 @@ #' as.ab("ERY") #' as.ab("eritromicine") # spelled wrong, yet works #' as.ab("Erythrocin") # trade name -#' as.ab("Romycin") # trade name #' #' # spelling from different languages and dyslexia are no problem #' ab_atc("ceftriaxon") diff --git a/R/sir.R b/R/sir.R index f4a31244f..8fdb20267 100755 --- a/R/sir.R +++ b/R/sir.R @@ -680,8 +680,8 @@ as.sir.disk <- function(x, } #' @rdname as.sir -#' @param parallel A [logical] to indicate if parallel computing must be used, defaults to `FALSE`. -#' @param max_cores Maximum number of cores to use if `parallel = TRUE`. Use a negative value to subtract that number from the available number of cores, e.g. a value of `-2` on an 8-core machine means that 6 cores will be used. Defaults to `-1`. The available number of cores are detected using [parallelly::availableCores()] if that package is installed, and base \R's [parallel::detectCores()] otherwise. +#' @param parallel A [logical] to indicate if parallel computing must be used, defaults to `FALSE`. This requires no additional packages, as the used `parallel` package is part of base \R. +#' @param max_cores Maximum number of cores to use if `parallel = TRUE`. Use a negative value to subtract that number from the available number of cores, e.g. a value of `-2` on an 8-core machine means that at most 6 cores will be used. Defaults to `-1`. There will never be used more cores than variables to analyse. The available number of cores are detected using [parallelly::availableCores()] if that package is installed, and base \R's [parallel::detectCores()] otherwise. #' @export as.sir.data.frame <- function(x, ..., @@ -853,6 +853,7 @@ as.sir.data.frame <- function(x, # set up parallel computing n_cores <- get_n_cores(max_cores = max_cores) + n_cores <- min(n_cores, length(ab_cols)) # never more cores than variables required run_as_sir_column <- function(i) { ab_col <- ab_cols[i] @@ -952,7 +953,7 @@ as.sir.data.frame <- function(x, if (isTRUE(parallel) && n_cores > 1 && length(ab_cols) > 1) { if (isTRUE(info)) { message() - message_("Running SIR interpretation in parallel mode on ", nr2char(length(ab_cols)), " columns, using ", n_cores, " out of ", get_n_cores(Inf), " cores...", as_note = FALSE, appendLF = FALSE, add_fn = font_red) + message_("Running in parallel mode using ", n_cores, " out of ", get_n_cores(Inf), " cores, on columns ", vector_and(font_bold(ab_cols, collapse = NULL), quotes = "'", sort = FALSE), "...", as_note = FALSE, appendLF = FALSE, add_fn = font_red) } if (.Platform$OS.type == "windows") { cl <- parallel::makeCluster(n_cores, type = "PSOCK") @@ -976,10 +977,10 @@ as.sir.data.frame <- function(x, } } else { # sequential mode (non-parallel) - if (n_cores > 1 && isTRUE(info) && (NROW(x) > 2500 || length(ab_cols) >= 5)) { + if (isTRUE(info) && n_cores > 1 && NROW(x) * NCOL(x) > 10000) { # give a note that parallel mode might be better message() - message_("Running SIR interpretation in sequential mode. Consider setting `parallel = TRUE` to speed up processing on multiple cores.\n", add_fn = font_red) + message_("Running in sequential mode. Consider setting `parallel = TRUE` to speed up processing on multiple cores.\n", add_fn = font_red) } # this will contain a progress bar already result_list <- lapply(seq_along(ab_cols), run_as_sir_column) diff --git a/man/as.ab.Rd b/man/as.ab.Rd index 8a2573625..d7d2d3b95 100644 --- a/man/as.ab.Rd +++ b/man/as.ab.Rd @@ -85,7 +85,6 @@ as.ab("ERYT") as.ab("ERY") as.ab("eritromicine") # spelled wrong, yet works as.ab("Erythrocin") # trade name -as.ab("Romycin") # trade name # spelling from different languages and dyslexia are no problem ab_atc("ceftriaxon") diff --git a/man/as.sir.Rd b/man/as.sir.Rd index 7b3a8a195..fad1c5beb 100644 --- a/man/as.sir.Rd +++ b/man/as.sir.Rd @@ -138,9 +138,9 @@ The default \code{"standard"} setting ensures cautious handling of uncertain val \item{col_mo}{Column name of the names or codes of the microorganisms (see \code{\link[=as.mo]{as.mo()}}) - the default is the first column of class \code{\link{mo}}. Values will be coerced using \code{\link[=as.mo]{as.mo()}}.} -\item{parallel}{A \link{logical} to indicate if parallel computing must be used, defaults to \code{FALSE}.} +\item{parallel}{A \link{logical} to indicate if parallel computing must be used, defaults to \code{FALSE}. This requires no additional packages, as the used \code{parallel} package is part of base \R.} -\item{max_cores}{Maximum number of cores to use if \code{parallel = TRUE}. Use a negative value to subtract that number from the available number of cores, e.g. a value of \code{-2} on an 8-core machine means that 6 cores will be used. Defaults to \code{-1}. The available number of cores are detected using \code{\link[parallelly:availableCores]{parallelly::availableCores()}} if that package is installed, and base \R's \code{\link[parallel:detectCores]{parallel::detectCores()}} otherwise.} +\item{max_cores}{Maximum number of cores to use if \code{parallel = TRUE}. Use a negative value to subtract that number from the available number of cores, e.g. a value of \code{-2} on an 8-core machine means that at most 6 cores will be used. Defaults to \code{-1}. There will never be used more cores than variables to analyse. The available number of cores are detected using \code{\link[parallelly:availableCores]{parallelly::availableCores()}} if that package is installed, and base \R's \code{\link[parallel:detectCores]{parallel::detectCores()}} otherwise.} \item{clean}{A \link{logical} to indicate whether previously stored results should be forgotten after returning the 'logbook' with results.} } diff --git a/tests/testthat/test-ab.R b/tests/testthat/test-ab.R index 1ae43fac4..81cba0620 100755 --- a/tests/testthat/test-ab.R +++ b/tests/testthat/test-ab.R @@ -41,10 +41,9 @@ test_that("test-ab.R", { "ERYT", "ERY", "erytromicine", - "Erythrocin", - "Romycin" + "Erythrocin" ))), - rep("ERY", 9) + rep("ERY", 8) ) expect_identical(class(as.ab("amox")), c("ab", "character")) diff --git a/tests/testthat/test-antibiogram.R b/tests/testthat/test-antibiogram.R index 18bc5b426..4ffee6202 100644 --- a/tests/testthat/test-antibiogram.R +++ b/tests/testthat/test-antibiogram.R @@ -39,8 +39,7 @@ test_that("test-antibiogram.R", { ab2 <- antibiogram(example_isolates, antimicrobials = aminoglycosides(), ab_transform = "atc", - mo_transform = "gramstain", - add_total_n = TRUE + mo_transform = "gramstain" ) ab3 <- antibiogram(example_isolates, diff --git a/tests/testthat/test-resistance_predict.R b/tests/testthat/test-resistance_predict.R deleted file mode 100644 index 662860c20..000000000 --- a/tests/testthat/test-resistance_predict.R +++ /dev/null @@ -1,124 +0,0 @@ -# ==================================================================== # -# TITLE: # -# AMR: An R Package for Working with Antimicrobial Resistance Data # -# # -# SOURCE CODE: # -# https://github.com/msberends/AMR # -# # -# PLEASE CITE THIS SOFTWARE AS: # -# Berends MS, Luz CF, Friedrich AW, et al. (2022). # -# AMR: An R Package for Working with Antimicrobial Resistance Data. # -# Journal of Statistical Software, 104(3), 1-31. # -# https://doi.org/10.18637/jss.v104.i03 # -# # -# Developed at the University of Groningen and the University Medical # -# Center Groningen in The Netherlands, in collaboration with many # -# colleagues from around the world, see our website. # -# # -# This R package is free software; you can freely use and distribute # -# it for both personal and commercial purposes under the terms of the # -# GNU General Public License version 2.0 (GNU GPL-2), as published by # -# the Free Software Foundation. # -# We created this package for both routine data analysis and academic # -# research and it was publicly released in the hope that it will be # -# useful, but it comes WITHOUT ANY WARRANTY OR LIABILITY. # -# # -# Visit our website for the full manual and a complete tutorial about # -# how to conduct AMR data analysis: https://amr-for-r.org # -# ==================================================================== # - -test_that("test-resistance_predict.R", { - skip_on_cran() - - if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0", also_load = TRUE)) { - expect_output(AMX_R <- example_isolates %>% - filter(mo == "B_ESCHR_COLI") %>% - sir_predict( - col_ab = "AMX", - col_date = "date", - model = "binomial", - minimum = 10, - info = TRUE - ) %>% - pull("value")) - # AMX resistance will increase according to data set `example_isolates` - expect_true(AMX_R[3] < AMX_R[20]) - } - - expect_output(x <- suppressMessages(resistance_predict(example_isolates, - col_ab = "AMX", - year_min = 2010, - model = "binomial", - info = TRUE - ))) - pdf(NULL) # prevent Rplots.pdf being created - expect_silent(plot(x)) - if (AMR:::pkg_is_available("ggplot2")) { - expect_silent(ggplot_sir_predict(x)) - expect_silent(ggplot2::autoplot(x)) - expect_error(ggplot_sir_predict(example_isolates)) - } - expect_output(sir_predict( - x = subset(example_isolates, mo == "B_ESCHR_COLI"), - model = "binomial", - col_ab = "AMX", - col_date = "date", - info = TRUE - )) - expect_output(sir_predict( - x = subset(example_isolates, mo == "B_ESCHR_COLI"), - model = "loglin", - col_ab = "AMX", - col_date = "date", - info = TRUE - )) - expect_output(sir_predict( - x = subset(example_isolates, mo == "B_ESCHR_COLI"), - model = "lin", - col_ab = "AMX", - col_date = "date", - info = TRUE - )) - - expect_error(sir_predict( - x = subset(example_isolates, mo == "B_ESCHR_COLI"), - model = "INVALID MODEL", - col_ab = "AMX", - col_date = "date", - info = TRUE - )) - expect_error(sir_predict( - x = subset(example_isolates, mo == "B_ESCHR_COLI"), - model = "binomial", - col_ab = "NOT EXISTING COLUMN", - col_date = "date", - info = TRUE - )) - expect_error(sir_predict( - x = subset(example_isolates, mo == "B_ESCHR_COLI"), - model = "binomial", - col_ab = "AMX", - col_date = "NOT EXISTING COLUMN", - info = TRUE - )) - expect_error(sir_predict( - x = subset(example_isolates, mo == "B_ESCHR_COLI"), - col_ab = "AMX", - col_date = "NOT EXISTING COLUMN", - info = TRUE - )) - expect_error(sir_predict( - x = subset(example_isolates, mo == "B_ESCHR_COLI"), - col_ab = "AMX", - col_date = "date", - info = TRUE - )) - # almost all E. coli are MEM S in the Netherlands :) - expect_error(resistance_predict( - x = subset(example_isolates, mo == "B_ESCHR_COLI"), - model = "binomial", - col_ab = "MEM", - col_date = "date", - info = TRUE - )) -}) diff --git a/tests/testthat/test-sir.R b/tests/testthat/test-sir.R index e80431685..054cd938a 100644 --- a/tests/testthat/test-sir.R +++ b/tests/testthat/test-sir.R @@ -325,12 +325,14 @@ test_that("test-sir.R", { expect_message(as.sir(data.frame( mo = "E. coli", NIT = c("<= 2", 32), - uti = TRUE + uti = TRUE, + info = TRUE ))) expect_message(as.sir(data.frame( mo = "E. coli", NIT = c("<= 2", 32), - specimen = c("urine", "blood") + specimen = c("urine", "blood"), + info = TRUE ))) # SDD vs I in CLSI 2024