StanJulia / CmdStan.jl

CmdStan.jl v6 provides an alternative, older Julia wrapper to Stan's `cmdstan` executable. CmdStan will be deprecated in 2022.
MIT License
30 stars 12 forks source link

runlog and out.txt save to wrong location #119

Closed itsdfish closed 2 years ago

itsdfish commented 2 years ago

Hi Rob,

Hopefully, this is not user error this time. run.log and out.txt save to project_dir rather than project_dir/temp. I'm not sure if this is intentional, but I think it would be better to save to the temp folder. Here is a MWE:

using CmdStan

ProjDir = @__DIR__
cd(ProjDir)
stan_path = "/home/dfish/cmdstan"

  bernoullimodel = "
  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);
  }
  ";

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

  # Preserve these variables outside cd/do block.
  global stanmodel, rc, samples, cnames, df

  stanmodel = Stanmodel(name="bernoulli", model=bernoullimodel,
    printsummary=false, output_format = :mcmcchains);

  rc, samples, cnames = stan(stanmodel, observeddata, ProjDir,
    CmdStanDir=stan_path);
goedman commented 2 years ago

Hi Chris,

Yes, that would definitely be better (and has been taken care of in StanSample.jl).

If you really need it, I can try to set some time aside to fix this, but I would really suggest to switch to StanSample.jl.

I've kept CmdStan.jl around mainly for DiffEqBayes.jl, but DiffEqBayes has now also switched to StanSample.jl. I believe Turing doesn't use it anymore either (like they did until 1 or 2 years ago?).

But nothing is cast in concrete...

Let me know what you think. Below the StanSample version of your example (I think!).

Rob

using StanSample, MCMCChains

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);
}
";

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

# Keep tmpdir across multiple runs to prevent re-compilation
#tmpdir = joinpath(@__DIR__, "tmp")

sm = SampleModel("bernoulli", bernoulli_model;
    #tmpdir = tmpdir,
);

rc = stan_sample(sm; data=bernoulli_data);

if success(rc)
  chns = read_samples(sm, :mcmcchains)
  chns |> display
  println()

  read_summary(sm) |> display
end
itsdfish commented 2 years ago

Thanks for getting back to me. I am switching to stansample. The only thing that threw me off is that nchains is a vector. Is there a reason for that?

On Wed, Oct 13, 2021, 10:27 AM Rob J Goedman @.***> wrote:

Hi Chris,

Yes, that would definitely be better (and has been taken care of in StanSample.jl).

If you really need it, I can try to set some time aside to fix this, but I would really suggest to switch to StanSample.jl.

I've kept CmdStan.jl around mainly for DiffEqBayes.jl, but DiffEqBayes has now also switched to StanSample.jl. I believe Turing doesn't use it anymore either (like they did until 1 or 2 years ago?).

But nothing is cast in concrete...

Let me know what you think. Below the StanSample version of your example (I think!).

Rob

using StanSample, MCMCChains

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

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

Keep tmpdir across multiple runs to prevent re-compilation

tmpdir = joinpath(@DIR, "tmp")

sm = SampleModel("bernoulli", bernoulli_model;

tmpdir = tmpdir,

);

rc = stan_sample(sm; data=bernoulli_data);

if success(rc) chns = read_samples(sm, :mcmcchains) chns |> display println()

read_summary(sm) |> display end

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/StanJulia/CmdStan.jl/issues/119#issuecomment-942363249, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACJ2TADIXIRDZ3LFQSE3KS3UGWJMLANCNFSM5F42IH7Q . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

goedman commented 2 years ago

I believe I can't change it later on after the SampleModel is created. I would love to get rid of this and just turn it into an Int.