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 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"), # 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
# as.mo("sau") 8.4 8.8 11.0 9.1 9.9
# as.mo("stau") 32.0 33.0 37.0 34.0 35.0
# as.mo("STAU") 32.0 33.0 40.0 34.0 52.0
# as.mo("staaur") 9.1 9.2 9.4 9.3 9.4
# as.mo("STAAUR") 8.6 9.1 11.0 9.3 9.7
# as.mo("S. aureus") 24.0 25.0 33.0 26.0 45.0
# as.mo("S aureus") 24.0 25.0 27.0 25.0 26.0
# as.mo("Staphylococcus aureus") 4.0 4.1 4.5 4.3 4.6
# as.mo("Staphylococcus aureus (MRSA)") 1500.0 1600.0 1700.0 1700.0 1700.0
# as.mo("Sthafilokkockus aaureuz") 550.0 550.0 560.0 560.0 580.0
# as.mo("MRSA") 8.7 9.0 9.4 9.2 10.0
# as.mo("VISA") 19.0 20.0 26.0 21.0 37.0
# as.mo("VRSA") 19.0 19.0 20.0 20.0 20.0
# as.mo(22242419) 18.0 18.0 24.0 20.0 21.0
# max neval
# 27.0 10
# 61.0 10
# 58.0 10
# 10.0 10
# 26.0 10
# 48.0 10
# 42.0 10
# 5.9 10
# 1700.0 10
# 590.0 10
# 10.0 10
# 41.0 10
# 21.0 10
# 45.0 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") 300 300 320 320 330 340 10
# as.mo("THEISL") 290 320 330 320 330 400 10
# as.mo("T. islandicus") 150 150 160 170 170 170 10
# as.mo("T. islandicus") 140 150 150 150 160 170 10
# as.mo("Thermus islandicus") 53 55 65 59 76 80 10
That takes 1.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 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 uncommon):
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) 625 659 686 689 698 746 10
So transforming 500,000 values (!!) of 50 unique values only takes 0.69 seconds (688 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.930 7.380 7.87 7.580 8.20 9.54 10
# B 23.700 25.700 30.50 26.000 29.60 59.00 10
# C 0.859 0.912 0.99 0.958 1.05 1.24 10
So going from mo_name("Staphylococcus aureus")
to "Staphylococcus aureus"
takes 0.001 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.478 0.490 0.512 0.509 0.530 0.579 10
# B 0.644 0.670 0.711 0.673 0.740 0.930 10
# C 0.697 0.815 0.887 0.840 0.992 1.150 10
# D 0.456 0.486 0.511 0.503 0.521 0.643 10
# E 0.465 0.477 0.534 0.497 0.556 0.804 10
# F 0.459 0.472 0.490 0.477 0.509 0.556 10
# G 0.466 0.488 0.526 0.522 0.544 0.637 10
# H 0.455 0.455 0.476 0.465 0.485 0.535 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 19.02 19.15 20.50 19.34 21.18 27.84 10
# de 19.56 20.25 28.34 22.84 39.77 50.78 10
# nl 26.12 26.25 27.73 27.17 28.60 31.65 10
# es 20.02 20.58 22.36 22.26 23.95 25.71 10
# it 20.30 20.47 23.21 21.23 24.56 34.71 10
# fr 20.14 20.56 31.97 21.47 23.47 125.00 10
# pt 19.83 20.24 22.74 20.46 22.65 38.93 10
Currently supported are German, Dutch, Spanish, Italian, French and Portuguese.