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 into the microbial code of Staphylococcus aureus. Coercion is a computational process of forcing output based on an input. For microorganism names, coercing user input to taxonomically valid microorganism names is crucial to ensure correct interpretation and to enable grouping based on taxonomic properties.
The actual result is the same every time: it returns its microorganism code B_STPHY_AURS
(B stands for Bacteria, the taxonomic kingdom).
But the calculation time differs a lot:
S.aureus <- microbenchmark(
as.mo("sau"), # WHONET code
as.mo("stau"),
as.mo("STAU"),
as.mo("staaur"),
as.mo("STAAUR"),
as.mo("S. aureus"),
as.mo("S aureus"),
as.mo("Staphylococcus aureus"), # official taxonomic name
as.mo("Staphylococcus aureus (MRSA)"), # additional text
as.mo("Sthafilokkockus aaureuz"), # incorrect spelling
as.mo("MRSA"), # Methicillin Resistant S. aureus
as.mo("VISA"), # Vancomycin Intermediate S. aureus
as.mo("VRSA"), # Vancomycin Resistant S. aureus
as.mo(22242419), # Catalogue of Life ID
times = 10)
print(S.aureus, unit = "ms", signif = 2)
# Unit: milliseconds
# expr min lq mean median uq max
# as.mo("sau") 9.1 9.4 15.0 9.7 10.0 38.0
# as.mo("stau") 33.0 34.0 43.0 36.0 58.0 68.0
# as.mo("STAU") 33.0 35.0 52.0 39.0 56.0 130.0
# as.mo("staaur") 9.0 9.5 13.0 9.9 11.0 36.0
# as.mo("STAAUR") 8.8 9.3 12.0 9.9 10.0 32.0
# as.mo("S. aureus") 9.9 10.0 11.0 11.0 11.0 13.0
# as.mo("S aureus") 10.0 11.0 20.0 12.0 36.0 42.0
# as.mo("Staphylococcus aureus") 4.4 4.7 4.9 5.0 5.1 5.3
# as.mo("Staphylococcus aureus (MRSA)") 640.0 660.0 710.0 680.0 720.0 890.0
# as.mo("Sthafilokkockus aaureuz") 360.0 370.0 390.0 380.0 410.0 460.0
# as.mo("MRSA") 8.9 9.4 9.6 9.5 9.9 10.0
# as.mo("VISA") 21.0 22.0 28.0 24.0 27.0 50.0
# as.mo("VRSA") 20.0 21.0 33.0 22.0 45.0 63.0
# as.mo(22242419) 19.0 20.0 26.0 21.0 27.0 45.0
# neval
# 10
# 10
# 10
# 10
# 10
# 10
# 10
# 10
# 10
# 10
# 10
# 10
# 10
# 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 Methanosarcina semesiae (B_MTHNSR_SEMS
), a bug probably never found before in humans:
M.semesiae <- microbenchmark(as.mo("metsem"),
as.mo("METSEM"),
as.mo("M. semesiae"),
as.mo("M. semesiae"),
as.mo("Methanosarcina semesiae"),
times = 10)
print(M.semesiae, unit = "ms", signif = 4)
# Unit: milliseconds
# expr min lq mean median uq
# as.mo("metsem") 1406.000 1502.000 1535.000 1531.000 1607.000
# as.mo("METSEM") 1492.000 1535.000 1575.000 1581.000 1613.000
# as.mo("M. semesiae") 11.060 11.130 14.330 11.460 12.450
# as.mo("M. semesiae") 10.810 10.860 18.920 11.270 35.510
# as.mo("Methanosarcina semesiae") 5.155 5.427 8.507 5.601 6.023
# max neval
# 1616.00 10
# 1631.00 10
# 37.45 10
# 39.12 10
# 32.03 10
That takes 6.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 Methanosarcina semesiae) are always very fast and only take some thousands of seconds to coerce - they 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 Methanosarcina semesiae (which is uncommon):
The highest outliers are the first times. All next determinations were done in only thousands of seconds, because the as.mo()
function learns from its own output to speed up determinations for next times.
In below figure, this effect was disabled to show the difference with the boxplot above:
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 example_isolates data set
x <- example_isolates$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 = 100)
print(run_it, unit = "ms", signif = 3)
# Unit: milliseconds
# expr min lq mean median uq max neval
# mo_name(x) 570 630 657 650 674 777 100
So transforming 500,000 values (!!) of 50 unique values only takes 0.65 seconds (649 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_AURS"),
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.730 6.830 7.540 7.000 8.340 8.98 10
# B 11.600 11.700 15.500 11.900 13.200 44.90 10
# C 0.781 0.796 0.892 0.823 0.987 1.15 10
So going from mo_name("Staphylococcus aureus")
to "Staphylococcus aureus"
takes 0.0008 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.464 0.483 0.491 0.485 0.503 0.533 10
# B 0.504 0.515 0.533 0.528 0.534 0.612 10
# C 0.762 0.771 0.794 0.795 0.815 0.820 10
# D 0.498 0.503 0.529 0.530 0.541 0.584 10
# E 0.478 0.481 0.494 0.495 0.498 0.534 10
# F 0.471 0.477 0.484 0.481 0.490 0.517 10
# G 0.468 0.474 0.483 0.480 0.482 0.530 10
# H 0.464 0.476 0.503 0.482 0.492 0.705 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 = 100)
print(run_it, unit = "ms", signif = 4)
# Unit: milliseconds
# expr min lq mean median uq max neval
# en 21.72 22.92 27.04 23.44 24.14 62.83 100
# de 23.14 24.37 28.32 24.79 25.84 57.85 100
# nl 28.85 30.61 35.40 31.25 32.79 64.85 100
# es 23.17 24.51 28.62 24.85 25.51 65.76 100
# it 23.04 24.25 30.77 24.82 27.58 58.95 100
# fr 22.88 24.37 27.90 24.94 25.53 61.63 100
# pt 23.16 24.54 29.65 24.90 25.59 156.50 100
Currently supported are German, Dutch, Spanish, Italian, French and Portuguese.