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") 16.4 16.5 28.4 16.8 54.9 55.8 10
#> as.mo("stau") 86.0 86.5 97.3 88.9 100.0 132.0 10
#> as.mo("staaur") 16.3 16.6 23.0 16.8 21.0 56.8 10
#> as.mo("S. aureus") 25.3 25.9 39.4 27.2 64.0 74.5 10
#> as.mo("S. aureus") 25.2 25.3 29.7 25.6 27.0 64.3 10
#> as.mo("STAAUR") 16.3 16.7 21.1 17.0 18.0 56.8 10
#> as.mo("Staphylococcus aureus") 13.5 13.9 20.5 14.4 17.5 51.2 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 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
#> as.mo("myle") 135.0 135.0 147.0 135.0 174.0 176.0
#> as.mo("mycleo") 211.0 213.0 233.0 232.0 251.0 262.0
#> as.mo("M. leonicaptivi") 59.2 59.2 63.6 59.4 59.6 98.7
#> as.mo("M. leonicaptivi") 59.0 59.1 59.3 59.3 59.3 59.7
#> as.mo("MYCLEO") 211.0 211.0 220.0 211.0 222.0 250.0
#> as.mo("Mycoplasma leonicaptivi") 22.5 22.5 26.5 22.6 22.7 61.0
#> neval
#> 10
#> 10
#> 10
#> 10
#> 10
#> 10
That takes 3.4 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) 610 641 644 644 657 665 10
So transforming 500,000 values (!) of 95 unique values only takes 0.64 seconds (644 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 7.500 7.540 7.720 7.610 7.710 8.750 10
#> B 25.800 26.200 27.100 27.800 27.800 27.900 10
#> C 0.604 0.628 0.704 0.729 0.755 0.791 10
So going from mo_fullname("Staphylococcus aureus")
to "Staphylococcus aureus"
takes 0.0007 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.704 0.806 0.897 0.867 1.040 1.130 10
#> B 0.671 0.722 0.841 0.807 0.903 1.110 10
#> C 0.702 0.768 0.856 0.816 0.967 1.160 10
#> D 0.641 0.695 0.746 0.746 0.755 0.976 10
#> E 0.627 0.702 0.781 0.762 0.789 1.100 10
#> F 0.651 0.694 0.779 0.733 0.761 1.220 10
#> G 0.552 0.745 0.801 0.764 0.815 1.090 10
#> H 0.637 0.661 0.722 0.724 0.766 0.803 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 18.95 18.99 19.59 19.04 19.49 23.47 10
#> de 27.13 27.26 27.69 27.63 27.88 29.05 10
#> nl 26.95 27.62 34.90 27.99 32.35 61.05 10
#> es 27.42 27.55 38.05 27.75 60.86 61.40 10
#> it 26.99 27.30 34.13 27.48 27.70 61.28 10
#> fr 27.35 27.52 30.95 27.58 27.72 61.11 10
#> pt 27.26 27.33 27.67 27.53 27.91 28.44 10
Currently supported are German, Dutch, Spanish, Italian, French and Portuguese.