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

save_metric_files: fix doc and error message, and add tests #1021

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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: 2 additions & 1 deletion R/fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,8 @@ CmdStanFit$set("public", name = "cmdstan_diagnose", value = cmdstan_diagnose)
#'
#' For `$save_metric_files()` everything is the same as for
#' `$save_output_files()` except `"-metric-"` is included in the new
#' file name after `basename`.
#' file name after `basename`. Make sure to set `save_metric=TRUE` when
#' fitting the model.
#'
#' For `$save_config_files()` everything is the same as for
#' `$save_output_files()` except `"-config-"` is included in the new
Expand Down
6 changes: 6 additions & 0 deletions R/run.R
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,12 @@ CmdStanRun <- R6::R6Class(
timestamp = TRUE,
random = TRUE) {
current_files <- self$metric_files(include_failed = TRUE) # used so we get error if 0 files
if (is.null(current_files)) {
stop(
"No metric files found. Make sure to set 'save_metric=TRUE' when fitting the model.",
call. = FALSE
)
}
new_paths <- copy_temp_files(
current_paths = current_files,
new_dir = dir,
Expand Down
2 changes: 1 addition & 1 deletion man-roxygen/model-sample-args.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,5 @@
#' @param save_metric (logical) When `TRUE`, call CmdStan with argument
#' `"adaptation save_metric=1"` to save the adapted metric in separate JSON
#' file with elements "stepsize", "metric_type" and "inv_metric". The default
#' is `TRUE`. This option is only available in CmdStan 2.34.0 and later.
#' is `FALSE`. This option is only available in CmdStan 2.34.0 and later.
#'
3 changes: 2 additions & 1 deletion man/fit-method-save_output_files.Rd

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

2 changes: 1 addition & 1 deletion man/model-method-sample.Rd

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

11 changes: 11 additions & 0 deletions tests/testthat/test-fit-mcmc.R
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,14 @@ 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("save_metric_files works and has clear error message when no files", {
expect_error(
fit_mcmc$save_metric_files(),
"No metric files found"
)
fit_save_metric <- testing_fit("logistic", save_metric = TRUE)
paths <- fit_save_metric$save_metric_files()
checkmate::expect_file_exists(paths, extension = "json")
expect_true(all(file.size(paths) > 0))
})
Loading