Skip to content

Commit

Permalink
Implented the second round of changes requested in the pull request
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Brewer committed Feb 8, 2024
1 parent ad20bdf commit c71e9b0
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 48 deletions.
100 changes: 79 additions & 21 deletions R/antimalarial_resistance.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#' @title Parameterise antimalarial resistance
#'
#' @description
#' Parameterise antimalarial resistance
#'
#' @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
Expand All @@ -10,6 +12,7 @@
#' @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 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,
Expand All @@ -20,9 +23,9 @@ set_antimalarial_resistance <- function(parameters,
early_treatment_failure_prob,
late_clinical_failure_prob,
late_parasitological_prob,
reinfection_prob) {
reinfection_prob,
slow_parasite_clearance_time) {

# Check that the number of values input is equal for each resistance parameter
if(any(c(length(artemisinin_resistance),
length(partner_drug_resistance),
length(slow_parasite_clearance_prob),
Expand All @@ -33,59 +36,114 @@ set_antimalarial_resistance <- function(parameters,
stop("Length of one or more resistance parameter vectors does not match time steps specified for update")
}

# Ensure resistance proportions bounded between 0 and 1:
if(any(artemisinin_resistance < 0 | artemisinin_resistance > 1 |
if(any(artemisinin_resistance < 0 | artemisinin_resistance > 1 |
partner_drug_resistance < 0 | partner_drug_resistance > 1)) {
stop("Artemisinin and partner-drug resistance proportions must fall between 0 and 1")
}

# Ensure resistance outcome probabilities bounded 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)) {
stop("Resistance outcome probabilities must fall between 0 and 1")
}

if(length(slow_parasite_clearance_time) != 1) {
stop("Error: length of slow_parasite_clearance_time not equal to 1")
}

if(slow_parasite_clearance_time <= 0) {
stop("Error: slow_parasite_clearance_time is non-positive")
}

# Set antimalarial_resistance to TRUE:
parameters$antimalarial_resistance <- TRUE

# Store the number of drugs for which parameters are available in the parameter list:
n_drugs <- length(parameters$drug_efficacy)

# If the drug index falls outside range 1:n_drugs, terminate the operation:
if (drug < 1 | drug > n_drugs) {
stop('Drug index is invalid, please set drugs using set_drugs')
}

# Check the drug_index for the drug we're setting parameters for:
drug_index <- which(parameters$antimalarial_resistance_drug == drug)

# If drug_index not already assigned, assign the drug the next available index:
if (length(drug_index) == 0) {
drug_index <- length(parameters$antimalarial_resistance_drug) + 1
}

# Append the drug for which resistance is being assigned to the generated index:
parameters$antimalarial_resistance_drug[[drug_index]] <- drug

# Append the timesteps on which the resistance proportions are to be updated:
parameters$antimalarial_resistance_timesteps[[drug_index]] <- timesteps

# Append the proportions of all malarial infections that are artemisinin or partner-drug
# resistant:
parameters$prop_artemisinin_resistant[[drug_index]] <- artemisinin_resistance
parameters$prop_partner_drug_resistant[[drug_index]] <- partner_drug_resistance

# Append the probabilities that individuals will experience the resistance outcomes:
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$dt_slow_parasite_clearance[[drug_index]] <- slow_parasite_clearance_time

return(parameters)

}

#' @title Retrieve resistance parameters
#' @description
#' Retrieve the resistance parameters associated with the drug each individual receiving clinical
#' treatment has been administered in the current time step.
#'
#' @param parameters the model parameters
#' @param drug vector of integers representing the drugs administered to each individual receiving treatment
#' @param timestep the current time step
get_antimalarial_resistance_parameters <- function(parameters, drugs, timestep) {

if(!parameters$antimalarial_resistance) {
stop("Error: Antimalarial resistance has not been parameterised; antimalarial_resistance = FALSE")
}

artemisinin_resistance_proportion <- numeric(length = length(drugs))
partner_drug_resistance_proportion <- numeric(length = length(drugs))
slow_parasite_clearance_probability <- numeric(length = length(drugs))
early_treatment_failure_probability <- numeric(length = length(drugs))
late_clinical_failure_probability <- numeric(length = length(drugs))
late_parasitological_failure_probability <- numeric(length = length(drugs))
reinfection_during_prophylaxis_probability <- numeric(length = length(drugs))
dt_slow_parasite_clearance <- rep(parameters$dt, length = length(drugs))

for(i in seq_along(parameters$antimalarial_resistance_drug)) {
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]
dt_slow_parasite_clearance[treated_with_drug] <- parameters$dt_slow_parasite_clearance[[i]]
}

resistance_parameters <- list()
resistance_parameters$artemisinin_resistance_proportion <- artemisinin_resistance_proportion
resistance_parameters$partner_drug_resistance_proportion <- partner_drug_resistance_proportion
resistance_parameters$slow_parasite_clearance_probability <- slow_parasite_clearance_probability
resistance_parameters$early_treatment_failure_probability <- early_treatment_failure_probability
resistance_parameters$late_clinical_failure_probability <- late_clinical_failure_probability
resistance_parameters$late_parasitological_failure_probability <- late_parasitological_failure_probability
resistance_parameters$reinfection_during_prophylaxis_probability <- reinfection_during_prophylaxis_probability
resistance_parameters$dt_slow_parasite_clearance <- dt_slow_parasite_clearance

if(any(c(length(resistance_parameters$artemisinin_resistance_proportion),
length(resistance_parameters$partner_drug_resistance_proportion),
length(resistance_parameters$slow_parasite_clearance_probability),
length(resistance_parameters$early_treatment_failure_probability),
length(resistance_parameters$late_clinical_failure_probability),
length(resistance_parameters$late_parasitological_failure_probability),
length(resistance_parameters$reinfection_during_prophylaxis_probability),
length(resistance_parameters$dt_slow_parasite_clearance)) != length(drugs))) {
stop("Length of one or more resistance parameter vectors does not match length of drugs vector")
}

# Return the parameter list:
parameters
return(resistance_parameters)

}
27 changes: 8 additions & 19 deletions R/human_infection.R
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,6 @@ calculate_treated <- function(
renderer
) {

renderer$render('n_clin_infected', clinical_infections$size(), timestep)

if(clinical_infections$size() == 0) {
return(individual::Bitset$new(parameters$human_population))
}
Expand All @@ -296,27 +294,18 @@ calculate_treated <- function(
)
])

if(parameters$antimalarial_resistance == TRUE) {
antimalarial_resistance_drug_index <- as.numeric(parameters$antimalarial_resistance_drug)[drugs]
artemisinin_resistance_proportion <- vector()
early_treatment_failure_probability <- vector()
drug_indices <- list(); drug_index <- vector()

for(i in seq_along(parameters$antimalarial_resistance_drug)) {
drug_indices[[i]] <- which(drugs == i)
drug_index[i] <- which(parameters$antimalarial_resistance_drug == i)
matched_t <- match_timestep(ts = parameters$antimalarial_resistance_timesteps[[drug_index[i]]], t = timestep)
artemisinin_resistance_proportion[drug_indices[[i]]] <- parameters$prop_artemisinin_resistant[[drug_index[i]]][matched_t]
early_treatment_failure_probability[drug_indices[[i]]] <- parameters$early_treatment_failure_prob[[drug_index[i]]][matched_t]
}

unsuccessful_treatment_probability <- artemisinin_resistance_proportion * early_treatment_failure_probability
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)
drugs <- drugs[susceptible_to_treatment_index]
susceptible_to_treatment <- bitset_at(seek_treatment, susceptible_to_treatment_index)
drugs <- drugs[susceptible_to_treatment_index]
n_early_treatment_failure <- n_treat - susceptible_to_treatment$size()
renderer$render('n_early_treatment_failure', n_early_treatment_failure, timestep)

} else {
susceptible_to_treatment <- seek_treatment
n_early_treatment_failure <- n_treat - susceptible_to_treatment$size()
Expand Down
3 changes: 1 addition & 2 deletions R/model.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,9 @@
#' susceptible
#' * net_usage: the number people protected by a bed net
#' * mosquito_deaths: number of adult female mosquitoes who die this timestep
#' * n_clin_infected: number of new clinically infected individuals in 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 newly successfully treated individuals in this timestep
#' * n_treat_success: number of successfully treated individuals in this timestep
#'
#' @param timesteps the number of timesteps to run the simulation for (in days)
#' @param parameters a named list of parameters to use
Expand Down
1 change: 1 addition & 0 deletions R/parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@
#' * 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
#' * dt_slow_parasite_clearance - the delay for humans experiencing slow parasite clearance to move from state Tr to S; default = NULL
#'
#' rendering:
#' All values are in timesteps and all ranges are inclusive
Expand Down
19 changes: 19 additions & 0 deletions man/get_antimalarial_resistance_parameters.Rd

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

1 change: 1 addition & 0 deletions man/get_parameters.Rd

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

3 changes: 1 addition & 2 deletions man/run_simulation.Rd

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

5 changes: 4 additions & 1 deletion man/set_antimalarial_resistance.Rd

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

Loading

0 comments on commit c71e9b0

Please sign in to comment.