Improve the session caches of as.ab(), as.mo() and all mo_*() functions (#313) (#314)

- One cache design for as.ab() and as.mo() in R/aa_session_cache.R: lazily built
  lookup indices (ATC codes, drug synonyms, SNOMED codes) and coercion caches with
  the same structure (key, input, value), keyed on input plus outcome-relevant settings
- Caches are invalidated whenever the lookup tables change (custom antimicrobials or
  microorganisms, set_mo_source())
- mo_*() functions use a fast path for valid <mo> input and work on unique values only
- synonym_mo_to_accepted_mo() is memoised per code
- Fix as.mo() cache keys that never matched again, fast_mode results of as.ab() being
  reused by regular calls, and ab_reset_session() clearing as.mo() uncertainties

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Matthijs BerendsandClaude Opus 5.5 authored and GitHub committed 2026-10-07 14:29:48 +02:00
1 parent 0ca43df459
commit 891d248fab
14 files changed
+462 -124

No files matched your search

+4 -8
View File
@@ -278,11 +278,11 @@ add_custom_microorganisms <- function(x) {
new_df[, col] <- x[, col, drop = TRUE]
}
# clear previous coercions
suppressMessages(mo_reset_session())
AMR_env$MO_lookup <- unique(rbind_AMR(AMR_env$MO_lookup, new_df))
class(AMR_env$MO_lookup$mo) <- c("mo", "character")
# the lookup table changed, so earlier coercions and lookup indices are no longer valid
reset_mo_cache()
AMR_env$mo_uncertainties <- AMR_env$mo_uncertainties[0, , drop = FALSE]
if (nrow(x) <= 3) {
message_("Added ", vector_and(italicise(x$fullname), quotes = FALSE), " to the internal {.code microorganisms} data set.")
} else {
@@ -295,16 +295,12 @@ add_custom_microorganisms <- function(x) {
clear_custom_microorganisms <- function() {
n <- nrow(AMR_env$MO_lookup)
# reset
# reset, which also invalidates earlier coercions and lookup indices
AMR_env$MO_lookup <- NULL
add_MO_lookup_to_AMR_env()
# clear previous coercions
suppressMessages(mo_reset_session())
n2 <- nrow(AMR_env$MO_lookup)
AMR_env$custom_mo_codes <- character(0)
AMR_env$mo_previously_coerced <- AMR_env$mo_previously_coerced[which(AMR_env$mo_previously_coerced$mo %in% AMR_env$MO_lookup$mo), , drop = FALSE]
AMR_env$mo_uncertainties <- AMR_env$mo_uncertainties[0, , drop = FALSE]
message_("Cleared ", nr2char(n - n2), " custom record", ifelse(n - n2 > 1, "s", ""), " from the internal {.code microorganisms} data set.")
}