Skip to content
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
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion R/csv.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
3 changes: 2 additions & 1 deletion man/read_cmdstan_csv.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions tests/testthat/test-csv.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
Loading