inowas / frontend-app

INOWAS-DSS on Create React App
2 stars 4 forks source link

T15: QMRA improvements #410

Closed mrustl closed 3 years ago

mrustl commented 3 years ago

Content of attached dataset (input: qmra_config_veronika.json, r-script: qmra_improvements.Rmd, output: rendered qmra_improvements.Rmd to html, docx, pdf): qmra_improvements.zip

Below the rendered content of the qmra_improvements.Rmd as markdown, including to-dos for @Roschl:

General improvement for UI inflow

Remove triangle distribution (does not make sense here!)

Debugging Veronika`s Config

Load config

### Download config as JSON from https://dev.inowas.com/tools
config <- jsonlite::fromJSON("qmra_config_veronika.json")
config <- config$config

Try risk calculation

risk_fails <- kwb.qmra::opencpu_simulate_risk(config)
#> 
#> # STEP 0: BASIC CONFIGURATION
#> 
#> Simulated 1 pathogen(s): Campylobacter jejuni and Campylobacter coli
#> Number of random distribution repeatings: 100
#> Number of exposure events: 365
#> 
#> # STEP 1: INFLOW
#> 
#> Simulated pathogen: Campylobacter jejuni and Campylobacter coli
#> Create 100 random distribution(s): 10^rnorm  (n: 365, mean: 0.000000, sd: 1.823870)
#> Warning in data.frame(..., check.names = FALSE): Zeilennamen wurden in einer
#> short Variablen gefunden und wurden verworfen
#> Providing inflow events ... ok. (0.00s) 
#> 
#> # STEP 2: TREATMENT SCHEMES
#> Error in sprintf(fmt, ...): ungültiges Format '%f'; benutze Format %s für Zeichenketten-Objekte

Check treatment train

config$treatment$schemes
##   TreatmentSchemeID  TreatmentSchemeName TreatmentID         TreatmentName
## 1                 1  SMARTplus for campy           1   Secondary treatment
## 2                 1 New Treatment Scheme           2 Rapid sand filtration
## 3                 1 New Treatment Scheme           3             SMARTplus
## 4                 1 New Treatment Scheme           4        UV irradiation
## 5                 1 New Treatment Scheme           5    Subsurface removal
## 6                 1 New Treatment Scheme           6 Dual media filtration

Identified Problems:

TreatmentSchemeName defined in UI (SMARTplus for campy) is not copied for all processes. To be fixed by @Roschl

Check treatment steps

config$treatment$processes
##   TreatmentID         TreatmentName      TreatmentGroup PathogenGroup          type  min max
## 1           1   Secondary treatment New Treatment Group      Bacteria       uniform  0.6 3.5
## 2           2 Rapid sand filtration New Treatment Group      Bacteria       uniform  0.6 2.8
## 3           3             SMARTplus New Treatment Group      Bacteria       uniform    2   6
## 4           4        UV irradiation New Treatment Group      Bacteria       uniform    4   6
## 5           5    Subsurface removal New Treatment Group      Bacteria log10_uniform 0.02 1.5
## 6           6 Dual media filtration New Treatment Group      Bacteria       uniform    0   1
str(config$treatment$processes)
## 'data.frame':    6 obs. of  7 variables:
##  $ TreatmentID   : int  1 2 3 4 5 6
##  $ TreatmentName : chr  "Secondary treatment" "Rapid sand filtration" "SMARTplus" "UV irradiation" ...
##  $ TreatmentGroup: chr  "New Treatment Group" "New Treatment Group" "New Treatment Group" "New Treatment Group" ...
##  $ PathogenGroup : chr  "Bacteria" "Bacteria" "Bacteria" "Bacteria" ...
##  $ type          : chr  "uniform" "uniform" "uniform" "uniform" ...
##  $ min           : chr  "0.6" "0.6" "2" "4" ...
##  $ max           : chr  "3.5" "2.8" "6" "6" ...

Identified Problems:

Fix config

By manually converting min and max to numeric (@Roschl: please fix this in the UI by providing numeric instead of character)

config$treatment$processes$min <- as.numeric(config$treatment$processes$min)
config$treatment$processes$max <- as.numeric(config$treatment$processes$max)

str(config$treatment$processes)
## 'data.frame':    6 obs. of  7 variables:
##  $ TreatmentID   : int  1 2 3 4 5 6
##  $ TreatmentName : chr  "Secondary treatment" "Rapid sand filtration" "SMARTplus" "UV irradiation" ...
##  $ TreatmentGroup: chr  "New Treatment Group" "New Treatment Group" "New Treatment Group" "New Treatment Group" ...
##  $ PathogenGroup : chr  "Bacteria" "Bacteria" "Bacteria" "Bacteria" ...
##  $ type          : chr  "uniform" "uniform" "uniform" "uniform" ...
##  $ min           : num  0.6 0.6 2 4 0.02 0
##  $ max           : num  3.5 2.8 6 6 1.5 1

Re-run calculation

R simulation now works:

risk_works <- kwb.qmra::opencpu_simulate_risk(config)
## 
## # STEP 0: BASIC CONFIGURATION
## 
## Simulated 1 pathogen(s): Campylobacter jejuni and Campylobacter coli
## Number of random distribution repeatings: 100
## Number of exposure events: 365
## 
## # STEP 1: INFLOW
## 
## Simulated pathogen: Campylobacter jejuni and Campylobacter coli
## Create 100 random distribution(s): 10^rnorm  (n: 365, mean: 0.000000, sd: 1.823870)
## Warning in data.frame(..., check.names = FALSE): Zeilennamen wurden in einer short Variablen gefunden und wurden verworfen
## Providing inflow events ... ok. (0.00s) 
## 
## # STEP 2: TREATMENT SCHEMES
## 
## Create 100 random distribution(s): uniform (n: 365, min: 0.600000, max: 3.500000)
## Create 100 random distribution(s): uniform (n: 365, min: 0.600000, max: 2.800000)
## Create 100 random distribution(s): uniform (n: 365, min: 2.000000, max: 6.000000)
## Create 100 random distribution(s): uniform (n: 365, min: 4.000000, max: 6.000000)
## Create 100 random distribution(s): 10^runif (n: 365, min: -1.698970, max: 0.176091)
## Create 100 random distribution(s): uniform (n: 365, min: 0.000000, max: 1.000000)
## 
## # STEP 3: EXPOSURE
## 
## Simulated exposure: volume per event
## Replicate 36500 times constant value 2.000000
## 
## # STEP 4: DOSE RESPONSE
## 
## 
## # STEP 5: HEALTH

Risk Output total

risk_works$total
## # A tibble: 100 x 12
## # Groups:   repeatID, TreatmentSchemeID [100]
##    repeatID TreatmentSchemeID PathogenID events inflow_median logreduction_medi~ volume_sum exposure_sum dose_sum infectionProb_s~
##       <int>             <int>      <int>  <int>         <dbl>              <dbl>      <int>        <dbl>    <int>            <dbl>
##  1        1                 1          1    365         1.10                13.6        730  0.000000256        0                0
##  2        2                 1          1    365         1.32                13.6        730  0.00000126         0                0
##  3        3                 1          1    365         1.42                13.6        730  0.000000399        0                0
##  4        4                 1          1    365         1.93                13.6        730  0.000000363        0                0
##  5        5                 1          1    365         0.658               13.6        730  0.000000822        0                0
##  6        6                 1          1    365         1.12                13.8        730  0.0000305          0                0
##  7        7                 1          1    365         0.813               13.6        730  0.00000408         0                0
##  8        8                 1          1    365         0.868               13.6        730  0.00000232         0                0
##  9        9                 1          1    365         0.990               13.5        730  0.000000138        0                0
## 10       10                 1          1    365         0.738               13.6        730  0.00000521         0                0
## # ... with 90 more rows, and 2 more variables: illnessProb_sum <dbl>, dalys_sum <dbl>

This table can get quite large (dependent on number of pathogens, treatment schemes, monte carlo runs) thus only TreatmentSchemeID and PathogenID are provided as integer (less data demand).

If names are needed for plotting these can be integrated in the UI by using the content of the following configurations:

@Roschl: could you add the names to the CSV / JSON files for download in the UI using the available data described above?

Session info

sessioninfo::session_info()
## - Session info -----------------------------------------------------------------------------------------------------------------
##  setting  value                       
##  version  R version 4.1.0 (2021-05-18)
##  os       Windows 10 x64              
##  system   x86_64, mingw32             
##  ui       RStudio                     
##  language (EN)                        
##  collate  German_Germany.1252         
##  ctype    German_Germany.1252         
##  tz       Europe/Berlin               
##  date     2021-07-06                  
## 
## - Packages ---------------------------------------------------------------------------------------------------------------------
##  ! package           * version    date       lib source                                  
##    bslib               0.2.5.1    2021-05-18 [1] CRAN (R 4.1.0)                          
##    callr               3.7.0      2021-04-20 [1] CRAN (R 4.1.0)                          
##    cellranger          1.1.0      2016-07-27 [1] CRAN (R 4.1.0)                          
##  V cli                 2.5.0      2021-06-30 [1] CRAN (R 4.1.0)                          
##    colorspace          2.0-1      2021-05-04 [1] CRAN (R 4.1.0)                          
##    crayon              1.4.1      2021-02-08 [1] CRAN (R 4.1.0)                          
##    curl                4.3.1      2021-04-30 [1] CRAN (R 4.1.0)                          
##    digest              0.6.27     2020-10-24 [1] CRAN (R 4.1.0)                          
##    dplyr             * 1.0.6      2021-05-05 [1] CRAN (R 4.1.0)                          
##    dwc.wells           0.0.0.9000 2021-07-05 [1] Github (kwb-r/dwc.wells@834535e)        
##    ellipsis            0.3.2      2021-04-29 [1] CRAN (R 4.1.0)                          
##    evaluate            0.14       2019-05-28 [1] CRAN (R 4.1.0)                          
##    fansi               0.5.0      2021-05-25 [1] CRAN (R 4.1.0)                          
##    fastmap             1.1.0      2021-01-25 [1] CRAN (R 4.1.0)                          
##    forcats             0.5.1      2021-01-27 [1] CRAN (R 4.1.0)                          
##    generics            0.1.0      2020-10-31 [1] CRAN (R 4.1.0)                          
##  V ggplot2             3.3.4      2021-06-25 [1] CRAN (R 4.1.0)                          
##    glue                1.4.2      2020-08-27 [1] CRAN (R 4.1.0)                          
##    gtable              0.3.0      2019-03-25 [1] CRAN (R 4.1.0)                          
##    hms                 1.1.0      2021-05-17 [1] CRAN (R 4.1.0)                          
##    htmltools           0.5.1.1    2021-01-22 [1] CRAN (R 4.1.0)                          
##    httpuv              1.6.1      2021-05-07 [1] CRAN (R 4.1.0)                          
##    jquerylib           0.1.4      2021-04-26 [1] CRAN (R 4.1.0)                          
##    jsonlite            1.7.2      2020-12-09 [1] CRAN (R 4.1.0)                          
##    knitr               1.33       2021-04-24 [1] CRAN (R 4.1.0)                          
##    kwb.datetime        0.4.0      2021-06-15 [1] Github (kwb-r/kwb.datetime@0ffea49)     
##    kwb.db              0.5.0.9000 2021-07-05 [1] Github (kwb-r/kwb.db@d728b9f)           
##    kwb.qmra            0.3.0      2021-06-14 [1] Github (kwb-r/kwb.qmra@9245953)         
##    kwb.utils           0.8.0      2021-06-16 [1] Github (kwb-r/kwb.utils@f58dba7)        
##    later               1.2.0      2021-04-23 [1] CRAN (R 4.1.0)                          
##    lifecycle           1.0.0      2021-02-15 [1] CRAN (R 4.1.0)                          
##    lubridate           1.7.10     2021-02-26 [1] CRAN (R 4.1.0)                          
##    magrittr            2.0.1      2020-11-17 [1] CRAN (R 4.1.0)                          
##    mime                0.10       2021-02-13 [1] CRAN (R 4.1.0)                          
##    munsell             0.5.0      2018-06-12 [1] CRAN (R 4.1.0)                          
##    odbc32              0.2.9.9002 2021-07-05 [1] Github (hsonne/odbc32@4bdda9b)          
##    pillar              1.6.1      2021-05-16 [1] CRAN (R 4.1.0)                          
##    pkgbuild            1.2.0      2020-12-15 [1] CRAN (R 4.1.0)                          
##    pkgconfig           2.0.3      2019-09-22 [1] CRAN (R 4.1.0)                          
##    plyr                1.8.6      2020-03-03 [1] CRAN (R 4.1.0)                          
##    prettyunits         1.1.1      2020-01-24 [1] CRAN (R 4.1.0)                          
##    processx            3.5.2      2021-04-30 [1] CRAN (R 4.1.0)                          
##    promises            1.2.0.1    2021-02-11 [1] CRAN (R 4.1.0)                          
##    ps                  1.6.0      2021-02-28 [1] CRAN (R 4.1.0)                          
##    purrr               0.3.4      2020-04-17 [1] CRAN (R 4.1.0)                          
##    R6                  2.5.0      2020-10-28 [1] CRAN (R 4.1.0)                          
##    RColorBrewer        1.1-2      2014-12-07 [1] CRAN (R 4.1.0)                          
##    Rcpp                1.0.6      2021-01-15 [1] CRAN (R 4.1.0)                          
##    readr               1.4.0      2020-10-05 [1] CRAN (R 4.1.0)                          
##    readxl              1.3.1      2019-03-13 [1] CRAN (R 4.1.0)                          
##    remotes             2.4.0      2021-06-02 [1] CRAN (R 4.1.0)                          
##    rlang               0.4.11     2021-04-30 [1] CRAN (R 4.1.0)                          
##    rmarkdown           2.8        2021-05-07 [1] CRAN (R 4.1.0)                          
##    RODBC               1.3-16     2019-09-03 [1] Github (cran/RODBC@275b1a6)             
##    rprojroot           2.0.2      2020-11-15 [1] CRAN (R 4.1.0)                          
##    rstudioapi          0.13       2020-11-12 [1] CRAN (R 4.1.0)                          
##    sass                0.4.0      2021-05-12 [1] CRAN (R 4.1.0)                          
##    scales              1.1.1      2020-05-11 [1] CRAN (R 4.1.0)                          
##    sema.berlin.utils   0.1.0      2021-07-05 [1] Github (kwb-r/sema.berlin.utils@f9e80a5)
##    sessioninfo         1.1.1      2018-11-05 [1] CRAN (R 4.1.0)                          
##    sfsmisc             1.1-11     2021-04-12 [1] CRAN (R 4.1.0)                          
##    shiny               1.6.0      2021-01-25 [1] CRAN (R 4.1.0)                          
##    stringi             1.6.1      2021-05-10 [1] CRAN (R 4.1.0)                          
##    stringr             1.4.0      2019-02-10 [1] CRAN (R 4.1.0)                          
##    tibble              3.1.2      2021-05-16 [1] CRAN (R 4.1.0)                          
##    tidyr               1.1.3      2021-03-03 [1] CRAN (R 4.1.0)                          
##    tidyselect          1.1.1      2021-04-30 [1] CRAN (R 4.1.0)                          
##    tinytex             0.32       2021-05-29 [1] CRAN (R 4.1.0)                          
##    utf8                1.2.1      2021-03-12 [1] CRAN (R 4.1.0)                          
##    vctrs               0.3.8      2021-04-29 [1] CRAN (R 4.1.0)                          
##    withr               2.4.2      2021-04-18 [1] CRAN (R 4.1.0)                          
##    xfun                0.23       2021-05-15 [1] CRAN (R 4.1.0)                          
##    xtable              1.8-4      2019-04-21 [1] CRAN (R 4.1.0)                          
##    yaml                2.2.1      2020-02-01 [1] CRAN (R 4.1.0)                          
## 
## [1] C:/Users/mrustl/Documents/R/win-library/4.1
## [2] C:/Program Files/R/R-4.1.0/library
## 
##  V -- Loaded and on-disk version mismatch.
Roschl commented 3 years ago

Fix config: min and max value in config$treatment$processes should be parsed as numbers already since https://github.com/inowas/frontend-app/releases/tag/v1.8.2

For old models, you might need to reenter the values, though!