Usage: niwidgets examples

An online interactive version of this notebook can be found at: beta.mybinder.org/repo/nipy/niwidgets

Volume data

The main (and only, for now) class for volume images is NiftiWidget. It takes a path to a .nii file as input.

In [1]:
# import the .nii widget:
from niwidgets import NiftiWidget

Interactively plotting an image

We’ll start by demonstrating the most useful aspect of the package: Being able to interactively slice an image file. To do so, we will import one of the example files that ships with niwidgets, a T1 weighted structural scan.

In [2]:
# Let's try a simple parcellation map from a standard atlas
from niwidgets import examplet1

test_widget = NiftiWidget(examplet1)
test_widget.nifti_plotter()

Use plotting functions from nilearn

niwidgets also lets you turn standard plots from the nilearn package into widgets. You can use any of them, and provide your own keyword arguments to set the slider options (if no key word argument is provided defaults are used).

In particular, niwidgets should allow you to pick a colormap interactively for almost any plot type

plot_epi

Plotting the same image but with the nilearn function plot_epi, we get this:

In [3]:
import nilearn.plotting as nip

test_widget.nifti_plotter(plotting_func=nip.plot_epi, display_mode=['ortho', 'x', 'y', 'z', 'yx', 'xz', 'yz'])
/Users/janfreyberg/anaconda/envs/niwidgetsdev/lib/python3.6/site-packages/matplotlib/__init__.py:1405: UserWarning:
This call to matplotlib.use() has no effect because the backend has already
been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.

  warnings.warn(_use_error_msg)

plot_glass_brain

This is an example of a glass brain plot with a standard visual perception activation map from neurosynth (this also ships as an example with niwidgets:

In [4]:
from niwidgets import examplezmap
test = NiftiWidget(examplezmap)
test.nifti_plotter(plotting_func=nip.plot_glass_brain, threshold=(0.0, 10.0, 0.01),
                   display_mode=['ortho','xz'])
<matplotlib.figure.Figure at 0x118960908>

plot_img

Another image slicer type plot from the nilearn package, this time with an example atlas (the CC400 atlas), and setting the colormap:

In [5]:
from niwidgets import exampleatlas
atlas_widget = NiftiWidget(exampleatlas)
atlas_widget.nifti_plotter(plotting_func=nip.plot_img, display_mode=['ortho', 'x', 'y', 'z'], colormap='hot')
<matplotlib.figure.Figure at 0x116b51208>

Surface data

If you have surface data, you can import the SurfaceWidget and use it in a similar fashion to the NiftiWidget. Import and define it in the same way:

In [6]:
from niwidgets import SurfaceWidget
from niwidgets.exampledata import examplesurface

print(f'Plotting file {examplesurface.name}')

surface_widget = SurfaceWidget(examplesurface)

surface_widget.surface_plotter()
Plotting file lh.inflated

If you want to plot additional data as overlays, you can pass those as either loaded GiftiImages (loaded using nibabel), or as file paths to a .annot, .thickness, .curv, .sulc, or .gii file:

In [7]:
from niwidgets.exampledata import exampleoverlays

print(f'Overlaying (f.name for f in exampleoverlays)')

surface_widget = SurfaceWidget(examplesurface, overlayfiles=exampleoverlays)

surface_widget.surface_plotter()
Overlaying (f.name for f in exampleoverlays)