CBIOMES / bayesian_cbiomes

notebooks and files for the Bayesian CBIOMES workshop
11 stars 18 forks source link

Tough time running Stan in Julia #1

Open gregbritten opened 5 years ago

gregbritten commented 5 years ago

Wu (lab mate) and I tried installing and running Stan in Julia yesterday and had a bitch of a time. It's still not working on my PC. Wu was able to get it to work on his Mac after having to fix several weird paths and settings. (I remember folks in the past having a harder time with Julia). Could you give it a go on your system and let me know if you run into issues? We'll have to sort out potential issues if we're going to support Julia in the workshop.. I'm going to give it another go on my PC and will post a question to the author of CmdStan.jl/Stan.jl I'm also going to try it on my old Mac to see how it goes.

jpmattern commented 5 years ago

Alright, trying it out on two Linux machines (office computer running Julia 1.0.3 and cluster main node running Julia 1.1.1).

On office computer I am having a bad start:

julia> using Stan
[ Info: Precompiling Stan [682df890-35be-576f-97d0-3d8c8b33a550]
ERROR: LoadError: No deps.jl file could be found. Please try running Pkg.build("Arpack").
Currently, the build command might fail when Julia has been built from source
and the recommendation is to use the official binaries from julialang.org.
For more info see https://github.com/JuliaLinearAlgebra/Arpack.jl/issues/5.

On the cluster I am progressing a bit farther bu then I run into this error:

ERROR: The environment variable JULIA_CMDSTAN_HOME needs to be set.

... which tell me that cmdstan is needed to run Stan and I cannot do that on the cluster computer (though I could perhaps request it). BTW, I was trying to run this example

gregbritten commented 5 years ago

Indeed. CmdStan.jl and Stan.jl (both seem to do the same thing - I think perhaps Stan.jl is the new package still in development) are just Julia interfaces for cmdstan which is a package to run Stan at command line.

For whatever reason I cannot get CmdStan.jl to start cmdstan.. I fix one error and another pops up.. over and over...

jpmattern commented 5 years ago

OK, I'll try a bit more as well...

BTW, I noticed that you are writing the installation instructions based on the OS first. I'll follow that example and start writing instructions for Linux...

jpmattern commented 5 years ago

I tried again and success! I downloaded the latest binary of Julia (version 1.2.0) and used that. But I needed to start in bash:

cd /some/path
git clone https://github.com/stan-dev/cmdstan.git --recursive
cd cmdstan
make build
export JULIA_CMDSTAN_HOME=/some/path/cmdstan

Now in Julia:

(v1.2) pkg> add Stan
(v1.2) pkg> add Arpack
(v1.2) pkg> build Arpack
julia> using Arpack
[ Info: Precompiling Arpack [7d9fca2a-8960-54d3-9f78-7d1dccf2cb97]
julia> using Stan
[ Info: Precompiling Stan [682df890-35be-576f-97d0-3d8c8b33a550]
[ Info: Loading DataFrames support into Gadfly.jl
(v1.2) pkg> add StatsPlots
(v1.2) pkg> add StanSample

Finally, and I am not sure if this step is needed but I did it:

julia> using StanSample
julia> StanSample.set_cmdstan_home!("/some/path/cmdstan/")

After that I was able to run the example.

jpmattern commented 5 years ago

Hmmm... one odd complication is apparently that "Stan" requires CMDSTAN_HOME to be set, while "StanSample" relies on JULIA_CMDSTAN_HOME. Am I correct in assuming, that we'll be using StanSample and that Stan is some future version?

gregbritten commented 5 years ago

Great to know that you had relatively minimal issues.. although Arpack was a new twist. My PC is still stuck, but no problems on my Mac (after setting CMDSTAN_HOME and JULIA_CMDSTAN_HOME). I still don't know why I opted for a PC laptop... :-S Anyway, I will keep working to see if I can isolate the PC issue, and I will try on another PC to see if the headaches appear consistently.. and whether we will be able to give the group a robust set of instructions for the PC/Julia combination.

As for StanSample.jl vs. Stan.jl, I'm not too sure, because I see StanSample examples on the Stan.jl GitHub.. and I also see the authors note that big changes are coming to Stan.jl so perhaps you are right that it is a work in progress, and perhaps StanSample.jl will become part of Stan.jl. CmdStan.jl seems to be the older and more common when I Google around, so we may want to try working with that too..

I'll keep the issue open until I have more PC updates. For now I am going to continue working on the introduction to Bayes/Stan notebook as that is more fun and less frustrating..

jpmattern commented 5 years ago

Well, good to hear you got it working on the Mac at least, i think the combo Windows+Julia might be pretty rare, though we should have probably asked that in the survey. We could still send out an email asking everyone about their OS...

gaelforget commented 5 years ago

Not sure if this will help (and maybe it's already been discussed above) but one possibility could be to use an external installation (done via Conda or pip) through PyCall / pyImport.

That approach has worked for me in order to call pycmap in julia (see DarwinCmapJulia.ipynb in https://github.com/gaelforget/Cbiomes2019Notebooks) and also recently to use Cartopy in Julia (see src/PlotIndDisp.jl in https://github.com/gaelforget/IndividualDisplacements.jl).

Just in case this is useful...

gaelforget commented 5 years ago

Please edit the issue title to something like Tough time running Stan in Julia. Looking at the current title of this issue in my GitHub activity feed makes me incomfortable.

Please, as soon as you get a chance

Thanks @jpmattern for your prompt intervention

jpmattern commented 5 years ago

Haha, edited the title. I, too, got confused when I had a tab open that said "Bitch".

Gael, would you say your approach is easy enough for a bunch of participants to copy? You are using a mac, I assume?

gregbritten commented 5 years ago

Interesting, @gaelforget so you are suggesting that we interact with PyStan from Jupyter via PyCall? That may end up saving us some headaches because PyStan is quite a bit better developed than Stan.jl/CmdStan.jl/StanSample.jl which are all various wrappers for running Stan at command line outside of Julia. This intermediate wrapper step seems to be a bit finicky... If PyCall works well, then I think we would benefit from the greater robustness of PyStan.

gregbritten commented 5 years ago

And my apologies for making you guys uncomfortable with my bad language :-P

gregbritten commented 5 years ago

I am also noticing an RCall package!

gaelforget commented 5 years ago

would you say your approach is easy enough for a bunch of participants to copy?

Don't know -- calling c++ from Julia via python or R is probably never going to be easy enough

You are using a mac, I assume?

yes although my DarwinCmapJulia.ipynb example also has worked using a binder cloud instance which ran in linux I think (you can try via the badge; please post an issue to the repo if that fails)

are suggesting that we interact with PyStan from Jupyter via PyCall?

Not sure but figured you might want to give it a try

AWO-code commented 4 years ago

I am trying to install and run Stan in Julia right now. I ran into the JULIA_CMDSTAN_HOME issue. So how do you set JULIA_CMDSTAN_HOME?

gregbritten commented 4 years ago

This will probably fix you up: https://github.com/StanJulia/CmdStan.jl/blob/master/docs/src/INSTALLATION.md

We will add these steps to the install instructions...

AWO-code commented 4 years ago

OK, thanks! Now, I am getting the following scary error message:

ERROR: error when compiling Stan model at /var/folders/bm/6jw569yj77l9l56ql9s5lrth0000gn/T/jl_0xFUWG/mod.stan (CmdStan home: /Users/omta/Julia/Stan/cmdstan): fatal error: malformed or corrupted AST file: 'could not find file '/Users/omta/Stan/cmdstan/stan/lib/stan_math/lib/boost_1.69.0/boost/exception/detail/clone_current_exception.hpp' referenced by AST file 'stan/src/stan/model/model_header.hpp.gch'' 1 error generated. make: *** [/var/folders/bm/6jw569yj77l9l56ql9s5lrth0000gn/T/jl_0xFUWG/mod] Error 1

gregbritten commented 4 years ago

Yikes. Did a bit of Googling and it seems that 'malformed or corrupted AST file' is associated with a failure of your Xcode C++ compiler. (Oddly I did not find anyone reporting this error using Stan). Did you update Xcode or iOS recently? If not, perhaps try updating Xcode? Maybe @gaelforget has some insight on how Julia interacts with Xcode in iOS?

AndrewIrwin commented 4 years ago

I've tried a new installation using the steps on this page for stan + Julia on OSX / MacOS. Similar problem as above ERROR: The environment variable JULIA_CMDSTAN_HOME needs to be set. ERROR: UndefVarError: mod_compile not defined Adding instructions for setting the environment variable (from within Julia, or just in the shell?) to the instructions would be helpful.

AndrewIrwin commented 4 years ago

All good after adding JULIA_CMDSTAN_HOME=~/cmdstan to .bash_profile

maxEntropyProd commented 4 years ago

I had no problem running STAN under python via Jupyter under Windows, but I was unable to even build cmdstan for "StanSample.jl". I haven't used Anaconda on Windows (or anywhere), but it does not seem to install basic linux utils. I used conda to install make from anaconda channel, but it complains about not finding /bin/sh when attempting to execute make build, so it's expecting at least some minimal bash shell.

I uninstalled the anaconda make and installed one under the conda-forge channel. It got a bit farther in some ways but failed issuing the following:

(base) PS C:\users\jvallino\stan\cmdstan> make build
'cut' is not recognized as an internal or external command,
operable program or batch file.
'cut' is not recognized as an internal or external command,
operable program or batch file.
File not found - *cvodes*.hpp
File not found - *idas*.hpp
File not found - *kinsol*.hpp
File not found - *_inst.cpp
File not found - *_def.cpp
File not found - *.hpp
g++ -std=c++1y -m64 -D_REENTRANT -Wall -Wno-unused-function -Wno-uninitialized -Wno-unused-but-set-variable -Wno-unused-variable -Wno-sign-compare -Wno-unused-local-typedefs      -I stan/lib/stan_math/lib/tbb_2019_U8/include -O3 -I src -I stan/src -I lib/rapidjson_1.1.0/ -I stan/lib/stan_math/ -I stan/lib/stan_math/lib/eigen_3.3.3 -I stan/lib/stan_math/lib/boost_1.72.0 -I stan/lib/stan_math/lib/sundials_4.1.0/include  -D_USE_MATH_DEFINES  -DBOOST_DISABLE_ASSERTS      -c -MT src/cmdstan/stanc.o -M -E -MG -MP -MF src/cmdstan/stanc.d src/cmdstan/stanc.cpp
'cut' is not recognized as an internal or external command,
operable program or batch file.
'cut' is not recognized as an internal or external command,
operable program or batch file.
File not found - *cvodes*.hpp
File not found - *idas*.hpp
File not found - *kinsol*.hpp
File not found - *_inst.cpp
File not found - *_def.cpp
File not found - *.hpp
The syntax of the command is incorrect.
make: *** [make/stanc:29: bin/stanc.exe] Error 1

g++ seems to run OK, but I don't see any binary produced. I also don't understand the complaint about 'cut' from make. It also looks like it's having problems finding headers for sundials, given cvoids.hpp and similar files not being found. I will continue to mess around (at least for a bit), but any ideas are appreciated.

gaelforget commented 4 years ago

Example worked fine for me too (once JULIA_CMDSTAN_HOME / CMDSTAN_HOME is set) except that for some reason I cannot update DataFrames.jl to its v0.20.0 and the read_summary call returns an error with v0.19.4 of DataFrames.jl.

ERROR: function rename! does not accept keyword arguments

In case others are in the same situation, here is a work-around :

using CSV, DataFrames

function read_summary_gf(m::T) where {T <: CmdStanModels}
         fname = "$(m.output_base)_summary.csv"
         !isdir(fname) && stan_summary(m)

         df = CSV.read(fname, delim=",", comment="#")

         bnames=names(df)
         cnames = lowercase.(convert.(String, String.(bnames)))
         cnames[1] = "parameters"
         cnames[4] = "std"
         cnames[8] = "ess"
         tmp=Dict(bnames[i] => Symbol(cnames[i]) for i=1:length(bnames))
         rename!(df, tmp)

         df[!, :parameters] = Symbol.(df[!, :parameters])

         ChainDataFrame("CmdStan Summary", df)
end   # end of read_samples

println(read_summary_gf(mod_compile));
maxEntropyProd commented 4 years ago

I thought I was making progress

  1. I was able to compile cmdstan under Ubuntu under Windows 10 (but had to still install make and g++)

  2. I had to also install CSV, DataFrames, etc packages, but that worked fine.

  3. Had the env problems, but under Jupyter, I added ENV["JULIA_CMDSTAN_HOME"]="C:\Users\jvallino\GitHub\bayesian_cbiomes\cmdstan" etc,

  4. But when I got to: mod = SampleModel("linear_reg", stancode), I still get this same strange error:

C:\Users\jvallino\AppData\Local\Temp\jl_QoN1gF\linear_reg.stan updated.
error when compiling Stan model at C:\Users\jvallino\AppData\Local\Temp\jl_QoN1gF\linear_reg.stan
    (CmdStan home: C:\Users\jvallino\GitHub\bayesian_cbiomes\cmdstan):
'cut' is not recognized as an internal or external command,
operable program or batch file.
'cut' is not recognized as an internal or external command,
operable program or batch file.
File not found - *cvodes*.hpp
File not found - *idas*.hpp
File not found - *kinsol*.hpp
File not found - *_inst.cpp
File not found - *_def.cpp
File not found - *.hpp
The syntax of the command is incorrect.
make: *** [make/stanc:29: bin/stanc.exe] Error 1

Stacktrace:
 [1] ensure_executable(::StanModel{String}) at C:\Users\jvallino\.julia\packages\StanRun\E3YcJ\src\StanRun.jl:123
 [2] #SampleModel#6(::StanSample.Sample, ::StanBase.RandomSeed, ::StanBase.Init, ::StanBase.Output, ::String, ::Type{SampleModel}, ::String, ::String, ::Array{Int64,1}) at C:\Users\jvallino\.julia\packages\StanSample\e3hsD\src\stanmodel\SampleModel.jl:57
 [3] SampleModel at C:\Users\jvallino\.julia\packages\StanSample\e3hsD\src\stanmodel\SampleModel.jl:51 [inlined] (repeats 2 times)
 [4] top-level scope at In[11]:1

Seems like there is something I need to add under the Anaconda environment (like the set of linux tools)

gregbritten commented 4 years ago

Joe, haven't seen this error pop up yet.. Unfortunately the connection between Julia and cmdstan has been producing some incomprehensible errors. If issues persist I recommend sticking with your working PyStan implementation.

maxEntropyProd commented 4 years ago

Right. I'm now trying to go entirely via the Linux windows subsystem instead of trying to get windows and Linux to play well together. But, just getting off train so had to stop, but will pick up at meeting

Thanks!

On Wed, Jan 8, 2020, 6:54 AM Gregory L Britten notifications@github.com wrote:

Joe, haven't seen this error pop up yet.. Unfortunately the connection between Julia and cmdstan has been producing some incomprehensible errors. If issues persist I recommend sticking with your working PyStan implementation.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/jpmattern/bayesian_cbiomes/issues/1?email_source=notifications&email_token=AEAHGSRC2S7JJN25FNNJ5OTQ4W5IBA5CNFSM4JDEDOFKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEIMEPAY#issuecomment-572016515, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEAHGSWGTKAPVW5X5PGM34LQ4W5IBANCNFSM4JDEDOFA .