diff --git a/NEWS.md b/NEWS.md index 7020c8d15..51bdaf5f9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -248,6 +248,10 @@ CSV file from the optimization it runs first. That file is now named * `$laplace()` now passes `show_messages` and `show_exceptions` to the optimization it runs when `mode` isn't supplied. * `read_cmdstan_csv()` now expands `~` in file paths. (#1098) +* `read_cmdstan_csv()` and `as_cmdstan_fit()` now return all draws as +post-warmup draws for a CSV file that CmdStan wrote with +`algorithm=fixed_param` and `save_warmup=1`. Previously the first `iter_warmup` +draws were returned as warmup draws. (#1343) * Loading CmdStanR no longer causes `parallel::mclapply()` to leave zombie processes behind. (#1105) * Data with a `NULL` element now gives an error naming the variable. (#1129) diff --git a/R/csv.R b/R/csv.R index f8384194f..f6a9db88a 100644 --- a/R/csv.R +++ b/R/csv.R @@ -66,7 +66,8 @@ #' * `step_size`: A list (one element per chain) of the step sizes used. #' * `warmup_draws`: If `save_warmup` was `TRUE` when fitting the model then a #' [`draws_array`][posterior::draws_array] (or different format if `format` is -#' specified) of warmup draws. +#' specified) of warmup draws. The `fixed_param` sampler writes no warmup +#' draws, so `warmup_draws` is `NULL` and `metadata$save_warmup` is `0`. #' * `post_warmup_draws`: A [`draws_array`][posterior::draws_array] (or #' different format if `format` is specified) of post-warmup draws. #' * `warmup_sampler_diagnostics`: If `save_warmup` was `TRUE` when fitting the @@ -1160,6 +1161,10 @@ read_csv_metadata <- function(csv_file) { } item }) + # the fixed_param sampler writes no warmup draws, even with save_warmup + if (isTRUE(csv_file_info$algorithm == "fixed_param")) { + csv_file_info$save_warmup <- 0L + } csv_file_info } diff --git a/man/read_cmdstan_csv.Rd b/man/read_cmdstan_csv.Rd index eb41926e8..5398cc052 100644 --- a/man/read_cmdstan_csv.Rd +++ b/man/read_cmdstan_csv.Rd @@ -93,7 +93,8 @@ or their diagonals, depending on the type of metric used. \item \code{step_size}: A list (one element per chain) of the step sizes used. \item \code{warmup_draws}: If \code{save_warmup} was \code{TRUE} when fitting the model then a \code{\link[posterior:draws_array]{draws_array}} (or different format if \code{format} is -specified) of warmup draws. +specified) of warmup draws. The \code{fixed_param} sampler writes no warmup +draws, so \code{warmup_draws} is \code{NULL} and \code{metadata$save_warmup} is \code{0}. \item \code{post_warmup_draws}: A \code{\link[posterior:draws_array]{draws_array}} (or different format if \code{format} is specified) of post-warmup draws. \item \code{warmup_sampler_diagnostics}: If \code{save_warmup} was \code{TRUE} when fitting the diff --git a/tests/testthat/test-csv.R b/tests/testthat/test-csv.R index c432816cc..37ef58249 100644 --- a/tests/testthat/test-csv.R +++ b/tests/testthat/test-csv.R @@ -2134,3 +2134,32 @@ test_that("read_cmdstan_csv() errors for a truncated compressed CSV file", { expect_error(read_cmdstan_csv(truncated[1]), "truncated or corrupt") expect_error(read_cmdstan_csv(truncated[2]), "truncated or corrupt") }) + +test_that("read_cmdstan_csv() reads no warmup draws for fixed_param", { + fit <- testing_fit( + "bernoulli", + method = "sample", + seed = 123, + chains = 2, + iter_warmup = 30, + iter_sampling = 20, + fixed_param = TRUE + ) + # CmdStan accepts save_warmup with fixed_param, but writes no warmup draws + csv_files <- file.path(withr::local_tempdir(), basename(fit$output_files())) + for (i in seq_along(csv_files)) { + lines <- readLines(fit$output_files()[i]) + writeLines( + sub("save_warmup = (false|0)", "save_warmup = true", lines), + csv_files[i] + ) + } + expect_true(any(grepl("save_warmup = true", readLines(csv_files[1])))) + csv <- read_cmdstan_csv(csv_files) + expect_equal(csv$metadata$save_warmup, 0) + expect_null(csv$warmup_draws) + expect_equal(csv$post_warmup_draws, fit$draws()) + fit2 <- as_cmdstan_fit(csv_files) + expect_equal(fit2$draws(), fit$draws()) + expect_error(fit2$draws(inc_warmup = TRUE), "without them") +})