1
0
mirror of https://github.com/msberends/AMR.git synced 2025-02-23 00:30:26 +01:00

(v2.1.1.9148) scale fix, antibiogram fix

This commit is contained in:
dr. M.S. (Matthijs) Berends 2025-02-15 12:38:29 +01:00
parent d94efb0f5e
commit 9d636983ac
No known key found for this signature in database
12 changed files with 68 additions and 34 deletions

View File

@ -1,6 +1,6 @@
Package: AMR Package: AMR
Version: 2.1.1.9147 Version: 2.1.1.9148
Date: 2025-02-14 Date: 2025-02-15
Title: Antimicrobial Resistance Data Analysis Title: Antimicrobial Resistance Data Analysis
Description: Functions to simplify and standardise antimicrobial resistance (AMR) Description: Functions to simplify and standardise antimicrobial resistance (AMR)
data analysis and to work with microbial and antimicrobial properties by data analysis and to work with microbial and antimicrobial properties by

View File

@ -1,4 +1,4 @@
# AMR 2.1.1.9147 # AMR 2.1.1.9148
*(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://msberends.github.io/AMR/#latest-development-version).)* *(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://msberends.github.io/AMR/#latest-development-version).)*

View File

@ -1,6 +1,6 @@
Metadata-Version: 2.2 Metadata-Version: 2.2
Name: AMR Name: AMR
Version: 2.1.1.9147 Version: 2.1.1.9148
Summary: A Python wrapper for the AMR R package Summary: A Python wrapper for the AMR R package
Home-page: https://github.com/msberends/AMR Home-page: https://github.com/msberends/AMR
Author: Matthijs Berends Author: Matthijs Berends

Binary file not shown.

Binary file not shown.

View File

@ -2,7 +2,7 @@ from setuptools import setup, find_packages
setup( setup(
name='AMR', name='AMR',
version='2.1.1.9147', version='2.1.1.9148',
packages=find_packages(), packages=find_packages(),
install_requires=[ install_requires=[
'rpy2', 'rpy2',

View File

@ -179,12 +179,16 @@ globalVariables(c(
"microorganisms.codes", "microorganisms.codes",
"mo", "mo",
"n", "n",
"n_susceptible",
"n_tested",
"n_total",
"name", "name",
"new", "new",
"numerator", "numerator",
"observations", "observations",
"old", "old",
"old_name", "old_name",
"p_susceptible",
"pattern", "pattern",
"R", "R",
"rank_index", "rank_index",

View File

@ -441,7 +441,7 @@ antibiogram.default <- function(x,
x <- ascertain_sir_classes(x, "x") x <- ascertain_sir_classes(x, "x")
meet_criteria(wisca, allow_class = "logical", has_length = 1) meet_criteria(wisca, allow_class = "logical", has_length = 1)
if (isTRUE(wisca)) { if (isTRUE(wisca)) {
if (!missing(mo_transform)) { if (!is.null(mo_transform)) {
warning_("WISCA must be based on the species level as WISCA parameters are based on this. For that reason, `mo_transform` will be ignored.") warning_("WISCA must be based on the species level as WISCA parameters are based on this. For that reason, `mo_transform` will be ignored.")
} }
mo_transform <- function(x) suppressMessages(suppressWarnings(paste(mo_genus(x, keep_synonyms = TRUE, language = NULL), mo_species(x, keep_synonyms = TRUE, language = NULL)))) mo_transform <- function(x) suppressMessages(suppressWarnings(paste(mo_genus(x, keep_synonyms = TRUE, language = NULL), mo_species(x, keep_synonyms = TRUE, language = NULL))))
@ -469,7 +469,7 @@ antibiogram.default <- function(x,
# try to find columns based on type # try to find columns based on type
if (is.null(col_mo)) { if (is.null(col_mo)) {
col_mo <- search_type_in_df(x = x, type = "mo", info = interactive()) col_mo <- search_type_in_df(x = x, type = "mo", info = info)
stop_if(is.null(col_mo), "`col_mo` must be set") stop_if(is.null(col_mo), "`col_mo` must be set")
} }
# transform MOs # transform MOs
@ -594,7 +594,7 @@ antibiogram.default <- function(x,
} }
if (all(out$n_tested < minimum, na.rm = TRUE) && wisca == FALSE) { if (all(out$n_tested < minimum, na.rm = TRUE) && wisca == FALSE) {
warning_("All combinations had less than `minimum = ", minimum, "` results, returning an empty antibiogram") warning_("All combinations had less than `minimum = ", minimum, "` results, returning an empty antibiogram")
return(as_original_data_class(data.frame(), class(out), extra_class = "antibiogram")) return(as_original_data_class(data.frame(), class(x), extra_class = "antibiogram"))
} else if (any(out$n_tested < minimum, na.rm = TRUE)) { } else if (any(out$n_tested < minimum, na.rm = TRUE)) {
out <- out %pm>% out <- out %pm>%
# also for WISCA, refrain from anything below 15 isolates: # also for WISCA, refrain from anything below 15 isolates:
@ -612,7 +612,7 @@ antibiogram.default <- function(x,
} }
if (NROW(out) == 0) { if (NROW(out) == 0) {
return(as_original_data_class(data.frame(), class(out), extra_class = "antibiogram")) return(as_original_data_class(data.frame(), class(x), extra_class = "antibiogram"))
} }
out$p_susceptible <- out$n_susceptible / out$n_tested out$p_susceptible <- out$n_susceptible / out$n_tested
@ -927,7 +927,6 @@ antibiogram.default <- function(x,
rownames(out) <- NULL rownames(out) <- NULL
rownames(wisca_parameters) <- NULL rownames(wisca_parameters) <- NULL
rownames(long_numeric) <- NULL rownames(long_numeric) <- NULL
structure(out, structure(out,
has_syndromic_group = has_syndromic_group, has_syndromic_group = has_syndromic_group,
combine_SI = combine_SI, combine_SI = combine_SI,
@ -943,7 +942,7 @@ antibiogram.default <- function(x,
#' @export #' @export
antibiogram.grouped_df <- function(x, antibiogram.grouped_df <- function(x,
antibiotics = where(is.sir), antibiotics = where(is.sir),
mo_transform = function (...) "no_mo", mo_transform = NULL,
ab_transform = "name", ab_transform = "name",
syndromic_group = NULL, syndromic_group = NULL,
add_total_n = FALSE, add_total_n = FALSE,
@ -960,6 +959,7 @@ antibiogram.grouped_df <- function(x,
conf_interval = 0.95, conf_interval = 0.95,
interval_side = "two-tailed", interval_side = "two-tailed",
info = interactive()) { info = interactive()) {
stop_ifnot(is.null(mo_transform), "`mo_transform` must not be set if creating an antibiogram using a grouped tibble. The groups will become the variables over which the antimicrobials are calculated, which could include the pathogen information (though not necessary). Nonetheless, this makes `mo_transform` redundant.", call = FALSE)
stop_ifnot(is.null(syndromic_group), "`syndromic_group` must not be set if creating an antibiogram using a grouped tibble. The groups will become the variables over which the antimicrobials are calculated, making `syndromic_groups` redundant.", call = FALSE) stop_ifnot(is.null(syndromic_group), "`syndromic_group` must not be set if creating an antibiogram using a grouped tibble. The groups will become the variables over which the antimicrobials are calculated, making `syndromic_groups` redundant.", call = FALSE)
groups <- attributes(x)$groups groups <- attributes(x)$groups
n_groups <- NROW(groups) n_groups <- NROW(groups)
@ -969,16 +969,19 @@ antibiogram.grouped_df <- function(x,
title = paste("Calculating AMR for", n_groups, "groups")) title = paste("Calculating AMR for", n_groups, "groups"))
on.exit(close(progress)) on.exit(close(progress))
out <- NULL
wisca_parameters <- NULL
long_numeric <- NULL
for (i in seq_len(n_groups)) { for (i in seq_len(n_groups)) {
if (i > 1) progress$tick() progress$tick()
rows <- unlist(groups[i, ]$.rows) rows <- unlist(groups[i, ]$.rows)
if (length(rows) == 0) { if (length(rows) == 0) {
next next
} }
new_out <- antibiogram(as.data.frame(x)[rows, , drop = FALSE], new_out <- antibiogram(as.data.frame(x)[rows, , drop = FALSE],
antibiotics = antibiotics, antibiotics = antibiotics,
mo_transform = function(x) "no_mo", mo_transform = NULL,
ab_transform = ab_transform, ab_transform = ab_transform,
syndromic_group = NULL, syndromic_group = NULL,
add_total_n = add_total_n, add_total_n = add_total_n,
@ -994,17 +997,15 @@ antibiogram.grouped_df <- function(x,
simulations = simulations, simulations = simulations,
conf_interval = conf_interval, conf_interval = conf_interval,
interval_side = interval_side, interval_side = interval_side,
info = i == 1 && info == TRUE) info = FALSE)
new_wisca_parameters <- attributes(new_out)$wisca_parameters new_wisca_parameters <- attributes(new_out)$wisca_parameters
new_long_numeric <- attributes(new_out)$long_numeric new_long_numeric <- attributes(new_out)$long_numeric
if (i == 1) progress$tick()
if (NROW(new_out) == 0) { if (NROW(new_out) == 0) {
next next
} }
# remove first column 'Pathogen' (in whatever language) # remove first column 'Pathogen' (in whatever language), except WISCA since that never has Pathogen column
if (isFALSE(wisca)) { if (isFALSE(wisca)) {
new_out <- new_out[, -1, drop = FALSE] new_out <- new_out[, -1, drop = FALSE]
new_long_numeric <- new_long_numeric[, -1, drop = FALSE] new_long_numeric <- new_long_numeric[, -1, drop = FALSE]
@ -1037,17 +1038,17 @@ antibiogram.grouped_df <- function(x,
long_numeric <- rbind_AMR(long_numeric, new_long_numeric) long_numeric <- rbind_AMR(long_numeric, new_long_numeric)
} }
} }
close(progress) close(progress)
out <- structure(as_original_data_class(out, class(x), extra_class = "antibiogram"), structure(as_original_data_class(out, class(x), extra_class = "antibiogram"),
has_syndromic_group = FALSE, has_syndromic_group = FALSE,
combine_SI = isTRUE(combine_SI), combine_SI = isTRUE(combine_SI),
wisca = isTRUE(wisca), wisca = isTRUE(wisca),
conf_interval = conf_interval, conf_interval = conf_interval,
formatting_type = formatting_type, formatting_type = formatting_type,
wisca_parameters = as_original_data_class(wisca_parameters, class(x)), wisca_parameters = as_original_data_class(wisca_parameters, class(x)),
long_numeric = as_original_data_class(long_numeric, class(x))) long_numeric = as_original_data_class(long_numeric, class(x)))
} }
#' @export #' @export
@ -1072,6 +1073,7 @@ wisca <- function(x,
antibiogram(x = x, antibiogram(x = x,
antibiotics = antibiotics, antibiotics = antibiotics,
ab_transform = ab_transform, ab_transform = ab_transform,
mo_transform = NULL,
syndromic_group = syndromic_group, syndromic_group = syndromic_group,
add_total_n = add_total_n, add_total_n = add_total_n,
only_all_tested = only_all_tested, only_all_tested = only_all_tested,

View File

@ -227,10 +227,16 @@
#' } #' }
NULL NULL
create_scale_mic <- function(aest, keep_operators, mic_range, ...) { create_scale_mic <- function(aest, keep_operators, mic_range = NULL, ...) {
ggplot_fn <- getExportedValue(paste0("scale_", aest, "_continuous"), ggplot_fn <- getExportedValue(paste0("scale_", aest, "_continuous"),
ns = asNamespace("ggplot2")) ns = asNamespace("ggplot2"))
args <- list(...) args <- list(...)
breaks_set <- args$breaks
if (!is.null(args$limits)) {
stop_ifnot(is.null(mic_range),
"In `scale_", aest, "_mic()`, `limits` cannot be combined with `mic_range`, as they working identically. Use `mic_range` OR `limits`.", call = FALSE)
mic_range <- args$limits
}
# do not take these arguments into account, as they will be overwritten and seem to allow weird behaviour # do not take these arguments into account, as they will be overwritten and seem to allow weird behaviour
args[c("aesthetics", "trans", "transform", "transform_df", "breaks", "labels", "limits")] <- NULL args[c("aesthetics", "trans", "transform", "transform_df", "breaks", "labels", "limits")] <- NULL
scale <- do.call(ggplot_fn, args) scale <- do.call(ggplot_fn, args)
@ -252,8 +258,30 @@ create_scale_mic <- function(aest, keep_operators, mic_range, ...) {
df[[aest]] <- self$`.values_log` df[[aest]] <- self$`.values_log`
df df
} }
scale$breaks <- function(..., self) log2(as.mic(self$`.values_levels`)) scale$breaks <- function(..., self) {
scale$labels <- function(..., self) self$`.values_levels` if (!is.null(breaks_set)) {
if (is.function(breaks_set)) {
breaks_set(...)
} else {
log2(as.mic(breaks_set))
}
} else {
log2(as.mic(self$`.values_levels`))
}
}
scale$labels <- function(..., self) {
if (is.null(breaks_set)) {
self$`.values_levels`
} else {
breaks <- tryCatch(scale$breaks(), error = function(e) NULL)
if (!is.null(breaks)) {
# for when breaks are set by the user
2 ^ breaks
} else {
self$`.values_levels`
}
}
}
scale$limits <- function(x, ..., self) { scale$limits <- function(x, ..., self) {
rng <- range(log2(as.mic(self$`.values_levels`))) rng <- range(log2(as.mic(self$`.values_levels`)))
# add 0.5 extra space # add 0.5 extra space

View File

@ -1,6 +1,6 @@
This knowledge base contains all context you must know about the AMR package for R. You are a GPT trained to be an assistant for the AMR package in R. You are an incredible R specialist, especially trained in this package and in the tidyverse. This knowledge base contains all context you must know about the AMR package for R. You are a GPT trained to be an assistant for the AMR package in R. You are an incredible R specialist, especially trained in this package and in the tidyverse.
First and foremost, you are trained on version 2.1.1.9147. Remember this whenever someone asks which AMR package version youre at. First and foremost, you are trained on version 2.1.1.9148. Remember this whenever someone asks which AMR package version youre at.
Below are the contents of the file, the file, and all the files (documentation) in the package. Every file content is split using 100 hypens. Below are the contents of the file, the file, and all the files (documentation) in the package. Every file content is split using 100 hypens.
---------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------

View File

@ -104,7 +104,7 @@ ab8 <- suppressWarnings(antibiogram(example_isolates,
wisca = TRUE)) wisca = TRUE))
expect_inherits(ab8, "antibiogram") expect_inherits(ab8, "antibiogram")
expect_equal(colnames(ab8), c("Pathogen", "Piperacillin/tazobactam", "Piperacillin/tazobactam + Gentamicin", "Piperacillin/tazobactam + Tobramycin")) expect_equal(colnames(ab8), c("Piperacillin/tazobactam", "Piperacillin/tazobactam + Gentamicin", "Piperacillin/tazobactam + Tobramycin"))
# grouped tibbles # grouped tibbles
@ -128,7 +128,7 @@ expect_silent(plot(ab5))
expect_silent(plot(ab6)) expect_silent(plot(ab6))
expect_silent(plot(ab7)) expect_silent(plot(ab7))
expect_silent(plot(ab8)) expect_silent(plot(ab8))
expect_error(plot(ab9)) expect_silent(plot(ab9))
if (AMR:::pkg_is_available("ggplot2")) { if (AMR:::pkg_is_available("ggplot2")) {
expect_inherits(ggplot2::autoplot(ab1), "gg") expect_inherits(ggplot2::autoplot(ab1), "gg")
@ -139,5 +139,5 @@ if (AMR:::pkg_is_available("ggplot2")) {
expect_inherits(ggplot2::autoplot(ab6), "gg") expect_inherits(ggplot2::autoplot(ab6), "gg")
expect_inherits(ggplot2::autoplot(ab7), "gg") expect_inherits(ggplot2::autoplot(ab7), "gg")
expect_inherits(ggplot2::autoplot(ab8), "gg") expect_inherits(ggplot2::autoplot(ab8), "gg")
expect_error(ggplot2::autoplot(ab9)) expect_inherits(ggplot2::autoplot(ab9), "gg")
} }