mila-iqia / blocks-extras

A collection of extensions to the Blocks framework
MIT License
27 stars 40 forks source link

iPython notebook embedding (see issue #10) #11

Open mdda opened 9 years ago

mdda commented 9 years ago

This is a somewhat reorganized plot.py, that works for embedding bokeh plots from blocks (on my machine at least)...

An example notebook that shows this is :

https://github.com/mdda/pycon.sg-2015_deep-learning/blob/master/ipynb/1-LivePlotting.ipynb

(which is in my https://github.com/mdda/pycon.sg-2015_deep-learning repo, which I prepared for the Singapore PyCon last week).

As I mentioned, there have been quite a few changes, and, in order to see whether it still works in its original setting, I'd like to make sure that the original author can still use it 'server-side'. I believe that I've included the necessary notebook switching stuff, but would like to verify.

One critical line for the iPython embedding is explained in the notebook itself

# Needs 'url' in order to stream (creating a session on the bokeh-server) 
# - and "default" is a special case
output_notebook(url="default", hide_banner=True)  # If you like banners, the default is False

My repo's README has some explanatory text about how to set the bokeh server up, which might be worth embedding here too.

Best Wishes, Martin

rizar commented 9 years ago

Since the author of this stuff is currently not available, I will review this PR. But for that I might need a bit of time, so please wait for the weekend :)

rizar commented 9 years ago

Seems like Plot is actually not compatible with Bokeh 0.9 (see the error message below). Since our policy is to support the latest versions of the packages, this should be fixed either in this PR or before it.

╰─$ ~/Dist/blocks-examples/mnist/__init__.py                                     1 ↵
INFO:blocks.algorithms:Taking the cost gradient
INFO:blocks.algorithms:The cost gradient computation graph is built
Using saved session configuration for http://localhost:5006/
To override, pass 'load_from_config=False' to Session
INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): localhost
INFO:bokeh.session:got read write apikey
Traceback (most recent call last):
  File "/home/rizar/Dist/blocks-examples/mnist/__init__.py", line 105, in <module>
    main(args.save_to, args.num_epochs)
  File "/home/rizar/Dist/blocks-examples/mnist/__init__.py", line 80, in main
    ['train_total_gradient_norm']]))
  File "/home/rizar/Dist/blocks-extras/blocks/extras/extensions/plot.py", line 106, in __init__
    line_color=self.colors[j % len(self.colors)])
  File "/home/rizar/.local/lib/python2.7/site-packages/bokeh/_glyph_functions.py", line 37, in func
    glyph = _make_glyph(glyphclass, kwargs, glyph_ca)
  File "/home/rizar/.local/lib/python2.7/site-packages/bokeh/plotting_helpers.py", line 140, in _make_glyph
    return glyphclass(**kws)
  File "/home/rizar/.local/lib/python2.7/site-packages/bokeh/plot_object.py", line 90, in __init__
    super(PlotObject, self).__init__(**kwargs)
  File "/home/rizar/.local/lib/python2.7/site-packages/bokeh/properties.py", line 357, in __init__
    setattr(self, name, value)
  File "/home/rizar/.local/lib/python2.7/site-packages/bokeh/properties.py", line 371, in __setattr__
    (name, self.__class__.__name__, text, nice_join(matches)))
AttributeError: unexpected attribute 'y_axis_label' to Line, possible attributes are line_alpha, line_cap, line_color, line_dash, line_dash_offset, line_join, line_width, name, session, tags, visible, x or y
rizar commented 9 years ago

And I get the same error message when trying it with your IPython notebook, which is strange...

mdda commented 9 years ago

Yes - that is strange. It definitely worked for me (I even have a screenshot in my presentation to prove it : http://redcatlabs.com/2015-06-19_Presentation-PyConSG/#/8/5 ) - and it still works.

Did you get your bokeh version 0.9.0 from PyPI, or git:master? (which has just flipped to 0.9.1, it seems)

I'd be happy to make the 'y_axis_label' fix here (I'm thinking it belongs to 'figure' rather than 'line', for instance), except for the wrinkle that I'm currently getting no error message, so I can't effectively debug the issue. My bokeh claims to be 0.9.0 and was installed from PyPI.

rizar commented 9 years ago

Travis build is also broken: https://travis-ci.org/mila-udem/blocks-examples/jobs/70233007

I am using 0.9.1, the same version was used for the build, so I guess if you update Bokeh you will experience the same issue.

rizar commented 9 years ago

Even without x_axis_label and y_axis_label plotting crashes for me with the latest Bokeh, see the message below. @mdda , can you reproduce it?

Blocks will attempt to run `on_error` extensions, potentially saving data, before exiting and reraising the error. Note that the usual `after_training` extensions will *not* be run. The original error will be re-raised and also stored in the training log. Press CTRL + C to halt Blocks immediately.
Traceback (most recent call last):
  File "/home/rizar/Dist/blocks-examples/mnist/__init__.py", line 105, in <module>
    main(args.save_to, args.num_epochs)
  File "/home/rizar/Dist/blocks-examples/mnist/__init__.py", line 93, in main
    main_loop.run()
  File "/home/rizar/Dist/blocks/blocks/main_loop.py", line 196, in run
    reraise_as(e)
  File "/home/rizar/Dist/blocks/blocks/utils/__init__.py", line 225, in reraise_as
    six.reraise(type(new_exc), new_exc, orig_exc_traceback)
  File "/home/rizar/Dist/blocks/blocks/main_loop.py", line 182, in run
    while self._run_epoch():
  File "/home/rizar/Dist/blocks/blocks/main_loop.py", line 229, in _run_epoch
    self._run_extensions('before_epoch')
  File "/home/rizar/Dist/blocks/blocks/main_loop.py", line 262, in _run_extensions
    extension.dispatch(CallbackName(method_name), *args)
  File "/home/rizar/Dist/blocks/blocks/extensions/__init__.py", line 338, in dispatch
    self.do(callback_invoked, *(from_main_loop + tuple(arguments)))
  File "/home/rizar/Dist/blocks-extras/blocks/extras/extensions/plot.py", line 143, in do
    push()
  File "/home/rizar/.local/lib/python2.7/site-packages/bokeh/io.py", line 375, in push
    return session.store_document(document)
  File "/home/rizar/.local/lib/python2.7/site-packages/bokeh/session.py", line 583, in store_document
    self.push(*json_objs)
  File "/home/rizar/.local/lib/python2.7/site-packages/bokeh/session.py", line 525, in push
    self.post_json(url, data=data)
  File "/home/rizar/.local/lib/python2.7/site-packages/bokeh/session.py", line 367, in post_json
    return self.execute_json('post', url, headers=headers, **kwargs)
  File "/home/rizar/.local/lib/python2.7/site-packages/bokeh/session.py", line 335, in execute_json
    return get_json(resp)
  File "/home/rizar/.local/lib/python2.7/site-packages/bokeh/util/serialization.py", line 80, in get_json
    return response.json()
  File "/home/rizar/.local/lib/python2.7/site-packages/requests/models.py", line 819, in json
    return json.loads(self.text, **kwargs)
  File "/usr/lib/python2.7/dist-packages/simplejson/__init__.py", line 488, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/dist-packages/simplejson/decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
  File "/usr/lib/python2.7/dist-packages/simplejson/decoder.py", line 389, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
simplejson.scanner.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Original exception:
    JSONDecodeError: Expecting value: line 1 column 1 (char 0)
mdda commented 9 years ago

This PR works on 0.9.1 from PyPI for me (while bokeh:master doesn't install correctly via pip, at least for now). It was simply a matter of moving the axes label parameters into the figure() from the line().

Also, should the up-front README suggest that 'editing-in-place' for live blocks.extras (with a non-editable blocks) requires :

pip install --upgrade --no-deps --force-reinstall .

between edits (though I admit it's a bit of an edge-case)...

rizar commented 9 years ago

For some reason on my laptop it still gives a weird JSON error. But apparently tests pass. I will also try with blocks-examples tests.

rizar commented 9 years ago

My error is reproduced on Travis: https://travis-ci.org/mila-udem/blocks-examples/jobs/70590204. Moreover, I've just realized that Plot is not tested in this repo, and I guess if it had been, we would have observed the same error.

I am in favour of adding your pip command to README.md, thanks!