diff --git a/R/model.R b/R/model.R index 7f298b11..e6cb1f95 100644 --- a/R/model.R +++ b/R/model.R @@ -1495,10 +1495,13 @@ optimize <- function(data = NULL, tol_grad = NULL, tol_rel_grad = NULL, tol_param = NULL, - history_size = NULL) { + history_size = NULL, + show_messages = TRUE, + show_exceptions = TRUE) { procs <- CmdStanProcs$new( num_procs = 1, - show_stdout_messages = (is.null(refresh) || refresh != 0), + show_stderr_messages = show_exceptions, + show_stdout_messages = show_messages, threads_per_proc = assert_valid_threads(threads, self$cpp_options()) ) model_variables <- NULL @@ -1624,7 +1627,9 @@ laplace <- function(data = NULL, mode = NULL, opt_args = NULL, jacobian = TRUE, # different default than for optimize! - draws = NULL) { + draws = NULL, + show_messages = TRUE, + show_exceptions = TRUE) { if (cmdstan_version() < "2.32") { stop("This method is only available in cmdstan >= 2.32", call. = FALSE) } @@ -1633,7 +1638,8 @@ laplace <- function(data = NULL, } procs <- CmdStanProcs$new( num_procs = 1, - show_stdout_messages = (is.null(refresh) || refresh != 0), + show_stderr_messages = show_exceptions, + show_stdout_messages = show_messages, threads_per_proc = assert_valid_threads(threads, self$cpp_options()) ) model_variables <- NULL @@ -1775,10 +1781,13 @@ variational <- function(data = NULL, tol_rel_obj = NULL, eval_elbo = NULL, output_samples = NULL, - draws = NULL) { + draws = NULL, + show_messages = TRUE, + show_exceptions = TRUE) { procs <- CmdStanProcs$new( num_procs = 1, - show_stdout_messages = (is.null(refresh) || refresh != 0), + show_stderr_messages = show_exceptions, + show_stdout_messages = show_messages, threads_per_proc = assert_valid_threads(threads, self$cpp_options()) ) model_variables <- NULL @@ -1902,10 +1911,13 @@ pathfinder <- function(data = NULL, num_paths = NULL, max_lbfgs_iters = NULL, num_elbo_draws = NULL, - save_single_paths = NULL) { + save_single_paths = NULL, + show_messages = TRUE, + show_exceptions = TRUE) { procs <- CmdStanProcs$new( num_procs = 1, - show_stdout_messages = (is.null(refresh) || refresh != 0), + show_stderr_messages = show_exceptions, + show_stdout_messages = show_messages, threads_per_proc = assert_valid_threads(num_threads, self$cpp_options()) ) model_variables <- NULL diff --git a/R/run.R b/R/run.R index 16a0c97b..9bce6c3e 100644 --- a/R/run.R +++ b/R/run.R @@ -844,7 +844,8 @@ CmdStanProcs <- R6::R6Class( report_time = function(id = NULL) { if (self$proc_state(id) == 7) { warning("Fitting finished unexpectedly! Use the $output() method for more information.\n", immediate. = TRUE, call. = FALSE) - } else { + } + if (private$show_stdout_messages_) { cat("Finished in ", base::format(round(self$total_time(), 1), nsmall = 1), "seconds.\n") diff --git a/man-roxygen/model-common-args.R b/man-roxygen/model-common-args.R index 7cdcbc49..baf67777 100644 --- a/man-roxygen/model-common-args.R +++ b/man-roxygen/model-common-args.R @@ -85,3 +85,16 @@ #' be compiled with `cpp_options = list(stan_opencl = TRUE)` for this #' argument to have an effect. #' +#' @param show_messages (logical) When `TRUE` (the default), prints all +#' output during the execution process, such as iteration numbers and elapsed times. +#' If the output is silenced then the [`$output()`][fit-method-output] method of +#' the resulting fit object can be used to display the silenced messages. +#' +#' @param show_exceptions (logical) When `TRUE` (the default), prints all +#' informational messages, for example rejection of the current proposal. +#' Disable if you wish to silence these messages, but this is not usually +#' recommended unless you are very confident that the model is correct up to +#' numerical error. If the messages are silenced then the +#' [`$output()`][fit-method-output] method of the resulting fit object can be +#' used to display the silenced messages. +#' diff --git a/man-roxygen/model-sample-args.R b/man-roxygen/model-sample-args.R index 2c8c0e26..240300a8 100644 --- a/man-roxygen/model-sample-args.R +++ b/man-roxygen/model-sample-args.R @@ -74,17 +74,6 @@ #' quantities block. If the parameters block is empty then using #' `fixed_param=TRUE` is mandatory. When `fixed_param=TRUE` the `chains` and #' `parallel_chains` arguments will be set to `1`. -#' @param show_messages (logical) When `TRUE` (the default), prints all -#' output during the sampling process, such as iteration numbers and elapsed times. -#' If the output is silenced then the [`$output()`][fit-method-output] method of -#' the resulting fit object can be used to display the silenced messages. -#' @param show_exceptions (logical) When `TRUE` (the default), prints all -#' informational messages, for example rejection of the current proposal. -#' Disable if you wish to silence these messages, but this is not usually -#' recommended unless you are very confident that the model is correct up to -#' numerical error. If the messages are silenced then the -#' [`$output()`][fit-method-output] method of the resulting fit object can be -#' used to display the silenced messages. #' @param diagnostics (character vector) The diagnostics to automatically check #' and warn about after sampling. Setting this to an empty string `""` or #' `NULL` can be used to prevent CmdStanR from automatically reading in the diff --git a/man/model-method-laplace.Rd b/man/model-method-laplace.Rd index 64852f88..2601f558 100644 --- a/man/model-method-laplace.Rd +++ b/man/model-method-laplace.Rd @@ -19,7 +19,9 @@ laplace( mode = NULL, opt_args = NULL, jacobian = TRUE, - draws = NULL + draws = NULL, + show_messages = TRUE, + show_exceptions = TRUE ) } \arguments{ @@ -138,6 +140,19 @@ optimization was originally run. If \code{mode} is \code{NULL} then the value of \code{jacobian} specified here is used when running optimization.} \item{draws}{(positive integer) The number of draws to take.} + +\item{show_messages}{(logical) When \code{TRUE} (the default), prints all +output during the execution process, such as iteration numbers and elapsed times. +If the output is silenced then the \code{\link[=fit-method-output]{$output()}} method of +the resulting fit object can be used to display the silenced messages.} + +\item{show_exceptions}{(logical) When \code{TRUE} (the default), prints all +informational messages, for example rejection of the current proposal. +Disable if you wish to silence these messages, but this is not usually +recommended unless you are very confident that the model is correct up to +numerical error. If the messages are silenced then the +\code{\link[=fit-method-output]{$output()}} method of the resulting fit object can be +used to display the silenced messages.} } \value{ A \code{\link{CmdStanLaplace}} object. diff --git a/man/model-method-optimize.Rd b/man/model-method-optimize.Rd index 88d40279..9513db9f 100644 --- a/man/model-method-optimize.Rd +++ b/man/model-method-optimize.Rd @@ -25,7 +25,9 @@ optimize( tol_grad = NULL, tol_rel_grad = NULL, tol_param = NULL, - history_size = NULL + history_size = NULL, + show_messages = TRUE, + show_exceptions = TRUE ) } \arguments{ @@ -158,6 +160,19 @@ argument should typically be set to \code{TRUE}.} \item{history_size}{(positive integer) The size of the history used when approximating the Hessian. Only available for L-BFGS.} + +\item{show_messages}{(logical) When \code{TRUE} (the default), prints all +output during the execution process, such as iteration numbers and elapsed times. +If the output is silenced then the \code{\link[=fit-method-output]{$output()}} method of +the resulting fit object can be used to display the silenced messages.} + +\item{show_exceptions}{(logical) When \code{TRUE} (the default), prints all +informational messages, for example rejection of the current proposal. +Disable if you wish to silence these messages, but this is not usually +recommended unless you are very confident that the model is correct up to +numerical error. If the messages are silenced then the +\code{\link[=fit-method-output]{$output()}} method of the resulting fit object can be +used to display the silenced messages.} } \value{ A \code{\link{CmdStanMLE}} object. diff --git a/man/model-method-pathfinder.Rd b/man/model-method-pathfinder.Rd index 1642ec59..aacf1fbc 100644 --- a/man/model-method-pathfinder.Rd +++ b/man/model-method-pathfinder.Rd @@ -28,7 +28,9 @@ pathfinder( num_paths = NULL, max_lbfgs_iters = NULL, num_elbo_draws = NULL, - save_single_paths = NULL + save_single_paths = NULL, + show_messages = TRUE, + show_exceptions = TRUE ) } \arguments{ @@ -164,6 +166,19 @@ calculating the ELBO of the approximation at each iteration of LBFGS.} \item{save_single_paths}{(logical) Whether to save the results of single pathfinder runs in multi-pathfinder.} + +\item{show_messages}{(logical) When \code{TRUE} (the default), prints all +output during the execution process, such as iteration numbers and elapsed times. +If the output is silenced then the \code{\link[=fit-method-output]{$output()}} method of +the resulting fit object can be used to display the silenced messages.} + +\item{show_exceptions}{(logical) When \code{TRUE} (the default), prints all +informational messages, for example rejection of the current proposal. +Disable if you wish to silence these messages, but this is not usually +recommended unless you are very confident that the model is correct up to +numerical error. If the messages are silenced then the +\code{\link[=fit-method-output]{$output()}} method of the resulting fit object can be +used to display the silenced messages.} } \value{ A \code{\link{CmdStanPathfinder}} object. diff --git a/man/model-method-sample.Rd b/man/model-method-sample.Rd index c3f2c1e6..ce21c60c 100644 --- a/man/model-method-sample.Rd +++ b/man/model-method-sample.Rd @@ -237,7 +237,7 @@ quantities block. If the parameters block is empty then using \code{parallel_chains} arguments will be set to \code{1}.} \item{show_messages}{(logical) When \code{TRUE} (the default), prints all -output during the sampling process, such as iteration numbers and elapsed times. +output during the execution process, such as iteration numbers and elapsed times. If the output is silenced then the \code{\link[=fit-method-output]{$output()}} method of the resulting fit object can be used to display the silenced messages.} diff --git a/man/model-method-sample_mpi.Rd b/man/model-method-sample_mpi.Rd index 8d961e62..8b0c3ddb 100644 --- a/man/model-method-sample_mpi.Rd +++ b/man/model-method-sample_mpi.Rd @@ -215,7 +215,7 @@ Increasing this value will result in larger output CSV files and thus an increased usage of disk space.} \item{show_messages}{(logical) When \code{TRUE} (the default), prints all -output during the sampling process, such as iteration numbers and elapsed times. +output during the execution process, such as iteration numbers and elapsed times. If the output is silenced then the \code{\link[=fit-method-output]{$output()}} method of the resulting fit object can be used to display the silenced messages.} diff --git a/man/model-method-variational.Rd b/man/model-method-variational.Rd index e7d9fa1e..6b894b74 100644 --- a/man/model-method-variational.Rd +++ b/man/model-method-variational.Rd @@ -26,7 +26,9 @@ variational( tol_rel_obj = NULL, eval_elbo = NULL, output_samples = NULL, - draws = NULL + draws = NULL, + show_messages = TRUE, + show_exceptions = TRUE ) } \arguments{ @@ -157,6 +159,19 @@ of the objective.} \item{draws}{(positive integer) Number of approximate posterior samples to draw and save.} + +\item{show_messages}{(logical) When \code{TRUE} (the default), prints all +output during the execution process, such as iteration numbers and elapsed times. +If the output is silenced then the \code{\link[=fit-method-output]{$output()}} method of +the resulting fit object can be used to display the silenced messages.} + +\item{show_exceptions}{(logical) When \code{TRUE} (the default), prints all +informational messages, for example rejection of the current proposal. +Disable if you wish to silence these messages, but this is not usually +recommended unless you are very confident that the model is correct up to +numerical error. If the messages are silenced then the +\code{\link[=fit-method-output]{$output()}} method of the resulting fit object can be +used to display the silenced messages.} } \value{ A \code{\link{CmdStanVB}} object. diff --git a/tests/testthat/test-fit-shared.R b/tests/testthat/test-fit-shared.R index 85c69eb4..691db31c 100644 --- a/tests/testthat/test-fit-shared.R +++ b/tests/testthat/test-fit-shared.R @@ -288,7 +288,7 @@ test_that("CmdStanArgs erorrs if idx is out of proc_ids range", { ) }) -test_that("no output with refresh = 0", { +test_that("no output with show_messages = FALSE", { mod <- testing_model("logistic") data_list <- testing_data("logistic") output <- utils::capture.output(tmp <- mod$variational(data = data_list, seed = 123)) @@ -298,12 +298,12 @@ test_that("no output with refresh = 0", { output <- utils::capture.output(tmp <- mod$sample(data = data_list, chains = 1, seed = 123)) expect_gt(length(output), 1) - output <- utils::capture.output(tmp <- mod$variational(data = data_list, refresh = 0, seed = 123)) - expect_equal(length(output), 1) - output <- utils::capture.output(tmp <- mod$optimize(data = data_list, refresh = 0, seed = 123)) - expect_equal(length(output), 1) - output <- utils::capture.output(tmp <- mod$sample(data = data_list, refresh = 0, chains = 1, seed = 123)) - expect_equal(length(output), 3) + output <- utils::capture.output(tmp <- mod$variational(data = data_list, show_messages = FALSE, seed = 123)) + expect_equal(length(output), 0) + output <- utils::capture.output(tmp <- mod$optimize(data = data_list, show_messages = FALSE, seed = 123)) + expect_equal(length(output), 0) + output <- utils::capture.output(tmp <- mod$sample(data = data_list, show_messages = FALSE, chains = 1, seed = 123)) + expect_equal(length(output), 0) }) test_that("sig_figs works with all methods", {