Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functionality for using nested variable and event lists #100

Closed
wants to merge 1 commit into from

Conversation

pwinskill
Copy link
Member

@pwinskill pwinskill commented Jun 10, 2021

Adding capability to setup nested lists of variables and events. This can aid the user in making "grouped" variables or events and is helpful if the group size may vary. For example the variables list may contain a sub list of variables "vaccines" of length number of unique vaccines that can be used to track an individual's vaccine status and be flexible to changing numbers or available vaccines.

This goes some way to resolving #52

A couple of points that it would be good to hear your feedback on @giovannic :

  1. This implementation does obfuscate the simulation loop code somewhat (it now calls a recursive function for events and variable updates). Not sure if there is a more clear way to do this?
  2. Would appreciate your feedback on tests for this addition (I can implement some, but the testing in this package is very concise and neat).

If it helps here is some example code using the changes to model 4, independent diseases in a population, adapted from the SIR vignette:


# 4 Diseases each with unique R0, all other parameters are the same
n_disease <- 4
R0 <- c(2, 3, 4, 5)

N <- 1e3
I0 <- 5
S0 <- N - I0
dt <- 0.1
tmax <- 100
steps <- tmax/dt
gamma <- 1/10
beta <- R0 * gamma
health_states <- c("S","I","R")
health_states_t0 <- rep("S",N)
health_states_t0[sample.int(n = N,size = I0)] <- "I"

# Created nested list of variables where states is a sub list of the health state for each disease
variables <- list()
variables$states <- lapply(1:n_disease, function(x){
  CategoricalVariable$new(categories = health_states, initial_values = health_states_t0)
})

# Infection process
infection_process <- function(t){
  for(disease in 1:n_disease){
    I <- variables$states[[disease]]$get_size_of("I")
    foi <- beta[disease] * I/N
    S <- variables$states[[disease]]$get_index_of("S")
    S$sample(rate = pexp(q = foi * dt))
    variables$states[[disease]]$queue_update(value = "I",index = S)
  }
}

# Recovery events
events <- list()
events$recovery <- lapply(1:n_disease, function(x){
  TargetedEvent$new(population_size = N)
})

update_status <- function(variable, new_status){
  force(variable)
  force(new_status)
  function(timestep, target){
    variable$queue_update(new_status, target)
  }
}


# add listeners
for(disease in 1:n_disease){
  events$recovery[[disease]]$add_listener(
    update_status(variables$states[[disease]], "R"))
}
# Recovery process
recovery_process <- function(t){
  for(disease in 1:n_disease){
    I <- variables$states[[disease]]$get_index_of("I")
    already_scheduled <- events$recovery[[disease]]$get_scheduled()
    I$and(already_scheduled$not())
    rec_times <- rgeom(n = I$size(),prob = pexp(q = gamma * dt)) + 1
    events$recovery[[disease]]$schedule(target = I, delay = rec_times)
  }
}

# Render output
health_render <- Render$new(timesteps = steps)
render_states <- function(t){
  for(disease in 1:n_disease){
    for(i in seq_along(health_states)){
      size <- variables$states[[disease]]$get_size_of(health_states[i])
      health_render$render(paste0(health_states[i], disease), size, t)
    }
  }
}

# Run simulation
out <- simulation_loop(
  variables = variables,
  events = events,
  processes = list(infection_process,recovery_process,render_states),
  timesteps = steps
)

# Visualise output
states <- health_render$to_dataframe() %>%
  tidyr::pivot_longer(-timestep, names_to = "state", values_to = "n") %>%
  tidyr::separate(state, sep = 1,  c("State", "Disease"))

ggplot2::ggplot(states, ggplot2::aes(x = timestep, y = n, col = Disease, linetype = State)) +
  ggplot2::geom_line() +
  ggplot2::theme_bw()

@codecov
Copy link

codecov bot commented Jun 10, 2021

Codecov Report

Merging #100 (21ddf40) into dev (c9b30ce) will decrease coverage by 0.24%.
The diff coverage is 75.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##              dev     #100      +/-   ##
==========================================
- Coverage   92.50%   92.26%   -0.25%     
==========================================
  Files          26       26              
  Lines        1054     1060       +6     
==========================================
+ Hits          975      978       +3     
- Misses         79       82       +3     
Impacted Files Coverage Δ
R/simulation.R 80.00% <75.00%> (-12.86%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 300a6fe...21ddf40. Read the comment docs.

@pwinskill pwinskill closed this Jun 10, 2021
@giovannic giovannic deleted the feat/nested_variables_events branch July 7, 2022 16:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant