1
0
mirror of https://github.com/msberends/AMR.git synced 2024-12-27 10:06:12 +01:00
AMR/vignettes/benchmarks.Rmd

253 lines
11 KiB
Plaintext
Raw Normal View History

2019-01-11 20:37:23 +01:00
---
title: "Benchmarks"
author: "Matthijs S. Berends"
2019-02-20 00:04:48 +01:00
date: '`r format(Sys.Date(), "%d %B %Y")`'
2019-01-11 20:37:23 +01:00
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 = "#",
2019-01-11 20:37:23 +01:00
fig.width = 7.5,
2019-09-01 13:16:57 +02:00
fig.height = 4.5,
dpi = 75
2019-01-11 20:37:23 +01:00
)
```
2019-09-01 13:16:57 +02:00
<small>Source: https://gitlab.com/msberends/AMR/blob/master/vignettes/benchmarks.Rmd</small>
2019-03-06 14:39:02 +01:00
One of the most important features of this package is the complete microbial taxonomic database, supplied by the [Catalogue of Life](http://catalogueoflife.org). 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.
2019-01-11 20:37:23 +01:00
2019-02-20 00:04:48 +01:00
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.
2019-01-11 20:37:23 +01:00
2019-02-21 18:55:52 +01:00
```{r, message = FALSE, echo = FALSE}
library(dplyr)
2019-09-01 13:16:57 +02:00
library(ggplot2)
ggplot.bm <- function(df, title = NULL) {
p <- df %>%
group_by(expr) %>%
summarise(t = median(time) / 1e+06) %>%
arrange(t) %>%
mutate(expr = factor(as.character(expr), levels = rev(as.character(expr))),
t_round = round(t, 1))
suppressWarnings(
print(
p %>%
ggplot(aes(x = expr, y = t)) +
geom_linerange(aes(ymin = 0, ymax = t), colour = "#555555") +
geom_text(aes(label = t_round, hjust = -0.5), size = 3) +
geom_point(size = 3, colour = "#555555") +
coord_flip() +
scale_y_log10(breaks = c(1, 2, 5,
10, 20, 50,
100, 200, 500,
1000, 2000, 5000),
limits = c(1, max(p$t) * 2)) +
labs(x = "Expression", y = "Median time in milliseconds (log scale)", title = title)
)
)
}
2019-02-21 18:55:52 +01:00
```
2019-02-20 10:38:24 +01:00
```{r, message = FALSE}
2019-01-11 20:37:23 +01:00
library(microbenchmark)
2019-01-17 12:08:04 +01:00
library(AMR)
2019-01-11 20:37:23 +01:00
```
2019-09-20 14:18:29 +02:00
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).
2019-01-11 20:37:23 +01:00
2019-03-06 14:39:02 +01:00
But the calculation time differs a lot:
2019-01-11 20:37:23 +01:00
2019-09-20 14:18:29 +02:00
```{r, echo = FALSE}
clear_mo_history()
```
2019-09-01 13:16:57 +02:00
```{r, warning=FALSE}
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)
2019-03-15 13:57:25 +01:00
print(S.aureus, unit = "ms", signif = 2)
2019-01-11 20:37:23 +01:00
```
2019-09-01 13:16:57 +02:00
```{r, echo = FALSE}
ggplot.bm(S.aureus)
```
2019-01-11 20:37:23 +01:00
2019-02-27 11:36:12 +01:00
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.
2019-02-20 00:04:48 +01:00
2019-09-20 14:18:29 +02:00
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:
2019-02-20 00:04:48 +01:00
2019-09-20 14:18:29 +02:00
```{r, echo = FALSE}
clear_mo_history()
```
2019-09-20 12:33:05 +02:00
```{r, warning=FALSE}
2019-09-20 14:18:29 +02:00
M.semesiae <- microbenchmark(as.mo("metsem"),
as.mo("METSEM"),
as.mo("M. semesiae"),
as.mo("M. semesiae"),
as.mo("Methanosarcina semesiae"),
2019-03-15 13:57:25 +01:00
times = 10)
2019-09-20 14:18:29 +02:00
print(M.semesiae, unit = "ms", signif = 4)
2019-01-11 20:37:23 +01:00
```
2019-09-20 14:18:29 +02:00
That takes `r round(mean(M.semesiae$time, na.rm = TRUE) / mean(S.aureus$time, na.rm = TRUE), 1)` 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.
2019-02-22 22:12:10 +01:00
2019-09-20 14:18:29 +02:00
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):
2019-02-14 10:23:51 +01:00
2019-09-01 13:16:57 +02:00
```{r, echo = FALSE}
2019-09-20 14:18:29 +02:00
clear_mo_history()
```
```{r, echo = FALSE}
par(mar = c(5, 16, 4, 2))
boxplot(microbenchmark(
2019-09-20 14:18:29 +02:00
'as.mo("Methanosarcina semesiae")' = as.mo("Methanosarcina semesiae"),
'as.mo("Prevotella brevis")' = as.mo("Prevotella brevis"),
'as.mo("Escherichia coli")' = as.mo("Escherichia coli"),
2019-09-20 14:18:29 +02:00
'as.mo("M. semesiae")' = as.mo("M. semesiae"),
'as.mo("P. brevis")' = as.mo("P. brevis"),
'as.mo("E. coli")' = as.mo("E. coli"),
times = 10),
2019-09-20 14:18:29 +02:00
horizontal = TRUE, las = 1, unit = "s", log = TRUE,
xlab = "", ylab = "Time in seconds (log)",
main = "Benchmarks per prevalence")
2019-09-01 13:16:57 +02:00
```
2019-03-15 13:57:25 +01:00
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:
```{r, echo = FALSE}
clear_mo_history()
2019-09-20 14:18:29 +02:00
```
```{r, echo = FALSE}
2019-03-15 13:57:25 +01:00
par(mar = c(5, 16, 4, 2))
boxplot(microbenchmark(
2019-09-20 14:18:29 +02:00
'as.mo("Methanosarcina semesiae")' = as.mo("Methanosarcina semesiae", force_mo_history = TRUE),
2019-03-15 13:57:25 +01:00
'as.mo("Prevotella brevis")' = as.mo("Prevotella brevis", force_mo_history = TRUE),
'as.mo("Escherichia coli")' = as.mo("Escherichia coli", force_mo_history = TRUE),
2019-09-20 14:18:29 +02:00
'as.mo("M. semesiae")' = as.mo("M. semesiae", force_mo_history = TRUE),
2019-03-15 13:57:25 +01:00
'as.mo("P. brevis")' = as.mo("P. brevis", force_mo_history = TRUE),
'as.mo("E. coli")' = as.mo("E. coli", force_mo_history = TRUE),
times = 10),
2019-09-22 12:41:45 +02:00
horizontal = TRUE, las = 1, unit = "s", log = TRUE,
xlab = "", ylab = "Time in seconds (log)",
2019-03-15 13:57:25 +01:00
main = "Benchmarks per prevalence")
2019-04-07 22:40:02 +02:00
```
2019-03-15 13:57:25 +01:00
The highest outliers are the first times. All next determinations were done in only thousands of seconds.
2019-04-07 22:40:02 +02:00
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**.
2019-01-11 20:37:23 +01:00
### 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_name()` for this test - a helper function that returns the full microbial name (genus, species and possibly subspecies) which uses `as.mo()` internally.
2019-01-11 20:37:23 +01:00
2019-02-20 10:38:24 +01:00
```{r, message = FALSE}
2019-01-11 20:37:23 +01:00
library(dplyr)
# take all MO codes from the example_isolates data set
x <- example_isolates$mo %>%
2019-02-25 15:52:32 +01:00
# keep only the unique ones
unique() %>%
# pick 50 of them at random
sample(50) %>%
# paste that 10,000 times
rep(10000) %>%
# scramble it
sample()
2019-01-11 20:37:23 +01:00
2019-02-25 15:52:32 +01:00
# got indeed 50 times 10,000 = half a million?
2019-01-11 20:37:23 +01:00
length(x)
# and how many unique values do we have?
n_distinct(x)
2019-02-20 00:04:48 +01:00
# now let's see:
run_it <- microbenchmark(mo_name(x),
2019-02-20 00:04:48 +01:00
times = 10)
2019-02-20 13:57:23 +01:00
print(run_it, unit = "ms", signif = 3)
2019-01-11 20:37:23 +01:00
```
2019-02-25 15:52:32 +01:00
So transforming 500,000 values (!!) of `r n_distinct(x)` unique values only takes `r round(median(run_it$time, na.rm = TRUE) / 1e9, 2)` seconds (`r as.integer(median(run_it$time, na.rm = TRUE) / 1e6)` ms). You only lose time on your unique input values.
2019-01-11 20:37:23 +01:00
### Precalculated results
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):
2019-01-11 20:37:23 +01:00
2019-09-20 14:18:29 +02:00
```{r, echo = FALSE}
clear_mo_history()
```
2019-02-20 00:04:48 +01:00
```{r}
2019-09-18 15:46:09 +02:00
run_it <- microbenchmark(A = mo_name("B_STPHY_AURS"),
B = mo_name("S. aureus"),
C = mo_name("Staphylococcus aureus"),
2019-02-20 00:04:48 +01:00
times = 10)
2019-02-20 13:57:23 +01:00
print(run_it, unit = "ms", signif = 3)
2019-01-11 20:37:23 +01:00
```
So going from `mo_name("Staphylococcus aureus")` to `"Staphylococcus aureus"` takes `r format(round(run_it %>% filter(expr == "C") %>% pull(time) %>% median() / 1e9, 4), scientific = FALSE)` 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:
2019-01-11 20:37:23 +01:00
2019-09-20 14:18:29 +02:00
```{r, echo = FALSE}
clear_mo_history()
```
2019-02-20 00:04:48 +01:00
```{r}
2019-02-20 13:57:23 +01:00
run_it <- microbenchmark(A = mo_species("aureus"),
B = mo_genus("Staphylococcus"),
C = mo_name("Staphylococcus aureus"),
2019-02-20 13:57:23 +01:00
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)
2019-01-11 20:37:23 +01:00
```
2019-02-20 00:04:48 +01:00
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.
2019-01-11 20:37:23 +01:00
### Results in other languages
2019-02-20 10:38:24 +01:00
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:
2019-01-11 20:37:23 +01:00
2019-09-20 14:18:29 +02:00
```{r, echo = FALSE}
clear_mo_history()
```
2019-02-20 00:04:48 +01:00
```{r}
mo_name("CoNS", language = "en") # or just mo_name("CoNS") on an English system
2019-01-11 20:37:23 +01:00
mo_name("CoNS", language = "es") # or just mo_name("CoNS") on a Spanish system
2019-02-20 13:57:23 +01:00
mo_name("CoNS", language = "nl") # or just mo_name("CoNS") on a Dutch system
2019-02-20 13:57:23 +01:00
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"),
2019-02-20 13:57:23 +01:00
times = 10)
print(run_it, unit = "ms", signif = 4)
2019-01-11 20:37:23 +01:00
```
Currently supported are German, Dutch, Spanish, Italian, French and Portuguese.