benchmarks.Rmd
One of the most important features of this package is the complete microbial taxonomic database, supplied by the Catalogue of Life. 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"),
times = 10)
print(S.aureus, unit = "ms", signif = 3)
#> Unit: milliseconds
#> expr min lq mean median uq max neval
#> as.mo("sau") 10.4 10.5 10.7 10.6 10.7 11.2 10
#> as.mo("stau") 84.4 84.7 95.6 85.2 101.0 136.0 10
#> as.mo("staaur") 10.5 10.6 10.8 10.6 11.1 11.2 10
#> as.mo("S. aureus") 21.3 21.4 31.4 21.9 41.6 60.3 10
#> as.mo("S. aureus") 21.3 21.4 21.8 21.4 21.5 24.9 10
#> as.mo("STAAUR") 10.5 10.6 23.5 10.6 43.8 65.0 10
#> as.mo("Staphylococcus aureus") 16.1 16.2 20.7 16.4 17.5 57.7 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.
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"),
times = 10)
print(M.leonicaptivi, unit = "ms", signif = 3)
#> Unit: milliseconds
#> expr min lq mean median uq max neval
#> as.mo("myle") 131 132 132 132 133 133 10
#> as.mo("mycleo") 439 445 471 481 488 505 10
#> as.mo("M. leonicaptivi") 202 205 234 243 247 262 10
#> as.mo("M. leonicaptivi") 202 202 221 212 242 249 10
#> as.mo("MYCLEO") 441 449 469 480 486 493 10
#> as.mo("Mycoplasma leonicaptivi") 143 143 165 165 185 190 10
That takes 9.2 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.
In the figure below, we compare Escherichia coli (which is very common) with Prevotella brevis (which is moderately common) and with Mycoplasma leonicaptivi (which is very uncommon):
par(mar = c(5, 16, 4, 2)) # set more space for left margin text (16)
boxplot(microbenchmark(as.mo("M. leonicaptivi"),
as.mo("Mycoplasma leonicaptivi"),
as.mo("P. brevis"),
as.mo("Prevotella brevis"),
as.mo("E. coli"),
as.mo("Escherichia coli"),
times = 50),
horizontal = TRUE, las = 1, unit = "s", log = FALSE,
xlab = "", ylab = "Time in seconds",
main = "Benchmarks per prevalence")
Uncommon microorganisms take a lot more time than common microorganisms. 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) 618 653 729 695 813 846 10
So transforming 500,000 values (!) of 95 unique values only takes 0.69 seconds (694 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 6.460 6.560 6.660 6.650 6.720 6.950 10
#> B 22.300 22.400 22.700 22.700 22.900 23.000 10
#> C 0.254 0.263 0.378 0.396 0.413 0.563 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.303 0.338 0.414 0.431 0.453 0.550 10
#> B 0.244 0.282 0.339 0.363 0.372 0.395 10
#> C 0.302 0.404 0.437 0.430 0.490 0.527 10
#> D 0.257 0.279 0.315 0.310 0.344 0.378 10
#> E 0.219 0.270 0.306 0.298 0.355 0.377 10
#> F 0.248 0.296 0.312 0.317 0.334 0.349 10
#> G 0.228 0.248 0.287 0.278 0.336 0.367 10
#> H 0.250 0.255 0.312 0.312 0.352 0.398 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 12.45 12.60 15.94 12.66 12.69 45.75 10
#> de 20.73 20.87 24.50 21.13 21.29 54.54 10
#> nl 21.02 21.14 24.63 21.22 21.44 54.44 10
#> es 20.56 21.15 21.46 21.21 22.02 22.39 10
#> it 20.54 20.80 21.08 20.93 21.19 22.15 10
#> fr 20.86 21.11 24.55 21.21 21.45 54.12 10
#> pt 20.74 20.93 28.96 21.17 21.60 66.52 10
Currently supported are German, Dutch, Spanish, Italian, French and Portuguese.