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 intelligent rules 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:
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 = 2)
# Unit: milliseconds
# expr min lq mean median uq max neval
# as.mo("sau") 8.6 9.2 9.9 9.7 11.0 12 10
# as.mo("stau") 32.0 33.0 33.0 33.0 34.0 35 10
# as.mo("staaur") 8.7 9.0 13.0 9.6 11.0 26 10
# as.mo("STAAUR") 8.6 9.1 13.0 9.5 9.7 28 10
# as.mo("S. aureus") 24.0 24.0 28.0 25.0 26.0 41 10
# as.mo("S. aureus") 24.0 24.0 37.0 24.0 39.0 120 10
# as.mo("Staphylococcus aureus") 4.0 4.2 6.0 4.3 4.6 21 10
In the table above, all measurements are shown in milliseconds (thousands of seconds). A value of 5 milliseconds means it can determine 200 input values per second. It case of 100 milliseconds, this is only 10 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 one is a WHONET code) or common laboratory codes, or common full organism names like the last one. Full organism names are always preferred.
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 = 2)
# Unit: milliseconds
# expr min lq mean median uq max neval
# as.mo("theisl") 270 280 290 290 310 320 10
# as.mo("THEISL") 280 290 300 290 300 310 10
# as.mo("T. islandicus") 140 140 150 140 160 170 10
# as.mo("T. islandicus") 140 150 160 160 160 170 10
# as.mo("Thermus islandicus") 48 49 60 60 68 77 10
That takes 9.5 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 = 10),
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 are unique values that are present more than once. Unique values will only be calculated once by as.mo()
. We will use mo_name()
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_name(x),
times = 10)
print(run_it, unit = "ms", signif = 3)
# Unit: milliseconds
# expr min lq mean median uq max neval
# mo_name(x) 596 622 635 626 635 704 10
So transforming 500,000 values (!!) of 50 unique values only takes 0.63 seconds (626 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_name()
, it almost doesn’t take any time at all (see ‘C’ below):
run_it <- microbenchmark(A = mo_name("B_STPHY_AUR"),
B = mo_name("S. aureus"),
C = mo_name("Staphylococcus aureus"),
times = 10)
print(run_it, unit = "ms", signif = 3)
# Unit: milliseconds
# expr min lq mean median uq max neval
# A 6.700 6.950 7.410 7.600 7.730 8.06 10
# B 22.900 23.900 27.000 24.100 24.200 46.00 10
# C 0.772 0.833 0.876 0.874 0.918 1.03 10
So going from mo_name("Staphylococcus aureus")
to "Staphylococcus aureus"
takes 0.0009 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_name("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.472 0.475 0.553 0.562 0.607 0.675 10
# B 0.474 0.482 0.560 0.493 0.577 0.973 10
# C 0.766 0.820 0.899 0.881 0.941 1.070 10
# D 0.459 0.485 0.539 0.503 0.569 0.744 10
# E 0.421 0.448 0.470 0.456 0.488 0.543 10
# F 0.430 0.457 0.534 0.495 0.592 0.738 10
# G 0.420 0.450 0.477 0.463 0.491 0.586 10
# H 0.426 0.437 0.500 0.447 0.461 0.776 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_name("CoNS", language = "en") # or just mo_name("CoNS") on an English system
# [1] "Coagulase-negative Staphylococcus (CoNS)"
mo_name("CoNS", language = "es") # or just mo_name("CoNS") on a Spanish system
# [1] "Staphylococcus coagulasa negativo (SCN)"
mo_name("CoNS", language = "nl") # or just mo_name("CoNS") on a Dutch system
# [1] "Coagulase-negatieve Staphylococcus (CNS)"
run_it <- microbenchmark(en = mo_name("CoNS", language = "en"),
de = mo_name("CoNS", language = "de"),
nl = mo_name("CoNS", language = "nl"),
es = mo_name("CoNS", language = "es"),
it = mo_name("CoNS", language = "it"),
fr = mo_name("CoNS", language = "fr"),
pt = mo_name("CoNS", language = "pt"),
times = 10)
print(run_it, unit = "ms", signif = 4)
# Unit: milliseconds
# expr min lq mean median uq max neval
# en 18.77 19.83 25.36 20.44 28.51 45.88 10
# de 20.37 20.86 23.19 21.45 22.23 39.08 10
# nl 26.50 27.09 27.74 27.58 28.10 29.74 10
# es 20.82 21.18 21.66 21.27 22.04 23.64 10
# it 19.82 20.65 25.98 21.22 22.13 50.42 10
# fr 20.07 21.20 21.68 21.47 21.89 23.76 10
# pt 19.87 20.63 22.76 21.07 21.68 38.11 10
Currently supported are German, Dutch, Spanish, Italian, French and Portuguese.