This is a short doc explaining how PyStan can be run in Atom on a Windows machine.
PyStan doesn't automatically run in Atom even if we use script to pick the correct python.exe file from the environment we have installed Stan in. The reason is that Windows is very sensitive to the activation of an environment. If the environment is not explicitly activated before running the PyStan code in Atom compiling errors will occur.
Install PyStan as explained here.
Install Atom. Atom will be added to your path variable during the installation.
Launch Atom in your activated Stan environment as follows:
Open Anaconda Prompt or Command Prompt.
- First time only on Command Prompt: Initialise anaconda on the Command Prompt by typing
conda init
. This will allow you to run conda commands with Command Prompt as you would with Anaconda Prompt.
- First time only on Command Prompt: Initialise anaconda on the Command Prompt by typing
Activate your Stan environment with
activate myenv
, wheremyenv
is the name of your environment such asstan_env
.Type
atom
to launch the Atom editor (once launched you can close the comand prompt window if you like).
To check if you can now run PyStan in Atom, copy and run the example code from the installation doc.
>>> import pystan
>>> model_code = 'parameters {real y;} model {y ~ normal(0,1);}'
>>> model = pystan.StanModel(model_code=model_code)
>>> y = model.sampling().extract()['y']
>>> y.mean() # with luck the result will be near 0
In my case it looked like pystan was running but after a while the program appeard to repeat itself showing the following output:
INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_19a09b474d1901f191444eaf8a6b8ce2 NOW. INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_19a09b474d1901f191444eaf8a6b8ce2 NOW. INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_19a09b474d1901f191444eaf8a6b8ce2 NOW. INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_19a09b474d1901f191444eaf8a6b8ce2 NOW. INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_19a09b474d1901f191444eaf8a6b8ce2 NOW.
Have a look at this comment for more details.
To remedy this we will need to add if __name__ == "__main__":
to our program which will look like the below. Jupyter does this by default and the line isn't necessary.
>>> import pystan
>>> model_code = 'parameters {real y;} model {y ~ normal(0,1);}'
>>> if __name__ == "__main__":
>>> model = pystan.StanModel(model_code=model_code)
>>> y = model.sampling().extract()['y']
>>> y.mean() # with luck the result will be near 0