benchmarks.Rmd
One of the most important features of this package is the complete microbial taxonomic database, supplied by the Catalogue of Life (http://catalogueoflife.org). We created a function as.mo()
that transforms any user input value to a valid microbial ID by using AI (Artificial Intelligence) combined with the taxonomic tree of Catalogue of Life.
Using the microbenchmark
package, we can review the calculation performance of this function. Its function microbenchmark()
runs different input expressions independently of each other and measures their time-to-result.
In the next test, we try to ‘coerce’ different input values for Staphylococcus aureus. The actual result is the same every time: it returns its MO code B_STPHY_AUR
(B stands for Bacteria, the taxonomic kingdom).
But the calculation time differs a lot. Here, the AI effect can be reviewed best:
S.aureus <- microbenchmark(as.mo("sau"),
as.mo("stau"),
as.mo("staaur"),
as.mo("S. aureus"),
as.mo("S. aureus"),
as.mo("STAAUR"),
as.mo("Staphylococcus aureus"),
as.mo("B_STPHY_AUR"),
times = 10)
print(S.aureus, unit = "ms", signif = 3)
#> Unit: milliseconds
#> expr min lq mean median uq max
#> as.mo("sau") 42.300 42.500 47.00 43.100 43.200 82.000
#> as.mo("stau") 75.900 76.100 82.70 76.700 77.900 125.000
#> as.mo("staaur") 42.400 43.300 53.60 44.600 49.000 98.200
#> as.mo("S. aureus") 18.400 18.600 20.60 18.700 19.200 34.100
#> as.mo("S. aureus") 18.400 18.500 18.80 18.600 19.200 19.600
#> as.mo("STAAUR") 42.300 42.700 43.30 43.000 43.800 45.700
#> as.mo("Staphylococcus aureus") 11.400 11.500 11.80 11.600 11.800 13.400
#> as.mo("B_STPHY_AUR") 0.261 0.418 0.44 0.434 0.493 0.542
#> neval
#> 10
#> 10
#> 10
#> 10
#> 10
#> 10
#> 10
#> 10
In the table above, all measurements are shown in milliseconds (thousands of seconds). A value of 10 milliseconds means it can determine 100 input values per second. It case of 50 milliseconds, this is only 20 input values per second. The more an input value resembles a full name, the faster the result will be found. In case of as.mo("B_STPHY_AUR")
, the input is already a valid MO code, so it only almost takes no time at all (261 millionths of seconds).
To achieve this speed, the as.mo
function also takes into account the prevalence of human pathogenic microorganisms. The downside is of course that less prevalent microorganisms will be determined less fast. See this example for the ID of Mycoplasma leonicaptivi (B_MYCPL_LEO
), a bug probably never found before in humans:
M.leonicaptivi <- microbenchmark(as.mo("myle"),
as.mo("mycleo"),
as.mo("M. leonicaptivi"),
as.mo("M. leonicaptivi"),
as.mo("MYCLEO"),
as.mo("Mycoplasma leonicaptivi"),
as.mo("B_MYCPL_LEO"),
times = 10)
print(M.leonicaptivi, unit = "ms", signif = 4)
#> Unit: milliseconds
#> expr min lq mean median
#> as.mo("myle") 111.9000 112.1000 121.9000 112.4000
#> as.mo("mycleo") 381.6000 381.9000 397.9000 384.7000
#> as.mo("M. leonicaptivi") 202.9000 203.8000 205.5000 204.1000
#> as.mo("M. leonicaptivi") 203.1000 203.3000 208.7000 203.8000
#> as.mo("MYCLEO") 381.5000 381.7000 388.1000 381.9000
#> as.mo("Mycoplasma leonicaptivi") 103.0000 103.1000 103.6000 103.3000
#> as.mo("B_MYCPL_LEO") 0.3021 0.5631 0.5459 0.5664
#> uq max neval
#> 113.5000 169.7000 10
#> 420.5000 420.7000 10
#> 206.1000 215.4000 10
#> 204.6000 249.4000 10
#> 386.0000 433.7000 10
#> 103.8000 105.4000 10
#> 0.5712 0.6199 10
That takes 5.9 times as much time on average! A value of 100 milliseconds means it can only determine ~10 different input values per second. We can conclude that looking up arbitrary codes of less prevalent microorganisms is the worst way to go, in terms of calculation performance:
par(mar = c(5, 16, 4, 2)) # set more space for left margin text (16)
# highest value on y axis
max_y_axis <- max(S.aureus$time, M.leonicaptivi$time, na.rm = TRUE) / 1e6
boxplot(S.aureus, horizontal = TRUE, las = 1, unit = "ms", log = FALSE, xlab = "", ylim = c(0, max_y_axis),
main = expression(paste("Benchmark of ", italic("Staphylococcus aureus"))))
boxplot(M.leonicaptivi, horizontal = TRUE, las = 1, unit = "ms", log = FALSE, xlab = "", ylim = c(0, max_y_axis),
main = expression(paste("Benchmark of ", italic("Mycoplasma leonicaptivi"))))
To relieve this pitfall and further improve performance, two important calculations take almost no time at all: repetitive results and already precalculated results.
Repetitive results mean that unique values are present more than once. Unique values will only be calculated once by as.mo()
. We will use mo_fullname()
for this test - a helper function that returns the full microbial name (genus, species and possibly subspecies) which uses as.mo()
internally.
library(dplyr)
# take 500,000 random MO codes from the septic_patients data set
x = septic_patients %>%
sample_n(500000, replace = TRUE) %>%
pull(mo)
# got the right length?
length(x)
#> [1] 500000
# and how many unique values do we have?
n_distinct(x)
#> [1] 95
# now let's see:
run_it <- microbenchmark(mo_fullname(x),
times = 10)
print(run_it, unit = "ms", signif = 3)
#> Unit: milliseconds
#> expr min lq mean median uq max neval
#> mo_fullname(x) 438 448 467 470 476 500 10
So transforming 500,000 values (!) of 95 unique values only takes 0.47 seconds (469 ms). You only lose time on your unique input values.
What about precalculated results? If the input is an already precalculated result of a helper function like mo_fullname()
, it almost doesn’t take any time at all (see ‘C’ below):
run_it <- microbenchmark(A = mo_fullname("B_STPHY_AUR"),
B = mo_fullname("S. aureus"),
C = mo_fullname("Staphylococcus aureus"),
times = 10)
print(run_it, unit = "ms", signif = 3)
#> Unit: milliseconds
#> expr min lq mean median uq max neval
#> A 38.500 38.600 38.700 38.700 38.900 39.100 10
#> B 19.400 19.500 20.900 19.800 20.100 31.200 10
#> C 0.256 0.293 0.389 0.395 0.473 0.507 10
So going from mo_fullname("Staphylococcus aureus")
to "Staphylococcus aureus"
takes 0.0004 seconds - it doesn’t even start calculating if the result would be the same as the expected resulting value. That goes for all helper functions:
run_it <- microbenchmark(A = mo_species("aureus"),
B = mo_genus("Staphylococcus"),
C = mo_fullname("Staphylococcus aureus"),
D = mo_family("Staphylococcaceae"),
E = mo_order("Bacillales"),
F = mo_class("Bacilli"),
G = mo_phylum("Firmicutes"),
H = mo_kingdom("Bacteria"),
times = 10)
print(run_it, unit = "ms", signif = 3)
#> Unit: milliseconds
#> expr min lq mean median uq max neval
#> A 0.277 0.328 0.410 0.450 0.467 0.483 10
#> B 0.291 0.307 0.363 0.374 0.390 0.467 10
#> C 0.299 0.336 0.400 0.400 0.485 0.498 10
#> D 0.271 0.288 0.319 0.328 0.346 0.371 10
#> E 0.202 0.263 0.288 0.270 0.304 0.405 10
#> F 0.241 0.255 0.296 0.283 0.350 0.362 10
#> G 0.260 0.264 0.303 0.281 0.312 0.425 10
#> H 0.240 0.256 0.310 0.327 0.346 0.378 10
Of course, when running mo_phylum("Firmicutes")
the function has zero knowledge about the actual microorganism, namely S. aureus. But since the result would be "Firmicutes"
too, there is no point in calculating the result. And because this package ‘knows’ all phyla of all known bacteria (according to the Catalogue of Life), it can just return the initial value immediately.
When the system language is non-English and supported by this AMR
package, some functions will have a translated result. This almost does’t take extra time:
mo_fullname("CoNS", language = "en") # or just mo_fullname("CoNS") on an English system
#> [1] "Coagulase Negative Staphylococcus (CoNS)"
mo_fullname("CoNS", language = "es") # or just mo_fullname("CoNS") on a Spanish system
#> [1] "Staphylococcus coagulasa negativo (CoNS)"
mo_fullname("CoNS", language = "nl") # or just mo_fullname("CoNS") on a Dutch system
#> [1] "Coagulase-negatieve Staphylococcus (CNS)"
run_it <- microbenchmark(en = mo_fullname("CoNS", language = "en"),
de = mo_fullname("CoNS", language = "de"),
nl = mo_fullname("CoNS", language = "nl"),
es = mo_fullname("CoNS", language = "es"),
it = mo_fullname("CoNS", language = "it"),
fr = mo_fullname("CoNS", language = "fr"),
pt = mo_fullname("CoNS", language = "pt"),
times = 10)
print(run_it, unit = "ms", signif = 4)
#> Unit: milliseconds
#> expr min lq mean median uq max neval
#> en 11.01 11.04 11.05 11.06 11.07 11.08 10
#> de 19.31 19.51 19.79 19.61 19.91 21.00 10
#> nl 19.13 19.37 26.23 19.59 21.11 52.30 10
#> es 19.13 19.42 19.51 19.53 19.58 20.00 10
#> it 19.16 19.34 29.12 19.55 51.61 52.06 10
#> fr 19.01 19.54 19.84 19.69 20.41 20.46 10
#> pt 19.00 19.33 19.44 19.49 19.59 19.67 10
Currently supported are German, Dutch, Spanish, Italian, French and Portuguese.