From d9c4197c19e49413e3cd7946bb31247df4f70ec8 Mon Sep 17 00:00:00 2001 From: Marijn van Vliet Date: Tue, 21 May 2019 13:54:38 +0300 Subject: [PATCH 1/3] Integrate better with Jupyuter notebook Mayavi can render things inside a jupyter notebook as either PNG or X3D. This functionality can be enabled with `mlab.init_notebook()`. This PR adds an `_ipython_display_` hook to the `Brain` class that renders the brain accordgin to the Mayavi notebook integration settings. --- surfer/viz.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/surfer/viz.py b/surfer/viz.py index 4fcde5d..14b921c 100644 --- a/surfer/viz.py +++ b/surfer/viz.py @@ -2776,6 +2776,23 @@ def animate(self, views, n_steps=180., fname=None, use_cache=False, if ret: print("\n\nError occured when exporting movie\n\n") + def __repr__(self): + return ('' % + (self.subject_id, self._hemi, self.surf)) + + def _ipython_display_(self): + """Called by Jupyter notebook to display a brain.""" + from IPython.display import display as idisplay + + if mlab.options.offscreen: + # Render the mayavi scenes to the notebook + for figure in self._figures: + for scene in figure: + idisplay(scene.scene) + else: + # Render string representation + print(repr(self)) + def _scale_sequential_lut(lut_table, fmin, fmid, fmax): """Scale a sequential colormap.""" From 427f18f5918ee27c575a9cc4dff15aca22709662 Mon Sep 17 00:00:00 2001 From: Marijn van Vliet Date: Wed, 22 May 2019 10:37:32 +0300 Subject: [PATCH 2/3] Add note on Jupyter notebook integration to docs --- doc/install.rst | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/doc/install.rst b/doc/install.rst index bb6c441..c53c4eb 100644 --- a/doc/install.rst +++ b/doc/install.rst @@ -77,6 +77,16 @@ notebook), you have to activate the correct GUI backend, which is probably qt:: This will allow you to have an open PySurfer window while still being able to execute code in the console/notebook. +It is also possible to embed the PySurfer visualization into a Jupyter notebook. +This is achieved by leveraging Mayavi's notebook integration:: + + from mayavi import mlab + mlab.init_notebook(backend='png') + +The ``backend`` parameter can either be ``'png'`` to render the visualization +as a static PNG image, or ``'x3d'`` to render it using +`X3D ` (still experimental). + If you are having trouble getting started using PySurfer, please describe the problem on the `nipy mailing list`_. .. include:: links_names.txt From 04eae3fdbe3531127dd3bc7237370b6078e39414 Mon Sep 17 00:00:00 2001 From: Marijn van Vliet Date: Wed, 22 May 2019 10:42:56 +0300 Subject: [PATCH 3/3] Fix links in doc --- doc/install.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/doc/install.rst b/doc/install.rst index c53c4eb..91b5691 100644 --- a/doc/install.rst +++ b/doc/install.rst @@ -78,14 +78,15 @@ This will allow you to have an open PySurfer window while still being able to execute code in the console/notebook. It is also possible to embed the PySurfer visualization into a Jupyter notebook. -This is achieved by leveraging Mayavi's notebook integration:: +This is achieved by leveraging `Mayavi's notebook integration +`_:: from mayavi import mlab mlab.init_notebook(backend='png') The ``backend`` parameter can either be ``'png'`` to render the visualization as a static PNG image, or ``'x3d'`` to render it using -`X3D ` (still experimental). +`X3D `_ (still experimental). If you are having trouble getting started using PySurfer, please describe the problem on the `nipy mailing list`_.