Skip to content

Commit

Permalink
Fix indexing of match_timestep
Browse files Browse the repository at this point in the history
  • Loading branch information
pwinskill committed Jul 26, 2022
1 parent 4670e6a commit 73022ed
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion R/mortality_processes.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ create_mortality_process <- function(variables, events, renderer, parameters) {
renderer$render('natural_deaths', died$size(), timestep)
} else {
age <- get_age(variables$birth$get_values(), timestep)
last_deathrate <- match_last_timestep(parameters$deathrate_timesteps, timestep)
last_deathrate <- match_timestep(parameters$deathrate_timesteps, timestep)
deathrates <- rep(1, pop)
age_groups <- .bincode(age, c(0, parameters$deathrate_agegroups))
in_range <- !is.na(age_groups)
Expand Down
2 changes: 1 addition & 1 deletion R/population_parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ find_birthrates <- function(pops, age_high, deathrates) {
}

get_human_population <- function(parameters, timestep) {
last_pop <- match_last_timestep(parameters$human_population_timesteps, timestep)
last_pop <- match_timestep(parameters$human_population_timesteps, timestep)
parameters$human_population[last_pop]
}
8 changes: 0 additions & 8 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,6 @@ rtexp <- function(n, m, t) { itexp(runif(n), m, t) }
#'@param t current timestep
#'@noRd
match_timestep <- function(ts, t) {
min(sum(ts < t) + 1, length(ts))
}

#'@title Find index of the latest timestep in vector of timesteps
#'@param ts timesteps, assumed to be sorted and unique
#'@param t current timestep
#'@noRd
match_last_timestep <- function(ts, t) {
min(sum(ts <= t), length(ts))
}

16 changes: 16 additions & 0 deletions tests/testthat/test-demography.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,19 @@ test_that('calculate_initial_ages calculates truncated exp custom demographic',
mockery::expect_args(mock_rtexp, 2, 2, .75, 50 * 365)
expect_setequal(ages, c(25 * 365, 75 * 365, 30 * 365, 80 * 365))
})

test_that('match_timestep always gives 0 for constant demography', {
expect_equal(match_timestep(c(0), 0), 1)
expect_equal(match_timestep(c(0), 1), 1)
expect_equal(match_timestep(c(0), 50), 1)
})

test_that('match_timestep works on the boundaries', {
expect_equal(match_timestep(c(0, 50, 100), 0), 1)
expect_equal(match_timestep(c(0, 50, 100), 1), 1)
expect_equal(match_timestep(c(0, 50, 100), 49), 1)
expect_equal(match_timestep(c(0, 50, 100), 50), 2)
expect_equal(match_timestep(c(0, 50, 100), 99), 2)
expect_equal(match_timestep(c(0, 50, 100), 100), 3)
expect_equal(match_timestep(c(0, 50, 100), 101), 3)
})

0 comments on commit 73022ed

Please sign in to comment.