Benchmarks

Matthijs S. Berends

03 June 2019

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.

library(microbenchmark)
library(AMR)

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"),
                           as.mo("stau"),
                           as.mo("staaur"),
                           as.mo("STAAUR"),
                           as.mo("S. aureus"),
                           as.mo("S.  aureus"),
                           as.mo("Staphylococcus aureus"),
                           times = 10)
print(S.aureus, unit = "ms", signif = 2)
# Unit: milliseconds
#                            expr min lq mean median   uq max neval
#                    as.mo("sau")  18 18   23   18.0 18.0  63    10
#                   as.mo("stau")  47 47   62   47.0 93.0  98    10
#                 as.mo("staaur")  18 18   18   18.0 19.0  19    10
#                 as.mo("STAAUR")  18 18   18   18.0 19.0  22    10
#              as.mo("S. aureus")  28 28   29   29.0 29.0  29    10
#             as.mo("S.  aureus")  28 29   34   29.0 31.0  74    10
#  as.mo("Staphylococcus aureus")   8  8   13    8.1  8.5  54    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") 470 470  500    490 520 520    10
#              as.mo("THEISL") 470 470  500    520 520 520    10
#       as.mo("T. islandicus")  74  74   79     74  74 120    10
#      as.mo("T.  islandicus")  74  74   89     75 120 130    10
#  as.mo("Thermus islandicus")  72  73   88     73 120 120    10

That takes 9 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 very uncommon):

par(mar = c(5, 16, 4, 2)) # set more space for left margin text (16)

boxplot(microbenchmark(as.mo("Thermus islandicus"),
                       as.mo("Prevotella brevis"),
                       as.mo("Escherichia coli"),
                       as.mo("T. islandicus"),
                       as.mo("P. brevis"),
                       as.mo("E. coli"),
                       times = 10),
        horizontal = TRUE, las = 1, unit = "s", log = FALSE,
        xlab = "", ylab = "Time in seconds",
        main = "Benchmarks per prevalence")

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

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_fullname() 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 septic_patients data set
x <- septic_patients$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_fullname(x),
                         times = 10)
print(run_it, unit = "ms", signif = 3)
# Unit: milliseconds
#            expr min  lq mean median   uq  max neval
#  mo_fullname(x) 956 992 1050   1030 1080 1250    10

So transforming 500,000 values (!!) of 50 unique values only takes 1.03 seconds (1030 ms). You only lose time on your unique input values.

Precalculated results

What about precalculated results? If the input is an already precalculated result of a helper function like mo_fullname(), it almost doesn’t take any time at all (see ‘C’ below):

run_it <- microbenchmark(A = mo_fullname("B_STPHY_AUR"),
                         B = mo_fullname("S. aureus"),
                         C = mo_fullname("Staphylococcus aureus"),
                         times = 10)
print(run_it, unit = "ms", signif = 3)
# Unit: milliseconds
#  expr   min    lq  mean median    uq   max neval
#     A 13.30 13.50 14.00  13.60 14.80 14.90    10
#     B 25.80 26.10 33.10  27.50 28.70 84.60    10
#     C  1.94  1.95  2.07   2.08  2.18  2.23    10

So going from mo_fullname("Staphylococcus aureus") to "Staphylococcus aureus" takes 0.0021 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_fullname("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.682 0.719 0.769  0.736 0.771 0.965    10
#     B 0.728 0.798 0.842  0.834 0.866 1.020    10
#     C 1.700 1.730 1.920  1.950 2.050 2.150    10
#     D 0.758 0.795 0.852  0.821 0.886 1.010    10
#     E 0.625 0.696 0.741  0.744 0.765 0.897    10
#     F 0.650 0.689 0.731  0.728 0.748 0.860    10
#     G 0.612 0.632 0.719  0.695 0.741 0.958    10
#     H 0.233 0.277 0.301  0.301 0.339 0.349    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.

Results in other languages

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_fullname("CoNS", language = "en") # or just mo_fullname("CoNS") on an English system
# [1] "Coagulase-negative Staphylococcus (CoNS)"

mo_fullname("CoNS", language = "es") # or just mo_fullname("CoNS") on a Spanish system
# [1] "Staphylococcus coagulasa negativo (SCN)"

mo_fullname("CoNS", language = "nl") # or just mo_fullname("CoNS") on a Dutch system
# [1] "Coagulase-negatieve Staphylococcus (CNS)"

run_it <- microbenchmark(en = mo_fullname("CoNS", language = "en"),
                         de = mo_fullname("CoNS", language = "de"),
                         nl = mo_fullname("CoNS", language = "nl"),
                         es = mo_fullname("CoNS", language = "es"),
                         it = mo_fullname("CoNS", language = "it"),
                         fr = mo_fullname("CoNS", language = "fr"),
                         pt = mo_fullname("CoNS", language = "pt"),
                         times = 10)
print(run_it, unit = "ms", signif = 4)
# Unit: milliseconds
#  expr   min    lq  mean median    uq   max neval
#    en 18.55 18.62 23.58  18.67 18.72 67.58    10
#    de 20.94 21.11 21.24  21.23 21.35 21.59    10
#    nl 34.92 34.98 44.64  35.22 38.82 80.69    10
#    es 21.14 21.20 21.33  21.27 21.35 21.85    10
#    it 20.96 21.13 21.38  21.46 21.61 21.92    10
#    fr 21.08 21.18 21.90  21.45 21.71 26.40    10
#    pt 20.98 21.14 25.91  21.46 21.66 66.87    10

Currently supported are German, Dutch, Spanish, Italian, French and Portuguese.