## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  message = FALSE,
  warning = FALSE,
  fig.width = 7,
  fig.height = 4.5,
  out.width = "100%"
)

## ----setup--------------------------------------------------------------------
library(ambre)
set.seed(2024)

## ----attach-dplyr-------------------------------------------------------------
library(dplyr)

## ----overview-str-------------------------------------------------------------
str(config_ambre, max.level = 1)

## ----overview-subtables-------------------------------------------------------
lapply(config_ambre[c("treatment", "path", "economic")], names)

## ----mapping-table------------------------------------------------------------
mapping <- tibble::tribble(
  ~`CSV in data-raw/`,                  ~`Slot in config_ambre`,
  "exposure.csv",                       "exposure",
  "ambre_pathogene.csv",                "inflow",
  "ambre_barriere_general.csv",         "treatment$processes",
  "treatment_schemes.csv",              "treatment$schemes",
  "ambre_barriere_voie.csv",            "treatment$barriere_path",
  "ambre_barriere_specifique.csv",      "treatment$barriere_specific",
  "ambre_barriere_deperissement.csv",   "treatment$barriere_decay",
  "ambre_doseresponse.csv",             "doseresponse",
  "ambre_voie_description.csv",         "path$description",
  "ambre_voie_frequence.csv",           "path$frequency",
  "ambre_voie_volume.csv",              "path$volume",
  "ambre_culture_hauteur.csv",          "crop",
  "ambre_sante.csv",                    "health",
  "ambre_culture_eau.csv",              "economic$water_need",
  "ambre_population.csv",               "economic$population",
  "ambre_barriere_cout.csv",            "economic$cost"
)
knitr::kable(mapping)

## ----er-path------------------------------------------------------------------
config_ambre$path$description |>
  select(PathID, PopulationName, MatrixName, PathDescription) |>
  head(4)

## ----er-pathogen--------------------------------------------------------------
config_ambre$health |>
  select(PathogenID, PathogenName, dalys_per_case) |>
  inner_join(
    config_ambre$inflow |> select(PathogenID, PathogenGroup),
    by = "PathogenID"
  ) |>
  head(4)

## ----barrier-groups-----------------------------------------------------------
config_ambre$treatment$processes |>
  distinct(TreatmentID, TreatmentName, TreatmentGroup) |>
  count(TreatmentGroup, name = "n_barriers")

## ----barrier-prefix-----------------------------------------------------------
config_ambre$treatment$processes |>
  distinct(TreatmentID, TreatmentName) |>
  mutate(family = sub("[.].*", "", TreatmentName)) |>
  count(family, name = "n_barriers")

## ----dist-legend--------------------------------------------------------------
knitr::kable(tibble::tribble(
  ~type,           ~`columns read`,   ~draws,
  "value",         "value",           "a constant, repeated",
  "uniform",       "min, max",        "runif(min, max)",
  "triangle",      "min, max, mode",  "EnvStats::rtri(min, max, mode)",
  "norm",          "mean, sd",        "rnorm(mean, sd)",
  "lognorm",       "meanlog, sdlog",  "rlnorm(meanlog, sdlog)",
  "log10_uniform", "min, max",        "10^runif(min, max)",
  "log10_norm",    "mean, sd",        "10^rnorm(mean, sd)"
))

## ----dist-row-barrier---------------------------------------------------------
config_ambre$treatment$processes |>
  filter(TreatmentName == "Q.1 - Activated Sludge") |>
  select(TreatmentName, PathogenGroup, type, value, min, max, mode)

## ----dist-row-inflow----------------------------------------------------------
config_ambre$inflow |>
  filter(PathogenName == "Campylobacter jejuni") |>
  select(PathogenName, PathogenGroup, type, value, min, max)

## ----dist-exposure------------------------------------------------------------
config_ambre$exposure

## ----customize, eval = FALSE--------------------------------------------------
#  # 1. add a new quality barrier to the treatment CSV
#  csv_path <- file.path("data-raw", "ambre_barriere_general.csv")
#  new_barrier <- data.frame(
#    TreatmentID = 99, TreatmentName = "Q.8 - Ozonation",
#    TreatmentGroup = "Quality", PathogenGroup = "Bacteria",
#    type = "uniform", value = NA, min = 2, max = 4,
#    mode = NA, mean = NA, sd = NA, meanlog = NA, sdlog = NA
#  )
#  readr::write_csv(
#    rbind(readr::read_csv(csv_path), new_barrier),
#    csv_path
#  )
#  
#  # 2. rebuild data/config_ambre.rda from the 16 CSVs
#  source(file.path("data-raw", "config_ambre.R"))
#  
#  # 3. verify the new barrier resolves to its ID
#  query_barrier(barrierName = "Q.8 - Ozonation")

## ----query-helpers------------------------------------------------------------
query_pathogen(pathogenName = "Campylobacter jejuni")
query_barrier(barrierName = "Q.3 - UV Reactor")
query_crop(cropName = "Tomato")

## ----query-path---------------------------------------------------------------
query_volume(pathID = 1)
query_frequency(pathID = 1)

## ----pitfall-class------------------------------------------------------------
class(config_ambre$crop)

## ----pitfall-crop-------------------------------------------------------------
config_ambre$crop |>
  select(CropName, CropHeight)

