Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Informative error message for sampler_diagnostics() with fixed_param #1033

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions R/fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -1616,6 +1616,9 @@ CmdStanMCMC$set("public", name = "loo", value = loo)
#' }
#'
sampler_diagnostics <- function(inc_warmup = FALSE, format = getOption("cmdstanr_draws_format", "draws_array")) {
if (isTRUE(private$metadata_$algorithm == "fixed_param")) {
stop("There are no sampler diagnostics when fixed_param = TRUE.", call. = FALSE)
}
if (is.null(private$sampler_diagnostics_) &&
!length(self$output_files(include_failed = FALSE))) {
stop("No chains finished successfully. Unable to retrieve the sampler diagnostics.", call. = FALSE)
Expand Down
13 changes: 13 additions & 0 deletions tests/testthat/test-fit-mcmc.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ fit_mcmc_3 <- testing_fit("logistic", method = "sample",
iter_sampling = 0,
save_warmup = 1,
refresh = 0, metric = "dense_e")
fit_mcmc_fixed_param <- testing_fit("logistic", method = "sample",
seed = 1234, chains = 1,
iter_warmup = 100,
iter_sampling = 0,
save_warmup = 1,
refresh = 0, fixed_param = TRUE)
PARAM_NAMES <- c("alpha", "beta[1]", "beta[2]", "beta[3]")

test_that("draws() stops for unkown variables", {
Expand Down Expand Up @@ -399,3 +405,10 @@ test_that("metadata()$time has chains rowss", {
expect_equal(nrow(fit_mcmc_2$metadata()$time), fit_mcmc_2$num_chains())
expect_equal(nrow(fit_mcmc_3$metadata()$time), fit_mcmc_3$num_chains())
})

test_that("sampler_diagnostics() throws informative error when fixed_param=TRUE", {
expect_error(
fit_mcmc_fixed_param$sampler_diagnostics(),
"There are no sampler diagnostics when fixed_param = TRUE"
)
})
Loading