StanJulia / StanSample.jl

WIP: Wrapper package for the sample method in Stan's cmdstan executable.
MIT License
18 stars 4 forks source link

Progress is not printed in the REPL #66

Closed FelixNoessler closed 1 year ago

FelixNoessler commented 1 year ago

Hello,

I manually installed Cmdstan in windows 11 and it works together with StanSample.jl. However, for long runs it is nice to see the progress printed to the console. At the moment, only warnings are printed. I know that I can look into the log file and see the progress there, but this is a bit cumbersome. The progress is printed when I use cmdstan directly. Is there an option to print the progress directly in the console with StanSample.jl?

Thank you for creating and maintaining the package!

Best, Felix

I tried the example code (but even for more complex models the progress is not printed):

using StanSample
using DataFrames

bernoulli_model = "
data {
  int<lower=1> N;
  int<lower=0,upper=1> y[N];
}
parameters {
  real<lower=0,upper=1> theta;
}
model {
  theta ~ beta(1,1);
  y ~ bernoulli(theta);
}
";

data = Dict("N" => 10, "y" => [0, 1, 0, 1, 0, 0, 0, 0, 0, 1])

sm = SampleModel("bernoulli", bernoulli_model);

rc = stan_sample(sm; 
                 data,
                 refresh=100,
                 num_samples=2000);

read_samples(sm, :dataframe)

Julia Version 1.8.2
Commit 36034abf26 (2022-09-29 15:21 UTC)
Platform Info:
OS: Windows (x86_64-w64-mingw32)
CPU: 8 × 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-13.0.1 (ORCJIT, tigerlake)

goedman commented 1 year ago

Hi Felix (@FelixNoessler) Thank you for the request. I'll take a look. I tried this many years ago (maybe 10 years or so) but didn't come up with a good solution for this. In principle it should be possible I think, but no guarantees ...

FelixNoessler commented 1 year ago

Hello Rob,

when I remove the stdout= part, then everything is printed to the console. Is it possible to include an option (keyword argument?) whether to print the log to the console or to the log file? Or even save it to file and print it?

https://github.com/StanJulia/StanSample.jl/blob/e292f5936ef3210ae9e9030a49926c4ab80c502d/src/stanrun/stan_run.jl#L129

goedman commented 1 year ago

I think I tried the 'tee' command to duplicate the streams. Maybe I'll try your suggestion initially. I am a bit busy right now, might take a few days.

goedman commented 1 year ago

When you say "save it and print it", do you mean print the file repeatedly? It is updated by multiple threads.

FelixNoessler commented 1 year ago

I think I tried the 'tee' command to duplicate the streams. Maybe I'll try your suggestion initially. I am a bit busy right now, might take a few days.

No worries :)

When you say "save it and print it", do you mean print the file repeatedly? It is updated by multiple threads.

No, I dont want to print it several times.

The idea with tee is quite good, this works:

run(pipeline(par(m.cmds); stdout=`tee -a $(m.log_file[1])`))
goedman commented 1 year ago

Great, thanks for testing that. I'll enable the tee version using a keyword argument.

goedman commented 1 year ago

Hi @FelixNoessler Just pushed StanSample.jl v6.13.6 which has a new keyword show_logging. E.g. in Pluto:

let
    m_schools = SampleModel("eight_schools", stan_schools)
    rc = stan_sample(m_schools; data, num_chains=5, use_cpp_chains=true, show_logging=true)
end;

That will hopefully work for you.

FelixNoessler commented 1 year ago

Thanks, that is perfect and was very quick! :)

goedman commented 1 year ago

Thanks @FelixNoessler

I'll update the README in the next release mentioning your contribution. Don't hear to often from folks on Windows so it is always nice to get confirmation it works ok!