(v1.5.0.9024) more speed improvements

This commit is contained in:
dr. M.S. (Matthijs) Berends 2021-02-22 20:21:33 +01:00
parent 0fdabff1ba
commit 31ceba5441
18 changed files with 151 additions and 128 deletions

View File

@ -1,6 +1,6 @@
Package: AMR
Version: 1.5.0.9023
Date: 2021-02-21
Version: 1.5.0.9024
Date: 2021-02-22
Title: Antimicrobial Resistance Data Analysis
Authors@R: c(
person(role = c("aut", "cre"),

View File

@ -62,6 +62,7 @@ S3method(print,mo_renamed)
S3method(print,mo_uncertainties)
S3method(print,pca)
S3method(print,rsi)
S3method(rep,mo)
S3method(skewness,data.frame)
S3method(skewness,default)
S3method(skewness,matrix)

View File

@ -1,5 +1,5 @@
# AMR 1.5.0.9023
## <small>Last updated: 21 February 2021</small>
# AMR 1.5.0.9024
## <small>Last updated: 22 February 2021</small>
### New
* Support for EUCAST Clinical Breakpoints v11.0 (2021), effective in the `eucast_rules()` function and in `as.rsi()` to interpret MIC and disk diffusion values. This is now the default guideline in this package.

41
R/mo.R
View File

@ -489,10 +489,10 @@ exec_as.mo <- function(x,
# now only continue where the right taxonomic output is not already known
if (any(!already_known)) {
x_known <- x[already_known]
# remove spp and species
x <- gsub(" +(spp.?|ssp.?|sp.? |ss ?.?|subsp.?|subspecies|biovar |serovar |species)", " ", x, perl = TRUE)
x <- gsub("(spp.?|subsp.?|subspecies|biovar|serovar|species)", "", x, perl = TRUE)
x <- gsub(" +(spp.?|ssp.?|sp.? |ss ?.?|subsp.?|subspecies|biovar |serovar |species)", " ", x)
x <- gsub("(spp.?|subsp.?|subspecies|biovar|serovar|species)", "", x)
x <- gsub("^([a-z]{2,4})(spe.?)$", "\\1", x, perl = TRUE) # when ending in SPE instead of SPP and preceded by 2-4 characters
x <- strip_whitespace(x, dyslexia_mode)
@ -528,12 +528,12 @@ exec_as.mo <- function(x,
# allow characters that resemble others = dyslexia_mode ----
if (dyslexia_mode == TRUE) {
x <- tolower(x)
x <- gsub("[iy]+", "[iy]+", x, perl = TRUE)
x <- gsub("(c|k|q|qu|s|z|x|ks)+", "(c|k|q|qu|s|z|x|ks)+", x, perl = TRUE)
x <- gsub("(ph|hp|f|v)+", "(ph|hp|f|v)+", x, perl = TRUE)
x <- gsub("(th|ht|t)+", "(th|ht|t)+", x, perl = TRUE)
x <- gsub("a+", "a+", x, perl = TRUE)
x <- gsub("u+", "u+", x, perl = TRUE)
x <- gsub("[iy]+", "[iy]+", x)
x <- gsub("(c|k|q|qu|s|z|x|ks)+", "(c|k|q|qu|s|z|x|ks)+", x)
x <- gsub("(ph|hp|f|v)+", "(ph|hp|f|v)+", x)
x <- gsub("(th|ht|t)+", "(th|ht|t)+", x)
x <- gsub("a+", "a+", x)
x <- gsub("u+", "u+", x)
# allow any ending of -um, -us, -ium, -icum, -ius, -icus, -ica, -ia and -a (needs perl for the negative backward lookup):
x <- gsub("(u\\+\\(c\\|k\\|q\\|qu\\+\\|s\\|z\\|x\\|ks\\)\\+)(?![a-z])",
"(u[s|m]|[iy][ck]?u[ms]|[iy]?[ck]?a)", x, perl = TRUE)
@ -543,9 +543,9 @@ exec_as.mo <- function(x,
"(u[s|m]|[iy][ck]?u[ms]|[iy]?[ck]?a)", x, perl = TRUE)
x <- gsub("(\\[iy\\]\\+a\\+)(?![a-z])",
"([iy]*a+|[iy]+a*)", x, perl = TRUE)
x <- gsub("e+", "e+", x, perl = TRUE)
x <- gsub("o+", "o+", x, perl = TRUE)
x <- gsub("(.)\\1+", "\\1+", x, perl = TRUE)
x <- gsub("e+", "e+", x)
x <- gsub("o+", "o+", x)
x <- gsub("(.)\\1+", "\\1+", x)
# allow multiplication of all other consonants
x <- gsub("([bdgjlnrw]+)", "\\1+", x, perl = TRUE)
# allow ending in -en or -us
@ -554,7 +554,7 @@ exec_as.mo <- function(x,
# this will allow "Pasteurella damatis" to be correctly read as "Pasteurella dagmatis".
consonants <- paste(letters[!letters %in% c("a", "e", "i", "o", "u")], collapse = "")
x[nchar(x_backup_without_spp) > 10] <- gsub("[+]", paste0("+[", consonants, "]?"), x[nchar(x_backup_without_spp) > 10])
# allow au and ou after all these regex implementations
# allow au and ou after all above regex implementations
x <- gsub("a+[bcdfghjklmnpqrstvwxyz]?u+[bcdfghjklmnpqrstvwxyz]?", "(a+u+|o+u+)[bcdfghjklmnpqrstvwxyz]?", x, fixed = TRUE)
x <- gsub("o+[bcdfghjklmnpqrstvwxyz]?u+[bcdfghjklmnpqrstvwxyz]?", "(a+u+|o+u+)[bcdfghjklmnpqrstvwxyz]?", x, fixed = TRUE)
}
@ -1469,7 +1469,7 @@ exec_as.mo <- function(x,
plural <- c("s", "them", "were")
}
msg <- paste0("Translation to ", nr2char(length(uncertainties$input)), " microorganism", plural[1],
" was guessed with uncertainty. Use mo_uncertainties() to review ", plural[2], ".")
" was guessed with uncertainty. Use `mo_uncertainties()` to review ", plural[2], ".")
message_(msg)
}
x[already_known] <- x_known
@ -1807,6 +1807,15 @@ unique.mo <- function(x, incomparables = FALSE, ...) {
y
}
#' @method rep mo
#' @export
#' @noRd
rep.mo <- function(x, ...) {
y <- NextMethod()
attributes(y) <- attributes(x)
y
}
#' @rdname as.mo
#' @export
mo_failures <- function() {
@ -1831,7 +1840,7 @@ print.mo_uncertainties <- function(x, ...) {
if (NROW(x) == 0) {
return(NULL)
}
message_("Matching scores are based on human pathogenic prevalence and the resemblance between the input and the full taxonomic name. See ?mo_matching_score.", as_note = FALSE)
message_("Matching scores are based on human pathogenic prevalence and the resemblance between the input and the full taxonomic name. See `?mo_matching_score`.", as_note = FALSE)
msg <- ""
for (i in seq_len(nrow(x))) {
@ -2009,7 +2018,7 @@ replace_old_mo_codes <- function(x, property) {
x[which(!is.na(matched))] <- mo_new[which(!is.na(matched))]
n_matched <- length(matched[!is.na(matched)])
if (property != "mo") {
message_(font_blue("The input contained old microbial codes (from previous package versions). Please update your MO codes with as.mo()."))
message_(font_blue("The input contained old microbial codes (from previous package versions). Please update your MO codes with `as.mo()`."))
} else {
if (n_matched == 1) {
message_(font_blue("1 old microbial code (from previous package versions) was updated to a current used MO code."))

Binary file not shown.

View File

@ -81,7 +81,7 @@
</button>
<span class="navbar-brand">
<a class="navbar-link" href="https://msberends.github.io/AMR//index.html">AMR (for R)</a>
<span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Latest development version">1.5.0.9023</span>
<span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Latest development version">1.5.0.9024</span>
</span>
</div>

View File

@ -81,7 +81,7 @@
</button>
<span class="navbar-brand">
<a class="navbar-link" href="index.html">AMR (for R)</a>
<span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Latest development version">1.5.0.9023</span>
<span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Latest development version">1.5.0.9024</span>
</span>
</div>

View File

@ -39,7 +39,7 @@
</button>
<span class="navbar-brand">
<a class="navbar-link" href="../index.html">AMR (for R)</a>
<span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Latest development version">1.5.0.9023</span>
<span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Latest development version">1.5.0.9024</span>
</span>
</div>
@ -200,17 +200,17 @@
<p>One of the most important features of this package is the complete microbial taxonomic database, supplied by the <a href="http://catalogueoflife.org">Catalogue of Life</a>. We created a function <code><a href="../reference/as.mo.html">as.mo()</a></code> that transforms any user input value to a valid microbial ID by using intelligent rules combined with the taxonomic tree of Catalogue of Life.</p>
<p>Using the <code>microbenchmark</code> package, we can review the calculation performance of this function. Its function <code>microbenchmark()</code> runs different input expressions independently of each other and measures their time-to-result.</p>
<p>One of the most important features of this package is the complete microbial taxonomic database, supplied by the <a href="http://www.catalogueoflife.org">Catalogue of Life</a> (CoL) and the <a href="https://lpsn.dsmz.de">List of Prokaryotic names with Standing in Nomenclature</a> (LPSN). We created a function <code><a href="../reference/as.mo.html">as.mo()</a></code> that transforms any user input value to a valid microbial ID by using intelligent rules combined with the microbial taxonomy.</p>
<p>Using the <code>microbenchmark</code> package, we can review the calculation performance of this function. Its function <code><a href="https://rdrr.io/pkg/microbenchmark/man/microbenchmark.html">microbenchmark()</a></code> runs different input expressions independently of each other and measures their time-to-result.</p>
<div class="sourceCode" id="cb1"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">microbenchmark</span> <span class="op">&lt;-</span> <span class="fu">microbenchmark</span><span class="fu">::</span><span class="va"><a href="https://rdrr.io/pkg/microbenchmark/man/microbenchmark.html">microbenchmark</a></span>
<code class="sourceCode R"><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://github.com/joshuaulrich/microbenchmark/">microbenchmark</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://msberends.github.io/AMR/">AMR</a></span><span class="op">)</span>
<span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://dplyr.tidyverse.org">dplyr</a></span><span class="op">)</span></code></pre></div>
<p>In the next test, we try to coerce different input values into the microbial code of <em>Staphylococcus aureus</em>. 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.</p>
<p>The actual result is the same every time: it returns its microorganism code <code>B_STPHY_AURS</code> (<em>B</em> stands for <em>Bacteria</em>, the taxonomic kingdom).</p>
<p>The actual result is the same every time: it returns its microorganism code <code>B_STPHY_AURS</code> (<em>B</em> stands for <em>Bacteria</em>, its taxonomic kingdom).</p>
<p>But the calculation time differs a lot:</p>
<div class="sourceCode" id="cb2"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">S.aureus</span> <span class="op">&lt;-</span> <span class="fu">microbenchmark</span><span class="op">(</span>
<code class="sourceCode R"><span class="va">S.aureus</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/microbenchmark/man/microbenchmark.html">microbenchmark</a></span><span class="op">(</span>
<span class="fu"><a href="../reference/as.mo.html">as.mo</a></span><span class="op">(</span><span class="st">"sau"</span><span class="op">)</span>, <span class="co"># WHONET code</span>
<span class="fu"><a href="../reference/as.mo.html">as.mo</a></span><span class="op">(</span><span class="st">"stau"</span><span class="op">)</span>,
<span class="fu"><a href="../reference/as.mo.html">as.mo</a></span><span class="op">(</span><span class="st">"STAU"</span><span class="op">)</span>,
@ -223,40 +223,47 @@
<span class="fu"><a href="../reference/as.mo.html">as.mo</a></span><span class="op">(</span><span class="st">"Sthafilokkockus aaureuz"</span><span class="op">)</span>, <span class="co"># incorrect spelling</span>
<span class="fu"><a href="../reference/as.mo.html">as.mo</a></span><span class="op">(</span><span class="st">"MRSA"</span><span class="op">)</span>, <span class="co"># Methicillin Resistant S. aureus</span>
<span class="fu"><a href="../reference/as.mo.html">as.mo</a></span><span class="op">(</span><span class="st">"VISA"</span><span class="op">)</span>, <span class="co"># Vancomycin Intermediate S. aureus</span>
<span class="fu"><a href="../reference/as.mo.html">as.mo</a></span><span class="op">(</span><span class="st">"VRSA"</span><span class="op">)</span>, <span class="co"># Vancomycin Resistant S. aureus</span>
times <span class="op">=</span> <span class="fl">10</span><span class="op">)</span>
times <span class="op">=</span> <span class="fl">25</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/r/base/print.html">print</a></span><span class="op">(</span><span class="va">S.aureus</span>, unit <span class="op">=</span> <span class="st">"ms"</span>, signif <span class="op">=</span> <span class="fl">2</span><span class="op">)</span>
<span class="co"># Unit: milliseconds</span>
<span class="co"># expr min lq mean median uq max neval</span>
<span class="co"># as.mo("sau") 11.0 12.0 27.0 13.0 49.0 51 10</span>
<span class="co"># as.mo("stau") 53.0 57.0 76.0 74.0 93.0 100 10</span>
<span class="co"># as.mo("STAU") 53.0 54.0 69.0 56.0 58.0 190 10</span>
<span class="co"># as.mo("staaur") 11.0 12.0 21.0 13.0 42.0 44 10</span>
<span class="co"># as.mo("STAAUR") 11.0 12.0 16.0 13.0 14.0 48 10</span>
<span class="co"># as.mo("S. aureus") 27.0 27.0 38.0 32.0 35.0 75 10</span>
<span class="co"># as.mo("S aureus") 27.0 29.0 38.0 30.0 36.0 73 10</span>
<span class="co"># as.mo("Staphylococcus aureus") 3.1 3.2 6.9 3.5 3.7 38 10</span>
<span class="co"># as.mo("Staphylococcus aureus (MRSA)") 250.0 260.0 270.0 260.0 280.0 290 10</span>
<span class="co"># as.mo("Sthafilokkockus aaureuz") 160.0 200.0 200.0 200.0 210.0 230 10</span>
<span class="co"># as.mo("MRSA") 10.0 11.0 12.0 11.0 13.0 14 10</span>
<span class="co"># as.mo("VISA") 19.0 20.0 26.0 22.0 24.0 61 10</span>
<span class="co"># as.mo("VRSA") 19.0 20.0 24.0 21.0 22.0 56 10</span></code></pre></div>
<p><img src="benchmarks_files/figure-html/unnamed-chunk-4-1.png" width="562.5"></p>
<p>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. It is clear that accepted taxonomic names are extremely fast, but some variations can take up to 500-1000 times as much time.</p>
<p>To improve performance, two important calculations take almost no time at all: <strong>repetitive results</strong> and <strong>already precalculated results</strong>.</p>
<span class="co"># expr min lq mean median uq max neval</span>
<span class="co"># as.mo("sau") 9.3 10 11.0 10 11.0 13.0 25</span>
<span class="co"># as.mo("stau") 52.0 55 73.0 58 92.0 100.0 25</span>
<span class="co"># as.mo("STAU") 50.0 54 73.0 58 96.0 110.0 25</span>
<span class="co"># as.mo("staaur") 9.7 10 14.0 11 12.0 57.0 25</span>
<span class="co"># as.mo("STAAUR") 8.9 10 14.0 10 11.0 52.0 25</span>
<span class="co"># as.mo("S. aureus") 26.0 28 41.0 29 67.0 76.0 25</span>
<span class="co"># as.mo("S aureus") 27.0 28 41.0 30 65.0 76.0 25</span>
<span class="co"># as.mo("Staphylococcus aureus") 2.6 3 3.2 3 3.3 4.6 25</span>
<span class="co"># as.mo("Staphylococcus aureus (MRSA)") 240.0 260 270.0 260 270.0 380.0 25</span>
<span class="co"># as.mo("Sthafilokkockus aaureuz") 160.0 190 200.0 200 200.0 300.0 25</span>
<span class="co"># as.mo("MRSA") 9.3 10 15.0 10 12.0 49.0 25</span>
<span class="co"># as.mo("VISA") 18.0 19 31.0 21 54.0 67.0 25</span></code></pre></div>
<p><img src="benchmarks_files/figure-html/unnamed-chunk-4-1.png" width="750"></p>
<p>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 200 times slower to determine.</p>
<p>To improve performance, we implemented two important algorithms to save unnecessary calculations: <strong>repetitive results</strong> and <strong>already precalculated results</strong>.</p>
<div id="repetitive-results" class="section level3">
<h3 class="hasAnchor">
<a href="#repetitive-results" class="anchor"></a>Repetitive results</h3>
<p>Repetitive results are unique values that are present more than once. Unique values will only be calculated once by <code><a href="../reference/as.mo.html">as.mo()</a></code>. We will use <code><a href="../reference/mo_property.html">mo_name()</a></code> for this test - a helper function that returns the full microbial name (genus, species and possibly subspecies) which uses <code><a href="../reference/as.mo.html">as.mo()</a></code> internally.</p>
<p>Repetitive results are values that are present more than once in a vector. Unique values will only be calculated once by <code><a href="../reference/as.mo.html">as.mo()</a></code>. So running <code><a href="../reference/as.mo.html">as.mo(c("E. coli", "E. coli"))</a></code> will check the value <code>"E. coli"</code> only once.</p>
<p>To prove this, we will use <code><a href="../reference/mo_property.html">mo_name()</a></code> for testing - a helper function that returns the full microbial name (genus, species and possibly subspecies) which uses <code><a href="../reference/as.mo.html">as.mo()</a></code> internally.</p>
<div class="sourceCode" id="cb3"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="co"># take all MO codes from the example_isolates data set</span>
<span class="va">x</span> <span class="op">&lt;-</span> <span class="va">example_isolates</span><span class="op">$</span><span class="va">mo</span> <span class="op">%&gt;%</span>
<code class="sourceCode R"><span class="co"># start with the example_isolates data set</span>
<span class="va">x</span> <span class="op">&lt;-</span> <span class="va">example_isolates</span> <span class="op">%&gt;%</span>
<span class="co"># take all MO codes from the 'mo' column</span>
<span class="fu"><a href="https://dplyr.tidyverse.org/reference/pull.html">pull</a></span><span class="op">(</span><span class="va">mo</span><span class="op">)</span> <span class="op">%&gt;%</span>
<span class="co"># and copy them a thousand times</span>
<span class="fu"><a href="https://rdrr.io/r/base/rep.html">rep</a></span><span class="op">(</span><span class="fl">1000</span><span class="op">)</span> <span class="op">%&gt;%</span>
<span class="co"># then scramble them</span>
<span class="fu"><a href="https://rdrr.io/r/base/sample.html">sample</a></span><span class="op">(</span><span class="op">)</span>
<span class="co"># what do these values look like? They are of class &lt;mo&gt;:</span>
<span class="fu"><a href="https://rdrr.io/r/utils/head.html">head</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span>
<span class="co"># Class &lt;mo&gt;</span>
<span class="co"># [1] B_STPHY_AURS B_STRPT_GRPC B_STPHY_CONS B_STPHY_EPDR B_STRPT_PNMN</span>
<span class="co"># [6] B_PROTS_VLGR</span>
<span class="co"># as the example_isolates has 2,000 rows, we should have 2 million items</span>
<span class="co"># as the example_isolates data set has 2,000 rows, we should have 2 million items</span>
<span class="fu"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span>
<span class="co"># [1] 2000000</span>
@ -265,32 +272,32 @@
<span class="co"># [1] 90</span>
<span class="co"># now let's see:</span>
<span class="va">run_it</span> <span class="op">&lt;-</span> <span class="fu">microbenchmark</span><span class="op">(</span><span class="fu"><a href="../reference/mo_property.html">mo_name</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span>,
<span class="va">run_it</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/microbenchmark/man/microbenchmark.html">microbenchmark</a></span><span class="op">(</span><span class="fu"><a href="../reference/mo_property.html">mo_name</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span>,
times <span class="op">=</span> <span class="fl">10</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/r/base/print.html">print</a></span><span class="op">(</span><span class="va">run_it</span>, unit <span class="op">=</span> <span class="st">"ms"</span>, signif <span class="op">=</span> <span class="fl">3</span><span class="op">)</span>
<span class="co"># Unit: milliseconds</span>
<span class="co"># expr min lq mean median uq max neval</span>
<span class="co"># mo_name(x) 137 146 178 172 193 282 10</span></code></pre></div>
<p>So getting official taxonomic names of 2,000,000 (!!) items consisting of 90 unique values only takes 0.172 seconds. You only lose time on your unique input values.</p>
<span class="co"># mo_name(x) 157 187 222 206 224 372 10</span></code></pre></div>
<p>So getting official taxonomic names of 2,000,000 (!!) items consisting of 90 unique values only takes 0.206 seconds. That is 2.471 milliseconds per unique item on average. You only lose time on your unique input values.</p>
</div>
<div id="precalculated-results" class="section level3">
<h3 class="hasAnchor">
<a href="#precalculated-results" class="anchor"></a>Precalculated results</h3>
<p>What about precalculated results? If the input is an already precalculated result of a helper function like <code><a href="../reference/mo_property.html">mo_name()</a></code>, it almost doesnt take any time at all (see C below):</p>
<p>What about precalculated results? If the input is an already precalculated result of a helper function such as <code><a href="../reference/mo_property.html">mo_name()</a></code>, it almost doesnt take any time at all. In other words, if you run <code><a href="../reference/mo_property.html">mo_name()</a></code> on a valid taxonomic name, it will return the results immediately (see C below):</p>
<div class="sourceCode" id="cb4"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">run_it</span> <span class="op">&lt;-</span> <span class="fu">microbenchmark</span><span class="op">(</span>A <span class="op">=</span> <span class="fu"><a href="../reference/mo_property.html">mo_name</a></span><span class="op">(</span><span class="st">"STAAUR"</span><span class="op">)</span>,
<code class="sourceCode R"><span class="va">run_it</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/microbenchmark/man/microbenchmark.html">microbenchmark</a></span><span class="op">(</span>A <span class="op">=</span> <span class="fu"><a href="../reference/mo_property.html">mo_name</a></span><span class="op">(</span><span class="st">"STAAUR"</span><span class="op">)</span>,
B <span class="op">=</span> <span class="fu"><a href="../reference/mo_property.html">mo_name</a></span><span class="op">(</span><span class="st">"S. aureus"</span><span class="op">)</span>,
C <span class="op">=</span> <span class="fu"><a href="../reference/mo_property.html">mo_name</a></span><span class="op">(</span><span class="st">"Staphylococcus aureus"</span><span class="op">)</span>,
times <span class="op">=</span> <span class="fl">10</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/r/base/print.html">print</a></span><span class="op">(</span><span class="va">run_it</span>, unit <span class="op">=</span> <span class="st">"ms"</span>, signif <span class="op">=</span> <span class="fl">3</span><span class="op">)</span>
<span class="co"># Unit: milliseconds</span>
<span class="co"># expr min lq mean median uq max neval</span>
<span class="co"># A 7.12 7.56 7.89 7.7 8.39 8.69 10</span>
<span class="co"># B 23.90 24.50 35.10 24.8 27.40 77.50 10</span>
<span class="co"># C 1.73 1.84 1.95 1.9 2.09 2.36 10</span></code></pre></div>
<p>So going from <code><a href="../reference/mo_property.html">mo_name("Staphylococcus aureus")</a></code> to <code>"Staphylococcus aureus"</code> takes 0.0019 seconds - it doesnt even start calculating <em>if the result would be the same as the expected resulting value</em>. That goes for all helper functions:</p>
<span class="co"># A 6.97 7.10 7.55 7.21 8.18 8.32 10</span>
<span class="co"># B 23.70 24.20 36.30 26.30 29.00 89.70 10</span>
<span class="co"># C 1.53 1.65 1.80 1.76 2.00 2.19 10</span></code></pre></div>
<p>So going from <code><a href="../reference/mo_property.html">mo_name("Staphylococcus aureus")</a></code> to <code>"Staphylococcus aureus"</code> takes 0.0018 seconds - it doesnt even start calculating <em>if the result would be the same as the expected resulting value</em>. That goes for all helper functions:</p>
<div class="sourceCode" id="cb5"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span class="va">run_it</span> <span class="op">&lt;-</span> <span class="fu">microbenchmark</span><span class="op">(</span>A <span class="op">=</span> <span class="fu"><a href="../reference/mo_property.html">mo_species</a></span><span class="op">(</span><span class="st">"aureus"</span><span class="op">)</span>,
<code class="sourceCode R"><span class="va">run_it</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/microbenchmark/man/microbenchmark.html">microbenchmark</a></span><span class="op">(</span>A <span class="op">=</span> <span class="fu"><a href="../reference/mo_property.html">mo_species</a></span><span class="op">(</span><span class="st">"aureus"</span><span class="op">)</span>,
B <span class="op">=</span> <span class="fu"><a href="../reference/mo_property.html">mo_genus</a></span><span class="op">(</span><span class="st">"Staphylococcus"</span><span class="op">)</span>,
C <span class="op">=</span> <span class="fu"><a href="../reference/mo_property.html">mo_name</a></span><span class="op">(</span><span class="st">"Staphylococcus aureus"</span><span class="op">)</span>,
D <span class="op">=</span> <span class="fu"><a href="../reference/mo_property.html">mo_family</a></span><span class="op">(</span><span class="st">"Staphylococcaceae"</span><span class="op">)</span>,
@ -302,15 +309,15 @@
<span class="fu"><a href="https://rdrr.io/r/base/print.html">print</a></span><span class="op">(</span><span class="va">run_it</span>, unit <span class="op">=</span> <span class="st">"ms"</span>, signif <span class="op">=</span> <span class="fl">3</span><span class="op">)</span>
<span class="co"># Unit: milliseconds</span>
<span class="co"># expr min lq mean median uq max neval</span>
<span class="co"># A 1.54 1.57 1.73 1.69 1.88 2.00 10</span>
<span class="co"># B 1.51 1.52 1.75 1.70 1.86 2.41 10</span>
<span class="co"># C 1.55 1.60 1.70 1.70 1.81 1.86 10</span>
<span class="co"># D 1.55 1.59 1.71 1.66 1.83 1.94 10</span>
<span class="co"># E 1.50 1.55 1.68 1.61 1.78 2.10 10</span>
<span class="co"># F 1.51 1.62 1.75 1.79 1.82 2.02 10</span>
<span class="co"># G 1.52 1.53 1.68 1.60 1.80 2.02 10</span>
<span class="co"># H 1.53 1.54 1.63 1.60 1.67 1.84 10</span></code></pre></div>
<p>Of course, when running <code><a href="../reference/mo_property.html">mo_phylum("Firmicutes")</a></code> the function has zero knowledge about the actual microorganism, namely <em>S. aureus</em>. But since the result would be <code>"Firmicutes"</code> anyway, 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.</p>
<span class="co"># A 1.44 1.72 1.69 1.74 1.75 1.78 10</span>
<span class="co"># B 1.47 1.55 1.67 1.73 1.74 1.89 10</span>
<span class="co"># C 1.52 1.59 1.73 1.74 1.89 1.91 10</span>
<span class="co"># D 1.46 1.51 1.64 1.64 1.75 1.89 10</span>
<span class="co"># E 1.42 1.47 1.60 1.52 1.71 1.97 10</span>
<span class="co"># F 1.44 1.71 1.69 1.72 1.75 1.88 10</span>
<span class="co"># G 1.42 1.50 1.68 1.61 1.73 2.36 10</span>
<span class="co"># H 1.52 1.54 1.66 1.71 1.73 1.87 10</span></code></pre></div>
<p>Of course, when running <code><a href="../reference/mo_property.html">mo_phylum("Firmicutes")</a></code> the function has zero knowledge about the actual microorganism, namely <em>S. aureus</em>. But since the result would be <code>"Firmicutes"</code> 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.</p>
</div>
<div id="results-in-other-languages" class="section level3">
<h3 class="hasAnchor">
@ -326,7 +333,7 @@
<span class="fu"><a href="../reference/mo_property.html">mo_name</a></span><span class="op">(</span><span class="st">"CoNS"</span>, language <span class="op">=</span> <span class="st">"nl"</span><span class="op">)</span> <span class="co"># or just mo_name("CoNS") on a Dutch system</span>
<span class="co"># [1] "Coagulase-negatieve Staphylococcus (CNS)"</span>
<span class="va">run_it</span> <span class="op">&lt;-</span> <span class="fu">microbenchmark</span><span class="op">(</span>en <span class="op">=</span> <span class="fu"><a href="../reference/mo_property.html">mo_name</a></span><span class="op">(</span><span class="st">"CoNS"</span>, language <span class="op">=</span> <span class="st">"en"</span><span class="op">)</span>,
<span class="va">run_it</span> <span class="op">&lt;-</span> <span class="fu"><a href="https://rdrr.io/pkg/microbenchmark/man/microbenchmark.html">microbenchmark</a></span><span class="op">(</span>en <span class="op">=</span> <span class="fu"><a href="../reference/mo_property.html">mo_name</a></span><span class="op">(</span><span class="st">"CoNS"</span>, language <span class="op">=</span> <span class="st">"en"</span><span class="op">)</span>,
de <span class="op">=</span> <span class="fu"><a href="../reference/mo_property.html">mo_name</a></span><span class="op">(</span><span class="st">"CoNS"</span>, language <span class="op">=</span> <span class="st">"de"</span><span class="op">)</span>,
nl <span class="op">=</span> <span class="fu"><a href="../reference/mo_property.html">mo_name</a></span><span class="op">(</span><span class="st">"CoNS"</span>, language <span class="op">=</span> <span class="st">"nl"</span><span class="op">)</span>,
es <span class="op">=</span> <span class="fu"><a href="../reference/mo_property.html">mo_name</a></span><span class="op">(</span><span class="st">"CoNS"</span>, language <span class="op">=</span> <span class="st">"es"</span><span class="op">)</span>,
@ -336,15 +343,15 @@
times <span class="op">=</span> <span class="fl">100</span><span class="op">)</span>
<span class="fu"><a href="https://rdrr.io/r/base/print.html">print</a></span><span class="op">(</span><span class="va">run_it</span>, unit <span class="op">=</span> <span class="st">"ms"</span>, signif <span class="op">=</span> <span class="fl">4</span><span class="op">)</span>
<span class="co"># Unit: milliseconds</span>
<span class="co"># expr min lq mean median uq max neval</span>
<span class="co"># en 17.38 17.71 25.63 18.11 19.75 81.61 100</span>
<span class="co"># de 20.14 20.61 24.87 20.91 21.57 85.23 100</span>
<span class="co"># nl 25.02 25.46 28.40 25.83 26.58 78.47 100</span>
<span class="co"># es 19.90 20.41 24.86 20.77 21.78 81.38 100</span>
<span class="co"># it 20.01 20.44 24.40 20.80 21.57 76.08 100</span>
<span class="co"># fr 19.85 20.23 29.97 20.89 23.56 192.40 100</span>
<span class="co"># pt 19.90 20.26 26.53 20.75 22.56 85.69 100</span></code></pre></div>
<p>Currently supported are German, Dutch, Spanish, Italian, French and Portuguese.</p>
<span class="co"># expr min lq mean median uq max neval</span>
<span class="co"># en 17.12 17.40 22.65 17.54 18.39 78.39 100</span>
<span class="co"># de 19.93 20.26 22.60 20.48 20.97 81.27 100</span>
<span class="co"># nl 24.87 25.24 30.93 25.50 26.90 87.17 100</span>
<span class="co"># es 20.00 20.26 24.39 20.58 21.13 82.01 100</span>
<span class="co"># it 19.92 20.26 26.54 20.66 21.38 79.15 100</span>
<span class="co"># fr 19.62 19.90 24.74 20.10 21.04 77.20 100</span>
<span class="co"># pt 19.74 20.02 26.76 20.41 22.68 82.61 100</span></code></pre></div>
<p>Currently supported non-English languages are German, Dutch, Spanish, Italian, French and Portuguese.</p>
</div>
</div>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 68 KiB

After

Width:  |  Height:  |  Size: 80 KiB

View File

@ -81,7 +81,7 @@
</button>
<span class="navbar-brand">
<a class="navbar-link" href="../index.html">AMR (for R)</a>
<span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Latest development version">1.5.0.9023</span>
<span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Latest development version">1.5.0.9024</span>
</span>
</div>

View File

@ -81,7 +81,7 @@
</button>
<span class="navbar-brand">
<a class="navbar-link" href="index.html">AMR (for R)</a>
<span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Latest development version">1.5.0.9023</span>
<span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Latest development version">1.5.0.9024</span>
</span>
</div>

View File

@ -43,7 +43,7 @@
</button>
<span class="navbar-brand">
<a class="navbar-link" href="index.html">AMR (for R)</a>
<span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Latest development version">1.5.0.9023</span>
<span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Latest development version">1.5.0.9024</span>
</span>
</div>

View File

@ -81,7 +81,7 @@
</button>
<span class="navbar-brand">
<a class="navbar-link" href="../index.html">AMR (for R)</a>
<span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Latest development version">1.5.0.9023</span>
<span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Latest development version">1.5.0.9024</span>
</span>
</div>
@ -236,13 +236,13 @@
<small>Source: <a href='https://github.com/msberends/AMR/blob/master/NEWS.md'><code>NEWS.md</code></a></small>
</div>
<div id="amr-1509023" class="section level1">
<h1 class="page-header" data-toc-text="1.5.0.9023">
<a href="#amr-1509023" class="anchor"></a>AMR 1.5.0.9023<small> Unreleased </small>
<div id="amr-1509024" class="section level1">
<h1 class="page-header" data-toc-text="1.5.0.9024">
<a href="#amr-1509024" class="anchor"></a>AMR 1.5.0.9024<small> Unreleased </small>
</h1>
<div id="last-updated-21-february-2021" class="section level2">
<div id="last-updated-22-february-2021" class="section level2">
<h2 class="hasAnchor">
<a href="#last-updated-21-february-2021" class="anchor"></a><small>Last updated: 21 February 2021</small>
<a href="#last-updated-22-february-2021" class="anchor"></a><small>Last updated: 22 February 2021</small>
</h2>
<div id="new" class="section level3">
<h3 class="hasAnchor">

View File

@ -12,7 +12,7 @@ articles:
datasets: datasets.html
resistance_predict: resistance_predict.html
welcome_to_AMR: welcome_to_AMR.html
last_built: 2021-02-21T22:18Z
last_built: 2021-02-22T19:20Z
urls:
reference: https://msberends.github.io/AMR//reference
article: https://msberends.github.io/AMR//articles

View File

@ -81,7 +81,7 @@
</button>
<span class="navbar-brand">
<a class="navbar-link" href="../index.html">AMR (for R)</a>
<span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Latest development version">1.5.0.9023</span>
<span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Latest development version">1.5.0.9024</span>
</span>
</div>

View File

@ -82,7 +82,7 @@
</button>
<span class="navbar-brand">
<a class="navbar-link" href="../index.html">AMR (for R)</a>
<span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Latest development version">1.5.0.9021</span>
<span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Latest development version">1.5.0.9024</span>
</span>
</div>

View File

@ -81,7 +81,7 @@
</button>
<span class="navbar-brand">
<a class="navbar-link" href="index.html">AMR (for R)</a>
<span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Latest development version">1.5.0.9023</span>
<span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Latest development version">1.5.0.9024</span>
</span>
</div>

View File

@ -17,47 +17,47 @@ knitr::opts_chunk$set(
collapse = TRUE,
comment = "#",
fig.width = 7.5,
fig.height = 4.5,
dpi = 75
fig.height = 5.5,
dpi = 100
)
```
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.
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.
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.
```{r, message = FALSE, echo = FALSE}
library(ggplot2)
ggplot.bm <- function(df, title = NULL) {
s <- summary(df)[order(summary(df)$median), ]
suppressWarnings(
print(
s %>%
ggplot(aes(x = expr, y = median)) +
geom_linerange(aes(ymin = 0, ymax = median), colour = "#555555") +
geom_text(aes(label = round(s$median, 0), 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(s$median) * 2)) +
labs(x = "Expression", y = "Median time in milliseconds (log scale)", title = title)
)
)
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))
}
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"))
}
```
```{r, message = FALSE}
microbenchmark <- microbenchmark::microbenchmark
library(microbenchmark)
library(AMR)
library(dplyr)
```
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.
The actual result is the same every time: it returns its microorganism code ``r as.character(as.mo("Staphylococcus aureus"))`` (*B* stands for *Bacteria*, the taxonomic kingdom).
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).
But the calculation time differs a lot:
@ -75,31 +75,37 @@ S.aureus <- microbenchmark(
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
times = 10)
times = 25)
print(S.aureus, unit = "ms", signif = 2)
```
```{r, echo = FALSE}
ggplot.bm(S.aureus)
```
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. It is clear that accepted taxonomic names are extremely fast, but some variations can take up to 500-1000 times as much time.
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 200 times slower to determine.
To improve performance, two important calculations take almost no time at all: **repetitive results** and **already precalculated results**.
To improve performance, we implemented two important algorithms to save unnecessary calculations: **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_name()` for this test - a helper function that returns the full microbial name (genus, species and possibly subspecies) which uses `as.mo()` internally.
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.
```{r, message = FALSE}
# take all MO codes from the example_isolates data set
x <- example_isolates$mo %>%
# start with the example_isolates data set
x <- example_isolates %>%
# take all MO codes from the 'mo' column
pull(mo) %>%
# and copy them a thousand times
rep(1000) %>%
# then scramble them
sample()
# what do these values look like? They are of class <mo>:
head(x)
# as the example_isolates has 2,000 rows, we should have 2 million items
# as the example_isolates data set has 2,000 rows, we should have 2 million items
length(x)
# and how many unique values do we have?
@ -111,11 +117,11 @@ run_it <- microbenchmark(mo_name(x),
print(run_it, unit = "ms", signif = 3)
```
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. You only lose time on your unique input values.
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(mean(run_it$time, na.rm = TRUE) / 1e6 / n_distinct(x), 3)` milliseconds per unique item on average. 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_name()`, it almost doesn't take any time at all (see 'C' below):
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):
```{r, warning=FALSE, message=FALSE}
run_it <- microbenchmark(A = mo_name("STAAUR"),
@ -140,7 +146,7 @@ run_it <- microbenchmark(A = mo_species("aureus"),
print(run_it, unit = "ms", signif = 3)
```
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 'knows' all phyla of all known bacteria (according to the Catalogue of Life), it can just return the initial value immediately.
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.
### Results in other languages
@ -164,4 +170,4 @@ run_it <- microbenchmark(en = mo_name("CoNS", language = "en"),
print(run_it, unit = "ms", signif = 4)
```
Currently supported are German, Dutch, Spanish, Italian, French and Portuguese.
Currently supported non-English languages are German, Dutch, Spanish, Italian, French and Portuguese.