Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sbfnk committed Nov 21, 2023
1 parent c604e7e commit e152fe6
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 76 deletions.
2 changes: 1 addition & 1 deletion tests/testthat/setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ if (identical(Sys.getenv("NOT_CRAN"), "true")) {
withr::defer(future::plan("sequential"), teardown_env())

## process warning once as previous behaviour has been deprecated
empty <- suppressWarnings(fixed(0)))
empty <- suppressWarnings(fixed(0))
34 changes: 7 additions & 27 deletions tests/testthat/test-delays.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,26 @@ test_stan_delays <- function(generation_time = generation_time_opts(),
}

delay_params <-
c("delay_mean_mean", "delay_mean_sd", "delay_sd_mean", "delay_sd_sd", "delay_max",
"delay_np_pmf")
c("delay_params_mean", "delay_params_sd", "delay_max", "delay_np_pmf")

test_that("generation times can be specified in different ways", {
expect_equal(
test_stan_delays(params = delay_params),
c(0, 1)
c(0, 1, 1, 1)
)
expect_equal(
test_stan_delays(
generation_time = generation_time_opts(dist_spec(mean = 3)),
params = delay_params
),
c(0, 0, 0, 1)
c(0, 0, 0, 1, 1, 1)
)
expect_equal(
round(test_stan_delays(
generation_time = generation_time_opts(dist_spec(mean = 3, sd = 1, max = 4)),
params = delay_params
), digits = 2),
c(0.02, 0.11, 0.22, 0.30, 0.35)
c(0.02, 0.11, 0.22, 0.30, 0.35, 1.00, 1.00)
)
})

Expand All @@ -42,14 +41,14 @@ test_that("delay parameters can be specified in different ways", {
delays = delay_opts(dist_spec(mean = 3)),
params = delay_params
), n = -2),
c(0, 0, 0, 1)
c(0, 0, 0, 1, 1)
)
expect_equal(
tail(round(test_stan_delays(
delays = delay_opts(dist_spec(mean = 3, sd = 1, max = 4)),
params = delay_params
), digits = 2), n = -2),
c(0.02, 0.11, 0.22, 0.30, 0.35)
c(0.02, 0.11, 0.22, 0.30, 0.35, 1.00)
)
})

Expand All @@ -59,26 +58,7 @@ test_that("truncation parameters can be specified in different ways", {
truncation = trunc_opts(dist = dist_spec(mean = 3, sd = 1, max = 4)),
params = delay_params
), digits = 2), n = -2),
c(0.02, 0.11, 0.22, 0.30, 0.35)
)
})

test_that("contradictory generation times are caught", {
expect_error(generation_time_opts(dist_spec(mean = 3.5)), "must be an integer")
expect_error(
generation_time_opts(dist_spec(mean = 3, mean_sd = 1)),
"must be 0"
)
})

test_that("contradictory delays are caught", {
expect_error(
test_stan_delays(delays = delay_opts(dist_spec(mean = 3.5))),
"must be an integer"
)
expect_error(
test_stan_delays(delays = delay_opts(dist_spec(mean = 3, mean_sd = 1))),
"must be 0"
c(1.00, 0.02, 0.11, 0.22, 0.30, 0.35)
)
})

Expand Down
6 changes: 4 additions & 2 deletions tests/testthat/test-dist.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ test_that("distributions are the same in R and stan", {
)

pmf_stan_lognormal <- discretised_pmf(
args$mean, args$sd, args$max_value + 1, 0
unlist(lognormal_params), args$max_value + 1, 0
)
pmf_stan_gamma <- discretised_pmf(
unlist(gamma_params), args$max_value + 1, 1
)
pmf_stan_gamma <- discretised_pmf(args$mean, args$sd, args$max_value + 1, 1)

expect_equal(pmf_r_lognormal, pmf_stan_lognormal)
expect_equal(pmf_r_gamma, pmf_stan_gamma)
Expand Down
67 changes: 28 additions & 39 deletions tests/testthat/test-dist_spec.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@

test_that("dist_spec returns correct output for fixed lognormal distribution", {
result <- dist_spec(mean = 5, sd = 1, max = 19, distribution = "lognormal")
expect_equal(dim(result$mean_mean), 0)
expect_equal(dim(result$sd_mean), 0)
expect_equal(dim(result$params_mean), 0)
expect_equal(dim(result$dist), 0)
expect_equal(dim(result$max), 0)
expect_equal(result$fixed, array(1))
expect_equal(result$parametric, array(FALSE))
expect_equal(
as.vector(round(result$np_pmf, 2)),
c(0.00, 0.00, 0.00, 0.00, 0.01, 0.01, 0.02, 0.03,
Expand All @@ -16,26 +15,22 @@ test_that("dist_spec returns correct output for fixed lognormal distribution", {

test_that("dist_spec returns correct output for uncertain gamma distribution", {
result <- dist_spec(
mean = 3, sd = 2, mean_sd = 0.5, sd_sd = 0.5, max = 19,
distribution = "gamma"
params_mean = c(3, 2), params_sd = c(0.5, 0.5), distribution = "gamma",
max = 19
)
expect_equal(result$mean_mean, array(3L))
expect_equal(result$sd_mean, array(2))
expect_equal(result$mean_sd, array(0.5))
expect_equal(result$sd_sd, array(0.5))
expect_equal(result$params_mean, array(c(3, 2)))
expect_equal(result$params_sd, array(c(0.5, 0.5)))
expect_equal(result$dist, array("gamma"))
expect_equal(result$max, array(19))
expect_equal(result$fixed, array(0L))
expect_equal(result$parametric, array(TRUE))
})

test_that("dist_spec returns correct output for fixed distribution", {
result <- dist_spec(
result <- fix_dist(dist_spec(
mean = 5, mean_sd = 3, sd = 1, max = 19, distribution = "lognormal",
fixed = TRUE
)
expect_equal(dim(result$mean_mean), 0)
expect_equal(dim(result$sd_mean), 0)
expect_equal(result$fixed, array(1L))
))
expect_equal(dim(result$params_mean), 0)
expect_equal(result$parametric, array(FALSE))
expect_equal(
as.vector(round(result$np_pmf, 2)),
c(0.00, 0.00, 0.00, 0.00, 0.01, 0.01, 0.02, 0.03,
Expand All @@ -51,22 +46,18 @@ test_that("dist_spec returns error when both pmf and distributional parameters a

test_that("dist_spec returns error when mean is missing but other distributional parameters are given", {
expect_error(dist_spec(sd = 1, max = 20, distribution = "lognormal"),
"If any distributional parameters are given then so must the mean.")
})

test_that("dist_spec returns error when maximum of parametric distributions is not specified", {
expect_error(dist_spec(mean = 5, sd = 1, distribution = "lognormal"),
"Maximum of parametric distributions must be specified.")
"is missing.")
})

test_that("+.dist_spec returns correct output for sum of two distributions", {
lognormal <- dist_spec(mean = 5, sd = 1, max = 19, distribution = "lognormal")
gamma <- dist_spec(mean = 3, sd = 2, mean_sd = 0.5, sd_sd = 0.5, max = 20, distribution = "gamma")
gamma <- dist_spec(
params_mean = c(3, 2), params_sd = c(0.5, 0.5), max = 20,
distribution = "gamma"
)
result <- lognormal + gamma
expect_equal(result$mean_mean, array(3))
expect_equal(result$sd_mean, array(2))
expect_equal(result$mean_sd, array(0.5))
expect_equal(result$sd_sd, array(0.5))
expect_equal(result$params_mean, array(c(3, 2)))
expect_equal(result$params_sd, array(c(0.5, 0.5)))
expect_equal(result$n, 2)
expect_equal(result$n_p, 1)
expect_equal(result$n_np, 1)
Expand All @@ -81,8 +72,7 @@ test_that("+.dist_spec returns correct output for sum of two fixed distributions
mean = 3, sd = 2, max = 19, distribution = "gamma", fixed = TRUE
)
result <- lognormal + gamma
expect_equal(dim(result$mean_mean), 0)
expect_equal(dim(result$sd_mean), 0)
expect_equal(dim(result$params_mean), 0)
expect_equal(result$n, 1)
expect_equal(result$n_p, 0)
expect_equal(result$n_np, 1)
Expand All @@ -93,8 +83,7 @@ test_that("+.dist_spec returns correct output for sum of two nonparametric distr
lognormal <- dist_spec(pmf = c(0.1, 0.2, 0.3, 0.4))
gamma <- dist_spec(pmf = c(0.1, 0.2, 0.3, 0.4))
result <- lognormal + gamma
expect_equal(dim(result$mean_mean), 0)
expect_equal(dim(result$sd_mean), 0)
expect_equal(dim(result$params_mean), 0)
expect_equal(result$n, 1)
expect_equal(result$n_p, 0)
expect_equal(result$n_np, 1)
Expand Down Expand Up @@ -151,9 +140,9 @@ test_that("mean.dist_spec returns correct output for fixed lognormal distributio
})

test_that("mean.dist_spec returns correct output for uncertain gamma distribution", {
gamma <- dist_spec(mean = 3, sd = 2, mean_sd = 0.5, sd_sd = 0.5, max = 19, distribution = "gamma")
gamma <- dist_spec(params_mean = c(3, 2), params_sd = c(0.5, 0.5), max = 19, distribution = "gamma")
result <- EpiNow2:::mean.dist_spec(gamma)
expect_equal(result, 3)
expect_equal(result, 1.5)
})

test_that("mean.dist_spec returns correct output for sum of two distributions", {
Expand All @@ -166,30 +155,30 @@ test_that("mean.dist_spec returns correct output for sum of two distributions",
test_that("print.dist_spec correctly prints the parameters of the fixed lognormal", {
lognormal <- dist_spec(mean = 1.5, sd = 0.5, max = 19, distribution = "lognormal")

expect_output(print(lognormal), "\\n Fixed distribution with PMF \\[0\\.0014 0\\.052 0\\.16 0\\.2 0\\.18 0\\.13 0\\.094 0\\.063 0\\.042 0\\.027 0\\.018 0\\.012 0\\.0079 0\\.0052 0\\.0035 0\\.0024 0\\.0016 0\\.0011 0\\.00078 0\\.00055\\]\\n")
expect_output(print(lognormal), "\\n distribution with PMF \\[0\\.0014 0\\.052 0\\.16 0\\.2 0\\.18 0\\.13 0\\.094 0\\.063 0\\.042 0\\.027 0\\.018 0\\.012 0\\.0079 0\\.0052 0\\.0035 0\\.0024 0\\.0016 0\\.0011 0\\.00078 0\\.00055\\]\\.\\n")
})

test_that("print.dist_spec correctly prints the parameters of the uncertain gamma", {
gamma <- dist_spec(
mean = 3, sd = 2, mean_sd = 0.5, sd_sd = 0.5, max = 19,
params_mean = c(3, 2), params_sd = c(0.5, 0.5), max = 19,
distribution = "gamma"
)

expect_output(print(gamma), "\\n Uncertain gamma distribution with \\(untruncated\\) mean 3 \\(SD 0\\.5\\) and SD 2 \\(SD 0\\.5\\)\\n")
expect_output(print(gamma), "\\n gamma distribution \\(max: 19\\) with uncertain shape \\(mean = 3, sd = 0.5\\) and uncertain rate \\(mean = 2, sd = 0\\.5\\)\\.\\n")
})

test_that("print.dist_spec correctly prints the parameters of the uncertain lognormal", {
lognormal_uncertain <- dist_spec(mean = 1.5, sd = 0.5, mean_sd = 0.1, sd_sd = 0.1, max = 19, distribution = "lognormal")

expect_output(print(lognormal_uncertain), "\\n Uncertain lognormal distribution with \\(untruncated\\) logmean 1\\.5 \\(SD 0\\.1\\) and logSD 0\\.5 \\(SD 0\\.1\\)\\n")
expect_output(print(lognormal_uncertain), "\\n lognormal distribution \\(max: 19\\) with uncertain meanlog \\(mean = 1\\.5, sd = 0\\.1\\) and uncertain sdlog \\(mean = 0\\.5, sd = 0\\.1\\)\\.\\n")
})

test_that("print.dist_spec correctly prints the parameters of a combination of distributions", {
lognormal <- dist_spec(mean = 1.5, sd = 0.5, max = 19, distribution = "lognormal")
gamma <- dist_spec(mean = 3, sd = 2, mean_sd = 0.5, sd_sd = 0.5, max = 19, distribution = "gamma")
gamma <- dist_spec(params_mean = c(3, 2), params_sd = c(0.5, 0.5), max = 19, distribution = "gamma")
combined <- lognormal + gamma

expect_output(print(combined), "Combination of delay distributions:\\n Fixed distribution with PMF \\[0\\.0014 0\\.052 0\\.16 0\\.2 0\\.18 0\\.13 0\\.094 0\\.063 0\\.042 0\\.027 0\\.018 0\\.012 0\\.0079 0\\.0052 0\\.0035 0\\.0024 0\\.0016 0\\.0011 0\\.00078 0\\.00055\\]\\n Uncertain gamma distribution with \\(untruncated\\) mean 3 \\(SD 0\\.5\\) and SD 2 \\(SD 0\\.5\\)\\n")
expect_output(print(combined), "\\nComposite delay distribution:\\n distribution with PMF \\[0\\.0014 0\\.052 0\\.16 0\\.2 0\\.18 0\\.13 0\\.094 0\\.063 0\\.042 0\\.027 0\\.018 0\\.012 0\\.0079 0\\.0052 0\\.0035 0\\.0024 0\\.0016 0\\.0011 0\\.00078 0\\.00055\\]\\.\\n gamma distribution \\(max: 19\\) with uncertain shape \\(mean = 3, sd = 0\\.5\\) and uncertain rate \\(mean = 2, sd = 0\\.5\\)\\.\\n")
})

test_that("plot.dist_spec returns a ggplot object", {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-estimate_secondary.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ inc <- estimate_secondary(cases[1:60],

# extract posterior variables of interest
params <- c(
"meanlog" = "delay_mean[1]", "sdlog" = "delay_sd[1]",
"meanlog" = "delay_params[1]", "sdlog" = "delay_params[2]",
"scaling" = "frac_obs[1]"
)

Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-estimate_truncation.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ construct_truncation <- function(index, cases, dist) {
cmf <- cumsum(
dlnorm(
1:(dist$max + 1),
rnorm(1, dist$mean_mean, dist$mean_sd),
rnorm(1, dist$sd_mean, dist$sd_sd)
rnorm(1, dist$params_mean[1], dist$params_sd[1]),
rnorm(1, dist$params_mean[2], dist$params_sd[2])
)
)
cmf <- cmf / cmf[dist$max + 1]
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-report_cases.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ test_that("report_cases can simulate infections forward", {
expect_equal(class(reported_cases), "list")
expect_equal(class(reported_cases$samples), c("data.table", "data.frame"))
expect_equal(class(reported_cases$summarised), c("data.table", "data.frame"))
expect_equal(nrow(reported_cases$summarised), 10)
expect_equal(nrow(reported_cases$summarised), 9)
expect_equal(class(reported_cases$summarised$median), "numeric")
})
4 changes: 2 additions & 2 deletions tests/testthat/test-stan-infections.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ test_that("update_infectiousness works as expected with default settings", {
expect_error(update_infectiousness(rep(1, 20), rep(0.1, 5), 5, 10, 10))
})

pmf <- discretised_pmf(3, 2, 15, 1)
pmf <- discretised_pmf(c(2.25, 0.75), 15, 1)
gt_rev_pmf <- get_delay_rev_pmf(
1L, 15L, array(0L), array(1L),
array(c(1L, 2L)), array(15L), pmf,
array(c(1L, 16L)), numeric(0), numeric(0), 0L,
array(c(1L, 16L)), numeric(0), 1L, 0L,
1L, 1L, 0L
)

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-stan-secondary.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ skip_on_os("windows")
# test primary reports and observations
reports <- rep(10, 20)
obs <- rep(4, 20)
delay_pmf <- reverse_mf(discretised_pmf(log(3), 0.1, 5, 0))
delay_pmf <- reverse_mf(discretised_pmf(c(log(3), 0.1), 5, 0))

check_equal <- function(args, target, dof = 0, dev = FALSE) {
out <- do.call(calculate_secondary, args)
Expand Down

0 comments on commit e152fe6

Please sign in to comment.