Skip to content

Commit

Permalink
Removed code redundancies. Adjusted extra arguments inputted into com…
Browse files Browse the repository at this point in the history
…peting hazards object and reverting to previous competing hazard tests.
  • Loading branch information
RJSheppard committed Oct 15, 2024
1 parent 3b833ff commit 7166cba
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 32 deletions.
2 changes: 1 addition & 1 deletion R/competing_hazards.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ CompetingOutcome <- R6::R6Class(
self$args <- list(...)
},
execute = function(t, target){
do.call(private$targeted_process, list(t, target, self$args))
do.call(private$targeted_process, c(t, list(target), self$args))
},
reset = function() {
self$target$clear()
Expand Down
5 changes: 1 addition & 4 deletions R/human_infection.R
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,6 @@ calculate_vivax_infections <- function(
infection_rates[hyp_source_index] <- infection_rates[hyp_source_index] + relapse_rates
# Get and store relative rates for bite/relapse competing hazards resolution
relative_rates <- relapse_rates/infection_rates[hyp_source_index]
# infection_outcome$set_relative_rates(
# hypnozoites_humans,
# relative_rates)
}

prob <- rate_to_prob(infection_rates) * (1 - prophylaxis) * (1 - vaccine_efficacy)
Expand Down Expand Up @@ -944,7 +941,7 @@ severe_immunity <- function(age, acquired_immunity, maternal_immunity, parameter
parameters$theta0 * (parameters$theta1 + (1 - parameters$theta1) / (
1 + fv * (
(acquired_immunity + maternal_immunity) / parameters$iv0) ** parameters$kv
)
)
)
}

Expand Down
8 changes: 4 additions & 4 deletions R/processes.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ create_processes <- function(

if(parameters$parasite == "falciparum"){
infection_outcome <- CompetingOutcome$new(
targeted_process = function(timestep, target, args){
targeted_process = function(timestep, target){
falciparum_infection_outcome_process(timestep, target,
variables, renderer, parameters
)
Expand All @@ -106,18 +106,18 @@ create_processes <- function(

} else if (parameters$parasite == "vivax"){
infection_outcome <- CompetingOutcome$new(
targeted_process = function(timestep, target, args){
targeted_process = function(timestep, target, relative_rates){
vivax_infection_outcome_process(timestep, target,
variables, renderer, parameters,
args$relative_rates
relative_rates
)
},
size = parameters$human_population
)
}

progression_outcome <- CompetingOutcome$new(
targeted_process = function(timestep, target, ...){
targeted_process = function(timestep, target){
progression_outcome_process(timestep, target, variables, parameters, renderer)
},
size = parameters$human_population
Expand Down
38 changes: 15 additions & 23 deletions tests/testthat/test-competing-hazards.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ test_that("hazard resolves two disjoint outcomes", {

outcome_1$set_rates(population, c(10, 0, 10, 0))
outcome_2$set_rates(population, c(0, 10, 0, 10))

hazard$resolve(0)

mockery::expect_args(outcome_1_process, 1, 0,
individual::Bitset$new(size)$insert(c(1, 3)),
list())
individual::Bitset$new(size)$insert(c(1, 3)))

mockery::expect_args(outcome_2_process, 1, 0,
individual::Bitset$new(size)$insert(c(2, 4)),
list())
individual::Bitset$new(size)$insert(c(2, 4)))

})

test_that("hazard resolves two competing outcomes", {
Expand Down Expand Up @@ -63,11 +64,9 @@ test_that("hazard resolves two competing outcomes", {
hazard$resolve(0)

mockery::expect_args(outcome_1_process, 1, 0,
individual::Bitset$new(size)$insert(c(2, 3)),
list())
individual::Bitset$new(size)$insert(c(2, 3)))
mockery::expect_args(outcome_2_process, 1, 0,
individual::Bitset$new(size)$insert(c(1, 4)),
list())
individual::Bitset$new(size)$insert(c(1, 4)))
})

test_that("hazard may resolve to neither outcome", {
Expand Down Expand Up @@ -97,11 +96,9 @@ test_that("hazard may resolve to neither outcome", {
hazard$resolve(0)

mockery::expect_args(outcome_1_process, 1, 0,
individual::Bitset$new(size)$insert(c(3)),
list())
individual::Bitset$new(size)$insert(c(3)))
mockery::expect_args(outcome_2_process, 1, 0,
individual::Bitset$new(size)$insert(c(2, 4)),
list())
individual::Bitset$new(size)$insert(c(2, 4)))
})

test_that("outcomes can define a partial set of rates", {
Expand Down Expand Up @@ -133,11 +130,9 @@ test_that("outcomes can define a partial set of rates", {
hazard$resolve(0)

mockery::expect_args(outcome_1_process, 1, 0,
individual::Bitset$new(size)$insert(c(1, 3)),
list())
individual::Bitset$new(size)$insert(c(1, 3)))
mockery::expect_args(outcome_2_process, 1, 0,
individual::Bitset$new(size)$insert(c(4)),
list())
individual::Bitset$new(size)$insert(c(4)))
})

test_that("hazard resolves three competing outcomes", {
Expand Down Expand Up @@ -173,12 +168,9 @@ test_that("hazard resolves three competing outcomes", {
hazard$resolve(0)

mockery::expect_args(outcome_1_process, 1, 0,
individual::Bitset$new(size)$insert(c(1)),
list())
individual::Bitset$new(size)$insert(c(1)))
mockery::expect_args(outcome_2_process, 1, 0,
individual::Bitset$new(size)$insert(c(2)),
list())
individual::Bitset$new(size)$insert(c(2)))
mockery::expect_args(outcome_3_process, 1, 0,
individual::Bitset$new(size)$insert(c(3, 4)),
list())
individual::Bitset$new(size)$insert(c(3, 4)))
})

0 comments on commit 7166cba

Please sign in to comment.