Skip to content

Commit

Permalink
Only set seeds in testing when needed. (#590)
Browse files Browse the repository at this point in the history
* remove unneeded seeds

* update snapshots

* add news item

* add missing dependency

* add reviewer
  • Loading branch information
sbfnk authored Mar 1, 2024
1 parent e1ff1f5 commit 97e07cf
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 30 deletions.
1 change: 1 addition & 0 deletions .github/workflows/R-CMD-as-cran-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
dependencies: NA
extra-packages: |
rcmdcheck
rmarkdown
stan-dev/cmdstanr
testthat
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* Added the possibility of specifying a fixed observation scaling. By @sbfnk in #550 and reviewed by @seabbs.
* Added the possibility of specifying fixed overdispersion. By @sbfnk in #560 and reviewed by @seabbs.
* The example in `estimate_truncation()` has been simplified. The package now ships with a dataset `example_truncated`, which is used in the `estimate_truncation()` example and tests. The steps for creating the `example_truncated` is stored in `./data-raw/estimate-truncation.R`. By @jamesmbaazam in #584 and reviewed by @seabbs and @sbfnk.
* Tests have been updated to only set random seeds before snapshot tests involving random number generation, and unset them subsequently. By @sbfnk in #590 and reviewed by @seabbs.

## Model changes

Expand Down
56 changes: 28 additions & 28 deletions tests/testthat/_snaps/simulate-infections.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@
12: infections 2023-01-12 117.41367
13: infections 2023-01-13 93.93093
14: infections 2023-01-14 75.14475
15: reported_cases 2023-01-01 128.00000
16: reported_cases 2023-01-02 151.00000
17: reported_cases 2023-01-03 145.00000
18: reported_cases 2023-01-04 188.00000
19: reported_cases 2023-01-05 252.00000
20: reported_cases 2023-01-06 276.00000
21: reported_cases 2023-01-07 371.00000
22: reported_cases 2023-01-08 273.00000
23: reported_cases 2023-01-09 234.00000
24: reported_cases 2023-01-10 192.00000
25: reported_cases 2023-01-11 157.00000
26: reported_cases 2023-01-12 120.00000
27: reported_cases 2023-01-13 78.00000
28: reported_cases 2023-01-14 63.00000
15: reported_cases 2023-01-01 125.00000
16: reported_cases 2023-01-02 135.00000
17: reported_cases 2023-01-03 195.00000
18: reported_cases 2023-01-04 224.00000
19: reported_cases 2023-01-05 253.00000
20: reported_cases 2023-01-06 328.00000
21: reported_cases 2023-01-07 364.00000
22: reported_cases 2023-01-08 278.00000
23: reported_cases 2023-01-09 206.00000
24: reported_cases 2023-01-10 169.00000
25: reported_cases 2023-01-11 144.00000
26: reported_cases 2023-01-12 109.00000
27: reported_cases 2023-01-13 80.00000
28: reported_cases 2023-01-14 95.00000
variable date value

# simulate_infections works as expected with additional parameters
Expand All @@ -50,19 +50,19 @@
12: infections 2023-01-12 190.8991
13: infections 2023-01-13 180.8132
14: infections 2023-01-14 171.1484
15: reported_cases 2023-01-01 425.0000
16: reported_cases 2023-01-02 335.0000
17: reported_cases 2023-01-03 376.0000
18: reported_cases 2023-01-04 250.0000
19: reported_cases 2023-01-05 301.0000
20: reported_cases 2023-01-06 275.0000
21: reported_cases 2023-01-07 844.0000
22: reported_cases 2023-01-08 235.0000
23: reported_cases 2023-01-09 205.0000
24: reported_cases 2023-01-10 251.0000
25: reported_cases 2023-01-11 239.0000
26: reported_cases 2023-01-12 80.0000
27: reported_cases 2023-01-13 128.0000
28: reported_cases 2023-01-14 276.0000
15: reported_cases 2023-01-01 155.0000
16: reported_cases 2023-01-02 563.0000
17: reported_cases 2023-01-03 146.0000
18: reported_cases 2023-01-04 644.0000
19: reported_cases 2023-01-05 282.0000
20: reported_cases 2023-01-06 473.0000
21: reported_cases 2023-01-07 193.0000
22: reported_cases 2023-01-08 262.0000
23: reported_cases 2023-01-09 19.0000
24: reported_cases 2023-01-10 277.0000
25: reported_cases 2023-01-11 177.0000
26: reported_cases 2023-01-12 273.0000
27: reported_cases 2023-01-13 97.0000
28: reported_cases 2023-01-14 265.0000
variable date value

1 change: 0 additions & 1 deletion tests/testthat/test-report_cases.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ test_that("report_cases can simulate infections forward", {
# data for speed in this example.
cases <- cases[, cases := as.integer(confirm)]
cases <- cases[, confirm := NULL][, sample := 1]
set.seed(123)
reported_cases <- report_cases(
case_estimates = cases,
delays = delay_opts(example_incubation_period + example_reporting_delay),
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-simulate-infections.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,23 @@ test_simulate_infections <- function(obs = obs_opts(family = "poisson"), ...) {
}

test_that("simulate_infections works as expected with standard parameters", {
set.seed(123)
sim <- test_simulate_infections()
expect_equal(nrow(sim), 2 * nrow(R))
expect_snapshot_output(sim)
set.seed(Sys.time())
})

test_that("simulate_infections works as expected with additional parameters", {
set.seed(123)
sim <- test_simulate_infections(
generation_time = generation_time_opts(fix_dist(example_generation_time)),
delays = delay_opts(fix_dist(example_reporting_delay)),
obs = obs_opts(family = "negbin", phi = list(mean = 0.5, sd = 0))
)
expect_equal(nrow(sim), 2 * nrow(R))
expect_snapshot_output(sim)
set.seed(Sys.time())
})

test_that("simulate_infections fails with uncertain parameters", {
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/test-stan-convole.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ test_that("convolve can combine two pmfs as expected", {
})

test_that("convolve performs the same as a numerical convolution", {
set.seed(123)
# Sample and analytical PMFs for two Poisson distributions
x <- rpois(10000, 3)
xpmf <- dpois(0:20, 3)
Expand Down

0 comments on commit 97e07cf

Please sign in to comment.