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

Function for setting age-structured outputs. #297

Merged
merged 42 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
1ba67c0
Documentation updates
giovannic Aug 15, 2022
08b1f62
mixing matrix row sums == 1 error -> warning
htopazian Aug 26, 2022
c5a7bec
Reduce mixed transmission with testing
giovannic Oct 3, 2022
4e14e50
rebase rdt_mixing to time_varying_metapop
htopazian Oct 5, 2022
4b54e80
Reduce mixed transmission with testing
giovannic Oct 3, 2022
4a58966
Remove platform specific build files
giovannic Oct 6, 2022
3236f76
Add new formula for border test and treat:
giovannic Oct 6, 2022
8e759c1
Update documentation for border check model
giovannic Oct 6, 2022
247432c
fixing metapopulation vignette error
htopazian Oct 7, 2022
f3669f5
Change test and treat coefficients
giovannic Oct 13, 2022
1d4f4a2
Update RDT detection model
giovannic Oct 13, 2022
e568127
Fix regression tests
giovannic Oct 13, 2022
0057fda
RDT+
giovannic Oct 13, 2022
a591eb0
Add test case for completely removing transmission
giovannic Oct 14, 2022
ba4bedc
Add asymmetric transmission in mixing:
giovannic Sep 6, 2023
462b0fe
Merge pull request #277 from mrc-ide/feat/metapop_asym
htopazian Apr 9, 2024
610c5cb
Function for setting age-structured outputs for incidence, prevalence…
RJSheppard May 8, 2024
55d83ea
Added immunity parameter info to documentation of epi outputs function.
RJSheppard May 9, 2024
abdcef0
Function for setting age-structured outputs for incidence, prevalence…
RJSheppard May 8, 2024
8786b81
Added immunity parameter info to documentation of epi outputs function.
RJSheppard May 9, 2024
a542d10
Changed age limits back to continuous groups (no discrete age groups …
RJSheppard May 23, 2024
482fc83
Rebased with latest dev branch, merging with detect pcr/lm outputs.
RJSheppard May 23, 2024
c9c90f5
Merged tip with remote counterpart.
RJSheppard May 23, 2024
8f11a06
Merge branch 'dev' into new_epi_outputs_function
RJSheppard May 24, 2024
09a8c3e
Merge branch 'dev' into new_epi_outputs_function
RJSheppard May 24, 2024
5746a6b
Merge branch 'dev' into feat/metapopulation_model
htopazian Jun 6, 2024
b852847
update documentation
htopazian Jun 6, 2024
24a0253
fix metapop vignette
htopazian Jun 6, 2024
3989fdc
Merge branch 'dev' into new_epi_outputs_function
RJSheppard Jun 12, 2024
7bd8c0d
Update R/mixing.R
htopazian Jul 1, 2024
6d34636
Update R/mixing.R
htopazian Jul 1, 2024
06c1e8d
Remove unnecessary gitignore i386
giovannic Jul 3, 2024
91f8270
Export the run_resumable_simulation
plietar Jul 3, 2024
d05b335
Merge pull request #323 from mrc-ide/export_resumable_simulation
plietar Jul 3, 2024
a4f66e2
Merge branch 'dev' into feat/metapopulation_model
giovannic Jul 4, 2024
e51c6bc
Merge pull request #259 from mrc-ide/feat/metapopulation_model
giovannic Jul 4, 2024
b4825c7
Function for setting age-structured outputs for incidence, prevalence…
RJSheppard May 8, 2024
4701748
Added immunity parameter info to documentation of epi outputs function.
RJSheppard May 9, 2024
e004d5b
Changed age limits back to continuous groups (no discrete age groups …
RJSheppard May 23, 2024
65e5899
Rebased with latest dev branch, merging with detect pcr/lm outputs.
RJSheppard May 23, 2024
0ff9f95
Function for setting age-structured outputs for incidence, prevalence…
RJSheppard May 8, 2024
1886755
Merge branch 'new_epi_outputs_function' of github.com:mrc-ide/malaria…
RJSheppard Jul 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export(set_carrying_capacity)
export(set_clinical_treatment)
export(set_demography)
export(set_drugs)
export(set_epi_outputs)
export(set_equilibrium)
export(set_mass_pev)
export(set_mda)
Expand Down
59 changes: 59 additions & 0 deletions R/output_parameters.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#' @title Parameterise age grouped output rendering
Copy link
Member

Choose a reason for hiding this comment

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

I found this a little tricky to parse (although I think it all works!).

I was wondering if we can avoid having to input lists, and (maybe) make the process inthe function a bit simpler. Does the following work in the correct way? I haven't tested it!

#' @title Parameterise age grouped output rendering
#'
#' @details this function produces discrete and contiguous age groups, inclusive of the lower
#' age limit and exclusive of the upper age limit: e.g., c(0, 10, 100, 200, 250) will produce
#' three age groups: 0-9, 10-99 and 200-249 in days.
#' @param parameters the model parameters
#' @param age_group age breaks for population size outputs; default = NULL
#' @param incidence age breaks for incidence outputs (D+Tr+A); default = NULL
#' @param clinical_incidence age breaks for clinical incidence outputs (symptomatic); default = c(0, 1825)
#' @param severe_incidence age breaks for severe incidence outputs; default = NULL
#' @param prevalence age breaks for clinical prevalence outputs (pcr and lm detectable infections); default = c(730, 3650)
#' @param ica age breaks for average acquired clinical immunity; default = NULL
#' @param icm age breaks for average maternal clinical immunity; default = NULL
#' @param iva age breaks for average acquired severe immunity; default = NULL
#' @param ivm age breaks for average maternal severe immunity; default = NULL
#' @param id age breaks for average immunity to detectability; default = NULL
#' @param ib age breaks for average blood immunity; default = NULL
#' @export
#'
set_epi_outputs <- function(parameters,
                            age_group = c(0, 10, 100, 200, 250),
                            incidence = NULL,
                            clinical_incidence = c(0, 1825),
                            severe_incidence = NULL,
                            prevalence = c(730, 3650),
                            ica = NULL,
                            icm = NULL,
                            iva = NULL,
                            ivm = NULL,
                            id = NULL,
                            ib = NULL
){
  
  input <- list(
    age_group = age_group,
    incidence = incidence,
    clinical_incidence = clinical_incidence,
    severe_incidence = severe_incidence,
    prevalence = prevalence,
    ica = ica,
    icm = icm,
    iva = iva,
    ivm = ivm,
    id = id,
    ib = ib
  )
  input <- input[!sapply(input, is.null)]
  
  for(i in seq_along(input)){
    name <- names(input)[i]
    ages <- input[[i]]
    min_ages <- ages[-length(ages)]
    max_ages <- ages[-1] - 1
    parameters[[paste0(name, "_rendering_min_ages")]] <- min_ages
    parameters[[paste0(name, "_rendering_max_ages")]] <- max_ages
  }
  
  return(parameters)
}

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 was trying to capture discrete age blocks with the lists, e.g., if you want to output 1-100 and 200-300, but not 101-199. I don't think this code would do that, but maybe that's not so important for it to do.

#'
#' @details this function produces contiguous age groups, inclusive of the lower
#' age limit and exclusive of the upper age limit: e.g., c(0, 10, 100) will produce
#' two age groups: 0-9 and 10-99 in days.
#' @param parameters the model parameters
#' @param age_group age breaks for population size outputs; default = NULL
#' @param incidence age breaks for incidence outputs (D+Tr+A); default = NULL
#' @param clinical_incidence age breaks for clinical incidence outputs (symptomatic); default = c(0, 1825)
#' @param severe_incidence age breaks for severe incidence outputs; default = NULL
#' @param prevalence age breaks for clinical prevalence outputs (pcr and lm detectable infections); default = c(730, 3650)
#' @param ica age breaks for average acquired clinical immunity; default = NULL
#' @param icm age breaks for average maternal clinical immunity; default = NULL
#' @param iva age breaks for average acquired severe immunity; default = NULL
#' @param ivm age breaks for average maternal severe immunity; default = NULL
#' @param id age breaks for average immunity to detectability; default = NULL
#' @param ib age breaks for average blood immunity; default = NULL
#' @export
#'
set_epi_outputs <- function(parameters,
age_group = NULL,
incidence = NULL,
clinical_incidence = NULL,
severe_incidence = NULL,
prevalence = NULL,
ica = NULL,
icm = NULL,
iva = NULL,
ivm = NULL,
id = NULL,
ib = NULL
){

input <- list(
age_group = age_group,
incidence = incidence,
clinical_incidence = clinical_incidence,
severe_incidence = severe_incidence,
prevalence = prevalence,
ica = ica,
icm = icm,
iva = iva,
ivm = ivm,
id = id,
ib = ib
)
input <- input[!sapply(input, is.null)]

Copy link
Member

Choose a reason for hiding this comment

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

We may want to validate that output_rendering... is in parameters? I feel like R lets you put any kwarg in without complaining. Something to double check

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry - would you mind explaining this a bit more? Are you saying to just check that the parameters appear in the parameter list after this function is used? I have a test that should check for this.

Copy link
Member

Choose a reason for hiding this comment

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

this is obsolete now since we don't use formals!

for(i in seq_along(input)){
name <- names(input)[i]
ages <- input[[i]]
min_ages <- ages[-length(ages)]
max_ages <- ages[-1] - 1
parameters[[paste0(name, "_rendering_min_ages")]] <- min_ages
parameters[[paste0(name, "_rendering_max_ages")]] <- max_ages
}

return(parameters)
}
16 changes: 13 additions & 3 deletions R/processes.R
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ create_processes <- function(
# =========
# Rendering
# =========

imm_var_names <- c('ica', 'icm', 'id', 'ib', 'iva', 'ivm')

processes <- c(
processes,
individual::categorical_count_renderer_process(
Expand All @@ -190,10 +193,10 @@ create_processes <- function(
),
create_variable_mean_renderer_process(
renderer,
c('ica', 'icm', 'ib', 'id', 'iva', 'ivm'),
variables[c('ica', 'icm', 'ib', 'id', 'iva', 'ivm')]
imm_var_names,
variables[imm_var_names]
),
create_prevelance_renderer(
create_prevalence_renderer(
variables$state,
variables$birth,
variables$id,
Expand All @@ -205,6 +208,13 @@ create_processes <- function(
parameters,
renderer
),
create_age_variable_mean_renderer_process(
imm_var_names[paste0(imm_var_names,"_rendering_min_ages") %in% names(parameters)],
variables[imm_var_names[paste0(imm_var_names,"_rendering_min_ages") %in% names(parameters)]],
variables$birth,
parameters,
renderer
),
create_compartmental_rendering_process(renderer, solvers, parameters)
)

Expand Down
26 changes: 25 additions & 1 deletion R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ in_age_range <- function(birth, timestep, lower, upper) {
#' @param renderer model renderer
#'
#' @noRd
create_prevelance_renderer <- function(
create_prevalence_renderer <- function(
state,
birth,
immunity,
Expand Down Expand Up @@ -128,6 +128,30 @@ create_variable_mean_renderer_process <- function(
}
}

create_age_variable_mean_renderer_process <- function(
names,
variables,
birth,
parameters,
renderer
) {
function(timestep) {
for (i in seq_along(variables)) {
for (j in seq_along(parameters[[paste0(names[[i]],"_rendering_min_ages")]])) {
lower <- parameters[[paste0(names[[i]],"_rendering_min_ages")]][[j]]
upper <- parameters[[paste0(names[[i]],"_rendering_max_ages")]][[j]]
in_age <- in_age_range(birth, timestep, lower, upper)
renderer$render(paste0('n_', lower, '_', upper), in_age$size(), timestep)
renderer$render(
paste0(names[[i]], '_mean_', lower, '_', upper),
mean(variables[[i]]$get_values(index = in_age)),
timestep
)
}
}
}
}

create_vector_count_renderer_individual <- function(
mosquito_state,
species,
Expand Down
54 changes: 54 additions & 0 deletions man/set_epi_outputs.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions tests/testthat/test-output.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
test_that('Test age parameter function works', {
parameters <- get_parameters()
age_limits <- c(0,1,2,3)*365
parameters <- set_epi_outputs(parameters,
age_group = age_limits,
incidence = age_limits+1,
clinical_incidence = age_limits+2,
severe_incidence = age_limits+3,
prevalence = age_limits+4,
ica = age_limits+5,
icm = age_limits+6,
id = age_limits+7,
ib = age_limits+8,
iva = age_limits+9,
ivm = age_limits+10
)

sim <- run_simulation(timesteps = 1, parameters)

expect_true(
all(
paste0(rep(c("n_age",
"n", "n_inc", "p_inc",
"n","n_inc_clinical","p_inc_clinical",
"n","n_inc_severe","p_inc_severe",
"n","n_detect_lm","p_detect_lm","n_detect_pcr",
"ica_mean", "icm_mean","id_mean","ib_mean","iva_mean","ivm_mean"), each = 3),"_",
age_limits[-4]+rep(c(0,rep(c(1:3), each = 3), rep(4, 4), 5:10), each = 3),"_",age_limits[-1]-1+rep(c(0,rep(c(1:3), each = 3), rep(4, 4), 5:10), each = 3)
) %in%
names(sim)))

})

4 changes: 2 additions & 2 deletions tests/testthat/test-render.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test_that('that default rendering works', {
immunity <- individual::DoubleVariable$new(rep(1, 4))

renderer <- mock_render(1)
process <- create_prevelance_renderer(
process <- create_prevalence_renderer(
state,
birth,
immunity,
Expand Down Expand Up @@ -70,7 +70,7 @@ test_that('that default rendering works when no one is in the age range', {
immunity <- individual::DoubleVariable$new(rep(1, 4))

renderer <- mock_render(1)
process <- create_prevelance_renderer(
process <- create_prevalence_renderer(
state,
birth,
immunity,
Expand Down
Loading