-
-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Run from JupyterLab / Jupyter Notebook #80
Comments
Could we run httpstan server with a |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
We need to figure out a solution. Perhaps we could create our own event loop? Python 3.7 also has a new function, Example of creating a new event loop in a thread: https://gist.github.com/lars-tiede/01e5f5a551f29a5f300e |
Things work for me with IPython and Python REPL. The code we have written is entirely valid. I think this is a bug against JupyterLab/Jupyter Notebook. |
Is this still failing with Jupyter? |
Yes. Closing this because the underlying issue (asyncio and jupyter notebook) is well documented elsewhere. It looks like the "official" workaround is to install an earlier version of |
I have dig this a bit deeper. edit. Code run from Windows10 in JupyterLab interface which is running on WSL (Ubuntu 18). Running similar code from terminal has no problems. Following code / "hack" let's to compile model and start the sampling.
But the fit fails randomly with
This seems to happen in random points Here is a msg that is failing to parse (first info where in the fit it happens) previous loc:
failure loc:
and here is the same message (but 1000 previous and next bytes; whitespace was added by me)
It somehow looks like the |
Noticed that there is an error in terminal (for Jupyter case)
|
I'd be open to discussing whether or not it's a good practice to spend time working around another package's bug --- especially when that bug is clearly identified and widely acknowledged. In general, I would not be inclined to support the practice. |
I'm not suggesting we change anything. I'm just keeping log what is going on. There is probably a way to fix this at some point. If there is no way to run code in jupyter, then at least my use is quite limited (given that I code in jupyter 99+%). Also this would restrict tutorial formats. Or force users to have some subprocess tricks. |
Thanks. I appreciate the clarification. |
That might work. Not sure if jupyterlab supports it. |
I finally cracked it. Silly mistake by my end (indentation error with return, which caused some interference between the threads or something similar) So to get sampling working on Jupyter (Notebook / Lab) with the recent Tornado one needs to do the following:
Not sure if there is a way to inject this pystan, probably not. |
I created this little wrapper, which can be imported in Jupyter as
import multiprocessing
multiprocessing.set_start_method('spawn', True)
del multiprocessing
from concurrent.futures import ThreadPoolExecutor as _ThreadPoolExecutor
def _exec_async(func, *args, **kwargs):
with _ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(func, *args, **kwargs)
return future.result()
from importlib import reload
import stan as _stan
_stan = reload(_stan) # force reload if stan was imported previously
del reload
def _inject_posterior(posterior):
"""Inject posterior.sample with call in thread."""
posterior._sample = posterior.sample
def sample(**kwargs):
""""""
return _exec_async(posterior._sample, **kwargs)
sample.__doc__ += posterior.sample.__doc__
posterior.sample = sample
def build(program_code, data=None, random_seed=None):
""""""
posterior = _exec_async(_stan.build, program_code, data=data, random_seed=random_seed)
_inject_posterior(posterior)
return posterior
build.__doc__ += _stan.build.__doc__
try:
__version__ = _stan.__version__
except:
pass ps. Running Jupyter from WSL works fine, but running it from WSL2 you need to set the correct ip for it; and it is dynamical ip --> this works (where
|
I've been thinking about where this kind of documentation should go (in
general, not in this particular case).
I think posting on the forums is the best place. We can link to it there
if people ask.
It certainly doesn't belong in the issues since those are for bug
reports. (This isn't a bug.) And I think we should have cleaner and
easier-to-maintain documentation than we did before, so I'd like to keep
it out of the docs.
Does this make sense?
…On 8/7/20 4:23 AM, Ari Hartikainen wrote:
I created this little wrapper, which can be imported in Jupyter as
|import stan_jupyter as stan |
import multiprocessing
multiprocessing.set_start_method('spawn',True)
del multiprocessing
from concurrent.futures import ThreadPoolExecutor as _ThreadPoolExecutor
def _exec_async(func,*args,**kwargs):
with _ThreadPoolExecutor(max_workers=1)as executor:
future = executor.submit(func,*args,**kwargs)
return future.result()
from importlib import reload
import stan as _stan
_stan = reload(_stan)# force reload if stan was imported previously
del reload
def _inject_posterior(posterior):
"""Inject posterior.sample with call in thread."""
posterior._sample = posterior.sample
def sample(**kwargs):
""""""
return _exec_async(posterior._sample,**kwargs)
sample.__doc__ += posterior.sample.__doc__
posterior.sample = sample
def build(program_code,data=None,random_seed=None):
""""""
posterior = _exec_async(_stan.build,program_code,data=data,random_seed=random_seed)
_inject_posterior(posterior)
return posterior
build.__doc__ += _stan.build.__doc__
try:
__version__ = _stan.__version__
except:
pass
ps. Running Jupyter from WSL works fine, but running it from WSL2 you
need to set the correct ip for it; and it is dynamical ip --> this works
(where |python| should be the main python you use, if you use system
python, change |python| to |python3|
|jupyter lab --ip $(python -c "import subprocess;
print(subprocess.run(['hostname', '-I'],
capture_output=True).stdout.strip().decode('utf-8'))") |
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#80 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AJQUBVVKUAXEMUISQHPONKTR7O2XJANCNFSM4IJWXK7Q>.
|
It does sound fine. |
Hi,
|
Hi, raise an issue here https://github.com/ahartikainen/pystan-jupyter I'm not 100% that running the stan process in a thread is the best option (this is what pystan-jupyter does). Have you tried |
Running httpstan from Jupyter Lab/Notebook fails due to jupyter is already running asyncio event
Using IPython works (or I'm running this on Windows, and Python crash when I exit the python, so I can save the results with ArviZ to netCDF and use it later).
The text was updated successfully, but these errors were encountered: