From ec5d9b3d3f7db7c52fae9506b81d31df8abdd368 Mon Sep 17 00:00:00 2001 From: jgabry Date: Mon, 25 Nov 2024 14:34:58 -0700 Subject: [PATCH 1/4] Allow models with no parameters without `fixed_param=TRUE` (in CmdStan >= 2.36) --- R/model.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/model.R b/R/model.R index e17ffbd4..e11603fd 100644 --- a/R/model.R +++ b/R/model.R @@ -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) From c341a4b6de132ea03dd49679d80404d480c95323 Mon Sep 17 00:00:00 2001 From: jgabry Date: Mon, 25 Nov 2024 14:48:03 -0700 Subject: [PATCH 2/4] suppress warnings from as.numeric(inv_metric) if no parameters --- R/csv.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/csv.R b/R/csv.R index 5adbdd66..209f6dc2 100644 --- a/R/csv.R +++ b/R/csv.R @@ -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 From d34458e4f02602a7c0889a8dd5e9923710d5d03b Mon Sep 17 00:00:00 2001 From: jgabry Date: Mon, 25 Nov 2024 14:48:10 -0700 Subject: [PATCH 3/4] add test for new behavior --- tests/testthat/test-model-sample.R | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-model-sample.R b/tests/testthat/test-model-sample.R index 8afe3053..d0ed29a1 100644 --- a/tests/testthat/test-model-sample.R +++ b/tests/testthat/test-model-sample.R @@ -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 { @@ -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", { From 038de9a0f8dfeae04c42724acbf9a10f5abb2d8a Mon Sep 17 00:00:00 2001 From: jgabry Date: Mon, 25 Nov 2024 15:11:32 -0700 Subject: [PATCH 4/4] Update csv.R --- R/csv.R | 1 + 1 file changed, 1 insertion(+) diff --git a/R/csv.R b/R/csv.R index 209f6dc2..4e585fa6 100644 --- a/R/csv.R +++ b/R/csv.R @@ -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)