Skip to content
James Booth edited this page Apr 27, 2014 · 3 revisions

1. Common imports

Some imports are very commonly needed. Importing these names in a consistent manor helps keep notebook code readable:

import numpy as np
import menpo.io as mio
import scipy.io as sio  # for reading/dumping .mat files
import matplotlib.pyplot as plt 

2. Default visualization tools

Currently, Menpo uses matplotlib for 2D visualization and Mayavi or 3D visualization. Care is taken to seperate visualization commands into the menpo.visualize package, to allow for the possibility of supporting new visualization tools in the future. You can see the defaults being set here.

In example notebooks, try to avoid calling specific viewing packages to do novel visualizations (this might be a sign that you should consider adding a new viewer to the visualize package). You can assume that images are best viewed inline and 3D visualizations best run under qt though.

3. Event loop intergration

IPython can hook into various GUI event loops in order to allow for asycnronous execution of GUI code seperate from the notebook kernel (this makes it possible to adjust the view of a 3D model in Mayavi while running other code in the notebook for instance). Only one event loop intergration can be active at one time. The following event loops are of interest to us:

  • inline allows for inline viewing of matplotlib plots in the notebook.
  • qt required for Mayavi to function. Note we use qt instead of wx due to its superior OS X support.

Integration can be done at the startup time of IPython using the --gui or --pylab commands. Dont do this! Commonly in one notebook we will need to switch between 2D and 3D backends. The above method would require a kernel restart to switch which is a terrible user experience. Thankfully, we can use the %matplotlib cell magic to change event loop integegration on a cell by cell level.

%matplotlib inline
image.view()  # by default show example images inline
...

Note that due to an open bug in IPython with qt the magic has to be run in an isolated cell before 3D viewing code is required:

%matplotlib qt  # note that we have to run this in isolation!

mesh.view()  # by default view meshes using qt in Mayavi
...

Notebooks should be structured so that the user can run the code without having to manually change event loop integration - i.e. sprinkle the %matplotlib fairly liberally if needed! Note that if a notebook operates exclusively with one datatype (e.g. just images) it's fairly safe to just set the back end once at the top of the file.

Clone this wiki locally