Skip to content

Commit

Permalink
Merge pull request #1046 from stan-dev/no-fixed-param-error
Browse files Browse the repository at this point in the history
Don't require `fixed_param` for models with zero parameters (only GQs) for CmdStan >= 2.36
  • Loading branch information
jgabry authored Nov 26, 2024
2 parents 2cd3a30 + 5a9156d commit 591b4ab
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
3 changes: 2 additions & 1 deletion R/csv.R
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ read_csv_metadata <- function(csv_file) {
dense_inv_metric <- TRUE
} else if (inv_metric_next) {
inv_metric_split <- strsplit(gsub("# ", "", line), ",")
numeric_inv_metric_split <- rapply(inv_metric_split, as.numeric)
numeric_inv_metric_split <- suppressWarnings(rapply(inv_metric_split, as.numeric))
if (inv_metric_rows == -1 && dense_inv_metric) {
inv_metric_rows <- length(inv_metric_split[[1]])
inv_metric_rows_to_read <- inv_metric_rows
Expand Down Expand Up @@ -786,6 +786,7 @@ read_csv_metadata <- function(csv_file) {
}
}
if (csv_file_info$method != "diagnose" &&
!isTRUE(csv_file_info$algorithm == "fixed_param") &&
length(csv_file_info$sampler_diagnostics) == 0 &&
length(csv_file_info$variables) == 0) {
stop("Supplied CSV file does not contain any variable names or data!", call. = FALSE)
Expand Down
2 changes: 1 addition & 1 deletion R/model.R
Original file line number Diff line number Diff line change
Expand Up @@ -1208,7 +1208,7 @@ sample <- function(data = NULL,
}
}

if (cmdstan_version() >= "2.27.0" && !fixed_param) {
if (cmdstan_version() >= "2.27.0" && cmdstan_version() < "2.36.0" && !fixed_param) {
if (self$has_stan_file() && file.exists(self$stan_file())) {
if (!is.null(self$variables()) && length(self$variables()$parameters) == 0) {
stop("Model contains no parameters. Please use 'fixed_param = TRUE'.", call. = FALSE)
Expand Down
13 changes: 12 additions & 1 deletion tests/testthat/test-model-sample.R
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ test_that("seed works for multi chain sampling", {
expect_false(all(chain_tdata_1 == chain_tdata_2))
})

test_that("fixed_param is set when the model has no parameters", {
test_that("Correct behavior if fixed_param not set when the model has no parameters", {
code <- "
model {}
generated quantities {
Expand All @@ -316,10 +316,21 @@ test_that("fixed_param is set when the model has no parameters", {
"
stan_file <- write_stan_file(code)
m <- cmdstan_model(stan_file)
fake_cmdstan_version("2.35.0")
expect_error(
m$sample(),
"Model contains no parameters. Please use 'fixed_param = TRUE'."
)

reset_cmdstan_version()
if (cmdstan_version() >= "2.36.0") {
# as of 2.36.0 we don't need fixed_param if no parameters
expect_no_error(
utils::capture.output(
fit <- m$sample(iter_warmup = 10, iter_sampling = 10, diagnostics = NULL)
)
)
}
})

test_that("sig_figs warning if version less than 2.25", {
Expand Down

0 comments on commit 591b4ab

Please sign in to comment.