The class for volume images is NiftiWidget
. It takes a path to a .nii
file as input.
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.
# 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>
Failed to display Jupyter Widget of type interactive
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
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:
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>
Failed to display Jupyter Widget of type interactive
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
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
:
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>
Failed to display Jupyter Widget of type interactive
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
plot_img
¶Another image slicer type plot from the nilearn package, this time with an example atlas (the CC400 atlas), and setting the colormap:
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>
Failed to display Jupyter Widget of type interactive
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
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:
from niwidgets import SurfaceWidget
from niwidgets.exampledata import examplesurface
surface_widget = SurfaceWidget(examplesurface)
surface_widget.surface_plotter()
Failed to display Jupyter Widget of type interactive
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
Failed to display Jupyter Widget of type Figure
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
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:
from niwidgets import SurfaceWidget
from niwidgets.exampledata import examplesurface
from niwidgets.exampledata import exampleoverlays
surface_widget = SurfaceWidget(examplesurface, overlayfiles=exampleoverlays)
surface_widget.surface_plotter()
Failed to display Jupyter Widget of type interactive
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
Failed to display Jupyter Widget of type Figure
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
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.
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)
Failed to display Jupyter Widget of type VBox
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
Failed to display Jupyter Widget of type interactive
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.