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_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.4 8.8 23.0 8.8 30.0 100
# as.mo("stau") 31.0 31.0 42.0 35.0 54.0 60
# as.mo("STAU") 31.0 32.0 39.0 33.0 53.0 56
# as.mo("staaur") 8.5 8.9 11.0 9.0 9.2 33
# as.mo("STAAUR") 8.6 8.8 9.4 9.3 9.4 11
# as.mo("S. aureus") 23.0 24.0 28.0 26.0 28.0 51
# as.mo("S aureus") 23.0 24.0 29.0 25.0 25.0 51
# as.mo("Staphylococcus aureus") 28.0 29.0 30.0 29.0 30.0 32
# as.mo("Staphylococcus aureus (MRSA)") 600.0 620.0 640.0 620.0 640.0 800
# as.mo("Sthafilokkockus aaureuz") 320.0 340.0 370.0 350.0 400.0 450
# as.mo("MRSA") 8.7 8.7 11.0 9.2 9.9 31
# as.mo("VISA") 19.0 19.0 20.0 19.0 20.0 23
# as.mo("VRSA") 19.0 19.0 25.0 20.0 28.0 44
# as.mo(22242419) 18.0 19.0 47.0 31.0 48.0 190
# 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 Thermus islandicus (B_THERMS_ISLN
), 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") 1300 1500 1500 1500 1500 1600 10
# as.mo("THEISL") 1400 1500 1500 1500 1500 1600 10
# as.mo("T. islandicus") 410 410 430 410 450 500 10
# as.mo("T. islandicus") 410 420 430 420 440 440 10
# as.mo("Thermus islandicus") 30 30 31 31 32 35 10
That takes 8.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):
# Warning:
# Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it.
# Warning:
# Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it.
# Warning:
# Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it.
# Warning:
# Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it.
# Warning:
# Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it.
# Warning:
# Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it.
# Warning:
# Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it.
# Warning:
# Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it.
# Warning:
# Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it.
# Warning:
# Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it.
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:
# NOTE: results are saved to /Users/msberends/Library/R/3.6/library/AMR/mo_history/mo_history.csv.
# Warning:
# Result of one value was guessed with uncertainty. Use mo_uncertainties() to review it.
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) 604 632 655 644 660 764 10
So transforming 500,000 values (!!) of 50 unique values only takes 0.64 seconds (644 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.190 6.270 6.560 6.290 7.160 7.280 10
# B 22.600 23.100 26.700 23.400 24.300 51.600 10
# C 0.704 0.765 0.827 0.829 0.906 0.913 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.456 0.476 0.484 0.483 0.488 0.516 10
# B 0.613 0.620 0.639 0.628 0.642 0.723 10
# C 0.675 0.700 0.763 0.796 0.807 0.816 10
# D 0.443 0.454 0.466 0.467 0.477 0.497 10
# E 0.453 0.459 0.465 0.464 0.472 0.483 10
# F 0.433 0.447 0.464 0.463 0.484 0.498 10
# G 0.453 0.460 0.469 0.464 0.478 0.502 10
# H 0.433 0.451 0.477 0.459 0.470 0.662 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.18 18.24 18.54 18.44 18.58 19.41 10
# de 19.52 19.79 20.03 19.90 20.15 20.95 10
# nl 24.54 24.94 26.29 25.70 26.35 31.90 10
# es 19.52 19.69 22.29 19.86 20.27 44.03 10
# it 19.52 19.57 22.05 19.74 20.46 41.43 10
# fr 19.61 19.67 22.28 20.04 20.30 42.90 10
# pt 19.51 19.79 25.16 20.01 20.73 49.48 10
Currently supported are German, Dutch, Spanish, Italian, French and Portuguese.