## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----setup--------------------------------------------------------------------
library(ringbp)
library(data.table)

## -----------------------------------------------------------------------------
# Put parameters that are grouped by disease into this data.table
scenarios <- data.table(
  expand.grid(
   delay_group = list(data.table(
     delay = c("SARS", "Wuhan"),
     onset_to_isolation = c(
       \(n) rweibull(n = n, shape = 1.651524, scale = 4.287786),
       \(n) rweibull(n = n, shape = 2.305172, scale = 9.483875)
     )
   )),
   r0_community = c(1.1, 1.5),
   r0_isolated = 0,
   disp_community = 0.16,
   disp_isolated = 1,
   prop_presymptomatic = c(0.01, 0.15),
   prop_asymptomatic = c(0, 0.1),
   prop_ascertain = seq(0, 1, 0.25),
   initial_cases = c(5, 10),
   quarantine = FALSE,
   cap_max_days = 365,
   cap_cases = 5000
 )
)

## -----------------------------------------------------------------------------
list_cols <- grep("_group", colnames(scenarios), value = TRUE)
non_list_cols <- setdiff(colnames(scenarios), list_cols)
scenarios <- scenarios[, rbindlist(delay_group), by = c(non_list_cols)]

## -----------------------------------------------------------------------------
scenarios[, scenario :=  1:.N]
incub <- \(n) rweibull(n = n, shape = 1.65, scale = 4.28)
scenarios[, incubation_period := rep(list(incub), .N)]

## -----------------------------------------------------------------------------
scenario_sims <- scenarios[, list(data = list(.SD)), by = scenario]

## -----------------------------------------------------------------------------
n <- 3
res <- lapply(scenario_sims$data, \(x, n) {
  scenario_sim(
    n = n,
    initial_cases = x$initial_cases,
    offspring = offspring_opts(
      community = \(n) rnbinom(n = n, mu = x$r0_community, size = x$disp_community),
      isolated = \(n) rnbinom(n = n, mu = x$r0_isolated, size = x$disp_isolated)
    ),
    delays = delay_opts(
      incubation_period = x$incubation_period[[1]],
      onset_to_isolation = x$onset_to_isolation[[1]]
    ),
    event_probs = event_prob_opts(
      asymptomatic = x$prop_asymptomatic,
      presymptomatic_transmission = x$prop_presymptomatic,
      symptomatic_traced = x$prop_ascertain
    ),
    interventions = intervention_opts(quarantine = x$quarantine),
    sim = sim_opts(cap_max_days = x$cap_max_days, cap_cases = x$cap_cases)
  )
  },
  n = n
)

## -----------------------------------------------------------------------------
head(res, 3)

## -----------------------------------------------------------------------------
scenario_sims[, sims := lapply(data, \(x, n) {
  scenario_sim(
    n = n,
    initial_cases = x$initial_cases,
    offspring = offspring_opts(
      community = \(n) rnbinom(n = n, mu = x$r0_community, size = x$disp_community),
      isolated = \(n) rnbinom(n = n, mu = x$r0_isolated, size = x$disp_isolated)
    ),
    delays = delay_opts(
      incubation_period = x$incubation_period[[1]],
      onset_to_isolation = x$onset_to_isolation[[1]]
    ),
    event_probs = event_prob_opts(
      asymptomatic = x$prop_asymptomatic,
      presymptomatic_transmission = x$prop_presymptomatic,
      symptomatic_traced = x$prop_ascertain
    ),
    interventions = intervention_opts(quarantine = x$quarantine),
    sim = sim_opts(cap_max_days = x$cap_max_days, cap_cases = x$cap_cases)
  )
  },
  n = n
)]
scenario_sims
head(scenario_sims$data, 3)
head(scenario_sims$sims, 3)

## -----------------------------------------------------------------------------
library(future)
library(future.apply)

## ----eval=FALSE---------------------------------------------------------------
# future::plan("multisession", workers = 2)

## ----eval=FALSE---------------------------------------------------------------
# scenario_sims[, sims := future_lapply(data, \(x, n) {
#   scenario_sim(
#     n = n,
#     initial_cases = x$initial_cases,
#     offspring = offspring_opts(
#       community = \(n) rnbinom(n = n, mu = x$r0_community, size = x$disp_community),
#       isolated = \(n) rnbinom(n = n, mu = x$r0_isolated, size = x$disp_isolated)
#     ),
#     delays = delay_opts(
#       incubation_period = x$incubation_period[[1]],
#       onset_to_isolation = x$onset_to_isolation[[1]]
#     ),
#     event_probs = event_prob_opts(
#       asymptomatic = x$prop_asymptomatic,
#       presymptomatic_transmission = x$prop_presymptomatic,
#       symptomatic_traced = x$prop_ascertain
#     ),
#     interventions = intervention_opts(quarantine = x$quarantine),
#     sim = sim_opts(cap_max_days = x$cap_max_days, cap_cases = x$cap_cases)
#   )
#   },
#   n = n,
#   future.seed = TRUE
# )]
# scenario_sims

