Skip to content

Commit

Permalink
Add queuing timing test to test-sir.R
Browse files Browse the repository at this point in the history
  • Loading branch information
apulsipher committed Nov 13, 2024
1 parent b03a1ca commit 734398f
Showing 1 changed file with 49 additions and 46 deletions.
95 changes: 49 additions & 46 deletions inst/tinytest/test-sir.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
# Run just this file with command:
# - tinytest::run_test_file("inst/tinytest/test-sir.R")
# Adding a Small world population without queuing ------------------------------
# Test just this file: tinytest::run_test_file("inst/tinytest/test-sir.R")

# Create function to test transition probability matrix ------------------------
test_tmat_matches_expected <- function(tmat) {
tmat_expected <- structure(
c(
0.961843, 0, 0,
0.03815696, 0.69985167, 0,
0, 0.3001483, 1
),
dim = c(3L, 3L),
dimnames = list(
c("Susceptible", "Infected", "Recovered"),
c("Susceptible", "Infected", "Recovered")
)
)

expect_equal(tmat, tmat_expected, tolerance = 0.0000001)
}

# Create small world population SIR Model --------------------------------------
sir_0 <- ModelSIR(
name = "COVID-19",
prevalence = .01,
Expand All @@ -11,9 +29,6 @@ sir_0 <- ModelSIR(
# Check model initialized correctly
expect_inherits(sir_0,"epiworld_sir")
expect_inherits(sir_0,"epiworld_model")
# Check can't plot before model is run
expect_warning(expect_error(plot(sir_0)))

expect_silent(agents_smallworld(
sir_0,
n = 50000,
Expand All @@ -22,37 +37,36 @@ expect_silent(agents_smallworld(
p = .01
))

# Initializing
queuing_off(sir_0)

# Running
verbose_off(sir_0)
run(sir_0, ndays = 50, seed = 1912)

# Check plots without issue after running model
# Running with queuing
# - Suppressing print output
expect_silent(verbose_off(sir_0))
# - Check plot fails before model is run
expect_warning(expect_error(plot(sir_0)))
# - Run model
expect_silent(run(sir_0, ndays = 50, seed = 1912))
# - Check plot succeeded after running model
expect_silent(plot(sir_0))

tmat_0 <- get_transition_probability(sir_0)
# Check transition probability matrix
tmat_0_queuing <- get_transition_probability(sir_0)
test_tmat_matches_expected(tmat_0_queuing)

expected_dimnames <- list(
c("Susceptible", "Infected", "Recovered"),
c("Susceptible", "Infected", "Recovered")
)
expected_dim <- c(3L, 3L)
# Run again without queuing and verify output is the same
expect_silent(queuing_off(sir_0))
run(sir_0, ndays = 50, seed = 1912)
tmat_0_noqueuing <- get_transition_probability(sir_0)
test_tmat_matches_expected(tmat_0_noqueuing)

expect_equal(dimnames(tmat_0), expected_dimnames)
expect_equal(dim(tmat_0), expected_dim)
expect_identical(tmat_0_noqueuing, tmat_0_queuing)

# Creating a SIR model with queuing --------------------------------------------
# TODO: Can we simply reset the model and run again?
# Create new SIR model without queuing -----------------------------------------
sir_1 <- ModelSIR(
name = "COVID-19",
prevalence = .01,
transmission_rate = .9,
recovery_rate = .3
)

# Adding a Small world population
agents_smallworld(
sir_1,
n = 50000,
Expand All @@ -61,28 +75,17 @@ agents_smallworld(
p = .01
)

# Running and printing
queuing_off(sir_1)

# Run the model and check output
verbose_off(sir_1)
run(sir_1, ndays = 50, seed = 1912)
tmat_1_noqueuing <- get_transition_probability(sir_1)

tmat_1 <- get_transition_probability(sir_1)

# Expected
tmat_expected <- structure(
c(
0.961843, 0, 0,
0.03815696, 0.69985167, 0,
0, 0.3001483, 1
),
dim = c(3L, 3L),
dimnames = list(
c("Susceptible", "Infected", "Recovered"),
c("Susceptible", "Infected", "Recovered")
)
)
expect_identical(tmat_1_noqueuing, tmat_0_queuing)

# Check matches expected output
expect_equal(tmat_0, tmat_expected, tolerance = 0.0000001)
# Check SIR produces same output with and without queuing
# TODO: why is this the case?
expect_equal(tmat_0, tmat_1)
# Check queuing is faster ------------------------------------------------------
runtime_0_noqueuing <- system.time(run(sir_0, ndays = 50, seed = 1912))
queuing_on(sir_0)
runtime_0_queuing <- system.time(run(sir_0, ndays = 50, seed = 1912))
expect_true(runtime_0_queuing["elapsed"] < runtime_0_noqueuing["elapsed"])

0 comments on commit 734398f

Please sign in to comment.