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

suppress all output from RStan #49

Open
bob-carpenter opened this issue Feb 19, 2014 · 35 comments
Open

suppress all output from RStan #49

bob-carpenter opened this issue Feb 19, 2014 · 35 comments
Assignees
Milestone

Comments

@bob-carpenter
Copy link

Requested by Avraham Adler on stan-users:

Firstly, as a medium-term lurker but first-time poster, I would like to thank the entire Stan development team for the creation and maintenance of Stan.

I am using Stan as part of a paper I am trying to write, and I am struggling with trying to suppress diagnostic messages. For example, when I have used JAGS, R2jags, and knitr, once I set echo, messages, and warnings to FALSE, I can have the model run every time I knit the paper, and only have the results I want displayed through \Sexpr{}. Using Stan, I am having much more of a problem. I have searched the group and have found the following threads:

  • Suppressing Warning Messages
  • silent stan?
  • RStan print() - Trivial request

I have verbose set to FALSE and refresh set to -1. I have used suppressMessages/Warnings as suggested in the thread (1), and that does not seem to do anything more than setting messages and warnings settings in the knitr chunk. I have wrapped my stan() call in sink() calls, as discussed in thread (2) as such:

<<'Bayes_Stan', eval=TRUE, echo=FALSE, message=FALSE, warning=FALSE, cache=0>>=
sink(file = "a", type = c("output", "message"))
library(rstan)
LN_S <- suppressMessages(suppressWarnings(stan(file = 'Bayes.stan', data = BayesData, pars = c('mu', 'sigma', 'LR_post', 'ASL_post', 'dev'), iter = 300000, warmup = 50000, thin = 25, chains = 5, seed = 12, refresh = -1, verbose = FALSE)))

[snip extractions and assignments]

LNPlus_S <- suppressMessages(suppressWarnings(stan(file = 'BayesPlus.stan', data = BayesDataPlus, pars = c('Pars', 'LR_post', 'ASL_post', 'dev'), iter = 300000, warmup = 50000, thin = 25, chains = 5, seed = 12, refresh = -1, verbose = FALSE)))

[snip extractions and assignments]
sink()
@

Regardless, my knitted output looks like:

…Therefore, the model was rebuilt in Stan (Stan Development Team, 2014a)—a Bayesian modeling language whose samplers are non-Gibbs.
##
## TRANSLATING MODEL 'Bayes' FROM Stan CODE TO C++ CODE NOW.
## COMPILING THE C++ CODE FOR MODEL 'Bayes' NOW.
## cygwin warning:
## MS-DOS style path detected: C:/R/RCurrent/R-30~1.2/etc/x64/Makeconf
## Preferred POSIX equivalent is: /cygdrive/c/R/RCurrent/R-30~1.2/etc/x64/Makeconf
## CYGWIN environment variable option "nodosfilewarning" turns off this warning.
## Consult the user's guide for more details about POSIX paths:
## http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
## C:/R/RCurrent/R-3.0.2/library/rstan/include//stansrc/stan/agrad/rev/var_stack.hpp:49:17: warning: 'void stan::agrad::free_memory()' defined but not used [-Wunused-function]
## C:/R/RCurrent/R-3.0.2/library/rstan/include//stansrc/stan/agrad/rev/chainable.hpp:87:17: warning: 'void stan::agrad::set_zero_all_adjoints()' defined but not used [-Wunused-function]
##
## TRANSLATING MODEL 'BayesPlus' FROM Stan CODE TO C++ CODE NOW.
## COMPILING THE C++ CODE FOR MODEL 'BayesPlus' NOW.
## cygwin warning:
## MS-DOS style path detected: C:/R/RCurrent/R-30~1.2/etc/x64/Makeconf
## Preferred POSIX equivalent is: /cygdrive/c/R/RCurrent/R-30~1.2/etc/x64/Makeconf
## CYGWIN environment variable option "nodosfilewarning" turns off this warning.
## Consult the user's guide for more details about POSIX paths:
## http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
## C:/R/RCurrent/R-3.0.2/library/rstan/include//stansrc/stan/agrad/rev/var_stack.hpp:49:17: warning: 'void stan::agrad::free_memory()' defined but not used [-Wunused-function]
## C:/R/RCurrent/R-3.0.2/library/rstan/include//stansrc/stan/agrad/rev/chainable.hpp:87:17: warning: 'void stan::agrad::set_zero_all_adjoints()' defined but not used [-Wunused-function]
## Error in function stan::prob::lognormal_log(N4stan5agrad3varE): Scale parameter is -0.34910650273000776:0, but must be > 0!Rejecting proposed initial value with zero density.
## Error in function stan::prob::lognormal_log(N4stan5agrad3varE): Scale parameter is -1.6172265406146098:0, but must be > 0!Rejecting proposed initial value with zero density.
## Error in function stan::prob::lognormal_log(N4stan5agrad3varE): Scale parameter is -1.2449912107872052:0, but must be > 0!Rejecting proposed initial value with zero density.
## Error in function stan::prob::lognormal_log(N4stan5agrad3varE): Scale parameter is -1.0263658781850085:0, but must be > 0!Rejecting proposed initial value with zero density.
## Error in function stan::prob::lognormal_log(N4stan5agrad3varE): Scale parameter is -1.7036664255500364:0, but must be > 0!Rejecting proposed initial value with zero density.
## Error in function stan::prob::lognormal_log(N4stan5agrad3varE): Scale parameter is -1.7912999475616009:0, but must be > 0!Rejecting proposed initial value with zero density.
## Error in function stan::prob::lognormal_log(N4stan5agrad3varE): Scale parameter is -0.67853001987262718:0, but must be > 0!Rejecting proposed initial value with zero density.
## Error in function stan::prob::lognormal_log(N4stan5agrad3varE): Scale parameter is -0.56723437122169695:0, but must be > 0!Rejecting proposed initial value with zero density.
## Error in function stan::prob::lognormal_log(N4stan5agrad3varE): Scale parameter is -1.9622578028376079:0, but must be > 0!Rejecting proposed initial value with zero density.
## Error in function stan::prob::lognormal_log(N4stan5agrad3varE): Scale parameter is -1.9058489407892381:0, but must be > 0!Rejecting proposed initial value with zero density.
The No-U-Turn-Sampler behind Stan is much more robust to highly autocorrelated parameters…

While I can always knit the Rnw, edit the resulting tex file to remove these lines, and re-texify that file, I would appreciate knowing if there is any way to prevent the need for such post-processing.

@aadler
Copy link
Contributor

aadler commented Feb 19, 2014

Thank you for posting this, Bob.

--Avraham

@maverickg
Copy link
Contributor

Those error messages are from Stan's c++ code. Which one of standard output and standard error output (std::cout or std::cerr) is used for them?

@syclik
Copy link
Member

syclik commented May 22, 2014

@maverickg, it's now piped to Rcpp::Rcout. Let me find the lines that we control that.

@syclik
Copy link
Member

syclik commented May 22, 2014

@aadler, do you have a simple example script that I can try? I have the latest and greatest RStan installed and could see if the refactoring fixed this issue.

@aadler
Copy link
Contributor

aadler commented May 23, 2014

Let me see what I can extract from my .Rnw. I'll send it to you offline once I have something.

@ksvanhorn
Copy link

The optimizing() function in RStan also has this issue -- even with verbose=FALSE, it still prints out a stream of progress messages and parameter settings. I have an application in which I typically run around 1000 calls to optimizing(), each of which takes a few seconds; the voluminous output this produces is a real annoyance.

@aadler
Copy link
Contributor

aadler commented Jan 23, 2015

Personally, I've resorted to just kniting the .Rnw and then open the resulting .tex file in Winedt and deleting all those statements. As things go, it's a minor annoyance which I can live with, although I certainly would appreciate damping them.

Dr. Denwood of runjags was able to dampen all output after I asked a while ago, maybe he has an idea? Then again, runjags is MUCH simpler than stan and lives completely within R, so there probably isn't much comparison.

Avi

@bob-carpenter
Copy link
Author

We'll get this fixed soon. We're taking it on
with our general infrastructure refactoring to try to
share more code among CmdStan, RStan, and PyStan (MatlabStan
and Stan.jl just call CmdStan).

The problem is not in RStan, but in the C++ API for Stan itself.

  • Bob

On Jan 22, 2015, at 7:11 PM, Avraham Adler notifications@github.com wrote:

Personally, I've resorted to just kniting the .Rnw and then open the resulting .tex file in Winedt and deleting all those statements. As things go, it's a minor annoyance which I can live with, although I certainly would appreciate damping them.

Dr. Denwood of runjags was able to dampen all output after I asked a while ago, maybe he has an idea? Then again, runjags is MUCH simpler than stan and lives completely within R, so there probably isn't much comparison.

Avi


Reply to this email directly or view it on GitHub.

@alyst
Copy link

alyst commented Apr 20, 2015

PR #154 partly addresses the issue.

@syclik syclik self-assigned this Feb 5, 2016
@syclik
Copy link
Member

syclik commented Feb 5, 2016

this is my reminder to fix this after the refactor.

@bob-carpenter bob-carpenter added this to the 3.0.0 milestone Feb 5, 2016
@camlionur
Copy link

hi,
I dont want to see details of rstan output while rstan is working. so how can I suppress the Rstan output as seen below? thanks.

AMPLING FOR MODEL 'test_par_est' NOW (CHAIN 1).

Chain 1, Iteration: 1 / 10000 0%
Chain 1, Iteration: 1000 / 10000 10%
Chain 1, Iteration: 2000 / 10000 20%
Chain 1, Iteration: 3000 / 10000 30%
Chain 1, Iteration: 4000 / 10000 40%
Chain 1, Iteration: 5000 / 10000 50%
Chain 1, Iteration: 5001 / 10000 50%
Chain 1, Iteration: 6000 / 10000 60%
Chain 1, Iteration: 7000 / 10000 70%
Chain 1, Iteration: 8000 / 10000 80%
Chain 1, Iteration: 9000 / 10000 90%
Chain 1, Iteration: 10000 / 10000 100%#

Camlionur

@jscamac
Copy link

jscamac commented Apr 12, 2016

@camlionur You can just set refresh to zero. Like below.
i.e.

  fit <-list(stan_data = stan_data, 
             fit = stan(model_code = model, 
                        data = stan_data, 
                        chains = 3,
                        iter = 10000,
                        refresh = 0))

@cathyleeyy
Copy link

@jscamac Do you know how I can suppress the sampling message in rstan::sampling()?

@jscamac
Copy link

jscamac commented Apr 13, 2016

@cathyleeyy I'm presuming you are referring to the types of diagnostic messages that occur during sampling. The same messages that @bob-carpenter and @aadler are referring to at the start of this issue? If so then no... this is still a work in progress I think.

@bob-carpenter
Copy link
Author

refresh=-1 will suppress those updates, but there's still a bit
of output trickling through.

We should get the rest suppressed soon.

  • Bob

On Apr 13, 2016, at 5:37 AM, Cathy Yuen Yi Lee notifications@github.com wrote:

@jscamac Do you know how I can suppress the sampling message in rstan::sampling()?


You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub

@camlionur
Copy link

Thank you so much for your help.
Onur

Date: Wed, 13 Apr 2016 09:26:33 -0700
From: notifications@github.com
To: rstan@noreply.github.com
CC: camlionur@hotmail.com
Subject: Re: [stan-dev/rstan] suppress all output from RStan (#49)

refresh=-1 will suppress those updates, but there's still a bit

of output trickling through.

We should get the rest suppressed soon.

  • Bob

On Apr 13, 2016, at 5:37 AM, Cathy Yuen Yi Lee notifications@github.com wrote:

@jscamac Do you know how I can suppress the sampling message in rstan::sampling()?

You are receiving this because you authored the thread.

Reply to this email directly or view it on GitHub


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub

@camlionur
Copy link

Thanks a lot for your help.
Onur

Date: Tue, 12 Apr 2016 15:08:08 -0700
From: notifications@github.com
To: rstan@noreply.github.com
CC: camlionur@hotmail.com
Subject: Re: [stan-dev/rstan] suppress all output from RStan (#49)

You can just set refresh to zero. Like below.

i.e.

fit <-list(stan_data = stan_data,
fit = stan(model_code = model,
data = stan_data,
chains = 3,
iter = 10000,
refresh = 0))


You are receiving this because you commented.
Reply to this email directly or view it on GitHub

@Ic3fr0g
Copy link

Ic3fr0g commented May 19, 2017

Hi,
Was using Facebook's prophet recently which calls rstan::optimizing and rstan::sampling. Setting verbose to False still gives a parametric output. Reading all the comments on this issue I tried passing refresh = 0 and refresh = -1. Neither worked as some output still trickled.
Other things I tried - invisible() and capture.output(). No success.
It would really help if this could be fixed.

@andersgs
Copy link

What I do is run the code live in my Rmd file, I set the model fitting chunk to eval=FALSE, save the output to an Rdata object, and in a hidden chunk I load it back. That way, I don't have to wait for the model to run when knitting the file.

For instance:

     ```{r, fit_stan, eval = FALSE}
      model_code <- "model code goes here"
      model_data <- list( model data )
      model_fit <- rstan::stan(...)
      save(model_fit, "model_fit.Rdata")
     ```

      ```{r, load_model_fit, echo = FALSE}
       load("model_fit.Rdata")
      ```

      ```{r, model_summary}
       print(model_fit, digit_summary=3)
      ```

@bgoodri
Copy link
Contributor

bgoodri commented Jun 28, 2017 via email

@isabelmgao
Copy link

Are there any updates on this (and for pystan rather than rstan)? I'm trying to use FB Prophet in custom code where stdout is directly streamed to a Hive table, and therefore I need to suppress only the pystan output (rather than suppressing all stdout). Any updates on this would be greatly, greatly appreciated!!

@bob-carpenter
Copy link
Author

@bgoodri or @ariddell --- any idea if this is Is this an RStan issue in particular or is there something we need to do down at the Stan level? I know we'll tackle this in Stan 3, but it'd be nice to get a temporary fix in through the low-level services if that's where it needs to be done. If so, this issue should be moved to stan-dev/stan.

@bob-carpenter
Copy link
Author

@isabelmgao Streaming stdout to a table seems dicey in any situation. I'm not sure if PyStan (or Prophet) lets you stream the draws to a CSV file, but it'd be safer to take that stream as output.

@syclik
Copy link
Member

syclik commented Jul 27, 2017 via email

@davidaknowles
Copy link

I'm having issues with this using rstan::optimizing. My use case involves fitting ~ a million small models so even one line of output becomes troublesome. I can wrap everything in sink()s but then I miss any actual useful output (and if I just wrap the optimizing() call I get issue with opening too many file handles).

@bob-carpenter
Copy link
Author

@davidaknowles Which output do you consider useful and which do you want to turn off?

@davidaknowles
Copy link

I want to be able to turn everything off. The stuff that leaks through for me even with verbose=F and refresh=-1 is e.g.

Initial log joint probability = -0.0650546
Optimization terminated normally: 
  Convergence detected: gradient norm is below tolerance

@bob-carpenter
Copy link
Author

Thanks---we will eventually sort out how to really turn everything off in RStan.

I'm confused about the message before last, where you were worried about losing useful output. If we have a mode to turn everything off, everything will get turned off. My question was more whether there is some output you did not want to turn off?

@davidaknowles
Copy link

Ah sorry, I wasn't very clear. It's not output from stan I'm worried about losing. My current solution to getting two much stdout from rstan::optimizing is to wrap everything using sink, e.g.

zz <- file( "/dev/null", open = "wt")
sink(zz)
sink(zz, type = "message")
# my big loop containing rstan::optimizing calls
sink(type="message")
sink()

This is a hack because anything that I print() in the loop gets lost, including warnings/errors. The former issue I could deal with by logging to file rather than console, but it doesn't help with seeing warnings/errors.

@bob-carpenter
Copy link
Author

bob-carpenter commented Sep 22, 2017 via email

@davidaknowles
Copy link

So if I do this for example:

zz <- file( "/dev/null", open = "wt")
sink(zz)
sink(zz, type = "message")
for(i in 1:N) {
    if (i %% 100==0) cat("Processing gene",i,"\n")
    rstan::optimizing(...)
}
sink(type="message")
sink()

I will not see the output from cat because of the sink(). So that's one type of output I want (my own). The only output I would want to see from stan itself are errors (e.g. -Inf log likelihood at the initialization).

@bob-carpenter
Copy link
Author

Got it. Thanks for bearing with me.

@cdriveraus
Copy link

It would be nice if compilation errors / notes could use the message feature of R, as messages can be easily suppressed.

@wjakethompson
Copy link

I am having a problem similar to those of people above in that some output is still leaking through. Is there a way to suppress the gradient information and elapsed time in addition to the updates that are suppressed with refresh = -1. For example:

library(rstanarm)
#> Loading required package: Rcpp
#> rstanarm (Version 2.17.4, packaged: 2018-04-13 01:51:52 UTC)
#> - Do not expect the default priors to remain the same in future rstanarm versions.
#> Thus, R scripts should specify priors explicitly, even if they are just the defaults.
#> - For execution on a local, multicore CPU with excess RAM we recommend calling
#> options(mc.cores = parallel::detectCores())
#> - Plotting theme set to bayesplot::theme_default().

data(kidiq)
kidiq$rand <- rnorm(nrow(kidiq))
post1 <- stan_glm(kid_score ~ mom_hs, data = kidiq,
  family = gaussian(link = "identity"), chains = 4, iter = 2000, refresh = -1)
#> 
#> Gradient evaluation took 4.5e-05 seconds
#> 1000 transitions using 10 leapfrog steps per transition would take 0.45 seconds.
#> Adjust your expectations accordingly!
#> 
#> 
#> 
#>  Elapsed Time: 0.238801 seconds (Warm-up)
#>                0.223752 seconds (Sampling)
#>                0.462553 seconds (Total)
#> 
#> 
#> Gradient evaluation took 4.5e-05 seconds
#> 1000 transitions using 10 leapfrog steps per transition would take 0.45 seconds.
#> Adjust your expectations accordingly!
#> 
#> 
#> 
#>  Elapsed Time: 0.245983 seconds (Warm-up)
#>                0.224522 seconds (Sampling)
#>                0.470505 seconds (Total)
#> 
#> 
#> Gradient evaluation took 3.2e-05 seconds
#> 1000 transitions using 10 leapfrog steps per transition would take 0.32 seconds.
#> Adjust your expectations accordingly!
#> 
#> 
#> 
#>  Elapsed Time: 0.294125 seconds (Warm-up)
#>                0.240587 seconds (Sampling)
#>                0.534712 seconds (Total)
#> 
#> 
#> Gradient evaluation took 5.5e-05 seconds
#> 1000 transitions using 10 leapfrog steps per transition would take 0.55 seconds.
#> Adjust your expectations accordingly!
#> 
#> 
#> 
#>  Elapsed Time: 0.306703 seconds (Warm-up)
#>                0.223262 seconds (Sampling)
#>                0.529965 seconds (Total)

In my ideal world, there would be no output printed when calling the function.

@bgoodri
Copy link
Contributor

bgoodri commented Jun 3, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests