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("STAAUR"),
as.mo("S. aureus"),
as.mo("S. aureus"),
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") 13.40 13.60 17.8 13.60 13.80 51.6 10
#> as.mo("stau") 83.00 83.30 96.5 85.30 88.40 163.0 10
#> as.mo("staaur") 13.50 13.50 19.1 13.70 14.90 51.5 10
#> as.mo("STAAUR") 13.50 13.50 14.1 13.60 13.70 18.2 10
#> as.mo("S. aureus") 21.40 21.40 22.1 21.50 21.70 25.4 10
#> as.mo("S. aureus") 21.40 21.40 25.7 21.60 23.30 60.1 10
#> as.mo("Staphylococcus aureus") 5.63 5.87 15.2 5.94 8.32 57.8 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 second input is the only one that has to be looked up thoroughly. All the others are known codes (the first is a WHONET code) or common laboratory codes, or common full organism names like the last one.
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 Thermus islandicus (B_THERMS_ISL
), a bug probably never found before in humans:
T.islandicus <- microbenchmark(as.mo("theisl"),
as.mo("THEISL"),
as.mo("T. islandicus"),
as.mo("T. islandicus"),
as.mo("Thermus islandicus"),
times = 10)
print(T.islandicus, unit = "ms", signif = 3)
#> Unit: milliseconds
#> expr min lq mean median uq max neval
#> as.mo("theisl") 448.0 486.0 483.0 489.0 490.0 510.0 10
#> as.mo("THEISL") 447.0 489.0 487.0 491.0 493.0 499.0 10
#> as.mo("T. islandicus") 78.0 78.2 78.9 78.7 78.9 82.3 10
#> as.mo("T. islandicus") 78.1 78.3 84.4 78.8 81.3 129.0 10
#> as.mo("Thermus islandicus") 61.8 62.1 75.4 62.8 104.0 109.0 10
That takes 8 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. Full names (like Thermus islandicus) are almost fast - these are the most probable input from most data sets.
In the figure below, we compare Escherichia coli (which is very common) with Prevotella brevis (which is moderately common) and with Thermus islandicus (which is very uncommon):
par(mar = c(5, 16, 4, 2)) # set more space for left margin text (16)
boxplot(microbenchmark(as.mo("Thermus islandicus"),
as.mo("Prevotella brevis"),
as.mo("Escherichia coli"),
as.mo("T. islandicus"),
as.mo("P. brevis"),
as.mo("E. 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 all MO codes from the septic_patients data set
x <- septic_patients$mo %>%
# keep only the unique ones
unique() %>%
# pick 50 of them at random
sample(50) %>%
# paste that 10,000 times
rep(10000) %>%
# scramble it
sample()
# got indeed 50 times 10,000 = half a million?
length(x)
#> [1] 500000
# and how many unique values do we have?
n_distinct(x)
#> [1] 50
# 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) 741 746 806 778 827 968 10
So transforming 500,000 values (!!) of 50 unique values only takes 0.78 seconds (778 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 10.200 10.300 10.600 10.400 11.00 11.300 10
#> B 20.500 20.700 21.300 21.400 22.00 22.100 10
#> C 0.308 0.504 0.589 0.591 0.73 0.863 10
So going from mo_fullname("Staphylococcus aureus")
to "Staphylococcus aureus"
takes 0.0006 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.318 0.340 0.388 0.382 0.434 0.474 10
#> B 0.339 0.362 0.424 0.428 0.449 0.555 10
#> C 0.331 0.369 0.522 0.526 0.637 0.673 10
#> D 0.269 0.278 0.313 0.300 0.353 0.384 10
#> E 0.252 0.266 0.322 0.302 0.349 0.448 10
#> F 0.241 0.264 0.310 0.313 0.347 0.379 10
#> G 0.241 0.258 0.310 0.317 0.355 0.386 10
#> H 0.278 0.289 0.316 0.313 0.334 0.375 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 13.23 13.57 16.92 13.69 13.73 46.78 10
#> de 22.09 22.20 25.72 22.32 23.16 55.31 10
#> nl 21.66 22.03 22.12 22.15 22.20 22.52 10
#> es 21.67 22.07 22.32 22.16 22.45 23.26 10
#> it 21.64 21.86 22.35 22.21 22.48 23.90 10
#> fr 21.70 22.10 28.72 22.21 22.33 55.28 10
#> pt 21.78 22.12 28.83 22.19 22.21 55.99 10
Currently supported are German, Dutch, Spanish, Italian, French and Portuguese.