benchmarks.Rmd
Source: https://gitlab.com/msberends/AMR/blob/master/vignettes/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 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") 8.5 8.6 11 8.7 9.1 34
# as.mo("stau") 31.0 31.0 39 31.0 56.0 58
# as.mo("STAU") 31.0 34.0 39 34.0 35.0 60
# as.mo("staaur") 8.5 8.7 15 8.9 9.6 67
# as.mo("STAAUR") 8.6 8.7 14 9.0 9.9 36
# as.mo("S. aureus") 23.0 23.0 40 25.0 26.0 180
# as.mo("S aureus") 23.0 24.0 30 26.0 30.0 51
# as.mo("Staphylococcus aureus") 28.0 28.0 31 29.0 29.0 51
# as.mo("Staphylococcus aureus (MRSA)") 570.0 600.0 620 620.0 640.0 710
# as.mo("Sthafilokkockus aaureuz") 280.0 310.0 320 320.0 330.0 340
# as.mo("MRSA") 8.4 8.6 11 8.8 9.5 35
# as.mo("VISA") 19.0 19.0 21 20.0 22.0 24
# as.mo("VRSA") 19.0 19.0 27 23.0 41.0 46
# as.mo(22242419) 18.0 18.0 22 21.0 22.0 43
# 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") 1310.00 1340.0 1361.00 1358 1387.00
# as.mo("METSEM") 1304.00 1320.0 1350.00 1341 1382.00
# as.mo("M. semesiae") 1839.00 1968.0 1990.00 2006 2032.00
# as.mo("M. semesiae") 1947.00 1978.0 2014.00 2019 2046.00
# as.mo("Methanosarcina semesiae") 30.49 31.2 35.04 32 32.81
# max neval
# 1401.00 10
# 1411.00 10
# 2049.00 10
# 2088.00 10
# 63.03 10
That takes 15.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 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 Methanosarcina semesiae (which is uncommon):
In reality, the as.mo()
functions learns from its own output to speed up determinations for next times. In above figure, this effect was disabled to show the difference with the boxplot below - when you would use as.mo()
yourself:
The highest outliers are the first times. All next determinations were done in only thousands of seconds.
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 = 10)
print(run_it, unit = "ms", signif = 3)
# Unit: milliseconds
# expr min lq mean median uq max neval
# mo_name(x) 598 639 656 657 671 735 10
So transforming 500,000 values (!!) of 50 unique values only takes 0.66 seconds (657 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.150 6.340 9.110 6.370 6.400 33.700 10
# B 22.000 22.200 22.900 22.300 22.400 28.300 10
# C 0.691 0.784 0.783 0.795 0.802 0.814 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.462 0.471 0.480 0.482 0.491 0.498 10
# B 0.609 0.627 0.645 0.638 0.657 0.714 10
# C 0.651 0.731 0.771 0.772 0.806 0.887 10
# D 0.431 0.457 0.488 0.468 0.485 0.675 10
# E 0.450 0.452 0.466 0.465 0.473 0.500 10
# F 0.461 0.466 0.481 0.474 0.495 0.514 10
# G 0.449 0.453 0.465 0.464 0.471 0.495 10
# H 0.455 0.458 0.481 0.465 0.485 0.594 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 17.93 18.18 19.34 18.76 19.02 26.27 10
# de 19.44 19.63 22.03 19.80 20.23 41.83 10
# nl 24.54 24.78 27.37 25.23 25.55 47.06 10
# es 19.51 19.94 20.27 20.20 20.55 21.16 10
# it 19.40 19.67 24.91 19.99 20.90 46.83 10
# fr 19.24 19.45 22.53 19.80 20.17 47.71 10
# pt 19.18 19.33 19.87 19.72 20.62 20.75 10
Currently supported are German, Dutch, Spanish, Italian, French and Portuguese.