stan-dev / pystan

PyStan, a Python interface to Stan, a platform for statistical modeling. Documentation: https://pystan.readthedocs.io
ISC License
342 stars 59 forks source link

Can't run `stan.build` in a Jupyter notebook #311

Closed ForceBru closed 3 years ago

ForceBru commented 3 years ago

Describe the bug

The error message is the same as here: https://github.com/stan-dev/pystan/issues/280. This comment (https://github.com/stan-dev/pystan/issues/280#issuecomment-828733049) says that "this is not a typical use of pystan", but... isn't a Jupyter notebook literally the most typical use of [insert any data science library, including PyStan]??

Error message:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-6-43aaeb297283> in <module>
----> 1 posterior = stan.build("", data=data, random_seed=1)

~/opt/miniconda3/lib/python3.9/site-packages/stan/model.py in build(program_code, data, random_seed)
    510 
    511     try:
--> 512         return asyncio.run(go())
    513     except KeyboardInterrupt:
    514         return  # type: ignore

~/opt/miniconda3/lib/python3.9/asyncio/runners.py in run(main, debug)
     31     """
     32     if events._get_running_loop() is not None:
---> 33         raise RuntimeError(
     34             "asyncio.run() cannot be called from a running event loop")
     35 

RuntimeError: asyncio.run() cannot be called from a running event loop

Describe your system

Code Sample, a copy-pastable example

Code adapted from this STAN case study:

import stan
import numpy as np

N = 1000
mu = np.array([-.2, .2])
Z = np.random.binomial(1, .3, N)
Y = np.random.normal(mu[Z])

data = {
    'N': Y.size,
    'y': Y
}

posterior = stan.build("", data=data, random_seed=1)

This works when pasted into the REPL, but raises a very non-descriptive error message (I guess that's because the script is an empty string?):

Python 3.9.5 (default, May 18 2021, 12:31:01) 
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import stan
>>> import numpy as np
>>> N = 1000
>>> mu = np.array([-.2, .2])
>>> Z = np.random.binomial(1, .3, N)
>>> Y = np.random.normal(mu[Z])
>>> data = {
...     'N': Y.size,
...     'y': Y
... }
>>> posterior = stan.build("", data=data, random_seed=1)
Building: 56.6s, done.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/forcebru/opt/miniconda3/lib/python3.9/site-packages/stan/model.py", line 512, in build
    return asyncio.run(go())
  File "/Users/forcebru/opt/miniconda3/lib/python3.9/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/Users/forcebru/opt/miniconda3/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "/Users/forcebru/opt/miniconda3/lib/python3.9/site-packages/stan/model.py", line 507, in go
    param_names, dims = zip(*((param["name"], param["dims"]) for param in params_list))
ValueError: not enough values to unpack (expected 2, got 0)
>>> 
ahartikainen commented 3 years ago

Hi, use nest-asyncio.

https://pystan.readthedocs.io/en/latest/faq.html#how-can-i-use-pystan-with-jupyter-notebook-or-jupyterlab

ForceBru commented 3 years ago

Oh, I even read about getting parameter summary statistics on that same page, but somehow completely missed the part about Jupyter notebooks... Sorry about that

ahartikainen commented 3 years ago

No harm was made. Thanks for using pystan.