StanSample.jl can't find cmdstan?

I am having trouble getting a Stan model to run through Julia. I have installed cmdstan following these instructions. cmdstan is now installed in /home/isaac/.conda/envs/stan-env/bin/cmdstan. I have added export CMDSTAN=/home/isaac/.conda/envs/stan-env/bin/cmdstan to ~/.bashrc and have also added ENV["CMDSTAN"]="/home/isaac/.conda/envs/stan-env/bin/cmdstan" to /home/isaac/packages/julias/julia-1.7/etc/julia/startup.jl following these instructions. I am currently running the example model provided here. When I attempt

sm = SampleModel("bernoulli", model);

I get the following stack trace:

julia> sm = SampleModel("bernoulli", model);

/tmp/jl_auygYa/bernoulli.stan updated.
ERROR: IOError: could not spawn `make -f /home/isaac/.conda/envs/stan-env/bin/cmdstan/makefile -C /home/isaac/.conda/envs/stan-env/bin/cmdstan /tmp/jl_auygYa/bernoulli`: no such file or directory (ENOENT)
Stacktrace:
  [1] _spawn_primitive(file::String, cmd::Cmd, stdio::Vector{Any})
    @ Base ./process.jl:100
  [2] #690
    @ ./process.jl:113 [inlined]
  [3] setup_stdios(f::Base.var"#690#691"{Cmd}, stdios::Vector{Any})
    @ Base ./process.jl:197
  [4] _spawn
    @ ./process.jl:112 [inlined]
  [5] _spawn(::Base.CmdRedirect, ::Vector{Any})
    @ Base ./process.jl:140
  [6] _spawn
    @ ./process.jl:107 [inlined]
  [7] success(cmd::Base.CmdRedirect)
    @ Base ./process.jl:502
  [8] (::StanSample.var"#25#26"{IOBuffer, String, String})()
    @ StanSample ~/.julia/packages/StanSample/Ka9pQ/src/stanmodel/SampleModel.jl:102
  [9] cd(f::StanSample.var"#25#26"{IOBuffer, String, String}, dir::String)
    @ Base.Filesystem ./file.jl:110
 [10] SampleModel(name::String, model::String, tmpdir::String)
    @ StanSample ~/.julia/packages/StanSample/Ka9pQ/src/stanmodel/SampleModel.jl:101
 [11] SampleModel(name::String, model::String)
    @ StanSample ~/.julia/packages/StanSample/Ka9pQ/src/stanmodel/SampleModel.jl:92
 [12] top-level scope
    @ REPL[3]:1

I am not good at understanding stack traces, and so am unsure where to go from here. I have a hunch that I have somehow misunderstood how to add cmdstan to the environment.

I often have trouble setting up the path in startup.jl and .bashrc. Lately, I have been adding the path directly to my script. Do you get the same error when you add ENV["CMDSTAN"]="/home/isaac/.conda/envs/stan-env/bin/cmdstan" to your script?

After adding ENV["CMDSTAN"]="/home/isaac/.conda/envs/stan-env/bin/cmdstan" as the first line of the script I still get the same error.

These are the contents of the path above. Maybe I am pointing to the wrong directory somehow?

> ls -al /home/isaac/.conda/envs/stan-env/bin/cmdstan
total 72
drwxr-sr-x 1 isaac isaac   404 Jan 29 18:44 .
drwxr-sr-x 1 isaac isaac  3996 Jan 29 18:44 ..
drwxr-sr-x 1 isaac isaac   138 Jan 29 18:44 bin
-rwxrwxr-x 1 isaac isaac  3090 Jan 29 18:44 build_env_setup.sh
-rwxrwxr-x 2 isaac isaac  2932 Nov 23 06:23 .clang-format
-rwxrwxr-x 2 isaac isaac  1429 Nov 23 06:23 conda_build.sh
drwxr-sr-x 1 isaac isaac    18 Jan 29 18:44 examples
drwxr-sr-x 1 isaac isaac   152 Jan 29 18:44 .github
-rwxrwxr-x 2 isaac isaac   499 Nov 23 06:23 .gitignore
-rwxrwxr-x 2 isaac isaac    79 Nov 23 06:23 .gitmodules
-rwxrwxr-x 2 isaac isaac   377 Nov 23 06:23 install-tbb.bat
-rwxrwxr-x 2 isaac isaac 10808 Nov 23 06:23 Jenkinsfile
drwxr-sr-x 1 isaac isaac    52 Jan 29 18:44 lib
-rwxrwxr-x 2 isaac isaac  1480 Nov 23 06:23 LICENSE
drwxr-sr-x 1 isaac isaac    84 Jan 29 18:44 make
-rwxrwxr-x 2 isaac isaac 12017 Nov 23 06:23 makefile
-rwxrwxr-x 2 isaac isaac  1505 Nov 23 06:23 metadata_conda_debug.yaml
-rwxrwxr-x 2 isaac isaac  3199 Nov 23 06:23 README.md
-rwxrwxr-x 1 isaac isaac  6397 Jan 29 18:44 runCmdStanTests.py
drwxr-sr-x 1 isaac isaac    14 Jan 29 18:44 src
drwxr-sr-x 1 isaac isaac   250 Jan 29 18:44 stan
-rwxrwxr-x 2 isaac isaac   580 Nov 23 06:23 test-all.sh

So I think I have figured out the short term solution, though I am unsure how it will affect working directories and Julia environments.

I run

> conda activate stan-env
> julia
julia> using StanSample, DataFrames
julia>  model = "
data { 
 int<lower=0> N; 
 int<lower=0,upper=1> y[N];
} 
parameters {
 real<lower=0,upper=1> theta;
} 
model {
 theta ~ beta(1,1);
   y ~ bernoulli(theta);
}
";
julia> sm = SampleModel("bernoulli", model);
julia> data = Dict("N" => 10, "y" => [0, 1, 0, 1, 0, 0, 0, 0, 0, 1]);
julia> rc = stan_sample(sm; data, num_threads=1);
julia> if success(rc)
 df = read_samples(sm, :dataframe);
 df |> display
end

I don’t run into any errors this way.

1 Like

Out of curiosity: why not Turing? It saves having to deal with a C++, R, Conda stack and instead works with just Julia (at least on Julia 1.6. 1.7 not yet)

A couple of reasons. 1) I had never heard of Turing before. 2) This is for a class, where the instruction is in R using RstanARM. Stan.jl seemed to be the closest, though it looks like Turing will be good for what I need. It certainly seems more straightforward to work with.

Turing should be fine for most things yes, but depending on your class can take some time to get used to. My 2cents are to use Pluto and ask here on Discourse with a turing tag if problems come up. Good luck with the assignment

1 Like