1
0
mirror of https://github.com/msberends/AMR.git synced 2024-12-26 08:46:11 +01:00
AMR/vignettes/benchmarks.Rmd.not

195 lines
7.7 KiB
Plaintext
Raw Normal View History

2019-01-11 20:37:23 +01:00
---
title: "Benchmarks"
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,
2021-02-22 20:21:33 +01:00
fig.height = 5.5,
dpi = 100
)
2019-01-11 20:37:23 +01:00
```
2021-02-22 20:21:33 +01:00
One of the most important features of this package is the complete microbial taxonomic database, supplied by the [Catalogue of Life](http://www.catalogueoflife.org) (CoL) and the [List of Prokaryotic names with Standing in Nomenclature](https://lpsn.dsmz.de) (LPSN). We created a function `as.mo()` that transforms any user input value to a valid microbial ID by using intelligent rules combined with the microbial taxonomy.
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}
2019-09-01 13:16:57 +02:00
library(ggplot2)
2021-02-22 20:21:33 +01:00
ggplot.bm <- function(df) {
reorder <- function(.f, .x, .fun, .desc = TRUE) {
summ <- tapply(.x, .f, .fun)
factor(.f, levels = names(summ)[order(summ, decreasing = .desc)], ordered = is.ordered(.f))
}
2022-08-28 10:31:50 +02:00
ggplot(
df,
aes(x = reorder(expr, time, median), y = time / 1000 / 1000)
) +
stat_boxplot(geom = "errorbar", width = 0.5) +
geom_boxplot(outlier.alpha = 0) +
coord_flip() +
scale_y_continuous(trans = "log", breaks = c(
1, 2, 5,
10, 20, 50,
100, 200, 500,
1000, 2000, 5000
)) +
labs(
x = "Expression",
y = "Time in milliseconds (log scale)"
) +
theme_minimal() +
theme(axis.text.y = element_text(family = "mono"))
2019-09-01 13:16:57 +02:00
}
2019-02-21 18:55:52 +01:00
```
2019-02-20 10:38:24 +01:00
```{r, message = FALSE}
2021-02-22 20:21:33 +01:00
library(microbenchmark)
2019-01-17 12:08:04 +01:00
library(AMR)
2020-05-18 10:30:53 +02:00
library(dplyr)
2019-01-11 20:37:23 +01:00
```
In the next test, we try to 'coerce' different input values into the microbial code of *Staphylococcus aureus*. Coercion is a computational process of forcing output based on an input. For microorganism names, coercing user input to taxonomically valid microorganism names is crucial to ensure correct interpretation and to enable grouping based on taxonomic properties.
2021-02-22 20:21:33 +01:00
The actual result is the same every time: it returns its microorganism code ``r as.character(as.mo("Staphylococcus aureus"))`` (*B* stands for *Bacteria*, its 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
```{r, warning=FALSE, message=FALSE}
2019-09-01 13:16:57 +02:00
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
2022-08-28 10:31:50 +02:00
times = 25
)
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}
2021-12-13 10:18:28 +01:00
mult_factor <- median(S.aureus[S.aureus$expr == S.aureus[which(S.aureus$time == max(S.aureus$time)), "expr"], "time"]) / median(S.aureus[S.aureus$expr == S.aureus[which(S.aureus$time == min(S.aureus$time)), "expr"], "time"])
2019-09-01 13:16:57 +02:00
ggplot.bm(S.aureus)
```
2019-01-11 20:37:23 +01:00
2021-12-13 10:18:28 +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 200 milliseconds, this is only 5 input values per second. It is clear that accepted taxonomic names are extremely fast, but some variations are up to `r ceiling(mult_factor)` times slower to determine.
2019-02-20 00:04:48 +01:00
2021-02-22 20:21:33 +01:00
To improve performance, we implemented two important algorithms to save unnecessary calculations: **repetitive results** and **already precalculated results**.
2019-01-11 20:37:23 +01:00
### Repetitive results
2021-02-22 20:21:33 +01:00
Repetitive results are values that are present more than once in a vector. Unique values will only be calculated once by `as.mo()`. So running `as.mo(c("E. coli", "E. coli"))` will check the value `"E. coli"` only once.
To prove this, we will use `mo_name()` for testing - 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
2020-05-16 13:05:47 +02:00
```{r, message = FALSE}
2021-02-22 20:21:33 +01:00
# start with the example_isolates data set
2022-08-28 10:31:50 +02:00
x <- example_isolates %>%
2021-02-22 20:21:33 +01:00
# take all MO codes from the 'mo' column
pull(mo) %>%
# and copy them a thousand times
rep(1000) %>%
# then scramble them
2019-02-25 15:52:32 +01:00
sample()
2021-02-22 20:21:33 +01:00
# what do these values look like? They are of class <mo>:
head(x)
2022-08-28 10:31:50 +02:00
2021-02-22 20:21:33 +01:00
# as the example_isolates data set has 2,000 rows, we should have 2 million items
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),
2022-08-28 10:31:50 +02: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 getting official taxonomic names of `r format(length(x), big.mark = ",")` (!!) items consisting of `r n_distinct(x)` unique values only takes `r round(median(run_it$time, na.rm = TRUE) / 1e9, 3)` seconds. That is `r round(median(run_it$time, na.rm = TRUE) / length(x), 0)` nanoseconds on average. You only lose time on your unique input values.
2019-01-11 20:37:23 +01:00
### Precalculated results
2021-02-22 20:21:33 +01:00
What about precalculated results? If the input is an already precalculated result of a helper function such as `mo_name()`, it almost doesn't take any time at all. In other words, if you run `mo_name()` on a valid taxonomic name, it will return the results immediately (see 'C' below):
2019-01-11 20:37:23 +01:00
```{r, warning=FALSE, message=FALSE}
2022-08-28 10:31:50 +02:00
run_it <- microbenchmark(
A = mo_name("STAAUR"),
B = mo_name("S. aureus"),
C = mo_name("Staphylococcus aureus"),
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-02-20 00:04:48 +01:00
```{r}
2022-08-28 10:31:50 +02:00
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
)
2019-02-20 13:57:23 +01:00
print(run_it, unit = "ms", signif = 3)
2019-01-11 20:37:23 +01:00
```
2021-02-22 20:21:33 +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"` anyway, there is no point in calculating the result. And because this package contains all phyla of all known bacteria, it can just return the initial value immediately.
2019-01-11 20:37:23 +01:00
### Results in other languages
2021-12-13 10:18:28 +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 (compare "en" from the table below with the other languages):
2019-01-11 20:37:23 +01:00
2019-02-20 00:04:48 +01:00
```{r}
2021-12-13 10:18:28 +01:00
CoNS <- as.mo("CoNS")
CoNS
2019-01-11 20:37:23 +01:00
2021-12-13 10:18:28 +01:00
mo_name(CoNS, language = "en") # or just mo_name(CoNS) on an English system
2019-02-20 13:57:23 +01:00
2021-12-13 10:18:28 +01:00
mo_name(CoNS, language = "es") # or just mo_name(CoNS) on a Spanish system
2019-02-20 13:57:23 +01:00
2021-12-13 10:18:28 +01:00
mo_name(CoNS, language = "nl") # or just mo_name(CoNS) on a Dutch system
2022-08-28 10:31:50 +02:00
run_it <- microbenchmark(
da = mo_name(CoNS, language = "da"),
de = mo_name(CoNS, language = "de"),
en = mo_name(CoNS, language = "en"),
es = mo_name(CoNS, language = "es"),
fr = mo_name(CoNS, language = "fr"),
it = mo_name(CoNS, language = "it"),
nl = mo_name(CoNS, language = "nl"),
pt = mo_name(CoNS, language = "pt"),
ru = mo_name(CoNS, language = "ru"),
sv = mo_name(CoNS, language = "sv"),
times = 100
)
2019-02-20 13:57:23 +01:00
print(run_it, unit = "ms", signif = 4)
2019-01-11 20:37:23 +01:00
```
2021-12-14 21:47:14 +01:00
Currently supported languages are `r AMR:::vector_and(names(AMR:::LANGUAGES_SUPPORTED), quotes = FALSE)`.