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 neval
# as.mo("sau") 9.1 9.3 15 9.8 13.0 34 10
# as.mo("stau") 33.0 34.0 40 35.0 39.0 60 10
# as.mo("STAU") 33.0 34.0 51 35.0 57.0 150 10
# as.mo("staaur") 8.8 9.2 16 10.0 13.0 43 10
# as.mo("STAAUR") 9.3 9.4 23 9.7 10.0 120 10
# as.mo("S. aureus") 10.0 10.0 16 11.0 12.0 41 10
# as.mo("S aureus") 10.0 10.0 28 12.0 35.0 110 10
# as.mo("Staphylococcus aureus") 4.6 4.8 10 4.9 5.2 56 10
# as.mo("Staphylococcus aureus (MRSA)") 660.0 670.0 700 680.0 710.0 770 10
# as.mo("Sthafilokkockus aaureuz") 330.0 350.0 370 370.0 390.0 430 10
# as.mo("MRSA") 9.2 9.2 14 9.4 9.5 35 10
# as.mo("VISA") 20.0 20.0 26 21.0 23.0 45 10
# as.mo("VRSA") 20.0 21.0 30 22.0 44.0 47 10
# as.mo(22242419) 19.0 20.0 43 20.0 28.0 130 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") 1469.000 1482.000 1515.000 1507.000 1545.000
# as.mo("METSEM") 1435.000 1452.000 1490.000 1479.000 1520.000
# as.mo("M. semesiae") 10.840 11.090 16.220 11.150 11.600
# as.mo("M. semesiae") 10.670 10.820 20.140 11.180 38.040
# as.mo("Methanosarcina semesiae") 5.138 5.185 7.838 5.366 5.493
# max neval
# 1574.00 10
# 1563.00 10
# 36.46 10
# 44.17 10
# 30.28 10
That takes 6.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. 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) 548 581 605 593 611 735 100
So transforming 500,000 values (!!) of 50 unique values only takes 0.59 seconds (593 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.380 6.430 6.800 6.540 6.690 8.94 10
# B 10.900 10.900 14.200 11.100 11.400 37.20 10
# C 0.735 0.772 0.832 0.792 0.875 1.01 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.445 0.453 0.463 0.464 0.467 0.492 10
# B 0.484 0.496 0.522 0.502 0.505 0.724 10
# C 0.667 0.746 0.755 0.758 0.786 0.800 10
# D 0.488 0.491 0.507 0.505 0.509 0.558 10
# E 0.454 0.455 0.462 0.461 0.465 0.490 10
# F 0.432 0.447 0.456 0.458 0.459 0.490 10
# G 0.438 0.446 0.456 0.454 0.460 0.486 10
# H 0.439 0.442 0.454 0.450 0.459 0.501 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.02 22.35 26.62 22.93 23.58 55.03 100
# de 22.22 23.82 29.38 24.32 25.30 60.12 100
# nl 27.58 28.91 33.76 30.03 30.78 144.40 100
# es 22.31 23.57 28.23 24.31 25.96 53.68 100
# it 22.02 23.74 30.82 24.32 26.97 158.30 100
# fr 22.38 23.39 28.85 24.29 25.71 143.30 100
# pt 22.14 23.44 27.47 24.17 25.12 56.93 100
Currently supported are German, Dutch, Spanish, Italian, French and Portuguese.