Usage: niwidgets examples

An online interactive version of this notebook can be found on Binder.

Volume data

The class for volume images is NiftiWidget. It takes a path to a .nii file as input.

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 NiftiWidget
from niwidgets import examplet1

test_widget = NiftiWidget(examplet1)
test_widget.nifti_plotter()
<matplotlib.figure.Figure at 0x7fd49ca49be0>

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 [4]:
import nilearn.plotting as nip

test_widget.nifti_plotter(plotting_func=nip.plot_epi, display_mode=['ortho', 'x', 'y', 'z', 'yx', 'xz', 'yz'])
<matplotlib.figure.Figure at 0x7fd4820fbe10>

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 [5]:
from niwidgets import examplezmap
import nilearn.plotting as nip
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 0x7fd482174b38>

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 [6]:
from niwidgets import NiftiWidget
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 0x7fd481b37c50>

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 [7]:
from niwidgets import SurfaceWidget
from niwidgets.exampledata import examplesurface

surface_widget = SurfaceWidget(examplesurface)
surface_widget.surface_plotter()

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.

If you pass them in a dictionary (see e.g. niwidgets.exampledata.exampleoverlays), the keys of the dictionary are used for the Options in a dropdown menu:

In [8]:
from niwidgets import SurfaceWidget
from niwidgets.exampledata import examplesurface
from niwidgets.exampledata import exampleoverlays

surface_widget = SurfaceWidget(examplesurface, overlayfiles=exampleoverlays)

surface_widget.surface_plotter()

Streamlines

If you have mrtrix or trackvis streamlines, you can display them using the StreamlineWidget. Instead of passing a streamlines file, one can also pass a nibabel streamline sequence to the widget.

In [9]:
from niwidgets import StreamlineWidget
from niwidgets.exampledata import streamlines

sw = StreamlineWidget(filename=streamlines)
style = {'axes': {'color': 'red',
                  'label': {'color': 'white'},
                  'ticklabel': {'color': 'white'},
                  'visible': False},
         'background-color': 'white',
         'box': {'visible': False}}
sw.plot(display_fraction=0.5, width=500, height=500, style=style, percentile=80)