Skip to content

Commit

Permalink
Interface changes after rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
richfitz committed Feb 29, 2024
1 parent b717201 commit e180d64
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
20 changes: 16 additions & 4 deletions R/model.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ mcstate_model_properties <- function(has_gradient = NULL,
has_direct_sample = NULL,
is_stochastic = NULL) {
ret <- list(has_gradient = has_gradient,
has_direct_sample = has_direct_sample)
has_direct_sample = has_direct_sample,
is_stochastic = is_stochastic)
class(ret) <- "mcstate_model_properties"
ret
}
Expand Down Expand Up @@ -247,12 +248,23 @@ validate_model_rng_state <- function(model, properties, call) {
if (isFALSE(properties$is_stochastic)) {
return(NULL)

Check warning on line 249 in R/model.R

View check run for this annotation

Codecov / codecov/patch

R/model.R#L249

Added line #L249 was not covered by tests
}
if (is.null(properties$is_stochastic) && is.null(model$set_rng_state)) {
return(NULL)
}
if (!is.function(model$set_rng_state)) {
if (isTRUE(model$is_stochastic)) {
hint <- paste("You have specified 'is_stochastic = TRUE', so in order",
"to use your stochastic model we need a way of setting",
"its state")

Check warning on line 258 in R/model.R

View check run for this annotation

Codecov / codecov/patch

R/model.R#L256-L258

Added lines #L256 - L258 were not covered by tests
} else {
hint <- paste("I found a non-function element 'set_rng_state' within",
"your model and you have not set the 'is_stochastic'",
"property")
}
cli::cli_abort(
c("Expected 'model$set_rng_state' to be a function",
i = paste("You have specified 'is_stochastic = TRUE', so in order",
"to use your stochastic model we need a way of setting",
"its state")))
i = hint),
arg = "model", call = call)
}
list(set = model$set_rng_state)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/helper-mcstate2.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,5 @@ ex_dust_sir <- function(n_particles = 100, n_threads = 1,
parameters = c("beta", "gamma"),
domain = cbind(c(0, 0), c(Inf, Inf)),
set_rng_state = set_rng_state),
is_stochastic = !deterministic)
mcstate_model_properties(is_stochastic = !deterministic))
}
5 changes: 5 additions & 0 deletions tests/testthat/test-model.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,9 @@ test_that("stochastic models need an rng setting function", {
mcstate_model_properties(is_stochastic = TRUE)),
"Expected 'model$set_rng_state' to be a function",
fixed = TRUE)
expect_error(
mcstate_model(
list(density = identity, parameters = "a", set_rng_state = TRUE)),
"Expected 'model$set_rng_state' to be a function",
fixed = TRUE)
})

0 comments on commit e180d64

Please sign in to comment.