diff --git a/R/fit.R b/R/fit.R index 30e7310a..87b8f648 100644 --- a/R/fit.R +++ b/R/fit.R @@ -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 diff --git a/R/run.R b/R/run.R index 9202f0d4..7ffc44b0 100644 --- a/R/run.R +++ b/R/run.R @@ -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, diff --git a/man-roxygen/model-sample-args.R b/man-roxygen/model-sample-args.R index fe4e1014..6ab0abd6 100644 --- a/man-roxygen/model-sample-args.R +++ b/man-roxygen/model-sample-args.R @@ -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. #' diff --git a/man/fit-method-save_output_files.Rd b/man/fit-method-save_output_files.Rd index beb1e35b..14ef8621 100644 --- a/man/fit-method-save_output_files.Rd +++ b/man/fit-method-save_output_files.Rd @@ -109,7 +109,8 @@ file name after \code{basename}. For \verb{$save_metric_files()} everything is the same as for \verb{$save_output_files()} except \code{"-metric-"} is included in the new -file name after \code{basename}. +file name after \code{basename}. Make sure to set \code{save_metric=TRUE} when +fitting the model. For \verb{$save_config_files()} everything is the same as for \verb{$save_output_files()} except \code{"-config-"} is included in the new diff --git a/man/model-method-sample.Rd b/man/model-method-sample.Rd index 2558e630..d1c23359 100644 --- a/man/model-method-sample.Rd +++ b/man/model-method-sample.Rd @@ -296,7 +296,7 @@ using the \code{\link[=fit-method-summary]{$summary()}} method.} \item{save_metric}{(logical) When \code{TRUE}, call CmdStan with argument \code{"adaptation save_metric=1"} to save the adapted metric in separate JSON file with elements "stepsize", "metric_type" and "inv_metric". The default -is \code{TRUE}. This option is only available in CmdStan 2.34.0 and later.} +is \code{FALSE}. This option is only available in CmdStan 2.34.0 and later.} \item{save_cmdstan_config}{(logical) When \code{TRUE} (the default), call CmdStan with argument \code{"output save_config=1"} to save a json file which contains diff --git a/tests/testthat/test-fit-mcmc.R b/tests/testthat/test-fit-mcmc.R index 122dc503..ec454c5b 100644 --- a/tests/testthat/test-fit-mcmc.R +++ b/tests/testthat/test-fit-mcmc.R @@ -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)) +})