mirror of
https://github.com/msberends/AMR.git
synced 2025-07-12 21:41:55 +02:00
styled, unit test fix
This commit is contained in:
@ -9,7 +9,7 @@
|
||||
# (c) 2018-2022 Berends MS, Luz CF et al. #
|
||||
# Developed at the University of Groningen, the Netherlands, in #
|
||||
# collaboration with non-profit organisations Certe Medical #
|
||||
# Diagnostics & Advice, and University Medical Center Groningen. #
|
||||
# Diagnostics & Advice, and University Medical Center Groningen. #
|
||||
# #
|
||||
# This R package is free software; you can freely use and distribute #
|
||||
# it for both personal and commercial purposes under the terms of the #
|
||||
@ -24,40 +24,59 @@
|
||||
# ==================================================================== #
|
||||
|
||||
# all four methods
|
||||
expect_equal(sum(first_isolate(x = example_isolates, method = "isolate-based", info = TRUE), na.rm = TRUE),
|
||||
1984)
|
||||
expect_equal(sum(first_isolate(x = example_isolates, method = "patient-based", info = TRUE), na.rm = TRUE),
|
||||
1265)
|
||||
expect_equal(sum(first_isolate(x = example_isolates, method = "episode-based", info = TRUE), na.rm = TRUE),
|
||||
1300)
|
||||
expect_equal(sum(first_isolate(x = example_isolates, method = "phenotype-based", info = TRUE), na.rm = TRUE),
|
||||
1379)
|
||||
expect_equal(
|
||||
sum(first_isolate(x = example_isolates, method = "isolate-based", info = TRUE), na.rm = TRUE),
|
||||
1984
|
||||
)
|
||||
expect_equal(
|
||||
sum(first_isolate(x = example_isolates, method = "patient-based", info = TRUE), na.rm = TRUE),
|
||||
1265
|
||||
)
|
||||
expect_equal(
|
||||
sum(first_isolate(x = example_isolates, method = "episode-based", info = TRUE), na.rm = TRUE),
|
||||
1300
|
||||
)
|
||||
expect_equal(
|
||||
sum(first_isolate(x = example_isolates, method = "phenotype-based", info = TRUE), na.rm = TRUE),
|
||||
1379
|
||||
)
|
||||
|
||||
# Phenotype-based, using key antimicrobials
|
||||
expect_equal(sum(first_isolate(x = example_isolates,
|
||||
method = "phenotype-based",
|
||||
type = "keyantimicrobials",
|
||||
antifungal = NULL, info = TRUE), na.rm = TRUE),
|
||||
1395)
|
||||
expect_equal(sum(first_isolate(x = example_isolates,
|
||||
method = "phenotype-based",
|
||||
type = "keyantimicrobials",
|
||||
antifungal = NULL, info = TRUE, ignore_I = FALSE), na.rm = TRUE),
|
||||
1418)
|
||||
expect_equal(
|
||||
sum(first_isolate(
|
||||
x = example_isolates,
|
||||
method = "phenotype-based",
|
||||
type = "keyantimicrobials",
|
||||
antifungal = NULL, info = TRUE
|
||||
), na.rm = TRUE),
|
||||
1395
|
||||
)
|
||||
expect_equal(
|
||||
sum(first_isolate(
|
||||
x = example_isolates,
|
||||
method = "phenotype-based",
|
||||
type = "keyantimicrobials",
|
||||
antifungal = NULL, info = TRUE, ignore_I = FALSE
|
||||
), na.rm = TRUE),
|
||||
1418
|
||||
)
|
||||
|
||||
|
||||
# first non-ICU isolates
|
||||
expect_equal(
|
||||
sum(
|
||||
first_isolate(example_isolates,
|
||||
col_mo = "mo",
|
||||
col_date = "date",
|
||||
col_patient_id = "patient",
|
||||
col_icu = example_isolates$ward == "ICU",
|
||||
info = TRUE,
|
||||
icu_exclude = TRUE),
|
||||
na.rm = TRUE),
|
||||
941)
|
||||
col_mo = "mo",
|
||||
col_date = "date",
|
||||
col_patient_id = "patient",
|
||||
col_icu = example_isolates$ward == "ICU",
|
||||
info = TRUE,
|
||||
icu_exclude = TRUE
|
||||
),
|
||||
na.rm = TRUE
|
||||
),
|
||||
941
|
||||
)
|
||||
|
||||
# set 1500 random observations to be of specimen type 'Urine'
|
||||
random_rows <- sample(x = 1:2000, size = 1500, replace = FALSE)
|
||||
@ -65,78 +84,98 @@ x <- example_isolates
|
||||
x$specimen <- "Other"
|
||||
x[random_rows, "specimen"] <- "Urine"
|
||||
expect_true(
|
||||
sum(first_isolate(x = x,
|
||||
col_date = "date",
|
||||
col_patient_id = "patient",
|
||||
col_mo = "mo",
|
||||
col_specimen = "specimen",
|
||||
filter_specimen = "Urine",
|
||||
info = TRUE), na.rm = TRUE) < 1501)
|
||||
sum(first_isolate(
|
||||
x = x,
|
||||
col_date = "date",
|
||||
col_patient_id = "patient",
|
||||
col_mo = "mo",
|
||||
col_specimen = "specimen",
|
||||
filter_specimen = "Urine",
|
||||
info = TRUE
|
||||
), na.rm = TRUE) < 1501
|
||||
)
|
||||
# same, but now exclude ICU
|
||||
expect_true(
|
||||
sum(first_isolate(x = x,
|
||||
col_date = "date",
|
||||
col_patient_id = "patient",
|
||||
col_mo = "mo",
|
||||
col_specimen = "specimen",
|
||||
filter_specimen = "Urine",
|
||||
col_icu = x$ward == "ICU",
|
||||
icu_exclude = TRUE,
|
||||
info = TRUE), na.rm = TRUE) < 1501)
|
||||
sum(first_isolate(
|
||||
x = x,
|
||||
col_date = "date",
|
||||
col_patient_id = "patient",
|
||||
col_mo = "mo",
|
||||
col_specimen = "specimen",
|
||||
filter_specimen = "Urine",
|
||||
col_icu = x$ward == "ICU",
|
||||
icu_exclude = TRUE,
|
||||
info = TRUE
|
||||
), na.rm = TRUE) < 1501
|
||||
)
|
||||
|
||||
# "No isolates found"
|
||||
test_iso <- example_isolates
|
||||
test_iso$specimen <- "test"
|
||||
expect_message(first_isolate(test_iso,
|
||||
"date",
|
||||
"patient",
|
||||
col_mo = "mo",
|
||||
col_specimen = "specimen",
|
||||
filter_specimen = "something_unexisting",
|
||||
info = TRUE))
|
||||
expect_message(first_isolate(test_iso,
|
||||
"date",
|
||||
"patient",
|
||||
col_mo = "mo",
|
||||
col_specimen = "specimen",
|
||||
filter_specimen = "something_unexisting",
|
||||
info = TRUE
|
||||
))
|
||||
|
||||
# printing of exclusion message
|
||||
expect_message(first_isolate(example_isolates,
|
||||
col_date = "date",
|
||||
col_mo = "mo",
|
||||
col_patient_id = "patient",
|
||||
col_testcode = "gender",
|
||||
testcodes_exclude = "M",
|
||||
info = TRUE))
|
||||
col_date = "date",
|
||||
col_mo = "mo",
|
||||
col_patient_id = "patient",
|
||||
col_testcode = "gender",
|
||||
testcodes_exclude = "M",
|
||||
info = TRUE
|
||||
))
|
||||
|
||||
# errors
|
||||
expect_error(first_isolate("date", "patient", col_mo = "mo"))
|
||||
expect_error(first_isolate(example_isolates,
|
||||
col_date = "non-existing col",
|
||||
col_mo = "mo"))
|
||||
col_date = "non-existing col",
|
||||
col_mo = "mo"
|
||||
))
|
||||
|
||||
if (AMR:::pkg_is_available("dplyr", min_version = "1.0.0")) {
|
||||
# if mo is not an mo class, result should be the same
|
||||
expect_identical(example_isolates %>%
|
||||
mutate(mo = as.character(mo)) %>%
|
||||
first_isolate(col_date = "date",
|
||||
col_mo = "mo",
|
||||
col_patient_id = "patient",
|
||||
info = FALSE),
|
||||
example_isolates %>%
|
||||
first_isolate(col_date = "date",
|
||||
col_mo = "mo",
|
||||
col_patient_id = "patient",
|
||||
info = FALSE))
|
||||
|
||||
expect_identical(
|
||||
example_isolates %>%
|
||||
mutate(mo = as.character(mo)) %>%
|
||||
first_isolate(
|
||||
col_date = "date",
|
||||
col_mo = "mo",
|
||||
col_patient_id = "patient",
|
||||
info = FALSE
|
||||
),
|
||||
example_isolates %>%
|
||||
first_isolate(
|
||||
col_date = "date",
|
||||
col_mo = "mo",
|
||||
col_patient_id = "patient",
|
||||
info = FALSE
|
||||
)
|
||||
)
|
||||
|
||||
# support for WHONET
|
||||
expect_message(example_isolates %>%
|
||||
select(-patient_id) %>%
|
||||
mutate(`First name` = "test",
|
||||
`Last name` = "test",
|
||||
Sex = "Female") %>%
|
||||
first_isolate(info = TRUE))
|
||||
|
||||
select(-patient_id) %>%
|
||||
mutate(
|
||||
`First name` = "test",
|
||||
`Last name` = "test",
|
||||
Sex = "Female"
|
||||
) %>%
|
||||
first_isolate(info = TRUE))
|
||||
|
||||
# groups
|
||||
x <- example_isolates %>% group_by(ward) %>% mutate(first = first_isolate())
|
||||
y <- example_isolates %>% group_by(ward) %>% mutate(first = first_isolate(.))
|
||||
x <- example_isolates %>%
|
||||
group_by(ward) %>%
|
||||
mutate(first = first_isolate())
|
||||
y <- example_isolates %>%
|
||||
group_by(ward) %>%
|
||||
mutate(first = first_isolate(.))
|
||||
expect_identical(x, y)
|
||||
|
||||
}
|
||||
|
||||
# missing dates should be no problem
|
||||
@ -144,33 +183,47 @@ df <- example_isolates
|
||||
df[1:100, "date"] <- NA
|
||||
expect_equal(
|
||||
sum(
|
||||
first_isolate(x = df,
|
||||
col_date = "date",
|
||||
col_patient_id = "patient",
|
||||
col_mo = "mo",
|
||||
info = TRUE),
|
||||
na.rm = TRUE),
|
||||
1382)
|
||||
first_isolate(
|
||||
x = df,
|
||||
col_date = "date",
|
||||
col_patient_id = "patient",
|
||||
col_mo = "mo",
|
||||
info = TRUE
|
||||
),
|
||||
na.rm = TRUE
|
||||
),
|
||||
1382
|
||||
)
|
||||
|
||||
# unknown MOs
|
||||
test_unknown <- example_isolates
|
||||
test_unknown$mo <- ifelse(test_unknown$mo == "B_ESCHR_COLI", "UNKNOWN", test_unknown$mo)
|
||||
expect_equal(sum(first_isolate(test_unknown, include_unknown = FALSE)),
|
||||
1108)
|
||||
expect_equal(sum(first_isolate(test_unknown, include_unknown = TRUE)),
|
||||
1591)
|
||||
expect_equal(
|
||||
sum(first_isolate(test_unknown, include_unknown = FALSE)),
|
||||
1108
|
||||
)
|
||||
expect_equal(
|
||||
sum(first_isolate(test_unknown, include_unknown = TRUE)),
|
||||
1591
|
||||
)
|
||||
|
||||
test_unknown$mo <- ifelse(test_unknown$mo == "UNKNOWN", NA, test_unknown$mo)
|
||||
expect_equal(sum(first_isolate(test_unknown)),
|
||||
1108)
|
||||
expect_equal(
|
||||
sum(first_isolate(test_unknown)),
|
||||
1108
|
||||
)
|
||||
|
||||
# empty rsi results
|
||||
expect_equal(sum(first_isolate(example_isolates, include_untested_rsi = FALSE)),
|
||||
1366)
|
||||
expect_equal(
|
||||
sum(first_isolate(example_isolates, include_untested_rsi = FALSE)),
|
||||
1366
|
||||
)
|
||||
|
||||
# shortcuts
|
||||
expect_identical(filter_first_isolate(example_isolates),
|
||||
subset(example_isolates, first_isolate(example_isolates)))
|
||||
expect_identical(
|
||||
filter_first_isolate(example_isolates),
|
||||
subset(example_isolates, first_isolate(example_isolates))
|
||||
)
|
||||
|
||||
|
||||
# notice that all mo's are distinct, so all are TRUE
|
||||
|
Reference in New Issue
Block a user