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

Allow simulation to be restored with new interventions. #286

Merged
merged 2 commits into from
May 1, 2024

Conversation

plietar
Copy link
Member

@plietar plietar commented Mar 22, 2024

This uses a few of individual's new features that make restoring more flexible. It also fixes a bug when restoring the mosquitto solvers, by correctly restoring the timestep, which is needed to model seasonality.

The intervention events use the new restore = FALSE flag to make sure their schedule can be modified when resuming. Instead of having the events re-schedule themselves everytime they fire, we setup the entire schedule upfront when initialising the simulation.

It adds end-to-end testing of this feature, across a range of scenarios. For each scenario, the outcomes of the simulation with and without restoring are compared and we make sure they are equivalent.

@plietar plietar force-pushed the flexible-event-restore branch from a601b59 to dfc82a6 Compare April 4, 2024 09:23
@plietar plietar force-pushed the flexible-event-restore branch from 709fae3 to 627a709 Compare April 16, 2024 16:19
@plietar plietar marked this pull request as ready for review April 16, 2024 17:08
@plietar plietar requested a review from giovannic April 16, 2024 17:08
Copy link
Member

@giovannic giovannic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lovely!

Perhaps a test case for setting correlation? Since it seems an important use case that you spent some time resolving.

tests/testthat/test-resume.R Outdated Show resolved Hide resolved
#' two phases. Between the two phases, the simulation state is saved and
#' restored. Optionally, the initial warmup phase can use a different set of
#' parameters, by specifying a value for warmup_parameters.
test_resume <- function(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really handy

expect_equal(data[data$n_pev_mass_dose_3 > 0, "timestep"], c(115, 190))
})

test_that("TBV intervention can be added when resuming", {
Copy link
Member

@giovannic giovannic Apr 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test seems different from the others, should we name it differently? or remove the TBV parameterisation from the warmup?

Suggested change
test_that("TBV intervention can be added when resuming", {
test_that("TBV intervention parameters can be changed when resuming", {

Copy link
Member Author

@plietar plietar Apr 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does both. There's a first call to test_resume with warmup_parameters = base and a second call with a TBV intervention during the warmup. The point is to make sure we can both add an intervention that didn't exist at all, or we can also add new instances of the same intervention.

I had done the same for PEV and bednets. MDA was actually the odd one out for which I hadn't done this, I'll add it now.
I could break each of those tests into two functions, but then I'd have to move the set_default_XXX helper functions to the global scope, so this seemed simpler.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tweaked the layout of the test a little bit which should make it easier to spot the two parts of it.

@plietar
Copy link
Member Author

plietar commented Apr 22, 2024

Perhaps a test case for setting correlation? Since it seems an important use case that you spent some time resolving.

Setting correlation doesn't work in this PR yet. I'll have a follow up soon that adds it with some test cases.

EDIT: the correlations follow-up is at #293

@plietar plietar requested a review from giovannic April 22, 2024 16:06
Copy link
Member

@giovannic giovannic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please merge in #293 first. Thanks!

plietar added 2 commits April 30, 2024 15:21
This uses a few of individual's new features that make restoring more
flexible. It also fixes a bug when restoring the mosquitto solvers, by
correctly restoring the timestep, which is needed to model seasonality.

The intervention events use the new `restore = FALSE` flag to make sure
their schedule can be modified when resuming. Instead of having the
events re-schedule themselves everytime they fire, we setup the entire
schedule upfront when initialising the simulation.

It adds end-to-end testing of this feature, across a range of scenarios.
For each scenario, the outcomes of the simulation with and without
restoring are compared and we make sure they are equivalent.
When resuming a simulation, it is possible to add new intervention.
The correlation of the new intervention may need configuring, both
relative to itself and to existing interventions.

The correlation parameters work by sampling, at the start of the
simulation, relative weights for each individual and intervention. The
weight are later used to select which individuals are targeted by the a
given intervention. There weights are drawn from a multivariate normal
distribution, whose variance-covariance matrix depends on the configured
correlation.

When adding interventions, new corresponding columns need to be added to
the weights matrix. A fresh mvnorm distribution cannot be used, since it
would override the already drawn weigths for the existing interventions.
The solution instead is to use a conditional mvnorm distribution on the
new columns, using the existing values as the conditions. This yields
new weigths which follow the determined variance-covariance matrix while
preserving the existing values.
@plietar plietar force-pushed the flexible-event-restore branch from 3f69bf5 to ac3703b Compare April 30, 2024 14:21
@plietar plietar merged commit 1111500 into dev May 1, 2024
4 checks passed
plietar added a commit that referenced this pull request May 1, 2024
This uses a few of individual's new features that make restoring more
flexible. It also fixes a bug when restoring the mosquitto solvers, by
correctly restoring the timestep, which is needed to model seasonality.

The intervention events use the new `restore = FALSE` flag to make sure
their schedule can be modified when resuming. Instead of having the
events re-schedule themselves everytime they fire, we setup the entire
schedule upfront when initialising the simulation.

It adds end-to-end testing of this feature, across a range of scenarios.
For each scenario, the outcomes of the simulation with and without
restoring are compared and we make sure they are equivalent.
@plietar plietar deleted the flexible-event-restore branch May 1, 2024 10:26
@giovannic giovannic mentioned this pull request Sep 11, 2024
Merged
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.

2 participants