Using PyStan 3 with Jupyter Notebook and Jupyterlab

Jupyter notebook and jupyterlab have a well-documented bug which prevents the use of libraries using asyncio event loops. Neither the Python REPL nor the IPython REPL have this problem. Since PyStan 3 uses an asyncio event loop, this is an issue for those who would like to use Jupyter software.

Here’s the currently recommended workaround mentioned in the bug report:

  1. Use a venv (python3 -m venv venv; source venv/bin/activate)
  2. pip install "tornado<5" "notebook<6" "jupyterlab==2.2.5"

If you don’t use jupyterlab you do not need the jupyterlab part in 2.

2 Likes

Hi,
I’ve used your instructions and I get the following error at the end of sampling:

DecodeError                               Traceback (most recent call last)
<ipython-input-25-b17b9817b7b6> in <module>
----> 1 fit = posterior3.sample(num_chains=4, num_samples=1000)

~/.conda/envs/stan3/lib/python3.7/site-packages/stan/model.py in sample(self, **kwargs)
    239 
    240         try:
--> 241             return asyncio.run(go())
    242         except KeyboardInterrupt:
    243             pass

~/.conda/envs/stan3/lib/python3.7/asyncio/runners.py in run(main, debug)
     41         events.set_event_loop(loop)
     42         loop.set_debug(debug)
---> 43         return loop.run_until_complete(main)
     44     finally:
     45         try:

~/.conda/envs/stan3/lib/python3.7/asyncio/base_events.py in run_until_complete(self, future)
    585             raise RuntimeError('Event loop stopped before Future completed.')
    586 
--> 587         return future.result()
    588 
    589     def stop(self):

~/.conda/envs/stan3/lib/python3.7/site-packages/stan/model.py in go()
    188                         if resp.status != 200:
    189                             raise RuntimeError((await resp.json())["message"])
--> 190                         stan_outputs.append(tuple(extract_protobuf_messages(await resp.read())))
    191 
    192                 def is_nonempty_logger_message(msg):

~/.conda/envs/stan3/lib/python3.7/site-packages/stan/model.py in extract_protobuf_messages(fit_bytes)
    131                 msg = callbacks_writer_pb2.WriterMessage()
    132                 next_pos, pos = varint_decoder(fit_bytes, pos)
--> 133                 msg.ParseFromString(fit_bytes[pos : pos + next_pos])
    134                 yield msg
    135                 pos += next_pos

DecodeError: Error parsing message

pystan3.0.04b
Linux

That looks like it might be a different sort of error. Does it occur if you use python or IPython? If it does, then opening an issue in GitHub would be a good next step, https://github.com/stan-dev/pystan-next/issues

Not sure. I’ve tried it only on Jupyter notebook for now.

if you want Stan in a Python Jupyter notebook, we’ve got several Jupyter notebooks that work with CmdStanPy, from Andrew’s blog:

also this case study:

1 Like