AMR/vignettes/benchmarks.Rmd

206 lines
9.8 KiB
Plaintext
Executable File

---
title: "Benchmarks"
author: "Matthijs S. Berends"
date: '`r format(Sys.Date(), "%d %B %Y")`'
output:
rmarkdown::html_vignette:
toc: true
toc_depth: 3
vignette: >
%\VignetteIndexEntry{Benchmarks}
%\VignetteEncoding{UTF-8}
%\VignetteEngine{knitr::rmarkdown}
editor_options:
chunk_output_type: console
---
```{r setup, include = FALSE, results = 'markup'}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#",
fig.width = 7.5,
fig.height = 4.5
)
```
One of the most important features of this package is the complete microbial taxonomic database, supplied by ITIS (https://www.itis.gov). We created a function `as.mo()` that transforms any user input value to a valid microbial ID by using AI (Artificial Intelligence) and based on the taxonomic tree of ITIS.
Using the `microbenchmark` package, we can review the calculation performance of this function.
```r
library(microbenchmark)
```
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. Here, the AI effect can be reviewed best:
```r
microbenchmark(A = as.mo("stau"),
B = as.mo("staaur"),
C = as.mo("S. aureus"),
D = as.mo("S. aureus"),
E = as.mo("STAAUR"),
F = as.mo("Staphylococcus aureus"),
G = as.mo("B_STPHY_AUR"),
times = 10,
unit = "ms")
# Unit: milliseconds
# expr min lq mean median uq max neval
# A 34.745551 34.798630 35.2596102 34.8994810 35.258325 38.067062 10
# B 7.095386 7.125348 7.2219948 7.1613865 7.240377 7.495857 10
# C 11.677114 11.733826 11.8304789 11.7715050 11.843756 12.317559 10
# D 11.694435 11.730054 11.9859313 11.8775585 12.206371 12.750016 10
# E 7.044402 7.117387 7.2271630 7.1923610 7.246104 7.742396 10
# F 6.642326 6.778446 6.8988042 6.8753165 6.923577 7.513945 10
# G 0.106788 0.131023 0.1351229 0.1357725 0.144014 0.146458 10
```
In the table above, all measurements are shown in milliseconds (thousands of seconds), tested on a quite regular Linux server from 2007 (Core 2 Duo 2.7 GHz, 2 GB DDR2 RAM). A value of 6.9 milliseconds means it will roughly determine 144 input values per second. It case of 39.2 milliseconds, this is only 26 input values per second. The more an input value resembles a full name (like C, D and F), the faster the result will be found. In case of G, the input is already a valid MO code, so it only almost takes no time at all (0.0001 seconds on our server).
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 far less faster. See this example for the ID of *Burkholderia nodosa* (`B_BRKHL_NOD`):
```r
microbenchmark(A = as.mo("buno"),
B = as.mo("burnod"),
C = as.mo("B. nodosa"),
D = as.mo("B. nodosa"),
E = as.mo("BURNOD"),
F = as.mo("Burkholderia nodosa"),
G = as.mo("B_BRKHL_NOD"),
times = 10,
unit = "ms")
# Unit: milliseconds
# expr min lq mean median uq max neval
# A 124.175427 124.474837 125.8610536 125.3750560 126.160945 131.485994 10
# B 154.249713 155.364729 160.9077032 156.8738940 157.136183 197.315105 10
# C 66.066571 66.162393 66.5538611 66.4488130 66.698077 67.623404 10
# D 86.747693 86.918665 90.7831016 87.8149725 89.440982 116.767991 10
# E 154.863827 155.208563 162.6535954 158.4062465 168.593785 187.378088 10
# F 32.427028 32.638648 32.9929454 32.7860475 32.992813 34.674241 10
# G 0.213155 0.216578 0.2369226 0.2338985 0.253734 0.285581 10
```
That takes up to 11 times as much time! A value of 158.4 milliseconds means it can only determine ~6 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.
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 mean that unique values 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) and uses `as.mo()` internally.
```r
library(dplyr)
# take 500,000 random MO codes from the septic_patients data set
x = septic_patients %>%
sample_n(500000, replace = TRUE) %>%
pull(mo)
# got the right length?
length(x)
# [1] 500000
# and how many unique values do we have?
n_distinct(x)
# [1] 96
# only 96, but distributed in 500,000 results. now let's see:
microbenchmark(X = mo_fullname(x),
times = 10,
unit = "ms")
# Unit: milliseconds
# expr min lq mean median uq max neval
# X 114.9342 117.1076 129.6448 120.2047 131.5005 168.6371 10
```
So transforming 500,000 values (!) of 96 unique values only takes 0.12 seconds (120 ms). You only lose time on your unique input values.
Results of a tenfold - 5,000,000 values:
```r
# Unit: milliseconds
# expr min lq mean median uq max neval
# X 882.9045 901.3011 1001.677 940.3421 1168.088 1226.846 10
```
Even the full names of 5 *Million* values are calculated within a second.
### 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):
```r
microbenchmark(A = mo_fullname("B_STPHY_AUR"),
B = mo_fullname("S. aureus"),
C = mo_fullname("Staphylococcus aureus"),
times = 10,
unit = "ms")
# Unit: milliseconds
# expr min lq mean median uq max neval
# A 11.364086 11.460537 11.5104799 11.4795330 11.524860 11.818263 10
# B 11.976454 12.012352 12.1704592 12.0853020 12.210004 12.881737 10
# C 0.095823 0.102528 0.1167754 0.1153785 0.132629 0.140661 10
```
So going from `mo_fullname("Staphylococcus aureus")` to `"Staphylococcus aureus"` takes 0.0001 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:
```r
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_subkingdom("Posibacteria"),
I = mo_kingdom("Bacteria"),
times = 10,
unit = "ms")
# Unit: milliseconds
# expr min lq mean median uq max neval
# A 0.105181 0.121314 0.1478538 0.1465265 0.166711 0.211409 10
# B 0.132558 0.146388 0.1584278 0.1499835 0.164895 0.208477 10
# C 0.135492 0.160355 0.2341847 0.1884665 0.348857 0.395931 10
# D 0.109650 0.115727 0.1270481 0.1264130 0.128648 0.168317 10
# E 0.081574 0.096940 0.0992582 0.0980915 0.101479 0.120477 10
# F 0.081575 0.088489 0.0988463 0.0989650 0.103365 0.126482 10
# G 0.091981 0.095333 0.1043568 0.1001530 0.111327 0.129625 10
# H 0.092610 0.093169 0.1009135 0.0985455 0.101828 0.120406 10
# I 0.087371 0.091213 0.1069758 0.0941815 0.109302 0.192831 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 microorganisms (according to ITIS), 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 take a little while longer:
```r
mo_fullname("CoNS", language = "en") # or just mo_fullname("CoNS") on an English system
# "Coagulase Negative Staphylococcus (CoNS)"
mo_fullname("CoNS", language = "fr") # or just mo_fullname("CoNS") on a French system
# "Staphylococcus à coagulase négative (CoNS)"
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,
unit = "ms")
# Unit: milliseconds
# expr min lq mean median uq max neval
# en 6.093583 6.51724 6.555105 6.562986 6.630663 6.99698 100
# de 13.934874 14.35137 16.891587 14.462210 14.764658 43.63956 100
# nl 13.900092 14.34729 15.943268 14.424565 14.581535 43.76283 100
# es 13.833813 14.34596 14.574783 14.439757 14.653994 17.49168 100
# it 13.811883 14.36621 15.179060 14.453515 14.812359 43.64284 100
# fr 13.798683 14.37019 16.344731 14.468775 14.697610 48.62923 100
# pt 13.789674 14.36244 15.706321 14.443772 14.679905 44.76701 100
```
Currently supported are German, Dutch, Spanish, Italian, French and Portuguese.