Skip to content

Commit

Permalink
Add show_messages and show_exceptions for all methods
Browse files Browse the repository at this point in the history
  • Loading branch information
andrjohns committed Jan 11, 2024
1 parent 5ae46d3 commit 790bf59
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 26 deletions.
28 changes: 20 additions & 8 deletions R/model.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion R/run.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
13 changes: 13 additions & 0 deletions man-roxygen/model-common-args.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#'
11 changes: 0 additions & 11 deletions man-roxygen/model-sample-args.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion man/model-method-laplace.Rd

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

17 changes: 16 additions & 1 deletion man/model-method-optimize.Rd

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

17 changes: 16 additions & 1 deletion man/model-method-pathfinder.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.

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

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

17 changes: 16 additions & 1 deletion man/model-method-variational.Rd

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

0 comments on commit 790bf59

Please sign in to comment.