Skip to content

Commit

Permalink
Resolved remaining merge conflict issues and renamed some probs to ra…
Browse files Browse the repository at this point in the history
…tes in competing hazards resolution function (including conversion of rates to probs).

Also ran the line ending git line, so testing to see if this has done the trick...?
  • Loading branch information
RJSheppard committed May 8, 2024
1 parent 903547a commit 91d50f4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 109 deletions.
6 changes: 3 additions & 3 deletions R/competing_hazards.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ CompetingHazard <- R6::R6Class(
'cbind',
lapply(private$outcomes, function(x) x$rates)
)
occur_prob <- rowSums(event_probs)
occur_rates <- rowSums(event_rates)
occur_rng <- private$rng(private$size)
occurs <- occur_rng < occur_prob
norm_probs <- event_probs / occur_prob
occurs <- occur_rng < rate_to_prob(occur_rates)
norm_probs <- event_rates / occur_rates
norm_probs[is.na(norm_probs)] <- 0
cum_probs <- apply(norm_probs, 1, cumsum)
event_rng <- private$rng(private$size)
Expand Down
121 changes: 15 additions & 106 deletions R/human_infection.R
Original file line number Diff line number Diff line change
Expand Up @@ -200,91 +200,6 @@ infection_process_resolved_hazard <- function(
)
}

<<<<<<< HEAD
#' @title Calculate overall infections for bitten humans
#' @description
#' Sample infected humans given prophylaxis and vaccination
#' @param variables a list of all of the model variables
#' @param bitten_humans bitset of bitten humans
#' @param parameters model parameters
#' @param renderer model render object
#' @param timestep current timestep
#' @noRd
calculate_infections <- function(
variables,
bitten_humans,
parameters,
renderer,
timestep
) {
source_humans <- variables$state$get_index_of(
c('S', 'A', 'U'))$and(bitten_humans)

b <- blood_immunity(variables$ib$get_values(source_humans), parameters)

source_vector <- source_humans$to_vector()

# calculate prophylaxis
prophylaxis <- rep(0, length(source_vector))
drug <- variables$drug$get_values(source_vector)
medicated <- (drug > 0)
if (any(medicated)) {
drug <- drug[medicated]
drug_time <- variables$drug_time$get_values(source_vector[medicated])
prophylaxis[medicated] <- weibull_survival(
timestep - drug_time,
parameters$drug_prophylaxis_shape[drug],
parameters$drug_prophylaxis_scale[drug]
)
}

# calculate vaccine efficacy
vaccine_efficacy <- rep(0, length(source_vector))
vaccine_times <- variables$last_eff_pev_timestep$get_values(source_vector)
pev_profile <- variables$pev_profile$get_values(source_vector)
# get vector of individuals who have received their 3rd dose
vaccinated <- vaccine_times > -1
pev_profile <- pev_profile[vaccinated]
if (length(vaccinated) > 0) {
antibodies <- calculate_pev_antibodies(
timestep - vaccine_times[vaccinated],
exp(sample_pev_param(pev_profile, parameters$pev_profiles, 'cs')),
invlogit(sample_pev_param(pev_profile, parameters$pev_profiles, 'rho')),
exp(sample_pev_param(pev_profile, parameters$pev_profiles, 'ds')),
exp(sample_pev_param(pev_profile, parameters$pev_profiles, 'dl')),
parameters
)
vmax <- vnapply(parameters$pev_profiles, function(p) p$vmax)
beta <- vnapply(parameters$pev_profiles, function(p) p$beta)
alpha <- vnapply(parameters$pev_profiles, function(p) p$alpha)
vaccine_efficacy[vaccinated] <- calculate_pev_efficacy(
antibodies,
vmax[pev_profile],
beta[pev_profile],
alpha[pev_profile]
)
}

prob <- b * (1 - prophylaxis) * (1 - vaccine_efficacy)
infected <- bitset_at(source_humans, bernoulli_multi_p(prob))

incidence_renderer(
variables$birth,
renderer,
infected,
source_humans,
prob,
'inc_',
parameters$incidence_rendering_min_ages,
parameters$incidence_rendering_max_ages,
timestep
)

infected
}
=======
>>>>>>> d694b08 (Infection and recovery competing hazard resolution.)

#' @title Calculate clinical infections
#' @description
#' Sample clinical infections from all infections
Expand Down Expand Up @@ -382,28 +297,22 @@ calculate_treated <- function(
timestep,
renderer
) {
<<<<<<< HEAD


if(clinical_infections$size() == 0) {
return(individual::Bitset$new(parameters$human_population))
}

=======
>>>>>>> d694b08 (Infection and recovery competing hazard resolution.)

treatment_coverages <- get_treatment_coverages(parameters, timestep)
ft <- sum(treatment_coverages)

if (ft == 0) {
return(individual::Bitset$new(parameters$human_population))
}

renderer$render('ft', ft, timestep)
seek_treatment <- sample_bitset(clinical_infections, ft)
n_treat <- seek_treatment$size()
<<<<<<< HEAD
=======

>>>>>>> d694b08 (Infection and recovery competing hazard resolution.)
renderer$render('n_treated', n_treat, timestep)

drugs <- as.numeric(parameters$clinical_treatment_drugs[
Expand All @@ -414,24 +323,24 @@ calculate_treated <- function(
replace = TRUE
)
])

#+++ 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,
drugs = drugs,
timestep = timestep
)

#+++ EARLY TREATMENT FAILURE +++#
#+++++++++++++++++++++++++++++++#
early_treatment_failure_probability <- resistance_parameters$artemisinin_resistance_proportion * resistance_parameters$early_treatment_failure_probability
Expand All @@ -441,7 +350,7 @@ calculate_treated <- function(
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] *
Expand All @@ -452,14 +361,14 @@ calculate_treated <- function(
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 {

successfully_treated <- effectively_treated
renderer$render('n_successfully_treated', successfully_treated$size(), timestep)

}

if (successfully_treated$size() > 0) {
variables$state$queue_update("Tr", successfully_treated)
variables$infectivity$queue_update(
Expand All @@ -485,9 +394,9 @@ calculate_treated <- function(
)
}
}

successfully_treated

}

#' @title Schedule infections
Expand Down

0 comments on commit 91d50f4

Please sign in to comment.