Skip to content

Commit

Permalink
Merge branch 'dev' into intervention-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
plietar authored Apr 26, 2024
2 parents 4993cdf + 380d113 commit e284e1b
Show file tree
Hide file tree
Showing 15 changed files with 782 additions and 593 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Suggests:
ggplot2,
covr,
mgcv
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Roxygen: list(markdown = TRUE)
LinkingTo:
Rcpp,
Expand Down
95 changes: 47 additions & 48 deletions R/antimalarial_resistance.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +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,
slow_parasite_clearance_prob > 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")
}

Expand Down Expand Up @@ -81,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)
Expand Down Expand Up @@ -122,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]]
}

Expand All @@ -144,4 +143,4 @@ get_antimalarial_resistance_parameters <- function(parameters, drugs, timestep)

return(resistance_parameters)

}
}
28 changes: 20 additions & 8 deletions R/disease_progression.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 (inherits(rate, "DoubleVariable")) {
rate <- rate$get_values(index)
}

# 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,
Expand Down
67 changes: 49 additions & 18 deletions R/human_infection.R
Original file line number Diff line number Diff line change
Expand Up @@ -295,50 +295,81 @@ 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]
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)
dt_slow_parasite_clearance <- 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(
dt_slow_parasite_clearance,
slow_parasite_clearance_individuals
)
}
}

successfully_treated

}


#' @title Schedule infections
#' @description
#' Schedule infections in humans after the incubation period
Expand Down
7 changes: 4 additions & 3 deletions R/model.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions R/mortality_processes.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
}
Expand Down
Loading

0 comments on commit e284e1b

Please sign in to comment.