1
0
mirror of https://github.com/msberends/AMR.git synced 2026-03-30 08:15:57 +02:00

2 Commits

Author SHA1 Message Date
Matthijs Berends
ec11b4ebc1 docs: instruct Claude to install git and gh before computing version 2026-03-29 16:53:37 +02:00
Matthijs Berends
6a7e8ce036 fix: convert Python lists to R vectors in wrapper to prevent R list coercion errors
Fixes #267. Python lists passed to R functions via rpy2 are received as
R lists, not R character/numeric vectors. This causes is.mic(), is.sir(),
is.disk() etc. to return length > 1 logicals, which break R's && operator.

Added convert_to_r() helper that maps Python list/tuple to the appropriate
typed R vector (StrVector, IntVector, FloatVector) based on element types.
The r_to_python decorator now applies this to all args and kwargs before
calling the R function.
2026-03-29 14:34:58 +02:00
10 changed files with 47 additions and 38 deletions

View File

@@ -152,7 +152,16 @@ All PRs are **squash-merged**, so each PR lands as exactly **one commit** on the
#### Computing the correct version number
Run the following from the repo root to determine the version string to use:
**First, ensure `git` and `gh` are installed** — both are required for the version computation and for pushing changes. Install them if missing before doing anything else:
```bash
which git || apt-get install -y git
which gh || apt-get install -y gh
# Also ensure all tags are fetched so git describe works
git fetch --tags
```
Then run the following from the repo root to determine the version string to use:
```bash
currenttag=$(git describe --tags --abbrev=0 | sed 's/v//')

View File

@@ -1,6 +1,6 @@
Package: AMR
Version: 3.0.1.9041
Date: 2026-03-26
Version: 3.0.1.9040
Date: 2026-03-24
Title: Antimicrobial Resistance Data Analysis
Description: Functions to simplify and standardise antimicrobial resistance (AMR)
data analysis and to work with microbial and antimicrobial properties by

View File

@@ -1,4 +1,4 @@
# AMR 3.0.1.9041
# AMR 3.0.1.9040
### New
* Integration with the **tidymodels** framework to allow seamless use of SIR, MIC and disk data in modelling pipelines via `recipes`
@@ -31,7 +31,6 @@
* Fixed SIR and MIC coercion of combined values, e.g. `as.sir("<= 0.002; S") ` or `as.mic("S; 0.002")` (#252)
### Updates
* Reproduced `clinical_breakpoints`, `microorganisms.codes`, and `microorganisms.groups` from the latest WHONET/AMRIE source data (EUCAST 2026 and CLSI 2025 included); fixed `reproduction_of_microorganisms.groups.R` to use `dplyr::if_else()` instead of base `ifelse()` to preserve the `<mo>` class in `bind_rows()`
* Extensive `cli` integration for better message handling and clickable links in messages and warnings (#191, #265)
* `mdro()` now infers resistance for a _missing_ base drug column from an _available_ corresponding drug+inhibitor combination showing resistance (e.g., piperacillin is absent but required, while piperacillin/tazobactam available and resistant). Can be set with the new argument `infer_from_combinations`, which defaults to `TRUE` (#209). Note that this can yield a higher MDRO detection (which is a good thing as it has become more reliable).
* `susceptibility()` and `resistance()` gained the argument `guideline`, which defaults to EUCAST, for interpreting the 'I' category correctly.

View File

@@ -141,6 +141,32 @@ import numpy as np
# Import the AMR R package
amr_r = importr('AMR')
def convert_to_r(value):
"""Convert Python lists/tuples to typed R vectors.
rpy2's default_converter passes Python lists to R as R lists, not as
character/numeric vectors. This causes element-wise type-check functions
such as is.mic(), is.sir(), and is.disk() to return a logical vector
rather than a single logical, breaking R's scalar && operator.
This helper converts Python lists and tuples to the appropriate R vector
type based on the element types, so R always receives a proper vector."""
if isinstance(value, (list, tuple)):
if len(value) == 0:
return StrVector([])
# bool must be checked before int because bool is a subclass of int
if all(isinstance(v, bool) for v in value):
return robjects.vectors.BoolVector(value)
if all(isinstance(v, int) for v in value):
return IntVector(value)
if all(isinstance(v, float) for v in value):
return FloatVector(value)
if all(isinstance(v, str) for v in value):
return StrVector(value)
# Mixed types: coerce all to string
return StrVector([str(v) for v in value])
return value
def convert_to_python(r_output):
# Check if it's a StrVector (R character vector)
if isinstance(r_output, StrVector):
@@ -166,10 +192,13 @@ def convert_to_python(r_output):
return r_output
def r_to_python(r_func):
"""Decorator that runs an rpy2 function under a localconverter
and then applies convert_to_python to its output."""
"""Decorator that converts Python list/tuple inputs to typed R vectors,
runs the rpy2 function under a localconverter, and converts the output
to a Python type."""
@functools.wraps(r_func)
def wrapper(*args, **kwargs):
args = tuple(convert_to_r(a) for a in args)
kwargs = {k: convert_to_r(v) for k, v in kwargs.items()}
with localconverter(default_converter + numpy2ri.converter + pandas2ri.converter):
return convert_to_python(r_func(*args, **kwargs))
return wrapper
@@ -312,4 +341,3 @@ cd ../PythonPackage/AMR
pip3 install build
python3 -m build
# python3 setup.py sdist bdist_wheel

View File

@@ -85,9 +85,9 @@ microorganisms.groups <- whonet_organisms %>%
"Mycobacterium canetti")) %>%
filter(!is.na(SPECIES_GROUP), SPECIES_GROUP != ORGANISM_CODE) %>%
transmute(mo_group = as.mo(SPECIES_GROUP),
mo = if_else(is.na(mo),
as.mo(ORGANISM, keep_synonyms = TRUE, minimum_matching_score = 0),
mo)) %>%
mo = ifelse(is.na(mo),
as.character(as.mo(ORGANISM, keep_synonyms = TRUE, minimum_matching_score = 0)),
mo)) %>%
# add our own CoNS and CoPS, WHONET does not strictly follow Becker et al. (2014, 2019, 2020)
filter(mo_group != as.mo("CoNS")) %>%
bind_rows(tibble(mo_group = as.mo("CoNS"), mo = MO_CONS)) %>%

View File

@@ -1,27 +0,0 @@
# Wrapper to run clinical breakpoints reproduction non-interactively
# Set UTF-8 locale so gsub() can handle Unicode patterns
Sys.setlocale("LC_CTYPE", "C.utf8")
Sys.setlocale("LC_ALL", "C.utf8")
# Overrides View() to just print a summary instead
View <- function(x, title = NULL) {
if (is.data.frame(x) || is.matrix(x)) {
cat("=== View() called:", if (!is.null(title)) title else deparse(substitute(x)), "===\n")
cat("Dimensions:", nrow(x), "rows x", ncol(x), "cols\n")
print(head(x, 10))
cat("...\n\n")
} else {
print(x)
}
invisible(x)
}
setwd("/home/user/AMR")
cat("=== Step 1: Running reproduction_of_microorganisms.groups.R ===\n")
source("data-raw/_reproduction_scripts/reproduction_of_microorganisms.groups.R")
cat("\n=== Step 2: Running reproduction_of_clinical_breakpoints.R ===\n")
source("data-raw/_reproduction_scripts/reproduction_of_clinical_breakpoints.R")
cat("\n=== Done! ===\n")

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.