Skip to content

Instantly share code, notes, and snippets.

@tommylees112
Last active February 17, 2022 11:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommylees112/d0e951013d09a603b1bacb67f4c2f896 to your computer and use it in GitHub Desktop.
Save tommylees112/d0e951013d09a603b1bacb67f4c2f896 to your computer and use it in GitHub Desktop.
Pystan2 environment for linux machines
conda create -n stan2 python=3.7 --yes
conda activate stan2
conda install -c anaconda gcc_linux-64 gxx_linux-64 --yes
## FOR MAC:
# conda install -c anaconda clang_osx-64 clangxx_osx-64 --yes
conda install -c conda-forge numpy pandas scipy cython ipython jupyterlab matplotlib freetype --yes
python -m pip install --upgrade pip
pip install pystan==2.19.0.0
conda install -c conda-forge arviz --yes
python -c "import pystan; import arviz"
@tommylees112
Copy link
Author

tommylees112 commented Feb 17, 2022

Test Code from here

import pystan
import arviz as az
import numpy as np


schools_code = """
data {
    int<lower=0> J; // number of schools
    vector[J] y; // estimated treatment effects
    vector<lower=0>[J] sigma; // s.e. of effect estimates
}
parameters {
    real mu;
    real<lower=0> tau;
    vector[J] eta;
}
transformed parameters {
    vector[J] theta;
    theta = mu + tau * eta;
}
model {
    eta ~ normal(0, 1);
    y ~ normal(theta, sigma);
}
"""

schools_dat = {'J': 8,
               'y': [28,  8, -3,  7, -1,  1, 18, 12],
               'sigma': [15, 10, 16, 11,  9, 11, 10, 18]}

sm = pystan.StanModel(model_code=schools_code)
fit = sm.sampling(data=schools_dat, iter=1000, chains=4)

## extract data
la = fit.extract(permuted=True)  # return a dictionary of arrays
mu = la['mu']

## return an array of three dimensions: iterations, chains, parameters
a = fit.extract(permuted=False)

## arviz read from pystan2
data = az.from_pystan(fit, coords={"school": np.arange(schools_dat["J"])}, dims={"eta": ["school"], "theta": ["school"]})
posterior = data.posterior

posterior

@tommylees112
Copy link
Author

tommylees112 commented Feb 17, 2022

Weird matplotlib errors:

$ python -c "import pystan; import arviz"

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/thomas_lees112/miniconda3/envs/stan2/site-packages/arviz/__init__.py", line 8, in <module>
    from matplotlib.colors import LinearSegmentedColormap
  File "/home/thomas_lees112/miniconda3/envs/stan2/site-packages/matplotlib/__init__.py", line 107, in <module>
    from . import _api, cbook, docstring, rcsetup
  File "/home/thomas_lees112/miniconda3/envs/stan2/site-packages/matplotlib/cbook/__init__.py", line 31, in <module>
    from matplotlib import _api, _c_internal_utils
ImportError: cannot import name '_c_internal_utils'

SO Answer Here
matplotlib/matplotlib#12601

FIX: force python to python=3.7

@tommylees112
Copy link
Author

tommylees112 commented Feb 17, 2022

Pystan environment for the Upcoming course: Principled Modelling in Stan. NOTE: have to fix Python=3.7 to fix this issue in matplotlib (imported by arviz) and then have to force pip to install pystan==2.19

@tommylees112
Copy link
Author

When running on M1 Mac:

conda install -c anaconda clang_osx-64 clangxx_osx-64 --yes
~ conda install -c anaconda clang_osx-64 clangxx_osx-64 --yes

Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: /Users/tommylees/miniconda3/envs/stan2

  added / updated specs:
    - clang_osx-64
    - clangxx_osx-64


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    cctools-927.0.2            |       h5ba7a2e_4         1.7 MB  anaconda
    clang-10.0.0               |default_hf57f61e_0        24.2 MB  anaconda
    clang_osx-64-10.0.0        |       h05bbb7f_0          15 KB  anaconda
    clangxx-10.0.0             |default_hf57f61e_0         123 KB  anaconda
    clangxx_osx-64-10.0.0      |       h05bbb7f_1          18 KB  anaconda
    compiler-rt-10.0.0         |       h47ead80_0          77 KB  anaconda
    compiler-rt_osx-64-10.0.0  |       hbcc88fd_0         3.1 MB  anaconda
    ld64-450.3                 |       h3c32e8a_4         1.2 MB  anaconda
    libllvm10-10.0.1           |       h76017ad_5        20.4 MB  anaconda
    llvm-openmp-10.0.0         |       h28b9765_0         270 KB  anaconda
    tapi-1000.10.8             |       ha1b3eb9_0         4.9 MB  anaconda
    ------------------------------------------------------------
                                           Total:        56.0 MB

The following NEW packages will be INSTALLED:

  cctools            anaconda/osx-64::cctools-927.0.2-h5ba7a2e_4
  clang              anaconda/osx-64::clang-10.0.0-default_hf57f61e_0
  clang_osx-64       anaconda/osx-64::clang_osx-64-10.0.0-h05bbb7f_0
  clangxx            anaconda/osx-64::clangxx-10.0.0-default_hf57f61e_0
  clangxx_osx-64     anaconda/osx-64::clangxx_osx-64-10.0.0-h05bbb7f_1
  compiler-rt        anaconda/osx-64::compiler-rt-10.0.0-h47ead80_0
  compiler-rt_osx-64 anaconda/noarch::compiler-rt_osx-64-10.0.0-hbcc88fd_0
  ld64               anaconda/osx-64::ld64-450.3-h3c32e8a_4
  libllvm10          anaconda/osx-64::libllvm10-10.0.1-h76017ad_5
  llvm-openmp        anaconda/osx-64::llvm-openmp-10.0.0-h28b9765_0
  tapi               anaconda/osx-64::tapi-1000.10.8-ha1b3eb9_0

The following packages will be SUPERSEDED by a higher-priority channel:

  ca-certificates    pkgs/main::ca-certificates-2021.10.26~ --> anaconda::ca-certificates-2020.10.14-0
  certifi            pkgs/main::certifi-2021.10.8-py37hecd~ --> anaconda::certifi-2020.6.20-py37_0



Downloading and Extracting Packages
compiler-rt-10.0.0   | 77 KB     | ################################################################################### | 100%
libllvm10-10.0.1     | 20.4 MB   | ################################################################################### | 100%
ld64-450.3           | 1.2 MB    | ################################################################################### | 100%
clangxx-10.0.0       | 123 KB    | ################################################################################### | 100%
compiler-rt_osx-64-1 | 3.1 MB    | ################################################################################### | 100%
llvm-openmp-10.0.0   | 270 KB    | ################################################################################### | 100%
cctools-927.0.2      | 1.7 MB    | ################################################################################### | 100%
clang-10.0.0         | 24.2 MB   | ################################################################################### | 100%
clangxx_osx-64-10.0. | 18 KB     | ################################################################################### | 100%
tapi-1000.10.8       | 4.9 MB    | ################################################################################### | 100%
clang_osx-64-10.0.0  | 15 KB     | ################################################################################### | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
objc[16431]: Class AMSupportURLConnectionDelegate is implemented in both /usr/lib/libamsupport.dylib (0x1e99eb678) and /Library/Apple/System/Library/PrivateFrameworks/MobileDevice.framework/Versions/A/MobileDevice (0x1042202c8). One of the two will be used. Which one is undefined.
objc[16431]: Class AMSupportURLSession is implemented in both /usr/lib/libamsupport.dylib (0x1e99eb6c8) and /Library/Apple/System/Library/PrivateFrameworks/MobileDevice.framework/Versions/A/MobileDevice (0x104220318). One of the two will be used. Which one is undefined.
objc[16437]: Class AMSupportURLConnectionDelegate is implemented in both /usr/lib/libamsupport.dylib (0x1e99eb678) and /Library/Apple/System/Library/PrivateFrameworks/MobileDevice.framework/Versions/A/MobileDevice (0x10442c2c8). One of the two will be used. Which one is undefined.
objc[16437]: Class AMSupportURLSession is implemented in both /usr/lib/libamsupport.dylib (0x1e99eb6c8) and /Library/Apple/System/Library/PrivateFrameworks/MobileDevice.framework/Versions/A/MobileDevice (0x10442c318). One of the two will be used. Which one is undefined.

When running: sm = pystan.StanModel(model_code=schools_code)

Get the error:

INFO:pystan:COMPILING THE C++ CODE FOR MODEL anon_model_19a09b474d1901f191444eaf8a6b8ce2 NOW.
---------------------------------------------------------------------------
DistutilsExecError                        Traceback (most recent call last)
~/miniconda3/envs/stan2/lib/python3.7/distutils/unixccompiler.py in _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts)
    117             self.spawn(compiler_so + cc_args + [src, '-o', obj] +
--> 118                        extra_postargs)
    119         except DistutilsExecError as msg:

~/miniconda3/envs/stan2/lib/python3.7/distutils/ccompiler.py in spawn(self, cmd)
    909     def spawn(self, cmd):
--> 910         spawn(cmd, dry_run=self.dry_run)
    911

~/miniconda3/envs/stan2/lib/python3.7/distutils/spawn.py in spawn(cmd, search_path, verbose, dry_run)
     35     if os.name == 'posix':
---> 36         _spawn_posix(cmd, search_path, dry_run=dry_run)
     37     elif os.name == 'nt':

~/miniconda3/envs/stan2/lib/python3.7/distutils/spawn.py in _spawn_posix(cmd, search_path, verbose, dry_run)
    158                           "command %r failed with exit status %d"
--> 159                           % (cmd, exit_status))
    160             elif os.WIFSTOPPED(status):

DistutilsExecError: command 'x86_64-apple-darwin13.4.0-clang' failed with exit status 254

During handling of the above exception, another exception occurred:

CompileError                              Traceback (most recent call last)
<ipython-input-2-25aa85272b50> in <module>
     24                'sigma': [15, 10, 16, 11,  9, 11, 10, 18]}
     25
---> 26 sm = pystan.StanModel(model_code=schools_code)
     27 fit = sm.sampling(data=schools_dat, iter=1000, chains=4)

~/miniconda3/envs/stan2/lib/python3.7/site-packages/pystan/model.py in __init__(self, file, charset, model_name, model_code, stanc_ret, include_paths, boost_lib, eigen_lib, verbose, obfuscate_model_name, extra_compile_args)
    347
    348         try:
--> 349             build_extension.run()
    350         finally:
    351             if redirect_stderr:

~/miniconda3/envs/stan2/lib/python3.7/distutils/command/build_ext.py in run(self)
    338
    339         # Now actually compile and link everything.
--> 340         self.build_extensions()
    341
    342     def check_extensions_list(self, extensions):

~/miniconda3/envs/stan2/lib/python3.7/distutils/command/build_ext.py in build_extensions(self)
    447             self._build_extensions_parallel()
    448         else:
--> 449             self._build_extensions_serial()
    450
    451     def _build_extensions_parallel(self):

~/miniconda3/envs/stan2/lib/python3.7/distutils/command/build_ext.py in _build_extensions_serial(self)
    472         for ext in self.extensions:
    473             with self._filter_build_errors(ext):
--> 474                 self.build_extension(ext)
    475
    476     @contextlib.contextmanager

~/miniconda3/envs/stan2/lib/python3.7/distutils/command/build_ext.py in build_extension(self, ext)
    532                                          debug=self.debug,
    533                                          extra_postargs=extra_args,
--> 534                                          depends=ext.depends)
    535
    536         # XXX outdated variable, kept here in case third-part code

~/miniconda3/envs/stan2/lib/python3.7/distutils/ccompiler.py in compile(self, sources, output_dir, macros, include_dirs, debug, extra_preargs, extra_postargs, depends)
    572             except KeyError:
    573                 continue
--> 574             self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
    575
    576         # Return *all* object filenames, not just the ones we just built.

~/miniconda3/envs/stan2/lib/python3.7/distutils/unixccompiler.py in _compile(self, obj, src, ext, cc_args, extra_postargs, pp_opts)
    118                        extra_postargs)
    119         except DistutilsExecError as msg:
--> 120             raise CompileError(msg)
    121
    122     def create_static_lib(self, objects, output_libname,

CompileError: command 'x86_64-apple-darwin13.4.0-clang' failed with exit status 254

@tommylees112
Copy link
Author

Error when trying to create environment on M1 Mac:

conda create -n stan2 python=3.7 --yes
conda activate stan2
## FOR MAC:
conda install -c anaconda clang_osx-64 clangxx_osx-64 --yes
conda install -c conda-forge numpy pandas scipy cython ipython jupyterlab matplotlib freetype --yes
python -m pip install --upgrade pip
pip install pystan==2.19.0.0
conda install -c conda-forge arviz --yes

The following discussion on Stan Discord

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment