From 2c9932bb87d5415a13cea5314544fd5ada116645 Mon Sep 17 00:00:00 2001 From: tbreweric Date: Tue, 26 Mar 2024 14:42:47 +0000 Subject: [PATCH 1/6] Combined ETF and SPC. Need to fix testthat files before PR --- DESCRIPTION | 2 +- R/antimalarial_resistance.R | 7 +++ R/disease_progression.R | 28 ++++++--- R/human_infection.R | 68 +++++++++++++++------ R/model.R | 7 ++- R/mortality_processes.R | 7 ++- R/parameters.R | 1 + R/processes.R | 24 +++++++- R/variables.R | 12 ++++ man/run_simulation.Rd | 7 ++- src/RcppExports.cpp | 2 +- tests/testthat/test-infection-integration.R | 4 ++ 12 files changed, 132 insertions(+), 37 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 1d971723..7ebbd7a0 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -91,7 +91,7 @@ Suggests: ggplot2, covr, mgcv -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.1 Roxygen: list(markdown = TRUE) LinkingTo: Rcpp, diff --git a/R/antimalarial_resistance.R b/R/antimalarial_resistance.R index 532c042b..7da3ad83 100644 --- a/R/antimalarial_resistance.R +++ b/R/antimalarial_resistance.R @@ -26,6 +26,13 @@ set_antimalarial_resistance <- function(parameters, reinfection_prob, slow_parasite_clearance_time) { + if(any(partner_drug_resistance > 0, + late_clinical_failure_prob > 0, + late_parasitological_prob > 0, + reinfection_prob > 0)) { + stop("Parameters set for unimplemented feature - late clinical failure, late parasitological failure, or reinfection during prophylaxis") + } + if(any(c(length(artemisinin_resistance), length(partner_drug_resistance), length(slow_parasite_clearance_prob), diff --git a/R/disease_progression.R b/R/disease_progression.R index 05367252..e8b35874 100644 --- a/R/disease_progression.R +++ b/R/disease_progression.R @@ -19,15 +19,27 @@ update_infection <- function( } create_progression_process <- function( - state, - from_state, - to_state, - rate, - infectivity, - new_infectivity - ) { + state, + from_state, + to_state, + rate, + infectivity, + new_infectivity +) { function(timestep) { - to_move <- state$get_index_of(from_state)$sample(1/rate) + + # Retrieve the indices of all individuals in the to_move state: + index <- state$get_index_of(from_state) + + # If the length of rate is greater than 1 (when it's a variable): + if (length(rate) > 1) { + rate <- rate$get_values(index$to_vector()) + } + + # Sample the individuals to be moved into a new Bitset using the transition rate(s): + to_move <- index$sample(1/rate) + + # Update the infection status of those individuals who are moving: update_infection( state, to_state, diff --git a/R/human_infection.R b/R/human_infection.R index 6b2ac3cf..4890e8a0 100644 --- a/R/human_infection.R +++ b/R/human_infection.R @@ -295,50 +295,82 @@ calculate_treated <- function( ) ]) + #+++ DRUG EFFICACY +++# + #+++++++++++++++++++++# + effectively_treated_index <- bernoulli_multi_p(parameters$drug_efficacy[drugs]) + effectively_treated <- bitset_at(seek_treatment, effectively_treated_index) + drugs <- drugs[effectively_treated_index] + n_drug_efficacy_failures <- n_treat - effectively_treated$size() + renderer$render('n_drug_efficacy_failures', n_drug_efficacy_failures, timestep) + + #+++ ANTIMALARIAL RESISTANCE +++# + #+++++++++++++++++++++++++++++++# if(parameters$antimalarial_resistance) { + resistance_parameters <- get_antimalarial_resistance_parameters( parameters = parameters, drugs = drugs, timestep = timestep ) - unsuccessful_treatment_probability <- resistance_parameters$artemisinin_resistance_proportion * resistance_parameters$early_treatment_failure_probability - susceptible_to_treatment_index <- bernoulli_multi_p(p = 1 - unsuccessful_treatment_probability) - susceptible_to_treatment <- bitset_at(seek_treatment, susceptible_to_treatment_index) - drugs <- drugs[susceptible_to_treatment_index] + + #+++ EARLY TREATMENT FAILURE +++# + #+++++++++++++++++++++++++++++++# + early_treatment_failure_probability <- resistance_parameters$artemisinin_resistance_proportion * resistance_parameters$early_treatment_failure_probability + successfully_treated_indices <- bernoulli_multi_p(p = 1 - early_treatment_failure_probability) + successfully_treated <- bitset_at(effectively_treated, successfully_treated_indices) + n_early_treatment_failure <- effectively_treated$size() - successfully_treated$size() + renderer$render('n_early_treatment_failure', n_early_treatment_failure, timestep) + drugs <- drugs[successfully_treated_indices] + resistance_parameters$dt_slow_parasite_clearance <- resistance_parameters$dt_slow_parasite_clearance[successfully_treated_indices] + + #+++ SLOW PARASITE CLEARANCE +++# + #+++++++++++++++++++++++++++++++# + slow_parasite_clearance_probability <- resistance_parameters$artemisinin_resistance_proportion[successfully_treated_indices] * + resistance_parameters$slow_parasite_clearance_probability[successfully_treated_indices] + slow_parasite_clearance_indices <- bernoulli_multi_p(p = slow_parasite_clearance_probability) + slow_parasite_clearance_individuals <- bitset_at(successfully_treated, slow_parasite_clearance_indices) + renderer$render('n_slow_parasite_clearance', slow_parasite_clearance_individuals$size(), timestep) + non_slow_parasite_clearance_individuals <- successfully_treated$copy()$set_difference(slow_parasite_clearance_individuals) + renderer$render('n_successfully_treated', successfully_treated$size(), timestep) + resistance_parameters$dt_slow_parasite_clearance <- resistance_parameters$dt_slow_parasite_clearance[slow_parasite_clearance_indices] + } else { - susceptible_to_treatment <- seek_treatment + + successfully_treated <- effectively_treated + renderer$render('n_successfully_treated', successfully_treated$size(), timestep) } - n_early_treatment_failure <- n_treat - susceptible_to_treatment$size() - successfully_treated_index <- bernoulli_multi_p(parameters$drug_efficacy[drugs]) - successfully_treated <- bitset_at(susceptible_to_treatment, successfully_treated_index) - successfully_treated_drugs <- drugs[successfully_treated_index] - n_treat_eff_fail <- susceptible_to_treatment$size() - length(successfully_treated_index) - renderer$render('n_early_treatment_failure', n_early_treatment_failure, timestep) - renderer$render('n_treat_eff_fail', n_treat_eff_fail, timestep) - renderer$render('n_treat_success', successfully_treated$size(), timestep) - - # Update variables of those who have been successfully treated: if (successfully_treated$size() > 0) { variables$state$queue_update("Tr", successfully_treated) variables$infectivity$queue_update( - parameters$cd * parameters$drug_rel_c[successfully_treated_drugs], + parameters$cd * parameters$drug_rel_c[drugs], successfully_treated ) variables$drug$queue_update( - successfully_treated_drugs, + drugs, successfully_treated ) variables$drug_time$queue_update( timestep, successfully_treated ) + if(parameters$antimalarial_resistance) { + variables$dt$queue_update( + parameters$dt, + non_slow_parasite_clearance_individuals + ) + variables$dt$queue_update( + resistance_parameters$dt_slow_parasite_clearance, + slow_parasite_clearance_individuals + ) + } } + successfully_treated + } - #' @title Schedule infections #' @description #' Schedule infections in humans after the incubation period diff --git a/R/model.R b/R/model.R index 4c4f7c2b..b4401855 100644 --- a/R/model.R +++ b/R/model.R @@ -74,9 +74,10 @@ #' susceptible #' * net_usage: the number people protected by a bed net #' * mosquito_deaths: number of adult female mosquitoes who die this timestep -#' * n_early_treatment_failure: number of clinically treated individuals who experienced early treatment failure in this timestep -#' * n_treat_eff_fail: number of clinically treated individuals who's treatment failed due to drug efficacy -#' * n_treat_success: number of successfully treated individuals in this timestep +#' * n_drug_efficacy_failures: number of clinically treated individuals whose treatment failed due to drug efficacy +#' * n_early_treatment_failure: number of clinically treated individuals who experienced early treatment failure +#' * n_successfully_treated: number of clinically treated individuals who are treated successfully (includes individuals who experience slow parasite clearance) +#' * n_slow_parasite_clearance: number of clinically treated individuals who experienced slow parasite clearance #' #' @param timesteps the number of timesteps to run the simulation for (in days) #' @param parameters a named list of parameters to use diff --git a/R/mortality_processes.R b/R/mortality_processes.R index f0f1b1f9..e9ea6ac9 100644 --- a/R/mortality_processes.R +++ b/R/mortality_processes.R @@ -74,7 +74,7 @@ sample_maternal_immunity <- function(variables, target, timestep, parameters) { } } -reset_target <- function(variables, events, target, state, timestep) { +reset_target <- function(variables, events, target, state, parameters, timestep) { if (target$size() > 0) { # clear events to_clear <- c( @@ -113,6 +113,11 @@ reset_target <- function(variables, events, target, state, timestep) { # onwards infectiousness variables$infectivity$queue_update(0, target) + + # treated compartment residence time: + if(!is.null(variables$dt)) { + variables$dt$queue_update(parameters$dt, target) + } # zeta and zeta group and vector controls survive rebirth } diff --git a/R/parameters.R b/R/parameters.R index 9abbeb60..c87e69bf 100644 --- a/R/parameters.R +++ b/R/parameters.R @@ -404,6 +404,7 @@ get_parameters <- function(overrides = list()) { late_clinical_failure_prob = NULL, late_parasitological_failure_prob = NULL, reinfection_during_prophylaxis = NULL, + dt_slow_parasite_clearance = NULL, # flexible carrying capacity carrying_capacity = FALSE, carrying_capacity_timesteps = NULL, diff --git a/R/processes.R b/R/processes.R index d3b11fda..bbd7a754 100644 --- a/R/processes.R +++ b/R/processes.R @@ -104,12 +104,32 @@ create_processes <- function( parameters$du, variables$infectivity, 0 - ), + ) + ) + + # ======================= + # Antimalarial Resistance + # ======================= + # Add an a new process which governs the transition from Tr to S when + # antimalarial resistance is simulated. The rate of transition switches + # from a parameter to a variable when antimalarial resistance == TRUE. + + # Assign the dt input to a separate object with the default single parameter value: + dt_input <- parameters$dt + + # If antimalarial resistance is switched on, assign dt variable values to the + if(parameters$antimalarial_resistance == TRUE) { + dt_input <- variables$dt + } + + # Create the progression process for Tr --> S specifying dt_input as the rate: + processes <- c( + processes, create_progression_process( variables$state, 'Tr', 'S', - parameters$dt, + dt_input, variables$infectivity, 0 ) diff --git a/R/variables.R b/R/variables.R index f9c75e6e..077a4f29 100644 --- a/R/variables.R +++ b/R/variables.R @@ -33,6 +33,9 @@ #' * drug - The last prescribed drug #' * drug_time - The timestep of the last drug #' +#' Antimalarial resistance variables are: +#' * dt - the delay for humans to move from state Tr to state S +#' #' Mosquito variables are: #' * mosquito_state - the state of the mosquito, a category Sm|Pm|Im|NonExistent #' * species - the species of mosquito, this is a category gamb|fun|arab @@ -226,6 +229,15 @@ create_variables <- function(parameters) { net_time = net_time, spray_time = spray_time ) + + # Add variables for antimalarial resistance state residency times (dt) + if(parameters$antimalarial_resistance == TRUE) { + dt <- individual::DoubleVariable$new(rep(parameters$dt, size)) + variables <- c( + variables, + dt = dt + ) + } # Add variables for individual mosquitoes if (parameters$individual_mosquitoes) { diff --git a/man/run_simulation.Rd b/man/run_simulation.Rd index 07871b13..b9e95e0a 100644 --- a/man/run_simulation.Rd +++ b/man/run_simulation.Rd @@ -90,8 +90,9 @@ subpatent susceptible \item net_usage: the number people protected by a bed net \item mosquito_deaths: number of adult female mosquitoes who die this timestep -\item n_early_treatment_failure: number of clinically treated individuals who experienced early treatment failure in this timestep -\item n_treat_eff_fail: number of clinically treated individuals who's treatment failed due to drug efficacy -\item n_treat_success: number of successfully treated individuals in this timestep +\item n_drug_efficacy_failures: number of clinically treated individuals whose treatment failed due to drug efficacy +\item n_early_treatment_failure: number of clinically treated individuals who experienced early treatment failure +\item n_successfully_treated: number of clinically treated individuals who are treated successfully +\item n_slow_parasite_clearance: number of clinically treated individuals who experienced slow parasite clearance } } diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index f5c226fd..affb233d 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -260,7 +260,7 @@ BEGIN_RCPP END_RCPP } -RcppExport SEXP run_testthat_tests(); +RcppExport SEXP run_testthat_tests(void); static const R_CallMethodDef CallEntries[] = { {"_malariasimulation_create_adult_mosquito_model", (DL_FUNC) &_malariasimulation_create_adult_mosquito_model, 5}, diff --git a/tests/testthat/test-infection-integration.R b/tests/testthat/test-infection-integration.R index 70a0ec6f..b2e2b3bc 100644 --- a/tests/testthat/test-infection-integration.R +++ b/tests/testthat/test-infection-integration.R @@ -315,6 +315,7 @@ test_that('calculate_treated correctly samples treated and updates the drug stat expect_bitset_update(variables$drug_time$queue_update, 5, c(1, 4)) }) +##' FIXING test_that('calculate_treated correctly samples treated and updates the drug state when resistance set', { parameters <- get_parameters() @@ -411,6 +412,7 @@ test_that('calculate_treated correctly samples treated and updates the drug stat }) +##' FIX test_that('calculate_treated correctly samples treated and updates the drug state when resistance not set for all drugs', { # Establish the parameters @@ -857,6 +859,7 @@ test_that('calculate_treated() returns an empty Bitset when the parameter list c in the absence of clinical treatment or resistance parameters") }) +##' FIX test_that('Number of treatment failures matches number of individuals treated when artemisinin resistance proportion and early treatment failure probability both set to 1', { parameters <- get_parameters() @@ -912,6 +915,7 @@ test_that('Number of treatment failures matches number of individuals treated wh and early treatment failure probability both equal 1") }) +##' FIX test_that('calculate_treated() successfully adds additional resistance columns to the renderer', { parameters <- get_parameters() parameters <- set_drugs(parameters = parameters, drugs = list(AL_params)) From 952108c3314fc767d0d6d5a60c925fced364897c Mon Sep 17 00:00:00 2001 From: Thomas Brewer Date: Wed, 3 Apr 2024 15:40:24 +0100 Subject: [PATCH 2/6] Fixed errors in unit tests which included amending the create_mortality_process function to include parameters as an input --- R/human_infection.R | 1 - R/mortality_processes.R | 2 +- tests/testthat/test-antimalarial-resistance.R | 100 +++++----- tests/testthat/test-infection-integration.R | 180 +++++++++++------- 4 files changed, 161 insertions(+), 122 deletions(-) diff --git a/R/human_infection.R b/R/human_infection.R index 4890e8a0..569757ef 100644 --- a/R/human_infection.R +++ b/R/human_infection.R @@ -306,7 +306,6 @@ calculate_treated <- function( #+++ ANTIMALARIAL RESISTANCE +++# #+++++++++++++++++++++++++++++++# if(parameters$antimalarial_resistance) { - resistance_parameters <- get_antimalarial_resistance_parameters( parameters = parameters, drugs = drugs, diff --git a/R/mortality_processes.R b/R/mortality_processes.R index e9ea6ac9..37792f6c 100644 --- a/R/mortality_processes.R +++ b/R/mortality_processes.R @@ -27,7 +27,7 @@ create_mortality_process <- function(variables, events, renderer, parameters) { died <- individual::Bitset$new(pop)$insert(bernoulli_multi_p(deathrates)) renderer$render('natural_deaths', died$size(), timestep) } - reset_target(variables, events, died, 'S', timestep) + reset_target(variables, events, died, 'S', parameters, timestep) sample_maternal_immunity(variables, died, timestep, parameters) } } diff --git a/tests/testthat/test-antimalarial-resistance.R b/tests/testthat/test-antimalarial-resistance.R index 400d93a3..193e09cf 100644 --- a/tests/testthat/test-antimalarial-resistance.R +++ b/tests/testthat/test-antimalarial-resistance.R @@ -12,9 +12,9 @@ test_that('set_antimalarial_resistance() toggles resistance on', { partner_drug_resistance = 0, slow_parasite_clearance_prob = 0.5, early_treatment_failure_prob = 0.6, - late_clinical_failure_prob = 0.2, - late_parasitological_prob = 0.3, - reinfection_prob = 0.4, + late_clinical_failure_prob = 0, + late_parasitological_prob = 0, + reinfection_prob = 0, slow_parasite_clearance_time = 10) -> simparams expect_identical(object = simparams$antimalarial_resistance, expected = TRUE) }) @@ -53,9 +53,9 @@ test_that('set_antimalarial_resistance() errors if resistance proportions outsid partner_drug_resistance = 0, slow_parasite_clearance_prob = 0.5, early_treatment_failure_prob = 0.6, - late_clinical_failure_prob = 0.2, - late_parasitological_prob = 0.3, - reinfection_prob = 0.4, + late_clinical_failure_prob = 0, + late_parasitological_prob = 0, + reinfection_prob = 0, slow_parasite_clearance_time = 10), regexp = "Artemisinin and partner-drug resistance proportions must fall between 0 and 1") }) @@ -121,35 +121,35 @@ test_that('set_antimalarial_resistance() assigns parameters correctly despite or drug = 3, timesteps = 1, artemisinin_resistance = 0, - partner_drug_resistance = 0.43, + partner_drug_resistance = 0, slow_parasite_clearance_prob = 0, early_treatment_failure_prob = 0, - late_clinical_failure_prob = 0.01, - late_parasitological_prob = 0.42, - reinfection_prob = 0.89, + late_clinical_failure_prob = 0, + late_parasitological_prob = 0, + reinfection_prob = 0, slow_parasite_clearance_time = 10) parameters <- set_antimalarial_resistance(parameters = parameters, drug = 1, timesteps = 1, artemisinin_resistance = 0.27, - partner_drug_resistance = 0.61, + partner_drug_resistance = 0, slow_parasite_clearance_prob = 0.23, early_treatment_failure_prob = 0.9, - late_clinical_failure_prob = 0.49, - late_parasitological_prob = 0.81, - reinfection_prob = 0.009, + late_clinical_failure_prob = 0, + late_parasitological_prob = 0, + reinfection_prob = 0, slow_parasite_clearance_time = 20) expect_identical(parameters$antimalarial_resistance, TRUE) expect_identical(unlist(parameters$antimalarial_resistance_drug), c(2, 3, 1)) expect_identical(unlist(parameters$antimalarial_resistance_timesteps), rep(1, 3)) expect_identical(unlist(parameters$prop_artemisinin_resistant), c(0.5, 0, 0.27)) - expect_identical(unlist(parameters$prop_partner_drug_resistant), c(0, 0.43, 0.61)) + expect_identical(unlist(parameters$prop_partner_drug_resistant), c(0, 0, 0)) expect_identical(unlist(parameters$slow_parasite_clearance_prob), c(0.41, 0, 0.23)) expect_identical(unlist(parameters$early_treatment_failure_prob), c(0.2, 0, 0.9)) - expect_identical(unlist(parameters$late_clinical_failure_prob), c(0, 0.01, 0.49)) - expect_identical(unlist(parameters$late_parasitological_failure_prob), c(0, 0.42, 0.81)) - expect_identical(unlist(parameters$reinfection_during_prophylaxis), c(0, 0.89, 0.009)) + expect_identical(unlist(parameters$late_clinical_failure_prob), c(0, 0, 0)) + expect_identical(unlist(parameters$late_parasitological_failure_prob), c(0, 0, 0)) + expect_identical(unlist(parameters$reinfection_during_prophylaxis), c(0, 0, 0)) expect_identical(unlist(parameters$dt_slow_parasite_clearance), c(5, 10, 20)) }) @@ -168,12 +168,12 @@ test_that(desc = "set_antimalarial_resistance errors if length slow_parasite_cle drug = 1, timesteps = c(0, 10), artemisinin_resistance = c(0.4, 0.8), - partner_drug_resistance = c(0.23, 0.43), + partner_drug_resistance = c(0, 0), slow_parasite_clearance_prob = c(0.2, 0.4), early_treatment_failure_prob = c(0, 0.45), - late_clinical_failure_prob = c(0.01, 0.01), - late_parasitological_prob = c(0.05, 0.06), - reinfection_prob = c(0.86, 0.86), + late_clinical_failure_prob = c(0, 0), + late_parasitological_prob = c(0, 0), + reinfection_prob = c(0, 0), slow_parasite_clearance_time = c(10 ,11)), "Error: length of slow_parasite_clearance_time not equal to 1") }) @@ -192,12 +192,12 @@ test_that(desc = "set_antimalarial_resistance errors if slow_parasite_clearance_ drug = 1, timesteps = c(0, 10), artemisinin_resistance = c(0.4, 0.8), - partner_drug_resistance = c(0.23, 0.43), + partner_drug_resistance = c(0, 0), slow_parasite_clearance_prob = c(0.2, 0.4), early_treatment_failure_prob = c(0, 0.45), - late_clinical_failure_prob = c(0.01, 0.01), - late_parasitological_prob = c(0.05, 0.06), - reinfection_prob = c(0.86, 0.86), + late_clinical_failure_prob = c(0, 0), + late_parasitological_prob = c(0, 0), + reinfection_prob = c(0, 0), slow_parasite_clearance_time = c(0)), "Error: slow_parasite_clearance_time is non-positive") }) @@ -213,32 +213,32 @@ test_that('get_antimalarial_resistance_parameters() correctly retrieves paramete set_antimalarial_resistance(drug = 2, timesteps = c(0, 20), artemisinin_resistance = c(0.02, 0.2), - partner_drug_resistance = c(0.02, 0.2), + partner_drug_resistance = c(0, 0), slow_parasite_clearance_prob = c(0.02, 0.2), early_treatment_failure_prob = c(0.02, 0.2), - late_clinical_failure_prob = c(0.02, 0.2), - late_parasitological_prob = c(0.02, 0.2), - reinfection_prob = c(0.02, 0.2), + late_clinical_failure_prob = c(0, 0), + late_parasitological_prob = c(0, 0), + reinfection_prob = c(0, 0), slow_parasite_clearance_time = 20) %>% set_antimalarial_resistance(drug = 1, timesteps = c(0, 10), artemisinin_resistance = c(0.01, 0.1), - partner_drug_resistance = c(0.01, 0.1), + partner_drug_resistance = c(0, 0), slow_parasite_clearance_prob = c(0.01, 0.1), early_treatment_failure_prob = c(0.01, 0.1), - late_clinical_failure_prob = c(0.01, 0.1), - late_parasitological_prob = c(0.01, 0.1), - reinfection_prob = c(0.01, 0.1), + late_clinical_failure_prob = c(0, 0), + late_parasitological_prob = c(0, 0), + reinfection_prob = c(0, 0), slow_parasite_clearance_time = 10) %>% set_antimalarial_resistance(drug = 3, timesteps = c(0, 30), artemisinin_resistance = c(0.03, 0.3), - partner_drug_resistance = c(0.03, 0.3), + partner_drug_resistance = c(0, 0), slow_parasite_clearance_prob = c(0.03, 0.3), early_treatment_failure_prob = c(0.03, 0.3), - late_clinical_failure_prob = c(0.03, 0.3), - late_parasitological_prob = c(0.03, 0.3), - reinfection_prob = c(0.03, 0.3), + late_clinical_failure_prob = c(0, 0), + late_parasitological_prob = c(0, 0), + reinfection_prob = c(0, 0), slow_parasite_clearance_time = 30) -> parameters drugs <- c(1, 3, 2, 1, 2, 3, 3, 3, 2, 1, 3, 1, 2, 3, 2) @@ -250,12 +250,12 @@ test_that('get_antimalarial_resistance_parameters() correctly retrieves paramete expected_resistance_parameters <- list() expected_resistance_parameters$artemisinin_resistance_proportion <- c(0.1, 0.03, 0.2, 0.1, 0.2, 0.03, 0.03, 0.03, 0.2, 0.1, 0.03, 0.1, 0.2, 0.03, 0.2) - expected_resistance_parameters$partner_drug_resistance_proportion <- c(0.1, 0.03, 0.2, 0.1, 0.2, 0.03, 0.03, 0.03, 0.2, 0.1, 0.03, 0.1, 0.2, 0.03, 0.2) + expected_resistance_parameters$partner_drug_resistance_proportion <- rep(0, 15) expected_resistance_parameters$slow_parasite_clearance_probability <- c(0.1, 0.03, 0.2, 0.1, 0.2, 0.03, 0.03, 0.03, 0.2, 0.1, 0.03, 0.1, 0.2, 0.03, 0.2) expected_resistance_parameters$early_treatment_failure_probability <- c(0.1, 0.03, 0.2, 0.1, 0.2, 0.03, 0.03, 0.03, 0.2, 0.1, 0.03, 0.1, 0.2, 0.03, 0.2) - expected_resistance_parameters$late_clinical_failure_probability <- c(0.1, 0.03, 0.2, 0.1, 0.2, 0.03, 0.03, 0.03, 0.2, 0.1, 0.03, 0.1, 0.2, 0.03, 0.2) - expected_resistance_parameters$late_parasitological_failure_probability <- c(0.1, 0.03, 0.2, 0.1, 0.2, 0.03, 0.03, 0.03, 0.2, 0.1, 0.03, 0.1, 0.2, 0.03, 0.2) - expected_resistance_parameters$reinfection_during_prophylaxis_probability <- c(0.1, 0.03, 0.2, 0.1, 0.2, 0.03, 0.03, 0.03, 0.2, 0.1, 0.03, 0.1, 0.2, 0.03, 0.2) + expected_resistance_parameters$late_clinical_failure_probability <- rep(0, 15) + expected_resistance_parameters$late_parasitological_failure_probability <- rep(0, 15) + expected_resistance_parameters$reinfection_during_prophylaxis_probability <- rep(0, 15) expected_resistance_parameters$dt_slow_parasite_clearance <- c(10, 30, 20, 10, 20, 30, 30, 30, 20, 10, 30, 10, 20, 30, 20) expect_identical(resistance_parameters, expected = expected_resistance_parameters) @@ -273,12 +273,12 @@ test_that('get_antimalarial_resistance_parameters() correctly retrieves paramete set_antimalarial_resistance(drug = 2, timesteps = c(0, 20), artemisinin_resistance = c(0.02, 0.2), - partner_drug_resistance = c(0.02, 0.2), + partner_drug_resistance = c(0, 0), slow_parasite_clearance_prob = c(0.02, 0.2), early_treatment_failure_prob = c(0.02, 0.2), - late_clinical_failure_prob = c(0.02, 0.2), - late_parasitological_prob = c(0.02, 0.2), - reinfection_prob = c(0.02, 0.2), + late_clinical_failure_prob = c(0, 0), + late_parasitological_prob = c(0, 0), + reinfection_prob = c(0, 0), slow_parasite_clearance_time = 20) -> parameters drugs <- c(1, 3, 2, 1, 2, 3, 3, 3, 2, 1, 3, 1, 2, 3, 2) @@ -290,12 +290,12 @@ test_that('get_antimalarial_resistance_parameters() correctly retrieves paramete expected_resistance_parameters <- list() expected_resistance_parameters$artemisinin_resistance_proportion <- c(0, 0, 0.2, 0, 0.2, 0, 0, 0, 0.2, 0, 0, 0, 0.2, 0, 0.2) - expected_resistance_parameters$partner_drug_resistance_proportion <- c(0, 0, 0.2, 0, 0.2, 0, 0, 0, 0.2, 0, 0, 0, 0.2, 0, 0.2) + expected_resistance_parameters$partner_drug_resistance_proportion <- rep(0, 15) expected_resistance_parameters$slow_parasite_clearance_probability <- c(0, 0, 0.2, 0, 0.2, 0, 0, 0, 0.2, 0, 0, 0, 0.2, 0, 0.2) expected_resistance_parameters$early_treatment_failure_probability <- c(0, 0, 0.2, 0, 0.2, 0, 0, 0, 0.2, 0, 0, 0, 0.2, 0, 0.2) - expected_resistance_parameters$late_clinical_failure_probability <- c(0, 0, 0.2, 0, 0.2, 0, 0, 0, 0.2, 0, 0, 0, 0.2, 0, 0.2) - expected_resistance_parameters$late_parasitological_failure_probability <- c(0, 0, 0.2, 0, 0.2, 0, 0, 0, 0.2, 0, 0, 0, 0.2, 0, 0.2) - expected_resistance_parameters$reinfection_during_prophylaxis_probability <- c(0, 0, 0.2, 0, 0.2, 0, 0, 0, 0.2, 0, 0, 0, 0.2, 0, 0.2) + expected_resistance_parameters$late_clinical_failure_probability <- rep(0, 15) + expected_resistance_parameters$late_parasitological_failure_probability <- rep(0, 15) + expected_resistance_parameters$reinfection_during_prophylaxis_probability <- rep(0, 15) expected_resistance_parameters$dt_slow_parasite_clearance <- c(5, 5, 20, 5, 20, 5, 5, 5, 20, 5, 5, 5, 20, 5, 20) expect_identical(resistance_parameters, expected = expected_resistance_parameters) diff --git a/tests/testthat/test-infection-integration.R b/tests/testthat/test-infection-integration.R index b2e2b3bc..409cc466 100644 --- a/tests/testthat/test-infection-integration.R +++ b/tests/testthat/test-infection-integration.R @@ -315,7 +315,6 @@ test_that('calculate_treated correctly samples treated and updates the drug stat expect_bitset_update(variables$drug_time$queue_update, 5, c(1, 4)) }) -##' FIXING test_that('calculate_treated correctly samples treated and updates the drug state when resistance set', { parameters <- get_parameters() @@ -336,7 +335,7 @@ test_that('calculate_treated correctly samples treated and updates the drug stat parameters <- set_antimalarial_resistance(parameters = parameters, drug = 2, timesteps = 1, - artemisinin_resistance = 0, + artemisinin_resistance = 0.3, partner_drug_resistance = 0, slow_parasite_clearance_prob = 0, early_treatment_failure_prob = 0.9, @@ -352,17 +351,24 @@ test_that('calculate_treated correctly samples treated and updates the drug stat state = list(queue_update = mockery::mock()), infectivity = list(queue_update = mockery::mock()), drug = list(queue_update = mockery::mock()), - drug_time = list(queue_update = mockery::mock()) + drug_time = list(queue_update = mockery::mock()), + dt = list(queue_update = mockery::mock()) ) + renderer <- individual::Render$new(timesteps = timestep) + # Set up seek_treatment mock and instruct calculate_treated() to return it when sample_bitset() called: seek_treatment <- individual::Bitset$new(20)$insert(c(1:10)) seek_treatment_mock <- mockery::mock(seek_treatment) mockery::stub(where = calculate_treated, what = 'sample_bitset', how = seek_treatment_mock) + # Set up drugs mock and instruct it to return it when sample.int() called: mock_drugs <- mockery::mock(c(2, 1, 1, 1, 2, 2, 2, 1, 2, 1)) mockery::stub(calculate_treated, 'sample.int', mock_drugs) - bernoulli_mock <- mockery::mock(c(1, 2, 3, 4, 5, 6, 7, 8, 9), c(1, 2, 3, 4, 5, 6, 7)) + # Set up bernoulli mock and instruct calculate_treated to return it when bernoulli_multi_p() called: + bernoulli_mock <- mockery::mock(c(1, 2, 3, 4, 5, 6, 7, 8, 9), + c(1, 2, 3, 4, 5, 6, 7), + c(1)) mockery::stub(calculate_treated, 'bernoulli_multi_p', bernoulli_mock) calculate_treated( @@ -370,7 +376,7 @@ test_that('calculate_treated correctly samples treated and updates the drug stat clinical_infections, parameters, timestep, - mock_render(timestep) + renderer ) mockery::expect_args( @@ -392,13 +398,25 @@ test_that('calculate_treated correctly samples treated and updates the drug stat mockery::expect_args( bernoulli_mock, 1, - c(1.0, 0.9, 0.9, 0.9, 1.0, 1.0, 1.0, 0.9, 1, 0.9) + c(0.9, 0.95, 0.95, 0.95, 0.9, 0.9, 0.9, 0.95, 0.9, 0.95) ) mockery::expect_args( bernoulli_mock, 2, - parameters$drug_efficacy[c(2, 1, 1, 1, 2, 2, 2, 1, 2)] + c(0.73, 0.9, 0.9, 0.9, 0.73, 0.73, 0.73, 0.9, 0.73) + ) + + mockery::expect_args( + bernoulli_mock, + 2, + 1 - (unlist(parameters$prop_artemisinin_resistant[c(2, 1, 1, 1, 2, 2, 2, 1, 2)]) * unlist(parameters$early_treatment_failure_prob[c(2, 1, 1, 1, 2, 2, 2, 1, 2)])) + ) + + mockery::expect_args( + bernoulli_mock, + 3, + unlist(parameters$prop_artemisinin_resistant[c(2, 1, 1, 1, 2, 2, 2)]) * unlist(parameters$slow_parasite_clearance_prob[c(2, 1, 1, 1, 2, 2, 2)]) ) expect_bitset_update(variables$state$queue_update, 'Tr', c(1, 2, 3, 4, 5, 6, 7)) @@ -409,10 +427,10 @@ test_that('calculate_treated correctly samples treated and updates the drug stat ) expect_bitset_update(variables$drug$queue_update, c(2, 1, 1, 1, 2, 2, 2), c(1, 2, 3, 4, 5, 6, 7)) expect_bitset_update(variables$drug_time$queue_update, 5, c(1, 2, 3, 4, 5, 6, 7)) - + expect_bitset_update(variables$dt$queue_update, 5, c(2, 3, 4, 5, 6, 7), 1) + expect_bitset_update(variables$dt$queue_update, 15, c(1), 2) }) -##' FIX test_that('calculate_treated correctly samples treated and updates the drug state when resistance not set for all drugs', { # Establish the parameters @@ -425,8 +443,8 @@ test_that('calculate_treated correctly samples treated and updates the drug stat timesteps = 1, artemisinin_resistance = 0.8, partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0, - early_treatment_failure_prob = 0.9, + slow_parasite_clearance_prob = 0.2, + early_treatment_failure_prob = 0.3, late_clinical_failure_prob = 0, late_parasitological_prob = 0, reinfection_prob = 0, @@ -446,7 +464,8 @@ test_that('calculate_treated correctly samples treated and updates the drug stat state = list(queue_update = mockery::mock()), infectivity = list(queue_update = mockery::mock()), drug = list(queue_update = mockery::mock()), - drug_time = list(queue_update = mockery::mock()) + drug_time = list(queue_update = mockery::mock()), + dt = list(queue_update = mockery::mock()) ) # Create a Bitset of individuals seeking treatment individuals: @@ -465,7 +484,9 @@ test_that('calculate_treated correctly samples treated and updates the drug stat mockery::stub(calculate_treated, 'sample.int', mock_drugs) # Create a bernoulli_mock of i) individuals susceptible, and ii) individuals successfully treated: - bernoulli_mock <- mockery::mock(c(1, 2, 3, 4, 5, 6, 7, 8, 9), c(1, 2, 3, 4, 5, 6, 7)) + bernoulli_mock <- mockery::mock(c(1, 2, 3, 4, 5, 6, 7, 8, 9), + c(1, 2, 3, 4, 5, 6, 7), + c(1)) # Specify that when calculate_treated() calls bernoulli_multi_p() it returns the bernoulli_mock: mockery::stub(calculate_treated, 'bernoulli_multi_p', bernoulli_mock) @@ -504,7 +525,7 @@ test_that('calculate_treated correctly samples treated and updates the drug stat mockery::expect_args( bernoulli_mock, 1, - c(0.28, 1, 1, 1, 0.28, 0.28, 0.28, 1, 0.28, 1) # (1 - (art_prop * etf_prob)) + parameters$drug_efficacy[c(2, 1, 1, 1, 2, 2, 2, 1, 2, 1)] ) # Check that the secnd time bernoulli_mock was called (bernoulli_multi_p()) the arguments used in @@ -512,7 +533,7 @@ test_that('calculate_treated correctly samples treated and updates the drug stat mockery::expect_args( bernoulli_mock, 2, - parameters$drug_efficacy[c(2, 1, 1, 1, 2, 2, 2, 1, 2)] + c(0.76, 1, 1, 1, 0.76, 0.76, 0.76, 1, 0.76) ) # Check that update queued that updates the state of successfully treated individuals to "Tr" @@ -545,6 +566,21 @@ test_that('calculate_treated correctly samples treated and updates the drug stat 5, c(1, 2, 3, 4, 5, 6, 7) ) + + # Check that update queued for dt for the non-slow parasite clearance individuals is correct: + expect_bitset_update( + variables$dt$queue_update, + parameters$dt, + c(2, 3, 4, 5, 6, 7), + 1) + + # Check that update queued for dt for the slow parasite clearance individuals is correct: + expect_bitset_update( + variables$dt$queue_update, + unlist(parameters$dt_slow_parasite_clearance), + c(1), + 2) + }) test_that('schedule_infections correctly schedules new infections', { @@ -859,63 +895,61 @@ test_that('calculate_treated() returns an empty Bitset when the parameter list c in the absence of clinical treatment or resistance parameters") }) -##' FIX test_that('Number of treatment failures matches number of individuals treated when artemisinin resistance proportion and early treatment failure probability both set to 1', { - parameters <- get_parameters() - parameters <- set_drugs(parameters = parameters, drugs = list(AL_params, SP_AQ_params)) - parameters <- set_clinical_treatment(parameters = parameters, - drug = 1, - timesteps = 1, - coverages = round(runif(1, 0, 1/2), - digits = 2)) - parameters <- set_clinical_treatment(parameters = parameters, - drug = 2, - timesteps = 1, - coverages = round(runif(1, 0, 1/2), - digits = 2)) - parameters <- set_antimalarial_resistance(parameters = parameters, - drug = 1, - timesteps = 1, - artemisinin_resistance = 1, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0, - early_treatment_failure_prob = 1, - late_clinical_failure_prob = 0, - late_parasitological_prob = 0, - reinfection_prob = 0, - slow_parasite_clearance_time = 10) - parameters <- set_antimalarial_resistance(parameters = parameters, - drug = 2, - timesteps = 1, - artemisinin_resistance = 1, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0, - early_treatment_failure_prob = 1, - late_clinical_failure_prob = 0, - late_parasitological_prob = 0, - reinfection_prob = 0, - slow_parasite_clearance_time = 20) - - clinical_infections <- individual::Bitset$new(100) - clinical_infections$insert(sample.int(n = 100, size = round(runif(n = 1, min = 10, max = 100)), replace = FALSE)) - timestep <- 5 - events <- create_events(parameters) - variables <- create_variables(parameters = parameters) - renderer <- individual::Render$new(timesteps = 10) - - treated <- calculate_treated(variables = variables, - clinical_infections = clinical_infections, - parameters = parameters, - timestep = timestep, - renderer = renderer) - - expect_identical(renderer$to_dataframe()[timestep,'n_early_treatment_failure'], renderer$to_dataframe()[timestep,'n_treated'], info = "Error: Number of - early treatment failures does not match number of treated individuals when artemisinin resistance proportion and + parameters <- get_parameters() + parameters <- set_drugs(parameters = parameters, drugs = list(AL_params, SP_AQ_params)) + parameters <- set_clinical_treatment(parameters = parameters, + drug = 1, + timesteps = 1, + coverages = round(runif(1, 0, 1/2), + digits = 2)) + parameters <- set_clinical_treatment(parameters = parameters, + drug = 2, + timesteps = 1, + coverages = round(runif(1, 0, 1/2), + digits = 2)) + parameters <- set_antimalarial_resistance(parameters = parameters, + drug = 1, + timesteps = 1, + artemisinin_resistance = 1, + partner_drug_resistance = 0, + slow_parasite_clearance_prob = 0, + early_treatment_failure_prob = 1, + late_clinical_failure_prob = 0, + late_parasitological_prob = 0, + reinfection_prob = 0, + slow_parasite_clearance_time = 10) + parameters <- set_antimalarial_resistance(parameters = parameters, + drug = 2, + timesteps = 1, + artemisinin_resistance = 1, + partner_drug_resistance = 0, + slow_parasite_clearance_prob = 0, + early_treatment_failure_prob = 1, + late_clinical_failure_prob = 0, + late_parasitological_prob = 0, + reinfection_prob = 0, + slow_parasite_clearance_time = 20) + + clinical_infections <- individual::Bitset$new(100) + clinical_infections$insert(sample.int(n = 100, size = round(runif(n = 1, min = 10, max = 100)), replace = FALSE)) + timestep <- 5 + events <- create_events(parameters) + variables <- create_variables(parameters = parameters) + renderer <- individual::Render$new(timesteps = 10) + + treated <- calculate_treated(variables = variables, + clinical_infections = clinical_infections, + parameters = parameters, + timestep = timestep, + renderer = renderer) + + expect_identical(renderer$to_dataframe()[timestep,'n_early_treatment_failure'], renderer$to_dataframe()[timestep,'n_treated'] - renderer$to_dataframe()[timestep,'n_drug_efficacy_failures'], info = "Error: Number of + early treatment failures does not match number of treated individuals (minus drug efficacy failures) when artemisinin resistance proportion and and early treatment failure probability both equal 1") -}) + }) -##' FIX test_that('calculate_treated() successfully adds additional resistance columns to the renderer', { parameters <- get_parameters() parameters <- set_drugs(parameters = parameters, drugs = list(AL_params)) @@ -939,7 +973,8 @@ test_that('calculate_treated() successfully adds additional resistance columns t state = list(queue_update = mockery::mock()), infectivity = list(queue_update = mockery::mock()), drug = list(queue_update = mockery::mock()), - drug_time = list(queue_update = mockery::mock()) + drug_time = list(queue_update = mockery::mock()), + dt = list(queue_update = mockery::mock()) ) renderer <- individual::Render$new(timesteps = 10) @@ -949,7 +984,12 @@ test_that('calculate_treated() successfully adds additional resistance columns t timestep = timestep, renderer = renderer) - calculate_treated_column_names <- c("ft", "n_treated", "n_early_treatment_failure", "n_treat_eff_fail", "n_treat_success") + calculate_treated_column_names <- c("ft", + "n_treated", + "n_drug_efficacy_failures", + "n_early_treatment_failure", + "n_slow_parasite_clearance", + "n_successfully_treated") expect_identical(sum(calculate_treated_column_names %in% colnames(renderer$to_dataframe())), length(calculate_treated_column_names), "calculate_treated() not renderering all resistance columns when resistance is present, clinical treatment coverage is non-zero, and the Bitset of clinically_infected individuals input is of non-zero length.") From 0cef2733583300c8ad3cb47f2937b09571c69f36 Mon Sep 17 00:00:00 2001 From: tbreweric Date: Thu, 4 Apr 2024 17:59:57 +0100 Subject: [PATCH 3/6] Added section modelling early treatment failure and slow parasite clearance to the vignette --- vignettes/Antimalarial_Resistance.Rmd | 153 +++++++++++++++++++++++++- 1 file changed, 152 insertions(+), 1 deletion(-) diff --git a/vignettes/Antimalarial_Resistance.Rmd b/vignettes/Antimalarial_Resistance.Rmd index 7bf197b7..9c873202 100644 --- a/vignettes/Antimalarial_Resistance.Rmd +++ b/vignettes/Antimalarial_Resistance.Rmd @@ -14,7 +14,6 @@ knitr::opts_chunk$set( ) ``` - ```{r setup} # Load the requisite packages: @@ -259,5 +258,157 @@ text(x = resistance_update_timesteps[2:5] + 30, y = 0.6, labels = paste0("Art. R Looking at the figure, we can see that the *Pf*PR~2-10~ decreases over the two years following the onset of clinical treatment in the absence of artemisinin resistance. However, as resistance is introduced and increases through time, the *Pf*PR~2-10~ increases towards the pre-intervention seasonal peak as SP-AQ becomes increasingly ineffective in the treatment of clinical cases of malaria. +## Simulating antimalarial resistance with multiple resistance outcomes + +As we've discussed, resistance to an ACT can manifest in multiple ways. For instance, resistance to the artemisinin component of an ACT can result in either early treatment failure or slow parasite clearance. + +Using `malariasimulation`, we can simulate the effects of multiple potential outcomes of resistance on malaria transmission dynamics. To illustrate, we'll parameterise and run a series of simulations in is i) no clinical treatment, ii) no resistance, iii) resistance with early treatment failure, iv) resistance with slow parasite clearance, and v) resistance with early treatment failure and slow parasite clearance. + +### Parameterisation +```{r} + +# Determine the number of timesteps to run for: +timesteps <- 365 * 6 + +# Set up a list to store the simulation parameter lists in: +simulation_parameters <- list() + +# Establish a list of the base parameters with no clinical treatment or antimalarial resistance: +get_parameters(overrides = list(human_population = 1000)) -> simulation_parameters$base + +# Establish a parameter list with clinical treatment starting after one year: +simulation_parameters$base |> + set_drugs(drugs = list(AL_params)) |> + set_clinical_treatment(drug = 1, timesteps = (365 * 1) + 1, coverages = c(0.8)) |> + set_equilibrium(init_EIR = 16) -> simulation_parameters$treatment + +# Set the equilibrium for the base parameters: +simulation_parameters$base |> + set_equilibrium(init_EIR = 16) -> simulation_parameters$base + +# Establish a parameter list with clinical treatment and early treatment failure +simulation_parameters$treatment |> + set_antimalarial_resistance(drug = 1, + timesteps = c(0, (365 * 3) + 1), + artemisinin_resistance = c(0, 0.8), + partner_drug_resistance = c(0, 0), + slow_parasite_clearance_prob = c(0, 0), + early_treatment_failure_prob = c(0, 0.8), + late_clinical_failure_prob = c(0, 0), + late_parasitological_prob = c(0, 0), + reinfection_prob = c(0, 0), + slow_parasite_clearance_time = 10) -> simulation_parameters$etf + +# Establish a parameter list with clinical treatment and slow parasite clearance +simulation_parameters$treatment |> + set_antimalarial_resistance(drug = 1, + timesteps = c(0, (365 * 3) + 1), + artemisinin_resistance = c(0, 0.8), + partner_drug_resistance = c(0, 0), + slow_parasite_clearance_prob = c(0, 0.8), + early_treatment_failure_prob = c(0, 0), + late_clinical_failure_prob = c(0, 0), + late_parasitological_prob = c(0, 0), + reinfection_prob = c(0, 0), + slow_parasite_clearance_time = 10) -> simulation_parameters$spc + +# Establish a parameter list with clinical treatment, early treatment failure and slow parasite clearance: +simulation_parameters$treatment |> + set_antimalarial_resistance(drug = 1, + timesteps = c(0, (365 * 3) + 1), + artemisinin_resistance = c(0, 0.8), + partner_drug_resistance = c(0, 0), + slow_parasite_clearance_prob = c(0, 0.8), + early_treatment_failure_prob = c(0, 0.8), + late_clinical_failure_prob = c(0, 0), + late_parasitological_prob = c(0, 0), + reinfection_prob = c(0, 0), + slow_parasite_clearance_time = 10) -> simulation_parameters$etf_spc + +``` + +### Simulation +We can now use our lists of `malariasimulation` parameters to run the simulations. + +```{r} + +# Open a list to store the simulation outputs in: +simulation_outputs <- list() + +# Run the simulations +for(i in seq(length(simulation_parameters))) { + + # Run the i-th simulation + simulation_temp <- run_simulation(timesteps = timesteps, + parameters = simulation_parameters[[i]]) + + # Append the simulation identifier: + simulation_temp$identifier <- names(simulation_parameters)[i] + + # Append the ith simulation outputs to the combined simulation dataframe: + simulation_outputs[[names(simulation_parameters)[i]]] <- simulation_temp + + # Print the number of columns in the i-th simulation outputs dataframe: + print(ncol(simulation_temp)) +} + +``` + +### Data Wranging + + + +### Visualisation + +We can compare the effects of independent resistance outcomes with combined resistance outcomes visually. + +```{R} + +# Open a new plotting window and add a grid: +plot.new(); par(mar = c(4, 4, 1, 1), new = TRUE) + +# Plot malaria prevalence in 2-10 years through time: +plot(x = simulation_outputs$base$timestep, + y = simulation_outputs$base$n_detect_730_3650/simulation_outputs$base$n_730_3650, + xlab = "Time (days)", + ylab = expression(paste(italic(Pf),"PR"[2-10])), cex = 0.8, + ylim = c(0, 1), type = "l", lwd = 2, xaxs = "i", yaxs = "i", + col = cols[3]) + +# Add the dynamics for no-intervention simulation +lines(x = simulation_outputs$treatment$timestep, + y = simulation_outputs$treatment$n_detect_730_3650/simulation_outputs$treatment$n_730_3650, + col = cols[4]) + +lines(x = simulation_outputs$etf$timestep, + y = simulation_outputs$etf$n_detect_730_3650/simulation_outputs$etf$n_730_3650, + col = cols[5]) + +lines(x = simulation_outputs$spc$timestep, + y = simulation_outputs$spc$n_detect_730_3650/simulation_outputs$spc$n_730_3650, + col = cols[6]) + +lines(x = simulation_outputs$etf_spc$timestep, + y = simulation_outputs$etf_spc$n_detect_730_3650/simulation_outputs$etf_spc$n_730_3650, + col = cols[7]) + +# Add vlines to indicate when SP-AQ were administered: +abline(v = 365 + 1, lty = "dashed", lwd = 1) +text(x = (365 * 1) - 40, y = 0.6, labels = "Treatment Introduced", adj = 0, cex = 0.8, srt = 90) + +abline(v = (365 * 3) + 1, lty = "dashed", lwd = 1) +text(x = (365 * 3) - 40, y = 0.6, labels = "Resistance Introduced", adj = 0, cex = 0.8, srt = 90) + +# Add gridlines: +grid(lty = 2, col = "grey80", nx = NULL, ny = NULL, lwd = 0.5); box() + +# Add a legend: +legend(x = 3000, y = 0.99, legend = c("Baseline", "Treatment", "ETF-only", "SPC-only", "ETF and SPC"), + col= c(cols[3:7]), box.col = "white", + lwd = 1, lty = c(1, 1), cex = 0.8) + + +``` + ## References Slater, H.C., Griffin, J.T., Ghani, A.C. and Okell, L.C., 2016. Assessing the potential impact of artemisinin and partner drug resistance in sub-Saharan Africa. Malaria journal, 15(1), pp.1-11. \ No newline at end of file From 5123d6e28c49c4e6de33de337ea06c383f35068f Mon Sep 17 00:00:00 2001 From: tbreweric Date: Thu, 4 Apr 2024 22:15:22 +0100 Subject: [PATCH 4/6] Made clearer the antimalarial resistance parameter names in set_antimalarial_resistance and get_antimalarial_resistance_parameters, then made additional changes to documentation where necessary to reflect changes. Fixed the effects of the changes on unit tests --- R/antimalarial_resistance.R | 94 ++-- R/parameters.R | 28 +- man/get_parameters.Rd | 14 +- man/run_simulation.Rd | 2 +- man/set_antimalarial_resistance.Rd | 28 +- tests/testthat/test-antimalarial-resistance.R | 459 +++++++++--------- tests/testthat/test-infection-integration.R | 230 ++++----- vignettes/Antimalarial_Resistance.Rmd | 74 +-- 8 files changed, 465 insertions(+), 464 deletions(-) diff --git a/R/antimalarial_resistance.R b/R/antimalarial_resistance.R index 7da3ad83..ce5d94f6 100644 --- a/R/antimalarial_resistance.R +++ b/R/antimalarial_resistance.R @@ -5,54 +5,54 @@ #' @param parameters the model parameters #' @param drug the index of the drug which resistance is being set, as set by the set_drugs() function, in the parameter list #' @param timesteps vector of time steps for each update to resistance proportion and resistance outcome probability -#' @param artemisinin_resistance vector of updates to the proportions of infections that are artemisinin resistant at time t -#' @param partner_drug_resistance vector of updates to the proportions of infections that are partner-drug resistant at time t -#' @param slow_parasite_clearance_prob vector of updates to the proportion of artemisinin-resistant infections that result in early treatment failure -#' @param early_treatment_failure_prob vector of updates to the proportion of artemisinin-resistant infections that result in slow parasite clearance -#' @param late_clinical_failure_prob vector of updates to the proportion of partner-drug-resistant infections that result in late clinical failure -#' @param late_parasitological_prob vector of updates to the proportion of partner-drug-resistant infections that result in late parasitological failure -#' @param reinfection_prob vector of updates to the proportion of partner-drug-resistant infections that result in reinfection during prophylaxis +#' @param artemisinin_resistance_proportion vector of updates to the proportions of infections that are artemisinin resistant at time t +#' @param partner_drug_resistance_proportion vector of updates to the proportions of infections that are partner-drug resistant at time t +#' @param slow_parasite_clearance_probability vector of updates to the proportion of artemisinin-resistant infections that result in early treatment failure +#' @param early_treatment_failure_probability vector of updates to the proportion of artemisinin-resistant infections that result in slow parasite clearance +#' @param late_clinical_failure_probability vector of updates to the proportion of partner-drug-resistant infections that result in late clinical failure +#' @param late_parasitological_failure_probability vector of updates to the proportion of partner-drug-resistant infections that result in late parasitological failure +#' @param reinfection_during_prophylaxis_probability vector of updates to the proportion of partner-drug-resistant infections that result in reinfection during prophylaxis #' @param slow_parasite_clearance_time single value representing the mean time individual's experiencing slow parasite clearance reside in the treated state #' @export set_antimalarial_resistance <- function(parameters, drug, timesteps, - artemisinin_resistance, - partner_drug_resistance, - slow_parasite_clearance_prob, - early_treatment_failure_prob, - late_clinical_failure_prob, - late_parasitological_prob, - reinfection_prob, + artemisinin_resistance_proportion, + partner_drug_resistance_proportion, + slow_parasite_clearance_probability, + early_treatment_failure_probability, + late_clinical_failure_probability, + late_parasitological_failure_probability, + reinfection_during_prophylaxis_probability, slow_parasite_clearance_time) { - if(any(partner_drug_resistance > 0, - late_clinical_failure_prob > 0, - late_parasitological_prob > 0, - reinfection_prob > 0)) { + if(any(partner_drug_resistance_proportion > 0, + late_clinical_failure_probability > 0, + late_parasitological_failure_probability > 0, + reinfection_during_prophylaxis_probability > 0)) { stop("Parameters set for unimplemented feature - late clinical failure, late parasitological failure, or reinfection during prophylaxis") } - if(any(c(length(artemisinin_resistance), - length(partner_drug_resistance), - length(slow_parasite_clearance_prob), - length(early_treatment_failure_prob), - length(late_clinical_failure_prob), - length(late_parasitological_prob), - length(reinfection_prob)) != length(timesteps))) { + if(any(c(length(artemisinin_resistance_proportion), + length(partner_drug_resistance_proportion), + length(slow_parasite_clearance_probability), + length(early_treatment_failure_probability), + length(late_clinical_failure_probability), + length(late_parasitological_failure_probability), + length(reinfection_during_prophylaxis_probability)) != length(timesteps))) { stop("Length of one or more resistance parameter vectors does not match time steps specified for update") } - if(any(artemisinin_resistance < 0 | artemisinin_resistance > 1 | - partner_drug_resistance < 0 | partner_drug_resistance > 1)) { + if(any(artemisinin_resistance_proportion < 0 | artemisinin_resistance_proportion > 1 | + partner_drug_resistance_proportion < 0 | partner_drug_resistance_proportion > 1)) { stop("Artemisinin and partner-drug resistance proportions must fall between 0 and 1") } - if(any(slow_parasite_clearance_prob < 0 | slow_parasite_clearance_prob > 1 | - early_treatment_failure_prob < 0 | early_treatment_failure_prob > 1 | - late_clinical_failure_prob < 0 | late_clinical_failure_prob > 1 | - late_parasitological_prob < 0 | late_parasitological_prob > 1 | - reinfection_prob < 0 | reinfection_prob > 1)) { + if(any(slow_parasite_clearance_probability < 0 | slow_parasite_clearance_probability > 1 | + early_treatment_failure_probability < 0 | early_treatment_failure_probability > 1 | + late_clinical_failure_probability < 0 | late_clinical_failure_probability > 1 | + late_parasitological_failure_probability < 0 | late_parasitological_failure_probability > 1 | + reinfection_during_prophylaxis_probability < 0 | reinfection_during_prophylaxis_probability > 1)) { stop("Resistance outcome probabilities must fall between 0 and 1") } @@ -80,13 +80,13 @@ set_antimalarial_resistance <- function(parameters, parameters$antimalarial_resistance_drug[[drug_index]] <- drug parameters$antimalarial_resistance_timesteps[[drug_index]] <- timesteps - parameters$prop_artemisinin_resistant[[drug_index]] <- artemisinin_resistance - parameters$prop_partner_drug_resistant[[drug_index]] <- partner_drug_resistance - parameters$slow_parasite_clearance_prob[[drug_index]] <- slow_parasite_clearance_prob - parameters$early_treatment_failure_prob[[drug_index]] <- early_treatment_failure_prob - parameters$late_clinical_failure_prob[[drug_index]] <- late_clinical_failure_prob - parameters$late_parasitological_failure_prob[[drug_index]] <- late_parasitological_prob - parameters$reinfection_during_prophylaxis[[drug_index]] <- reinfection_prob + parameters$artemisinin_resistance_proportion[[drug_index]] <- artemisinin_resistance_proportion + parameters$partner_drug_resistance_proportion[[drug_index]] <- partner_drug_resistance_proportion + parameters$slow_parasite_clearance_probability[[drug_index]] <- slow_parasite_clearance_probability + parameters$early_treatment_failure_probability[[drug_index]] <- early_treatment_failure_probability + parameters$late_clinical_failure_probability[[drug_index]] <- late_clinical_failure_probability + parameters$late_parasitological_failure_probability[[drug_index]] <- late_parasitological_failure_probability + parameters$reinfection_during_prophylaxis_probability[[drug_index]] <- reinfection_during_prophylaxis_probability parameters$dt_slow_parasite_clearance[[drug_index]] <- slow_parasite_clearance_time return(parameters) @@ -121,13 +121,13 @@ get_antimalarial_resistance_parameters <- function(parameters, drugs, timestep) drug <- parameters$antimalarial_resistance_drug[[i]] treated_with_drug <- which(drugs == drug) resistance_timestep <- match_timestep(ts = parameters$antimalarial_resistance_timesteps[[i]], t = timestep) - artemisinin_resistance_proportion[treated_with_drug] <- parameters$prop_artemisinin_resistant[[i]][resistance_timestep] - partner_drug_resistance_proportion[treated_with_drug] <- parameters$prop_partner_drug_resistant[[i]][resistance_timestep] - slow_parasite_clearance_probability[treated_with_drug] <- parameters$slow_parasite_clearance_prob[[i]][resistance_timestep] - early_treatment_failure_probability[treated_with_drug] <- parameters$early_treatment_failure_prob[[i]][resistance_timestep] - late_clinical_failure_probability[treated_with_drug] <- parameters$late_clinical_failure_prob[[i]][resistance_timestep] - late_parasitological_failure_probability[treated_with_drug] <- parameters$late_parasitological_failure_prob[[i]][resistance_timestep] - reinfection_during_prophylaxis_probability[treated_with_drug] <- parameters$reinfection_during_prophylaxis[[i]][resistance_timestep] + artemisinin_resistance_proportion[treated_with_drug] <- parameters$artemisinin_resistance_proportion[[i]][resistance_timestep] + partner_drug_resistance_proportion[treated_with_drug] <- parameters$partner_drug_resistance_proportion[[i]][resistance_timestep] + slow_parasite_clearance_probability[treated_with_drug] <- parameters$slow_parasite_clearance_probability[[i]][resistance_timestep] + early_treatment_failure_probability[treated_with_drug] <- parameters$early_treatment_failure_probability[[i]][resistance_timestep] + late_clinical_failure_probability[treated_with_drug] <- parameters$late_clinical_failure_probability[[i]][resistance_timestep] + late_parasitological_failure_probability[treated_with_drug] <- parameters$late_parasitological_failure_probability[[i]][resistance_timestep] + reinfection_during_prophylaxis_probability[treated_with_drug] <- parameters$reinfection_during_prophylaxis_probability[[i]][resistance_timestep] dt_slow_parasite_clearance[treated_with_drug] <- parameters$dt_slow_parasite_clearance[[i]] } @@ -143,4 +143,4 @@ get_antimalarial_resistance_parameters <- function(parameters, drugs, timestep) return(resistance_parameters) -} \ No newline at end of file +} diff --git a/R/parameters.R b/R/parameters.R index c87e69bf..b5b145ed 100644 --- a/R/parameters.R +++ b/R/parameters.R @@ -200,13 +200,13 @@ #' * antimalarial_resistance - boolean for if antimalarial resistance is enabled; default = FALSE #' * antimalarial_resistance_drug - vector of drugs for which resistance can be parameterised; default = NULL #' * antimalarial_resistance_timesteps - vector of time steps on which resistance updates occur; default = NULL -#' * prop_artemisinin_resistant - vector of proportions of infections resistant to the artemisinin component of a given drug; default = NULL -#' * prop_partner_drug_resistant - vector of proportions of infections resistant to the parter drug component of a given drug; default = NULL -#' * slow_parasite_clearance_prob - vector of probabilities of slow parasite clearance for a given drug; default = NULL -#' * early_treatment_failure_prob - vector of probabilities of early treatment failure for a given drug; default = NULL -#' * late_clinical_failure_prob - vector of probabilities of late clinical failure for a given drug; default = NULL -#' * late_parasitological_failure_prob - vector of probabilities of late parasitological failure for a given drug; default = NULL -#' * reinfection_during_prophylaxis - vector of probabilities of reinfection during prophylaxis for a given drug; default = NULL +#' * artemisinin_resistant_proportion - vector of proportions of infections resistant to the artemisinin component of a given drug; default = NULL +#' * partner_drug_resistance_proportion - vector of proportions of infections resistant to the parter drug component of a given drug; default = NULL +#' * slow_parasite_clearance_probability - vector of probabilities of slow parasite clearance for a given drug; default = NULL +#' * early_treatment_failure_probability - vector of probabilities of early treatment failure for a given drug; default = NULL +#' * late_clinical_failure_probability - vector of probabilities of late clinical failure for a given drug; default = NULL +#' * late_parasitological_failure_probability - vector of probabilities of late parasitological failure for a given drug; default = NULL +#' * reinfection_during_prophylaxis_probability - vector of probabilities of reinfection during prophylaxis for a given drug; default = NULL #' * dt_slow_parasite_clearance - the delay for humans experiencing slow parasite clearance to move from state Tr to S; default = NULL #' #' rendering: @@ -397,13 +397,13 @@ get_parameters <- function(overrides = list()) { antimalarial_resistance = FALSE, antimalarial_resistance_drug = NULL, antimalarial_resistance_timesteps = NULL, - prop_artemisinin_resistant = NULL, - prop_partner_drug_resistant = NULL, - slow_parasite_clearance_prob = NULL, - early_treatment_failure_prob = NULL, - late_clinical_failure_prob = NULL, - late_parasitological_failure_prob = NULL, - reinfection_during_prophylaxis = NULL, + artemisinin_resistance_proportion = NULL, + partner_drug_resistance_proportion = NULL, + slow_parasite_clearance_probability = NULL, + early_treatment_failure_probability = NULL, + late_clinical_failure_probability = NULL, + late_parasitological_failure_probability = NULL, + reinfection_during_prophylaxis_probability = NULL, dt_slow_parasite_clearance = NULL, # flexible carrying capacity carrying_capacity = FALSE, diff --git a/man/get_parameters.Rd b/man/get_parameters.Rd index fc6ba000..f1f7ddee 100644 --- a/man/get_parameters.Rd +++ b/man/get_parameters.Rd @@ -221,13 +221,13 @@ please set antimalarial resistance parameters with the convenience functions in \item antimalarial_resistance - boolean for if antimalarial resistance is enabled; default = FALSE \item antimalarial_resistance_drug - vector of drugs for which resistance can be parameterised; default = NULL \item antimalarial_resistance_timesteps - vector of time steps on which resistance updates occur; default = NULL -\item prop_artemisinin_resistant - vector of proportions of infections resistant to the artemisinin component of a given drug; default = NULL -\item prop_partner_drug_resistant - vector of proportions of infections resistant to the parter drug component of a given drug; default = NULL -\item slow_parasite_clearance_prob - vector of probabilities of slow parasite clearance for a given drug; default = NULL -\item early_treatment_failure_prob - vector of probabilities of early treatment failure for a given drug; default = NULL -\item late_clinical_failure_prob - vector of probabilities of late clinical failure for a given drug; default = NULL -\item late_parasitological_failure_prob - vector of probabilities of late parasitological failure for a given drug; default = NULL -\item reinfection_during_prophylaxis - vector of probabilities of reinfection during prophylaxis for a given drug; default = NULL +\item artemisinin_resistant_proportion - vector of proportions of infections resistant to the artemisinin component of a given drug; default = NULL +\item partner_drug_resistance_proportion - vector of proportions of infections resistant to the parter drug component of a given drug; default = NULL +\item slow_parasite_clearance_probability - vector of probabilities of slow parasite clearance for a given drug; default = NULL +\item early_treatment_failure_probability - vector of probabilities of early treatment failure for a given drug; default = NULL +\item late_clinical_failure_probability - vector of probabilities of late clinical failure for a given drug; default = NULL +\item late_parasitological_failure_probability - vector of probabilities of late parasitological failure for a given drug; default = NULL +\item reinfection_during_prophylaxis_probability - vector of probabilities of reinfection during prophylaxis for a given drug; default = NULL \item dt_slow_parasite_clearance - the delay for humans experiencing slow parasite clearance to move from state Tr to S; default = NULL } diff --git a/man/run_simulation.Rd b/man/run_simulation.Rd index b9e95e0a..383b38f6 100644 --- a/man/run_simulation.Rd +++ b/man/run_simulation.Rd @@ -92,7 +92,7 @@ susceptible \item mosquito_deaths: number of adult female mosquitoes who die this timestep \item n_drug_efficacy_failures: number of clinically treated individuals whose treatment failed due to drug efficacy \item n_early_treatment_failure: number of clinically treated individuals who experienced early treatment failure -\item n_successfully_treated: number of clinically treated individuals who are treated successfully +\item n_successfully_treated: number of clinically treated individuals who are treated successfully (includes individuals who experience slow parasite clearance) \item n_slow_parasite_clearance: number of clinically treated individuals who experienced slow parasite clearance } } diff --git a/man/set_antimalarial_resistance.Rd b/man/set_antimalarial_resistance.Rd index 82c65405..56627125 100644 --- a/man/set_antimalarial_resistance.Rd +++ b/man/set_antimalarial_resistance.Rd @@ -8,13 +8,13 @@ set_antimalarial_resistance( parameters, drug, timesteps, - artemisinin_resistance, - partner_drug_resistance, - slow_parasite_clearance_prob, - early_treatment_failure_prob, - late_clinical_failure_prob, - late_parasitological_prob, - reinfection_prob, + artemisinin_resistance_proportion, + partner_drug_resistance_proportion, + slow_parasite_clearance_probability, + early_treatment_failure_probability, + late_clinical_failure_probability, + late_parasitological_failure_probability, + reinfection_during_prophylaxis_probability, slow_parasite_clearance_time ) } @@ -25,19 +25,19 @@ set_antimalarial_resistance( \item{timesteps}{vector of time steps for each update to resistance proportion and resistance outcome probability} -\item{artemisinin_resistance}{vector of updates to the proportions of infections that are artemisinin resistant at time t} +\item{artemisinin_resistance_proportion}{vector of updates to the proportions of infections that are artemisinin resistant at time t} -\item{partner_drug_resistance}{vector of updates to the proportions of infections that are partner-drug resistant at time t} +\item{partner_drug_resistance_proportion}{vector of updates to the proportions of infections that are partner-drug resistant at time t} -\item{slow_parasite_clearance_prob}{vector of updates to the proportion of artemisinin-resistant infections that result in early treatment failure} +\item{slow_parasite_clearance_probability}{vector of updates to the proportion of artemisinin-resistant infections that result in early treatment failure} -\item{early_treatment_failure_prob}{vector of updates to the proportion of artemisinin-resistant infections that result in slow parasite clearance} +\item{early_treatment_failure_probability}{vector of updates to the proportion of artemisinin-resistant infections that result in slow parasite clearance} -\item{late_clinical_failure_prob}{vector of updates to the proportion of partner-drug-resistant infections that result in late clinical failure} +\item{late_clinical_failure_probability}{vector of updates to the proportion of partner-drug-resistant infections that result in late clinical failure} -\item{late_parasitological_prob}{vector of updates to the proportion of partner-drug-resistant infections that result in late parasitological failure} +\item{late_parasitological_failure_probability}{vector of updates to the proportion of partner-drug-resistant infections that result in late parasitological failure} -\item{reinfection_prob}{vector of updates to the proportion of partner-drug-resistant infections that result in reinfection during prophylaxis} +\item{reinfection_during_prophylaxis_probability}{vector of updates to the proportion of partner-drug-resistant infections that result in reinfection during prophylaxis} \item{slow_parasite_clearance_time}{single value representing the mean time individual's experiencing slow parasite clearance reside in the treated state} } diff --git a/tests/testthat/test-antimalarial-resistance.R b/tests/testthat/test-antimalarial-resistance.R index 193e09cf..ae45a20e 100644 --- a/tests/testthat/test-antimalarial-resistance.R +++ b/tests/testthat/test-antimalarial-resistance.R @@ -1,102 +1,103 @@ test_that('set_antimalarial_resistance() toggles resistance on', { - simparams <- get_parameters() - simparams <- set_drugs(parameters = simparams, drugs = list(SP_AQ_params)) - simparams <- set_clinical_treatment(parameters = simparams, - drug = 1, - timesteps = 1, - coverages = 1) - set_antimalarial_resistance(parameters = simparams, + parameters <- get_parameters() + parameters <- set_drugs(parameters = parameters, drugs = list(SP_AQ_params)) + parameters <- set_clinical_treatment(parameters = parameters, + drug = 1, + timesteps = 1, + coverages = 1) + set_antimalarial_resistance(parameters = parameters, drug = 1, timesteps = 1, - artemisinin_resistance = 0.5, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0.5, - early_treatment_failure_prob = 0.6, - late_clinical_failure_prob = 0, - late_parasitological_prob = 0, - reinfection_prob = 0, - slow_parasite_clearance_time = 10) -> simparams - expect_identical(object = simparams$antimalarial_resistance, expected = TRUE) + artemisinin_resistance_proportion = 0.5, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = 0.5, + early_treatment_failure_probability = 0.6, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0, + slow_parasite_clearance_time = 10) -> parameters + expect_identical(object = parameters$antimalarial_resistance, expected = TRUE) }) test_that('set_antimalarial_resistance() errors if parameter inputs of different length to timesteps', { - simparams <- get_parameters() - simparams <- set_drugs(parameters = simparams, drugs = list(SP_AQ_params)) - simparams <- set_clinical_treatment(parameters = simparams, - drug = 1, - timesteps = 1, - coverages = 1) - expect_error(object = set_antimalarial_resistance(parameters = simparams, + parameters <- get_parameters() + parameters <- set_drugs(parameters = parameters, drugs = list(SP_AQ_params)) + parameters <- set_clinical_treatment(parameters = parameters, + drug = 1, + timesteps = 1, + coverages = 1) + expect_error(object = set_antimalarial_resistance(parameters = parameters, drug = 1, timesteps = c(1, 10), - artemisinin_resistance = 0.5, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0.5, - early_treatment_failure_prob = 0.6, - late_clinical_failure_prob = 0.2, - late_parasitological_prob = 0.3, - reinfection_prob = 0.4, + artemisinin_resistance_proportion = 0.5, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = 0.5, + early_treatment_failure_probability = 0.6, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0, slow_parasite_clearance_time = 10)) }) test_that('set_antimalarial_resistance() errors if resistance proportions outside of range 0-1', { - simparams <- get_parameters() - simparams <- set_drugs(parameters = simparams, drugs = list(SP_AQ_params)) - simparams <- set_clinical_treatment(parameters = simparams, - drug = 1, - timesteps = 1, - coverages = 1) - expect_error(object = set_antimalarial_resistance(parameters = simparams, + parameters <- get_parameters() + parameters <- set_drugs(parameters = parameters, drugs = list(SP_AQ_params)) + parameters <- set_clinical_treatment(parameters = parameters, + drug = 1, + timesteps = 1, + coverages = 1) + expect_error(object = set_antimalarial_resistance(parameters = parameters, drug = 1, timesteps = 1, - artemisinin_resistance = 1.01, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0.5, - early_treatment_failure_prob = 0.6, - late_clinical_failure_prob = 0, - late_parasitological_prob = 0, - reinfection_prob = 0, + artemisinin_resistance_proportion = 1.01, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = 0.5, + early_treatment_failure_probability = 0.6, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0, slow_parasite_clearance_time = 10), regexp = "Artemisinin and partner-drug resistance proportions must fall between 0 and 1") }) test_that('set_antimalarial_resistance() errors if resistance phenotype probabilities outside bound of 0-1', { - simparams <- get_parameters() - simparams <- set_drugs(parameters = simparams, drugs = list(SP_AQ_params)) - simparams <- set_clinical_treatment(parameters = simparams, - drug = 1, - timesteps = 1, - coverages = 1) - expect_error(object = set_antimalarial_resistance(parameters = simparams, + parameters <- get_parameters() + parameters <- set_drugs(parameters = parameters, drugs = list(SP_AQ_params)) + parameters <- set_clinical_treatment(parameters = parameters, + drug = 1, + timesteps = 1, + coverages = 1) + expect_error(object = set_antimalarial_resistance(parameters = parameters, drug = 1, timesteps = 1, - artemisinin_resistance = 0.4, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = -0.5, - early_treatment_failure_prob = 0.6, - late_clinical_failure_prob = 0.2, - late_parasitological_prob = 0.3, - reinfection_prob = 0.4, + artemisinin_resistance_proportion = 0.4, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = -0.5, + early_treatment_failure_probability = 0.6, + late_clinical_failure_probability = 0.2, + late_parasitological_failure_probability = 0.3, + reinfection_during_prophylaxis_probability = 0.4, slow_parasite_clearance_time = 5)) }) test_that('set_antimalarial_resistance() errors if drug index > than number of drugs assigned using set_drugs()', { - simparams <- get_parameters() - simparams <- set_drugs(parameters = simparams, drugs = list(SP_AQ_params)) - simparams <- set_clinical_treatment(parameters = simparams, - drug = 1, - timesteps = 1, - coverages = 1) - expect_error(object = set_antimalarial_resistance(parameters = simparams, + parameters <- get_parameters() + parameters <- set_drugs(parameters = parameters, drugs = list(SP_AQ_params)) + parameters <- set_clinical_treatment(parameters = parameters, + drug = 1, + timesteps = 1, + coverages = 1) + expect_error(object = set_antimalarial_resistance(parameters = parameters, drug = 2, timesteps = 1, - artemisinin_resistance = 0.4, - partner_drug_resistance = 0.3, - slow_parasite_clearance_prob = 0.5, - early_treatment_failure_prob = 0.6, - late_clinical_failure_prob = 0.2, - late_parasitological_prob = 0.3, - reinfection_prob = 0.4)) + artemisinin_resistance_proportion = 0.4, + partner_drug_resistance_proportion = 0.3, + slow_parasite_clearance_probability = 0.5, + early_treatment_failure_probability = 0.6, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0.4, + slow_parasite_clearance_time = 10)) }) test_that('set_antimalarial_resistance() assigns parameters correctly despite order in which resistance parameters are specified', { @@ -109,52 +110,52 @@ test_that('set_antimalarial_resistance() assigns parameters correctly despite or parameters <- set_antimalarial_resistance(parameters = parameters, drug = 2, timesteps = 1, - artemisinin_resistance = 0.5, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0.41, - early_treatment_failure_prob = 0.2, - late_clinical_failure_prob = 0, - late_parasitological_prob = 0, - reinfection_prob = 0, + artemisinin_resistance_proportion = 0.5, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = 0.41, + early_treatment_failure_probability = 0.2, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0, slow_parasite_clearance_time = 5) parameters <- set_antimalarial_resistance(parameters = parameters, drug = 3, timesteps = 1, - artemisinin_resistance = 0, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0, - early_treatment_failure_prob = 0, - late_clinical_failure_prob = 0, - late_parasitological_prob = 0, - reinfection_prob = 0, + artemisinin_resistance_proportion = 0, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = 0, + early_treatment_failure_probability = 0, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0, slow_parasite_clearance_time = 10) parameters <- set_antimalarial_resistance(parameters = parameters, drug = 1, timesteps = 1, - artemisinin_resistance = 0.27, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0.23, - early_treatment_failure_prob = 0.9, - late_clinical_failure_prob = 0, - late_parasitological_prob = 0, - reinfection_prob = 0, + artemisinin_resistance_proportion = 0.27, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = 0.23, + early_treatment_failure_probability = 0.9, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0, slow_parasite_clearance_time = 20) expect_identical(parameters$antimalarial_resistance, TRUE) expect_identical(unlist(parameters$antimalarial_resistance_drug), c(2, 3, 1)) expect_identical(unlist(parameters$antimalarial_resistance_timesteps), rep(1, 3)) - expect_identical(unlist(parameters$prop_artemisinin_resistant), c(0.5, 0, 0.27)) - expect_identical(unlist(parameters$prop_partner_drug_resistant), c(0, 0, 0)) - expect_identical(unlist(parameters$slow_parasite_clearance_prob), c(0.41, 0, 0.23)) - expect_identical(unlist(parameters$early_treatment_failure_prob), c(0.2, 0, 0.9)) - expect_identical(unlist(parameters$late_clinical_failure_prob), c(0, 0, 0)) - expect_identical(unlist(parameters$late_parasitological_failure_prob), c(0, 0, 0)) - expect_identical(unlist(parameters$reinfection_during_prophylaxis), c(0, 0, 0)) + expect_identical(unlist(parameters$artemisinin_resistance_proportion), c(0.5, 0, 0.27)) + expect_identical(unlist(parameters$partner_drug_resistance_proportion), c(0, 0, 0)) + expect_identical(unlist(parameters$slow_parasite_clearance_probability), c(0.41, 0, 0.23)) + expect_identical(unlist(parameters$early_treatment_failure_probability), c(0.2, 0, 0.9)) + expect_identical(unlist(parameters$late_clinical_failure_probability), c(0, 0, 0)) + expect_identical(unlist(parameters$late_parasitological_failure_probability), c(0, 0, 0)) + expect_identical(unlist(parameters$reinfection_during_prophylaxis_probability), c(0, 0, 0)) expect_identical(unlist(parameters$dt_slow_parasite_clearance), c(5, 10, 20)) }) -test_that(desc = "set_antimalarial_resistance errors if length slow_parasite_clearance_time > 1", code = { +test_that("set_antimalarial_resistance errors if length slow_parasite_clearance_time > 1", { parameters <- get_parameters() parameters <- set_drugs(parameters = parameters, drugs = list(SP_AQ_params)) @@ -167,18 +168,18 @@ test_that(desc = "set_antimalarial_resistance errors if length slow_parasite_cle parameters <- set_antimalarial_resistance(parameters = parameters, drug = 1, timesteps = c(0, 10), - artemisinin_resistance = c(0.4, 0.8), - partner_drug_resistance = c(0, 0), - slow_parasite_clearance_prob = c(0.2, 0.4), - early_treatment_failure_prob = c(0, 0.45), - late_clinical_failure_prob = c(0, 0), - late_parasitological_prob = c(0, 0), - reinfection_prob = c(0, 0), + artemisinin_resistance_proportion = c(0.4, 0.8), + partner_drug_resistance_proportion = c(0, 0), + slow_parasite_clearance_probability = c(0.2, 0.4), + early_treatment_failure_probability = c(0, 0.45), + late_clinical_failure_probability = c(0, 0), + late_parasitological_failure_probability = c(0, 0), + reinfection_during_prophylaxis_probability = c(0, 0), slow_parasite_clearance_time = c(10 ,11)), "Error: length of slow_parasite_clearance_time not equal to 1") }) -test_that(desc = "set_antimalarial_resistance errors if slow_parasite_clearance_time not positive", code = { +test_that("set_antimalarial_resistance errors if slow_parasite_clearance_time not positive", { parameters <- get_parameters() parameters <- set_drugs(parameters = parameters, drugs = list(SP_AQ_params)) @@ -191,132 +192,132 @@ test_that(desc = "set_antimalarial_resistance errors if slow_parasite_clearance_ parameters <- set_antimalarial_resistance(parameters = parameters, drug = 1, timesteps = c(0, 10), - artemisinin_resistance = c(0.4, 0.8), - partner_drug_resistance = c(0, 0), - slow_parasite_clearance_prob = c(0.2, 0.4), - early_treatment_failure_prob = c(0, 0.45), - late_clinical_failure_prob = c(0, 0), - late_parasitological_prob = c(0, 0), - reinfection_prob = c(0, 0), + artemisinin_resistance_proportion = c(0.4, 0.8), + partner_drug_resistance_proportion = c(0, 0), + slow_parasite_clearance_probability = c(0.2, 0.4), + early_treatment_failure_probability = c(0, 0.45), + late_clinical_failure_probability = c(0, 0), + late_parasitological_failure_probability = c(0, 0), + reinfection_during_prophylaxis_probability = c(0, 0), slow_parasite_clearance_time = c(0)), "Error: slow_parasite_clearance_time is non-positive") }) test_that('get_antimalarial_resistance_parameters() correctly retrieves parameters when multiple drugs assigned', { - - get_parameters(overrides = list(human_population = 10000)) %>% - set_drugs(drugs = list(AL_params, SP_AQ_params, DHA_PQP_params)) %>% - set_clinical_treatment(drug = 1, timesteps = 1, coverages = 0.4) %>% - set_clinical_treatment(drug = 2, timesteps = 1, coverages = 0.3) %>% - set_clinical_treatment(drug = 3, timesteps = 1, coverages = 0.2) %>% - set_equilibrium(init_EIR = 20) %>% - set_antimalarial_resistance(drug = 2, - timesteps = c(0, 20), - artemisinin_resistance = c(0.02, 0.2), - partner_drug_resistance = c(0, 0), - slow_parasite_clearance_prob = c(0.02, 0.2), - early_treatment_failure_prob = c(0.02, 0.2), - late_clinical_failure_prob = c(0, 0), - late_parasitological_prob = c(0, 0), - reinfection_prob = c(0, 0), - slow_parasite_clearance_time = 20) %>% - set_antimalarial_resistance(drug = 1, - timesteps = c(0, 10), - artemisinin_resistance = c(0.01, 0.1), - partner_drug_resistance = c(0, 0), - slow_parasite_clearance_prob = c(0.01, 0.1), - early_treatment_failure_prob = c(0.01, 0.1), - late_clinical_failure_prob = c(0, 0), - late_parasitological_prob = c(0, 0), - reinfection_prob = c(0, 0), - slow_parasite_clearance_time = 10) %>% - set_antimalarial_resistance(drug = 3, - timesteps = c(0, 30), - artemisinin_resistance = c(0.03, 0.3), - partner_drug_resistance = c(0, 0), - slow_parasite_clearance_prob = c(0.03, 0.3), - early_treatment_failure_prob = c(0.03, 0.3), - late_clinical_failure_prob = c(0, 0), - late_parasitological_prob = c(0, 0), - reinfection_prob = c(0, 0), - slow_parasite_clearance_time = 30) -> parameters - - drugs <- c(1, 3, 2, 1, 2, 3, 3, 3, 2, 1, 3, 1, 2, 3, 2) - timestep <- 25 - - resistance_parameters <- get_antimalarial_resistance_parameters(parameters = parameters, - drugs = drugs, - timestep = timestep) - - expected_resistance_parameters <- list() - expected_resistance_parameters$artemisinin_resistance_proportion <- c(0.1, 0.03, 0.2, 0.1, 0.2, 0.03, 0.03, 0.03, 0.2, 0.1, 0.03, 0.1, 0.2, 0.03, 0.2) - expected_resistance_parameters$partner_drug_resistance_proportion <- rep(0, 15) - expected_resistance_parameters$slow_parasite_clearance_probability <- c(0.1, 0.03, 0.2, 0.1, 0.2, 0.03, 0.03, 0.03, 0.2, 0.1, 0.03, 0.1, 0.2, 0.03, 0.2) - expected_resistance_parameters$early_treatment_failure_probability <- c(0.1, 0.03, 0.2, 0.1, 0.2, 0.03, 0.03, 0.03, 0.2, 0.1, 0.03, 0.1, 0.2, 0.03, 0.2) - expected_resistance_parameters$late_clinical_failure_probability <- rep(0, 15) - expected_resistance_parameters$late_parasitological_failure_probability <- rep(0, 15) - expected_resistance_parameters$reinfection_during_prophylaxis_probability <- rep(0, 15) - expected_resistance_parameters$dt_slow_parasite_clearance <- c(10, 30, 20, 10, 20, 30, 30, 30, 20, 10, 30, 10, 20, 30, 20) - - expect_identical(resistance_parameters, expected = expected_resistance_parameters) - - }) + + get_parameters(overrides = list(human_population = 10000)) |> + set_drugs(drugs = list(AL_params, SP_AQ_params, DHA_PQP_params)) |> + set_clinical_treatment(drug = 1, timesteps = 1, coverages = 0.4) |> + set_clinical_treatment(drug = 2, timesteps = 1, coverages = 0.3) |> + set_clinical_treatment(drug = 3, timesteps = 1, coverages = 0.2) |> + set_equilibrium(init_EIR = 20) |> + set_antimalarial_resistance(drug = 2, + timesteps = c(0, 20), + artemisinin_resistance_proportion = c(0.02, 0.2), + partner_drug_resistance_proportion = c(0, 0), + slow_parasite_clearance_probability = c(0.02, 0.2), + early_treatment_failure_probability = c(0.02, 0.2), + late_clinical_failure_probability = c(0, 0), + late_parasitological_failure_probability = c(0, 0), + reinfection_during_prophylaxis_probability = c(0, 0), + slow_parasite_clearance_time = 20) |> + set_antimalarial_resistance(drug = 1, + timesteps = c(0, 10), + artemisinin_resistance_proportion = c(0.01, 0.1), + partner_drug_resistance_proportion = c(0, 0), + slow_parasite_clearance_probability = c(0.01, 0.1), + early_treatment_failure_probability = c(0.01, 0.1), + late_clinical_failure_probability = c(0, 0), + late_parasitological_failure_probability = c(0, 0), + reinfection_during_prophylaxis_probability = c(0, 0), + slow_parasite_clearance_time = 10) |> + set_antimalarial_resistance(drug = 3, + timesteps = c(0, 30), + artemisinin_resistance_proportion = c(0.03, 0.3), + partner_drug_resistance_proportion = c(0, 0), + slow_parasite_clearance_probability = c(0.03, 0.3), + early_treatment_failure_probability = c(0.03, 0.3), + late_clinical_failure_probability = c(0, 0), + late_parasitological_failure_probability = c(0, 0), + reinfection_during_prophylaxis_probability = c(0, 0), + slow_parasite_clearance_time = 30) -> parameters + + drugs <- c(1, 3, 2, 1, 2, 3, 3, 3, 2, 1, 3, 1, 2, 3, 2) + timestep <- 25 + + resistance_parameters <- get_antimalarial_resistance_parameters(parameters = parameters, + drugs = drugs, + timestep = timestep) + + expected_resistance_parameters <- list() + expected_resistance_parameters$artemisinin_resistance_proportion <- c(0.1, 0.03, 0.2, 0.1, 0.2, 0.03, 0.03, 0.03, 0.2, 0.1, 0.03, 0.1, 0.2, 0.03, 0.2) + expected_resistance_parameters$partner_drug_resistance_proportion <- rep(0, 15) + expected_resistance_parameters$slow_parasite_clearance_probability <- c(0.1, 0.03, 0.2, 0.1, 0.2, 0.03, 0.03, 0.03, 0.2, 0.1, 0.03, 0.1, 0.2, 0.03, 0.2) + expected_resistance_parameters$early_treatment_failure_probability <- c(0.1, 0.03, 0.2, 0.1, 0.2, 0.03, 0.03, 0.03, 0.2, 0.1, 0.03, 0.1, 0.2, 0.03, 0.2) + expected_resistance_parameters$late_clinical_failure_probability <- rep(0, 15) + expected_resistance_parameters$late_parasitological_failure_probability <- rep(0, 15) + expected_resistance_parameters$reinfection_during_prophylaxis_probability <- rep(0, 15) + expected_resistance_parameters$dt_slow_parasite_clearance <- c(10, 30, 20, 10, 20, 30, 30, 30, 20, 10, 30, 10, 20, 30, 20) + + expect_identical(resistance_parameters, expected = expected_resistance_parameters) + +}) test_that('get_antimalarial_resistance_parameters() correctly retrieves parameters when not all drugs assigned resistance', { - - get_parameters(overrides = list(human_population = 10000)) %>% - set_drugs(drugs = list(AL_params, SP_AQ_params, DHA_PQP_params)) %>% - set_clinical_treatment(drug = 1, timesteps = 1, coverages = 0.4) %>% - set_clinical_treatment(drug = 2, timesteps = 1, coverages = 0.3) %>% - set_clinical_treatment(drug = 3, timesteps = 1, coverages = 0.2) %>% - set_equilibrium(init_EIR = 20) %>% - set_antimalarial_resistance(drug = 2, - timesteps = c(0, 20), - artemisinin_resistance = c(0.02, 0.2), - partner_drug_resistance = c(0, 0), - slow_parasite_clearance_prob = c(0.02, 0.2), - early_treatment_failure_prob = c(0.02, 0.2), - late_clinical_failure_prob = c(0, 0), - late_parasitological_prob = c(0, 0), - reinfection_prob = c(0, 0), - slow_parasite_clearance_time = 20) -> parameters - - drugs <- c(1, 3, 2, 1, 2, 3, 3, 3, 2, 1, 3, 1, 2, 3, 2) - timestep <- 25 - - resistance_parameters <- get_antimalarial_resistance_parameters(parameters = parameters, - drugs = drugs, - timestep = timestep) - - expected_resistance_parameters <- list() - expected_resistance_parameters$artemisinin_resistance_proportion <- c(0, 0, 0.2, 0, 0.2, 0, 0, 0, 0.2, 0, 0, 0, 0.2, 0, 0.2) - expected_resistance_parameters$partner_drug_resistance_proportion <- rep(0, 15) - expected_resistance_parameters$slow_parasite_clearance_probability <- c(0, 0, 0.2, 0, 0.2, 0, 0, 0, 0.2, 0, 0, 0, 0.2, 0, 0.2) - expected_resistance_parameters$early_treatment_failure_probability <- c(0, 0, 0.2, 0, 0.2, 0, 0, 0, 0.2, 0, 0, 0, 0.2, 0, 0.2) - expected_resistance_parameters$late_clinical_failure_probability <- rep(0, 15) - expected_resistance_parameters$late_parasitological_failure_probability <- rep(0, 15) - expected_resistance_parameters$reinfection_during_prophylaxis_probability <- rep(0, 15) - expected_resistance_parameters$dt_slow_parasite_clearance <- c(5, 5, 20, 5, 20, 5, 5, 5, 20, 5, 5, 5, 20, 5, 20) - - expect_identical(resistance_parameters, expected = expected_resistance_parameters) - - }) + + get_parameters(overrides = list(human_population = 10000)) %>% + set_drugs(drugs = list(AL_params, SP_AQ_params, DHA_PQP_params)) %>% + set_clinical_treatment(drug = 1, timesteps = 1, coverages = 0.4) %>% + set_clinical_treatment(drug = 2, timesteps = 1, coverages = 0.3) %>% + set_clinical_treatment(drug = 3, timesteps = 1, coverages = 0.2) %>% + set_equilibrium(init_EIR = 20) %>% + set_antimalarial_resistance(drug = 2, + timesteps = c(0, 20), + artemisinin_resistance_proportion = c(0.02, 0.2), + partner_drug_resistance_proportion = c(0, 0), + slow_parasite_clearance_probability = c(0.02, 0.2), + early_treatment_failure_probability = c(0.02, 0.2), + late_clinical_failure_probability = c(0, 0), + late_parasitological_failure_probability = c(0, 0), + reinfection_during_prophylaxis_probability = c(0, 0), + slow_parasite_clearance_time = 20) -> parameters + + drugs <- c(1, 3, 2, 1, 2, 3, 3, 3, 2, 1, 3, 1, 2, 3, 2) + timestep <- 25 + + resistance_parameters <- get_antimalarial_resistance_parameters(parameters = parameters, + drugs = drugs, + timestep = timestep) + + expected_resistance_parameters <- list() + expected_resistance_parameters$artemisinin_resistance_proportion <- c(0, 0, 0.2, 0, 0.2, 0, 0, 0, 0.2, 0, 0, 0, 0.2, 0, 0.2) + expected_resistance_parameters$partner_drug_resistance_proportion <- rep(0, 15) + expected_resistance_parameters$slow_parasite_clearance_probability <- c(0, 0, 0.2, 0, 0.2, 0, 0, 0, 0.2, 0, 0, 0, 0.2, 0, 0.2) + expected_resistance_parameters$early_treatment_failure_probability <- c(0, 0, 0.2, 0, 0.2, 0, 0, 0, 0.2, 0, 0, 0, 0.2, 0, 0.2) + expected_resistance_parameters$late_clinical_failure_probability <- rep(0, 15) + expected_resistance_parameters$late_parasitological_failure_probability <- rep(0, 15) + expected_resistance_parameters$reinfection_during_prophylaxis_probability <- rep(0, 15) + expected_resistance_parameters$dt_slow_parasite_clearance <- c(5, 5, 20, 5, 20, 5, 5, 5, 20, 5, 5, 5, 20, 5, 20) + + expect_identical(resistance_parameters, expected = expected_resistance_parameters) + +}) test_that('get_antimalarial_resistance_parameters() returns an error when antimalarial resistance has not been parameterised', { - - get_parameters(overrides = list(human_population = 10000)) %>% - set_drugs(drugs = list(AL_params, SP_AQ_params, DHA_PQP_params)) %>% - set_clinical_treatment(drug = 1, timesteps = 1, coverages = 0.4) %>% - set_clinical_treatment(drug = 2, timesteps = 1, coverages = 0.3) %>% - set_clinical_treatment(drug = 3, timesteps = 1, coverages = 0.2) %>% - set_equilibrium(init_EIR = 20) -> parameters - - drugs <- c(1, 3, 2, 1, 2, 3, 3, 3, 2, 1, 3, 1, 2, 3, 2) - timestep <- 25 - - - expect_error(get_antimalarial_resistance_parameters(parameters = parameters, - drugs = drugs, - timestep = timestep), - "Error: Antimalarial resistance has not been parameterised; antimalarial_resistance = FALSE") -}) \ No newline at end of file + + get_parameters(overrides = list(human_population = 10000)) %>% + set_drugs(drugs = list(AL_params, SP_AQ_params, DHA_PQP_params)) %>% + set_clinical_treatment(drug = 1, timesteps = 1, coverages = 0.4) %>% + set_clinical_treatment(drug = 2, timesteps = 1, coverages = 0.3) %>% + set_clinical_treatment(drug = 3, timesteps = 1, coverages = 0.2) %>% + set_equilibrium(init_EIR = 20) -> parameters + + drugs <- c(1, 3, 2, 1, 2, 3, 3, 3, 2, 1, 3, 1, 2, 3, 2) + timestep <- 25 + + + expect_error(get_antimalarial_resistance_parameters(parameters = parameters, + drugs = drugs, + timestep = timestep), + "Error: Antimalarial resistance has not been parameterised; antimalarial_resistance = FALSE") +}) diff --git a/tests/testthat/test-infection-integration.R b/tests/testthat/test-infection-integration.R index 409cc466..2f05c463 100644 --- a/tests/testthat/test-infection-integration.R +++ b/tests/testthat/test-infection-integration.R @@ -6,7 +6,7 @@ test_that('simulate_infection integrates different types of infection and schedu )) events <- create_events(parameters) renderer <- mock_render(timestep) - + age <- c(20, 24, 5, 39, 20, 24, 5, 39) * 365 immunity <- c(.2, .3, .5, .9, .2, .3, .5, .9) asymptomatics <- mockery::mock() @@ -15,7 +15,7 @@ test_that('simulate_infection integrates different types of infection and schedu id = individual::DoubleVariable$new(immunity), state = list(get_index_of = mockery::mock(asymptomatics)) ) - + bitten <- individual::Bitset$new(population)$insert(c(1, 3, 5, 7)) boost_immunity_mock <- mockery::mock() infected <- individual::Bitset$new(population)$insert(c(1, 3, 5)) @@ -27,7 +27,7 @@ test_that('simulate_infection integrates different types of infection and schedu treated <- individual::Bitset$new(population)$insert(3) treated_mock <- mockery::mock(treated) schedule_mock <- mockery::mock() - + mockery::stub(simulate_infection, 'boost_immunity', boost_immunity_mock) mockery::stub(simulate_infection, 'calculate_infections', infection_mock) mockery::stub(simulate_infection, 'calculate_clinical_infections', clinical_infection_mock) @@ -45,7 +45,7 @@ test_that('simulate_infection integrates different types of infection and schedu timestep, renderer ) - + mockery::expect_args( boost_immunity_mock, 1, @@ -55,7 +55,7 @@ test_that('simulate_infection integrates different types of infection and schedu 5, parameters$ub ) - + mockery::expect_args( infection_mock, 1, @@ -65,7 +65,7 @@ test_that('simulate_infection integrates different types of infection and schedu renderer, timestep ) - + mockery::expect_args( clinical_infection_mock, 1, @@ -75,7 +75,7 @@ test_that('simulate_infection integrates different types of infection and schedu renderer, timestep ) - + mockery::expect_args( severe_infection_mock, 1, @@ -85,7 +85,7 @@ test_that('simulate_infection integrates different types of infection and schedu parameters, renderer ) - + mockery::expect_args( treated_mock, 1, @@ -95,7 +95,7 @@ test_that('simulate_infection integrates different types of infection and schedu timestep, renderer ) - + mockery::expect_args( schedule_mock, 1, @@ -125,7 +125,7 @@ test_that('calculate_infections works various combinations of drug and vaccinati booster_coverage = matrix(1), booster_profile = list(rtss_booster_profile) ) - + variables <- list( state = individual::CategoricalVariable$new( c('D', 'S', 'A', 'U', 'Tr'), @@ -137,7 +137,7 @@ test_that('calculate_infections works various combinations of drug and vaccinati pev_profile = individual::IntegerVariable$new(c(-1, 1, 2, -1)), ib = individual::DoubleVariable$new(c(.2, .3, .5, .9)) ) - + immunity_mock <- mockery::mock(c(.2, .3, .4)) weibull_mock <- mockery::mock(.2) vaccine_antibodies_mock <- mockery::mock(c(2, 3)) @@ -148,7 +148,7 @@ test_that('calculate_infections works various combinations of drug and vaccinati mockery::stub(calculate_infections, 'calculate_pev_antibodies', vaccine_antibodies_mock) mockery::stub(calculate_infections, 'calculate_pev_efficacy', vaccine_efficacy_mock) mockery::stub(calculate_infections, 'bernoulli_multi_p', bernoulli_mock) - + # remove randomness from vaccine parameters mockery::stub( calculate_infections, @@ -158,9 +158,9 @@ test_that('calculate_infections works various combinations of drug and vaccinati }, depth = 4 ) - + bitten_humans <- individual::Bitset$new(4)$insert(c(1, 2, 3, 4)) - + infections <- calculate_infections( variables, bitten_humans, @@ -168,9 +168,9 @@ test_that('calculate_infections works various combinations of drug and vaccinati mock_render(timestep), timestep ) - + expect_equal(infections$to_vector(), 3) - + mockery::expect_args(immunity_mock, 1, c(.3, .5, .9), parameters) mockery::expect_args( weibull_mock, @@ -179,7 +179,7 @@ test_that('calculate_infections works various combinations of drug and vaccinati parameters$drug_prophylaxis_shape[[2]], parameters$drug_prophylaxis_scale[[2]] ) - + mockery::expect_args( vaccine_antibodies_mock, 1, @@ -203,13 +203,13 @@ test_that('calculate_infections works various combinations of drug and vaccinati 1, c(.2 * .8 * .8, .3 * .7, .4) ) - + }) test_that('calculate_clinical_infections correctly samples clinically infected', { timestep <- 5 parameters <- get_parameters() - + variables <- list( ica = individual::DoubleVariable$new(c(.2, .3, .5, .9)), icm = individual::DoubleVariable$new(c(.2, .3, .5, .9)), @@ -217,25 +217,25 @@ test_that('calculate_clinical_infections correctly samples clinically infected', last_boosted_ica = individual::DoubleVariable$new(c(-1, -1, 1, -1)), last_boosted_id = individual::DoubleVariable$new(c(-1, -1, 1, -1)) ) - + immunity_mock <- mockery::mock(c(.2, .3, .4)) boost_mock <- mockery::mock() mockery::stub(calculate_clinical_infections, 'boost_immunity', boost_mock) - + mockery::stub(calculate_clinical_infections, 'clinical_immunity', immunity_mock) bernoulli_mock <- mockery::mock(c(1, 3)) mockery::stub(calculate_clinical_infections, 'bernoulli_multi_p', bernoulli_mock) - + infections <- individual::Bitset$new(4)$insert(c(2, 3, 4)) - + clinical_infections <- calculate_clinical_infections( variables, infections, parameters ) - + expect_equal(clinical_infections$to_vector(), c(2, 4)) - + mockery::expect_args( immunity_mock, 1, @@ -243,7 +243,7 @@ test_that('calculate_clinical_infections correctly samples clinically infected', c(.3, .5, .9), parameters ) - + mockery::expect_args( bernoulli_mock, 1, @@ -324,24 +324,24 @@ test_that('calculate_treated correctly samples treated and updates the drug stat parameters <- set_antimalarial_resistance(parameters = parameters, drug = 1, timesteps = 1, - artemisinin_resistance = 0.5, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0, - early_treatment_failure_prob = 0.2, - late_clinical_failure_prob = 0, - late_parasitological_prob = 0, - reinfection_prob = 0, + artemisinin_resistance_proportion = 0.5, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = 0, + early_treatment_failure_probability = 0.2, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0, slow_parasite_clearance_time = 10) parameters <- set_antimalarial_resistance(parameters = parameters, drug = 2, timesteps = 1, - artemisinin_resistance = 0.3, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0, - early_treatment_failure_prob = 0.9, - late_clinical_failure_prob = 0, - late_parasitological_prob = 0, - reinfection_prob = 0, + artemisinin_resistance_proportion = 0.3, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = 0, + early_treatment_failure_probability = 0.9, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0, slow_parasite_clearance_time = 15) clinical_infections <- individual::Bitset$new(20)$insert(1:20) @@ -410,13 +410,13 @@ test_that('calculate_treated correctly samples treated and updates the drug stat mockery::expect_args( bernoulli_mock, 2, - 1 - (unlist(parameters$prop_artemisinin_resistant[c(2, 1, 1, 1, 2, 2, 2, 1, 2)]) * unlist(parameters$early_treatment_failure_prob[c(2, 1, 1, 1, 2, 2, 2, 1, 2)])) + 1 - (unlist(parameters$artemisinin_resistance_proportion[c(2, 1, 1, 1, 2, 2, 2, 1, 2)]) * unlist(parameters$early_treatment_failure_probability[c(2, 1, 1, 1, 2, 2, 2, 1, 2)])) ) mockery::expect_args( bernoulli_mock, 3, - unlist(parameters$prop_artemisinin_resistant[c(2, 1, 1, 1, 2, 2, 2)]) * unlist(parameters$slow_parasite_clearance_prob[c(2, 1, 1, 1, 2, 2, 2)]) + unlist(parameters$artemisinin_resistance_proportion[c(2, 1, 1, 1, 2, 2, 2)]) * unlist(parameters$slow_parasite_clearance_probability[c(2, 1, 1, 1, 2, 2, 2)]) ) expect_bitset_update(variables$state$queue_update, 'Tr', c(1, 2, 3, 4, 5, 6, 7)) @@ -441,13 +441,13 @@ test_that('calculate_treated correctly samples treated and updates the drug stat parameters <- set_antimalarial_resistance(parameters = parameters, drug = 2, timesteps = 1, - artemisinin_resistance = 0.8, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0.2, - early_treatment_failure_prob = 0.3, - late_clinical_failure_prob = 0, - late_parasitological_prob = 0, - reinfection_prob = 0, + artemisinin_resistance_proportion = 0.8, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = 0.2, + early_treatment_failure_probability = 0.3, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0, slow_parasite_clearance_time = 20) # Establish Bitset of clinically infected individuals @@ -586,17 +586,17 @@ test_that('calculate_treated correctly samples treated and updates the drug stat test_that('schedule_infections correctly schedules new infections', { parameters <- get_parameters(list(human_population = 20)) variables <- create_variables(parameters) - + infections <- individual::Bitset$new(20)$insert(1:20) clinical_infections <- individual::Bitset$new(20)$insert(5:15) treated <- individual::Bitset$new(20)$insert(7:12) - + infection_mock <- mockery::mock() asymp_mock <- mockery::mock() - + mockery::stub(schedule_infections, 'update_infection', infection_mock) mockery::stub(schedule_infections, 'update_to_asymptomatic_infection', asymp_mock) - + schedule_infections( variables, clinical_infections, @@ -605,15 +605,15 @@ test_that('schedule_infections correctly schedules new infections', { parameters, 42 ) - + actual_infected <- mockery::mock_args(infection_mock)[[1]][[5]]$to_vector() actual_asymp_infected <- mockery::mock_args(asymp_mock)[[1]][[4]]$to_vector() - + expect_equal( actual_infected, c(5, 6, 13, 14, 15) ) - + expect_equal( actual_asymp_infected, c(1, 2, 3, 4, 16, 17, 18, 19, 20) @@ -625,7 +625,7 @@ test_that('prophylaxis is considered for medicated humans', { parameters <- set_drugs(parameters, list(AL_params, DHA_PQP_params)) events <- create_events(parameters) timestep <- 50 - + variables = list( state = individual::CategoricalVariable$new( c('D', 'S', 'A', 'U', 'Tr'), @@ -637,11 +637,11 @@ test_that('prophylaxis is considered for medicated humans', { pev_profile = individual::IntegerVariable$new(c(-1, -1, -1, -1)), ib = individual::DoubleVariable$new(c(.2, .3, .5, .9)) ) - + bitten <- individual::Bitset$new(4)$insert(seq(4)) m <- mockery::mock(seq(3)) mockery::stub(calculate_infections, 'bernoulli_multi_p', m) - + calculate_infections( variables, bitten, @@ -649,7 +649,7 @@ test_that('prophylaxis is considered for medicated humans', { mock_render(timestep), timestep ) - + expect_equal( mockery::mock_args(m)[[1]][[1]], c(2.491951e-07, 2.384032e-01, 5.899334e-01), @@ -661,17 +661,17 @@ test_that('boost_immunity respects the delay period', { level <- c(2.4, 1.2, 0., 4.) immunity <- individual::DoubleVariable$new(level) last_boosted <- individual::DoubleVariable$new(c(11, 5, 1, 13)) - + level_mock <- mockery::mock() mockery::stub(boost_immunity, 'immunity_variable$queue_update', level_mock) - + last_mock <- mockery::mock() mockery::stub(boost_immunity, 'last_boosted_variable$queue_update', last_mock) - + index <- individual::Bitset$new(4)$insert(seq(4)) timestep <- 15 delay <- 4 - + boost_immunity( immunity, index, @@ -679,14 +679,14 @@ test_that('boost_immunity respects the delay period', { timestep, delay ) - + mockery::expect_args( level_mock, 1, c(3.4, 2.2, 1), seq(3) ) - + mockery::expect_args( last_mock, 1, @@ -699,18 +699,18 @@ test_that('boost_immunity respects the delay period', { level <- c(2.4, 1.2, 0., 4., 0.) immunity <- individual::DoubleVariable$new(level) last_boosted <- individual::DoubleVariable$new(c(11, 5, 1, 13, -1)) - + index <- individual::Bitset$new(5) index$insert(seq(5)) timestep <- 15 delay <- 4 - + level_mock <- mockery::mock() mockery::stub(boost_immunity, 'immunity_variable$queue_update', level_mock) - + last_mock <- mockery::mock() mockery::stub(boost_immunity, 'last_boosted_variable$queue_update', last_mock) - + boost_immunity( immunity, index, @@ -718,14 +718,14 @@ test_that('boost_immunity respects the delay period', { timestep, delay ) - + mockery::expect_args( level_mock, 1, c(3.4, 2.2, 1, 1), c(seq(3), 5) ) - + mockery::expect_args( last_mock, 1, @@ -738,18 +738,18 @@ test_that('boost_immunity does not update when there is no-one to update', { level <- c(2.4, 1.2, 0., 4., 0.) immunity <- individual::DoubleVariable$new(level) last_boosted <- individual::DoubleVariable$new(c(11, 5, 1, 13, -1)) - + index <- individual::Bitset$new(5) index$insert(seq(5)) timestep <- 15 delay <- 4 - + level_mock <- mockery::mock() mockery::stub(boost_immunity, 'immunity$queue_update', level_mock) - + last_mock <- mockery::mock() mockery::stub(boost_immunity, 'last_boosted$queue_update', last_mock) - + boost_immunity( immunity, index, @@ -771,11 +771,11 @@ test_that('update_severe_disease renders with no infections', { severe_incidence_rendering_max_ages = 5 * 365 )) variables <- create_variables(parameters) - + render_function <- mockery::mock() mockery::stub(update_severe_disease, 'incidence_renderer', render_function) empty <- individual::Bitset$new(population) - + update_severe_disease( timestep, empty, @@ -783,7 +783,7 @@ test_that('update_severe_disease renders with no infections', { parameters, renderer ) - + mockery::expect_args( render_function, 1, @@ -806,13 +806,13 @@ test_that('calculate_treated returns empty Bitset when there is no clinical trea parameters <- set_antimalarial_resistance(parameters = parameters, drug = 1, timesteps = 1, - artemisinin_resistance = 0.5, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0, - early_treatment_failure_prob = 0.2, - late_clinical_failure_prob = 0, - late_parasitological_prob = 0, - reinfection_prob = 0, + artemisinin_resistance_proportion = 0.5, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = 0, + early_treatment_failure_probability = 0.2, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0, slow_parasite_clearance_time = 10) clinical_infections <- individual::Bitset$new(20)$insert(1:20) timestep <- 5 @@ -842,13 +842,13 @@ test_that('calculate_treated returns empty Bitset when the clinically_infected i parameters <- set_antimalarial_resistance(parameters = parameters, drug = 1, timesteps = 1, - artemisinin_resistance = 0.5, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0, - early_treatment_failure_prob = 0.2, - late_clinical_failure_prob = 0, - late_parasitological_prob = 0, - reinfection_prob = 0, + artemisinin_resistance_proportion = 0.5, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = 0, + early_treatment_failure_probability = 0.2, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0, slow_parasite_clearance_time = 10) clinical_infections <- individual::Bitset$new(20) timestep <- 5 @@ -893,7 +893,7 @@ test_that('calculate_treated() returns an empty Bitset when the parameter list c expect_identical(object = treated$size(), expected = 0, info = "Error: calculate_treated() returning non-zero number of treated individuals in the absence of clinical treatment or resistance parameters") -}) + }) test_that('Number of treatment failures matches number of individuals treated when artemisinin resistance proportion and early treatment failure probability both set to 1', { @@ -912,24 +912,24 @@ test_that('Number of treatment failures matches number of individuals treated wh parameters <- set_antimalarial_resistance(parameters = parameters, drug = 1, timesteps = 1, - artemisinin_resistance = 1, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0, - early_treatment_failure_prob = 1, - late_clinical_failure_prob = 0, - late_parasitological_prob = 0, - reinfection_prob = 0, + artemisinin_resistance_proportion = 1, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = 0, + early_treatment_failure_probability = 1, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0, slow_parasite_clearance_time = 10) parameters <- set_antimalarial_resistance(parameters = parameters, drug = 2, timesteps = 1, - artemisinin_resistance = 1, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0, - early_treatment_failure_prob = 1, - late_clinical_failure_prob = 0, - late_parasitological_prob = 0, - reinfection_prob = 0, + artemisinin_resistance_proportion = 1, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = 0, + early_treatment_failure_probability = 1, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0, slow_parasite_clearance_time = 20) clinical_infections <- individual::Bitset$new(100) @@ -957,13 +957,13 @@ test_that('calculate_treated() successfully adds additional resistance columns t parameters <- set_antimalarial_resistance(parameters = parameters, drug = 1, timesteps = 1, - artemisinin_resistance = 0.5, - partner_drug_resistance = 0, - slow_parasite_clearance_prob = 0, - early_treatment_failure_prob = 0.5, - late_clinical_failure_prob = 0, - late_parasitological_prob = 0, - reinfection_prob = 0, + artemisinin_resistance_proportion = 0.5, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = 0, + early_treatment_failure_probability = 0.5, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0, slow_parasite_clearance_time = 10) clinical_infections <- individual::Bitset$new(20)$insert(1:20) diff --git a/vignettes/Antimalarial_Resistance.Rmd b/vignettes/Antimalarial_Resistance.Rmd index 9c873202..9c529950 100644 --- a/vignettes/Antimalarial_Resistance.Rmd +++ b/vignettes/Antimalarial_Resistance.Rmd @@ -30,7 +30,7 @@ One of the major threats to the continued success of efforts to reduce the burde Resistance to the artemisinin component of an ACT can result either in slow parasite clearance (SPC), in which treatment with an ACT takes longer than 3 days to fully clear patients with resistant parasites, or early treatment failure (ETF), in which the ACT fails to clear the infection and the individual develops a clinical infection. Resistance to the partner drug, where the partner drug fails to clear the parasite after the artemisinin derivative is depleted, results in infections recrudescing to either clinical (D) or asymptomatic infections (A). Resistance to the partner drug can also result in individuals developing a novel, resistant infection following treatment, as the prophylaxis provided by the ACT fails to protect the individual against reinfection by a resistant strain. In the following vignette, we illustrate how to parameterise and run `malariasimulation` simulations with resistance to ACTs deployed as a clinical treatment ## Using set_antimalarial_resistance() to parameterise resistance -Simulations capturing the effects of resistance to clinical treatment using antimalarial drugs are parameterised using the `set_antimalarial_resistance()` function. This function appends user-defined resistance parameters to a `malariasimulation` parameter list and accepts ten inputs. The first is a list of `malariasimulation` parameters to append the resistance parameters to, and the second the index of the `drug` for which resistance is being parameterised, as set using the `set_drugs()` function. The `set_antimalarial_resistance()` function requires the `timesteps`, `artemisinin_resistance`, `partner_drug_resistance`, `slow_parasite_clearance_prob`, `early_treatment_failure_prob`, `late_clinical_failure_prob`, `late_parasitological_failure_prob`, and `reinfection_prob` inputs to be of equal length so that, for each time step in which an update occurs, a value is available for each parameter. Finally, the `slow_parasite_clearance_time` parameter represents the mean residence time, in days, for artemisinin-resistant individuals experiencing slow parasite clearance (SPC) in the Treated compartment, and must be input as a single, positive value. +Simulations capturing the effects of resistance to clinical treatment using antimalarial drugs are parameterised using the `set_antimalarial_resistance()` function. This function appends user-defined resistance parameters to a `malariasimulation` parameter list and accepts ten inputs. The first is a list of `malariasimulation` parameters to append the resistance parameters to, and the second the index of the `drug` for which resistance is being parameterised, as set using the `set_drugs()` function. The `set_antimalarial_resistance()` function requires the `timesteps`, `artemisinin_resistance_proportion`, `partner_drug_resistance_proportion_proportion`, `slow_parasite_clearance_probability`, `early_treatment_failure_probability`, `late_clinical_failure_probability`, `late_parasitological_failure_prob`, and `reinfection_during_prophylaxis_probability` inputs to be of equal length so that, for each time step in which an update occurs, a value is available for each parameter. Finally, the `slow_parasite_clearance_time` parameter represents the mean residence time, in days, for artemisinin-resistant individuals experiencing slow parasite clearance (SPC) in the Treated compartment, and must be input as a single, positive value. ## Simulating static resistance To illustrate how to parameterise resistance to an ACT using the `set_antimalarial_resistance()` function, we'll set-up and run three simulations. The first simulates malaria transmission in the absence of interventions or resistance. The second simulates a simple regime of clinical treatment in which 80% of clinical cases are treated with artemether lumefantrine (AL), beginning after one year, in the absence of antimalarial resistance. The third simulates the same clinical treatment programme but with resistance to the artemisinin component of AL emerging after two years. For illustrative purposes, we assume that the proportion of infections resistant to the artemisinin component of AL increases from 0% to 80%, and that these infections have a 90% chance of resulting in early treatment failure. @@ -74,13 +74,13 @@ simparams_clin_treatment <- set_clinical_treatment(parameters = simparams_clin_t simparams_resistance <- set_antimalarial_resistance(parameters = simparams_clin_treatment, drug = 1, timesteps = c(0, resistance_start), - artemisinin_resistance = c(0, 0.8), - partner_drug_resistance = rep(0, 2), - slow_parasite_clearance_prob = rep(0, 2), - early_treatment_failure_prob = c(0, 0.9), - late_clinical_failure_prob = rep(0, 2), - late_parasitological_prob = rep(0, 2), - reinfection_prob = rep(0, 2), + artemisinin_resistance_proportion = c(0, 0.8), + partner_drug_resistance_proportion = rep(0, 2), + slow_parasite_clearance_probability = rep(0, 2), + early_treatment_failure_probability = c(0, 0.9), + late_clinical_failure_probability = rep(0, 2), + late_parasitological_failure_probability = rep(0, 2), + reinfection_during_prophylaxis_probability = rep(0, 2), slow_parasite_clearance_time = 10) ``` @@ -195,13 +195,13 @@ simparams <- set_clinical_treatment(parameters = simparams, simparams <- set_antimalarial_resistance(parameters = simparams, drug = 1, timesteps = resistance_update_timesteps, - artemisinin_resistance = resistance_updates, - partner_drug_resistance = rep(0, length(resistance_update_timesteps)), - slow_parasite_clearance_prob = rep(0, length(resistance_update_timesteps)), - early_treatment_failure_prob = early_treatment_failure_updates, - late_clinical_failure_prob = rep(0, length(resistance_update_timesteps)), - late_parasitological_prob = rep(0, length(resistance_update_timesteps)), - reinfection_prob = rep(0, length(resistance_update_timesteps)), + artemisinin_resistance_proportion = resistance_updates, + partner_drug_resistance_proportion = rep(0, length(resistance_update_timesteps)), + slow_parasite_clearance_probability = rep(0, length(resistance_update_timesteps)), + early_treatment_failure_probability = early_treatment_failure_updates, + late_clinical_failure_probability = rep(0, length(resistance_update_timesteps)), + late_parasitological_failure_probability = rep(0, length(resistance_update_timesteps)), + reinfection_during_prophylaxis_probability = rep(0, length(resistance_update_timesteps)), slow_parasite_clearance_time = 10) # Calibrate the parameters to an initial EIR: @@ -290,39 +290,39 @@ simulation_parameters$base |> simulation_parameters$treatment |> set_antimalarial_resistance(drug = 1, timesteps = c(0, (365 * 3) + 1), - artemisinin_resistance = c(0, 0.8), - partner_drug_resistance = c(0, 0), - slow_parasite_clearance_prob = c(0, 0), - early_treatment_failure_prob = c(0, 0.8), - late_clinical_failure_prob = c(0, 0), - late_parasitological_prob = c(0, 0), - reinfection_prob = c(0, 0), + artemisinin_resistance_proportion = c(0, 0.8), + partner_drug_resistance_proportion = c(0, 0), + slow_parasite_clearance_probability = c(0, 0), + early_treatment_failure_probability = c(0, 0.8), + late_clinical_failure_probability = c(0, 0), + late_parasitological_failure_probability = c(0, 0), + reinfection_during_prophylaxis_probability = c(0, 0), slow_parasite_clearance_time = 10) -> simulation_parameters$etf # Establish a parameter list with clinical treatment and slow parasite clearance simulation_parameters$treatment |> set_antimalarial_resistance(drug = 1, timesteps = c(0, (365 * 3) + 1), - artemisinin_resistance = c(0, 0.8), - partner_drug_resistance = c(0, 0), - slow_parasite_clearance_prob = c(0, 0.8), - early_treatment_failure_prob = c(0, 0), - late_clinical_failure_prob = c(0, 0), - late_parasitological_prob = c(0, 0), - reinfection_prob = c(0, 0), + artemisinin_resistance_proportion = c(0, 0.8), + partner_drug_resistance_proportion = c(0, 0), + slow_parasite_clearance_probability = c(0, 0.8), + early_treatment_failure_probability = c(0, 0), + late_clinical_failure_probability = c(0, 0), + late_parasitological_failure_probability = c(0, 0), + reinfection_during_prophylaxis_probability = c(0, 0), slow_parasite_clearance_time = 10) -> simulation_parameters$spc # Establish a parameter list with clinical treatment, early treatment failure and slow parasite clearance: simulation_parameters$treatment |> set_antimalarial_resistance(drug = 1, timesteps = c(0, (365 * 3) + 1), - artemisinin_resistance = c(0, 0.8), - partner_drug_resistance = c(0, 0), - slow_parasite_clearance_prob = c(0, 0.8), - early_treatment_failure_prob = c(0, 0.8), - late_clinical_failure_prob = c(0, 0), - late_parasitological_prob = c(0, 0), - reinfection_prob = c(0, 0), + artemisinin_resistance_proportion = c(0, 0.8), + partner_drug_resistance_proportion = c(0, 0), + slow_parasite_clearance_probability = c(0, 0.8), + early_treatment_failure_probability = c(0, 0.8), + late_clinical_failure_probability = c(0, 0), + late_parasitological_failure_probability = c(0, 0), + reinfection_during_prophylaxis_probability = c(0, 0), slow_parasite_clearance_time = 10) -> simulation_parameters$etf_spc ``` @@ -360,7 +360,7 @@ for(i in seq(length(simulation_parameters))) { ### Visualisation -We can compare the effects of independent resistance outcomes with combined resistance outcomes visually. +We can compare the effects of independent resistance outcomes with combined resistance outcomes visually. In the following plot, we compare the *Pf*PR~2-10~ between a baseline without any clinical treatment or antimalarial resistance, a clinical-treatment only run, a clinical treatment with early treatment failure run, a clinical treatment with slow parasite clearance run, and a clinical treatment with both early treatment failure and slow parasite clearance run. ```{R} From d9ce4a68d1c88163ba487c9150199a856edb28e7 Mon Sep 17 00:00:00 2001 From: tbreweric Date: Thu, 11 Apr 2024 15:36:24 +0100 Subject: [PATCH 5/6] Implemented the changes recommended by in first round of PR comments --- R/disease_progression.R | 2 +- R/human_infection.R | 6 +- R/processes.R | 2 +- R/variables.R | 2 +- tests/testthat/test-infection-integration.R | 144 ++++++++++---------- vignettes/Antimalarial_Resistance.Rmd | 4 - 6 files changed, 78 insertions(+), 82 deletions(-) diff --git a/R/disease_progression.R b/R/disease_progression.R index e8b35874..455f79e0 100644 --- a/R/disease_progression.R +++ b/R/disease_progression.R @@ -33,7 +33,7 @@ create_progression_process <- function( # If the length of rate is greater than 1 (when it's a variable): if (length(rate) > 1) { - rate <- rate$get_values(index$to_vector()) + rate <- rate$get_values(index) } # Sample the individuals to be moved into a new Bitset using the transition rate(s): diff --git a/R/human_infection.R b/R/human_infection.R index 569757ef..d62b16f0 100644 --- a/R/human_infection.R +++ b/R/human_infection.R @@ -320,7 +320,7 @@ calculate_treated <- function( n_early_treatment_failure <- effectively_treated$size() - successfully_treated$size() renderer$render('n_early_treatment_failure', n_early_treatment_failure, timestep) drugs <- drugs[successfully_treated_indices] - resistance_parameters$dt_slow_parasite_clearance <- resistance_parameters$dt_slow_parasite_clearance[successfully_treated_indices] + dt_slow_parasite_clearance <- resistance_parameters$dt_slow_parasite_clearance[successfully_treated_indices] #+++ SLOW PARASITE CLEARANCE +++# #+++++++++++++++++++++++++++++++# @@ -331,7 +331,7 @@ calculate_treated <- function( renderer$render('n_slow_parasite_clearance', slow_parasite_clearance_individuals$size(), timestep) non_slow_parasite_clearance_individuals <- successfully_treated$copy()$set_difference(slow_parasite_clearance_individuals) renderer$render('n_successfully_treated', successfully_treated$size(), timestep) - resistance_parameters$dt_slow_parasite_clearance <- resistance_parameters$dt_slow_parasite_clearance[slow_parasite_clearance_indices] + dt_slow_parasite_clearance <- dt_slow_parasite_clearance[slow_parasite_clearance_indices] } else { @@ -360,7 +360,7 @@ calculate_treated <- function( non_slow_parasite_clearance_individuals ) variables$dt$queue_update( - resistance_parameters$dt_slow_parasite_clearance, + dt_slow_parasite_clearance, slow_parasite_clearance_individuals ) } diff --git a/R/processes.R b/R/processes.R index bbd7a754..5593a74d 100644 --- a/R/processes.R +++ b/R/processes.R @@ -118,7 +118,7 @@ create_processes <- function( dt_input <- parameters$dt # If antimalarial resistance is switched on, assign dt variable values to the - if(parameters$antimalarial_resistance == TRUE) { + if(parameters$antimalarial_resistance) { dt_input <- variables$dt } diff --git a/R/variables.R b/R/variables.R index 077a4f29..092a6431 100644 --- a/R/variables.R +++ b/R/variables.R @@ -231,7 +231,7 @@ create_variables <- function(parameters) { ) # Add variables for antimalarial resistance state residency times (dt) - if(parameters$antimalarial_resistance == TRUE) { + if(parameters$antimalarial_resistance) { dt <- individual::DoubleVariable$new(rep(parameters$dt, size)) variables <- c( variables, diff --git a/tests/testthat/test-infection-integration.R b/tests/testthat/test-infection-integration.R index 2f05c463..83d7def9 100644 --- a/tests/testthat/test-infection-integration.R +++ b/tests/testthat/test-infection-integration.R @@ -873,82 +873,82 @@ test_that('calculate_treated returns empty Bitset when the clinically_infected i test_that('calculate_treated() returns an empty Bitset when the parameter list contains no clinical treatment or resistance parameters', { - parameters <- get_parameters() - clinical_infections <- individual::Bitset$new(20)$insert(1:20) - timestep <- 5 - events <- create_events(parameters) - variables <- list( - state = list(queue_update = mockery::mock()), - infectivity = list(queue_update = mockery::mock()), - drug = list(queue_update = mockery::mock()), - drug_time = list(queue_update = mockery::mock()) - ) - renderer <- individual::Render$new(timesteps = 10) - - treated <- calculate_treated(variables = variables, - clinical_infections = clinical_infections, - parameters = parameters, - timestep = timestep, - renderer = renderer) - - expect_identical(object = treated$size(), expected = 0, info = "Error: calculate_treated() returning non-zero number of treated individuals + parameters <- get_parameters() + clinical_infections <- individual::Bitset$new(20)$insert(1:20) + timestep <- 5 + events <- create_events(parameters) + variables <- list( + state = list(queue_update = mockery::mock()), + infectivity = list(queue_update = mockery::mock()), + drug = list(queue_update = mockery::mock()), + drug_time = list(queue_update = mockery::mock()) + ) + renderer <- individual::Render$new(timesteps = 10) + + treated <- calculate_treated(variables = variables, + clinical_infections = clinical_infections, + parameters = parameters, + timestep = timestep, + renderer = renderer) + + expect_identical(object = treated$size(), expected = 0, info = "Error: calculate_treated() returning non-zero number of treated individuals in the absence of clinical treatment or resistance parameters") - }) +}) -test_that('Number of treatment failures matches number of individuals treated when artemisinin resistance proportion and - early treatment failure probability both set to 1', { - parameters <- get_parameters() - parameters <- set_drugs(parameters = parameters, drugs = list(AL_params, SP_AQ_params)) - parameters <- set_clinical_treatment(parameters = parameters, - drug = 1, - timesteps = 1, - coverages = round(runif(1, 0, 1/2), - digits = 2)) - parameters <- set_clinical_treatment(parameters = parameters, - drug = 2, - timesteps = 1, - coverages = round(runif(1, 0, 1/2), - digits = 2)) - parameters <- set_antimalarial_resistance(parameters = parameters, - drug = 1, - timesteps = 1, - artemisinin_resistance_proportion = 1, - partner_drug_resistance_proportion = 0, - slow_parasite_clearance_probability = 0, - early_treatment_failure_probability = 1, - late_clinical_failure_probability = 0, - late_parasitological_failure_probability = 0, - reinfection_during_prophylaxis_probability = 0, - slow_parasite_clearance_time = 10) - parameters <- set_antimalarial_resistance(parameters = parameters, - drug = 2, - timesteps = 1, - artemisinin_resistance_proportion = 1, - partner_drug_resistance_proportion = 0, - slow_parasite_clearance_probability = 0, - early_treatment_failure_probability = 1, - late_clinical_failure_probability = 0, - late_parasitological_failure_probability = 0, - reinfection_during_prophylaxis_probability = 0, - slow_parasite_clearance_time = 20) - - clinical_infections <- individual::Bitset$new(100) - clinical_infections$insert(sample.int(n = 100, size = round(runif(n = 1, min = 10, max = 100)), replace = FALSE)) - timestep <- 5 - events <- create_events(parameters) - variables <- create_variables(parameters = parameters) - renderer <- individual::Render$new(timesteps = 10) - - treated <- calculate_treated(variables = variables, - clinical_infections = clinical_infections, - parameters = parameters, - timestep = timestep, - renderer = renderer) - - expect_identical(renderer$to_dataframe()[timestep,'n_early_treatment_failure'], renderer$to_dataframe()[timestep,'n_treated'] - renderer$to_dataframe()[timestep,'n_drug_efficacy_failures'], info = "Error: Number of +test_that('Number of treatment failures matches number of individuals treated when artemisinin resistance + proportion and early treatment failure probability both set to 1', { + parameters <- get_parameters() + parameters <- set_drugs(parameters = parameters, drugs = list(AL_params, SP_AQ_params)) + parameters <- set_clinical_treatment(parameters = parameters, + drug = 1, + timesteps = 1, + coverages = round(runif(1, 0, 1/2), + digits = 2)) + parameters <- set_clinical_treatment(parameters = parameters, + drug = 2, + timesteps = 1, + coverages = round(runif(1, 0, 1/2), + digits = 2)) + parameters <- set_antimalarial_resistance(parameters = parameters, + drug = 1, + timesteps = 1, + artemisinin_resistance_proportion = 1, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = 0, + early_treatment_failure_probability = 1, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0, + slow_parasite_clearance_time = 10) + parameters <- set_antimalarial_resistance(parameters = parameters, + drug = 2, + timesteps = 1, + artemisinin_resistance_proportion = 1, + partner_drug_resistance_proportion = 0, + slow_parasite_clearance_probability = 0, + early_treatment_failure_probability = 1, + late_clinical_failure_probability = 0, + late_parasitological_failure_probability = 0, + reinfection_during_prophylaxis_probability = 0, + slow_parasite_clearance_time = 20) + + clinical_infections <- individual::Bitset$new(100) + clinical_infections$insert(sample.int(n = 100, size = round(runif(n = 1, min = 10, max = 100)), replace = FALSE)) + timestep <- 5 + events <- create_events(parameters) + variables <- create_variables(parameters = parameters) + renderer <- individual::Render$new(timesteps = 10) + + treated <- calculate_treated(variables = variables, + clinical_infections = clinical_infections, + parameters = parameters, + timestep = timestep, + renderer = renderer) + + expect_identical(renderer$to_dataframe()[timestep,'n_early_treatment_failure'], renderer$to_dataframe()[timestep,'n_treated'] - renderer$to_dataframe()[timestep,'n_drug_efficacy_failures'], info = "Error: Number of early treatment failures does not match number of treated individuals (minus drug efficacy failures) when artemisinin resistance proportion and and early treatment failure probability both equal 1") - }) +}) test_that('calculate_treated() successfully adds additional resistance columns to the renderer', { parameters <- get_parameters() diff --git a/vignettes/Antimalarial_Resistance.Rmd b/vignettes/Antimalarial_Resistance.Rmd index 9c529950..5b6acd40 100644 --- a/vignettes/Antimalarial_Resistance.Rmd +++ b/vignettes/Antimalarial_Resistance.Rmd @@ -354,10 +354,6 @@ for(i in seq(length(simulation_parameters))) { ``` -### Data Wranging - - - ### Visualisation We can compare the effects of independent resistance outcomes with combined resistance outcomes visually. In the following plot, we compare the *Pf*PR~2-10~ between a baseline without any clinical treatment or antimalarial resistance, a clinical-treatment only run, a clinical treatment with early treatment failure run, a clinical treatment with slow parasite clearance run, and a clinical treatment with both early treatment failure and slow parasite clearance run. From f58d48f7283a25616440e788583ebfadc5ef52b2 Mon Sep 17 00:00:00 2001 From: tbreweric Date: Thu, 18 Apr 2024 22:10:46 +0100 Subject: [PATCH 6/6] Altered check for rate data type in create_disease_progression() function --- R/disease_progression.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/disease_progression.R b/R/disease_progression.R index 455f79e0..d9ac11e4 100644 --- a/R/disease_progression.R +++ b/R/disease_progression.R @@ -32,7 +32,7 @@ create_progression_process <- function( index <- state$get_index_of(from_state) # If the length of rate is greater than 1 (when it's a variable): - if (length(rate) > 1) { + if (inherits(rate, "DoubleVariable")) { rate <- rate$get_values(index) }