Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Ipyniivue and Ipywidgets

Run this notebook

Visualizing Diverse Neuroimaging Formats

Author: Monika Doerig

Date: 12 Dec 2025

License:

MIT License

Note: If this notebook uses neuroimaging tools from Neurocontainers, those tools retain their original licenses. Please see Neurodesk citation guidelines for details.

Citation and Resources:

Tools included in this workflow

Ipyniivue

  • niivue/ipyniivue (2025): A WebGL-powered Jupyter Widget for Niivue based on anywidget. [Computer software]. https://github.com/niivue/ipyniivue

  • Taylor Hanayik, Chris Rorden, Christopher Drake, Jens Ochsenmeier, Matt McCormick, alexis, John lee, Paul Taylor, Edwin Bennink, Paul Wighton, Shun, Anthony Androulakis, Nell Hardcastle, Yaroslav Halchenko, Florian Rupprecht, Guilherme Povala, Korbinian Eckstein, Paul McCarthy, Sumit Jha, … Adam Basha. (2025). niivue/niivue: niivue/niivue-v0.66.0 (niivue/niivue-v0.66.0). Zenodo. Taylor Hanayik et al. (2026)

Ipywidgets

Educational resources

Data

  • Example neuroimaging images were obtained from the NiiVue demos resources

Introduction

This notebook demonstrates how to load, visualize, and interact with a range of common neuroimaging data formats in a Jupyter notebook using Ipyniivue, a Python interface to the Niivue web-based neuroimaging viewer. Examples combine ipyniivue with ipywidgets to enable interactive exploration and dynamic control of visualization parameters.

The notebook covers voxel-based images, including structural and diffusion-derived volumes (e.g., fractional anisotropy and principal diffusion directions), as well as surface meshes, per-vertex scalar data, CIFTI files, and tractography representations. Together, these examples illustrate how diverse neuroimaging modalities can be displayed within a unified, browser-based visualization environment without requiring a standalone desktop application.

The emphasis is on interactive visualization and data inspection, rather than preprocessing or statistical analysis, with each section highlighting practical patterns for working with different file formats and rendering modes supported by Ipyniivue.

All examples are inspired by the ipyniivues example repository and have been tested to work with ipyniivue version 2.4.4.

Import Python libraries

Download required data

example4d+orig.HEAD already exists.
example4d+orig.BRIK.gz already exists.
mni152.nii.gz already exists.
narps-4965_9U7M-hypo1_unthresh.nii.gz already exists.
narps-4735_50GV-hypo1_unthresh.nii.gz already exists.
sub-test02_left_hemisphere.srf.gz already exists.
sub-test02_left_hemisphere_4_curvature_maps.smp.gz already exists.
fs_LR.32k.L.inflated.surf.gii already exists.
fs_LR.32k.LR.curvature.dscalar.nii already exists.
Conte69.L.inflated.32k_fs_LR.surf.gii already exists.
Conte69.MyelinAndCorrThickness.32k_fs_LR.dtseries.nii already exists.
FA.nii.gz already exists.
V1.nii.gz already exists.
TR_S_R.tt.gz already exists.
tract.FAT_R.vtk already exists.
tract.IFOF_R.trk already exists.
tract.SLF1_R.tck already exists.
BrainMesh_ICBM152.lh.mz3 already exists.
Dataset downloaded successfully to images.
lh.pial already exists.
rh.white already exists.
Dataset downloaded successfully to images.
Shiny.jpg already exists.
Cortex.jpg already exists.
Cream.jpg already exists.
Fuzzy.jpg already exists.
Peach.jpg already exists.
Plastic.jpg already exists.
Gold.jpg already exists.
Dataset downloaded successfully to matcaps.
All datasets and matcaps downloaded successfully.

1. Volumetric Neuroimaging Formats

Multi-Volume Overlay

Visualizing anatomical and statistical maps simultaneously in 3D

This example demonstrates the visualization of multiple neuroimaging volumes in a single 3D viewer.

  • The base anatomical volume (mni152.nii.gz) is displayed in grayscale

  • Two statistical maps are overlaid with different colormaps:

    • narps-4965_9U7M-hypo1_unthresh.nii.gz in red

    • narps-4735_50GV-hypo1_unthresh.nii.gz in green

  • Interactive sliders adjust the minimum intensity threshold (cal_min) for each overlay

  • The 3D crosshair allows inspection of voxel locations

  • Colorbar indicates intensity ranges for the overlay volumes.

This setup highlights how multiple volumetric datasets can be integrated and visualized interactively, allowing exploration of anatomical structure alongside functional or statistical results in a single environment.

This example is an adaptation from this NiiVue demo.

⚠️ Note: ipywidgets that rely on Python callbacks (observe, on_click) require a running kernel and do not function in the static HTML version of this notebook. Widgets using client-side trait linking (jslink) remain interactive without a kernel.

Loading...

AFNI Volumes

This example demonstrates interactive exploration of a 4D AFNI dataset. The viewer displays multiplanar slices alongside NiiVue’s built-in graph panel, which plots the voxel-wise time series at the current crosshair position.

As you navigate through frames or use the animation controls, the graph updates to show intensity values across all timepoints for the selected voxel. The controls allow you to:

  • Step through frames manually (Back/Forward buttons)

  • Automatically cycle through all frames (Animate button)

  • Optionally normalize the graph display for better visualization

This setup demonstrates how 4D neuroimaging data can be explored both spatially (through the multiplanar views) and temporally (through the time series graph), providing a comprehensive view of how signal intensity varies across time at any brain location.

This example mirrors this NiiVue demo.

Create other buttons/checkboxes

Implement the callbacks

Create animate button

Reset frame index on image loaded

Display all

⚠️ Note: ipywidgets that rely on Python callbacks (observe, on_click) require a running kernel and do not function in the static HTML version of this notebook. Widgets using client-side trait linking (jslink) remain interactive without a kernel.

Loading...

Diffusion-Derived Volumes (FA and V1)

This example demonstrates interactive exploration of voxel-based diffusion metrics. We load fractional anisotropy (FA) and a V1 image as volumetric scalar maps. The V1 image encodes the principal diffusion direction (primary eigenvector) at each voxel, typically representing white matter fiber orientation. Interactive widgets allow you to visualize FA and V1 individually, overlay them, or apply a “Lines” effect (via the V1 slice shader) to highlight directional fiber architecture. The controls let you toggle which volume is visible, apply alpha clipping to improve contrast, and adjust FA intensity ranges. This approach enables examination of diffusion anisotropy alongside fiber orientation patterns without requiring complex modulation methods.

This example mirrors this NiiVue demo.

⚠️ Note: ipywidgets that rely on Python callbacks (observe, on_click) require a running kernel and do not function in the static HTML version of this notebook. Widgets using client-side trait linking (jslink) remain interactive without a kernel.

Loading...

2. Surface-Based Representations

Surface-based neuroimaging methods represent cortical structure and function directly on a two-dimensional mesh that conforms to the cortical sheet. Rather than sampling data in volumetric space, these formats associate scalar values with individual surface vertices, enabling high-resolution visualization of folding patterns, morphometry, and functional or statistical maps. NiiVue supports several widely used surface standards, including FreeSurfer meshes, CIFTI/fsLR hemisphere surfaces, and BrainVoyager SRF/SMP files. The following examples illustrate how to load and render each format, attach per-vertex data layers, and interactively explore mesh-based neuroimaging data.

FreeSurfer Surfaces

FreeSurfer generates subject-specific cortical surface meshes that explicitly encode geometry and topology in native anatomical space. These surfaces are stored in FreeSurfer’s own binary formats (e.g., white, pial, inflated) and represent the cortical sheet as a triangulated mesh with vertex-wise morphometric information. Unlike CIFTI files, FreeSurfer surfaces directly store geometry, allowing per-vertex measurements to be intrinsically defined on the mesh itself.

NiiVue can load FreeSurfer surface files directly, enabling interactive visualization of native-space cortical meshes and their associated morphometric properties.

⚠️ Note: ipywidgets that rely on Python callbacks (observe, on_click) require a running kernel and do not function in the static HTML version of this notebook. Widgets using client-side trait linking (jslink) remain interactive without a kernel.

Loading...

Interactive Clipping Plane

When visualizing surfaces together with a reference volume, NiiVue can apply an interactive clipping plane to reveal internal anatomy.

  • Right-click and drag to adjust the clipping plane’s angle (azimuth and elevation)

  • Scroll to move the clipping plane along its current orientation

The clipping plane is optional but particularly useful for understanding the spatial relationship between surface reconstructions and volumetric data.

4D Mesh Time Series (CIFTI-2)

Applying CIFTI Dense Time Series to a Standard Surface Mesh

Unlike FreeSurfer surface files, CIFTI files typically do not store surface geometry themselves; instead, they reference a standardized surface mesh onto which data are mapped.

CIFT-I files encode data defined on standardized cortical surface vertices and, optionally, subcortical voxels, unified within a single matrix representation. Conceptually, a CIFTI file is a labeled data matrix whose dimensions are mapped to brain locations, allowing surface-based and voxel-based structures to be represented together while excluding non-informative regions such as the medial wall. In the fsLR framework, per-vertex measurements (e.g., myelin maps, cortical thickness, or functional time series) are aligned to a common surface mesh, enabling consistent visualization and comparison across hemispheres and subjects. In this example, a CIFTI-2 dense time series (dtseries) is attached as a per-vertex data layer to an inflated fsLR surface, demonstrating how NiiVue maps time-varying surface data to mesh geometry and enables interactive exploration across time, opacity, and rendering shaders..

This example mirrors this NiiVue demo.

⚠️ Note: ipywidgets that rely on Python callbacks (observe, on_click) require a running kernel and do not function in the static HTML version of this notebook. Widgets using client-side trait linking (jslink) remain interactive without a kernel.

Loading...

BrainVoyager

Rendering SRF Surfaces with SMP Curvature and Statistical Maps

This example uses a high-resolution cortical surface of the left hemisphere from BrainVoyager’s sample dataset. The surface mesh (.srf) represents the folded cortical geometry, and the accompanying scalar map (.smp) encodes per-vertex measurements such as curvature or statistical values. BrainVoyager uses native surface and surface-map formats, in which geometry and per-vertex data are stored as separate but directly corresponding files in a shared surface space.

By loading the surface and associated scalar maps as layers in ipyniivue, the cortical mesh can be rendered with configurable colormaps, opacity settings, and mesh shaders, enabling visual inspection of surface-based measurements on the reconstructed anatomy.

This examples mirrors this NiiVue demo

⚠️ Note: ipywidgets that rely on Python callbacks (observe, on_click) require a running kernel and do not function in the static HTML version of this notebook. Widgets using client-side trait linking (jslink) remain interactive without a kernel.

Loading...
Loading...

Mesh curvature and Per-Vertex Scalars

This example demonstrates rendering an inflated cortical surface with a per-vertex curvature map. The inflated surface geometry is stored in a GIFTI file (.surf.gii), while the curvature data is provided as a CIFTI scalar file (.dscalar.nii), which assigns a scalar value to each vertex on the mesh. The inflated surface preserves the cortical topology while smoothing out folds, and the curvature layer encodes local folding patterns (gyrification) across the cortical sheet.

The curvature layer encodes local surface bending, providing a compact representation of sulcal and gyral structure in surface space. Interactive controls allow adjustment of layer opacity, application of MatCap textures, and selection of different mesh shaders, enabling flexible visualization of surface geometry together with vertex-wise scalar data. This example illustrates how NiiVue combines surface meshes and per-vertex measurements for surface-based visualization.

This example mirrors this NiiVue demo.

⚠️ Note: ipywidgets that rely on Python callbacks (observe, on_click) require a running kernel and do not function in the static HTML version of this notebook. Widgets using client-side trait linking (jslink) remain interactive without a kernel.

Loading...

3. Tractography

DSI-Studio tiny-tract files (TT)

This example demonstrates interactive visualization of diffusion MRI tractography. The tractography data is stored as a TinyTrack file (.tt.gz), a compact format used in DSI Studio to store track coordinates. Fiber tracts are represented as 3D streamline meshes, and a background anatomical volume (MNI152 template) provides spatial context. Interactive controls allow you to adjust fiber radius for visibility, apply dither to reduce visual clutter in dense fiber bundles, select color schemes (Global for direction-based coloring, Local for tract-specific patterns, or Fixed for uniform color), control X-ray transparency to see through tracts, and switch rendering shaders to explore different visual styles. These controls enable detailed examination of white matter pathway organization and geometry.

The background anatomy is provided as a standard NIfTI volume. Fiber tracts are represented as 3D streamline meshes, and a background anatomical volume provides spatial context. You can adjust fiber radius, apply dither, select color schemes, control X-ray transparency, and switch shaders to explore the organization and geometry of the fiber pathways.

This example mirrors this NiiVue demo.

⚠️ Note: ipywidgets that rely on Python callbacks (observe, on_click) require a running kernel and do not function in the static HTML version of this notebook. Widgets using client-side trait linking (jslink) remain interactive without a kernel.

Loading...

Advanced Tractography with Multiple Formats and Brain Surface

This example demonstrates advanced visualization combining multiple tractography datasets with anatomical surface rendering. Three white matter tracts are loaded from different file formats—VTK, TRK, and TCK—representing the right frontal aslant tract (FAT_R), inferior fronto-occipital fasciculus (IFOF_R), and superior longitudinal fasciculus (SLF1_R). A semi-transparent brain surface mesh (MZ3 format) provides anatomical context. Interactive controls allow you to adjust the shader applied to the brain surface for different visual styles, modify fiber coloring schemes (Global for direction-based coloring across the entire scene, Local for per-tract directional coloring, or Fixed to use each tract’s assigned color), change fiber radius for better visibility, and control X-ray transparency to see internal structures through the brain surface. This setup is useful for examining the spatial relationships between multiple fiber pathways and cortical anatomy, demonstrating interoperability across common tractography file formats.

This example mirrors this NiiVue demo.

⚠️ Note: ipywidgets that rely on Python callbacks (observe, on_click) require a running kernel and do not function in the static HTML version of this notebook. Widgets using client-side trait linking (jslink) remain interactive without a kernel.

Loading...

Dependencies in Jupyter/Python

  • Using the package watermark to document system environment and software versions used in this notebook, alongside the Neurodesktop version extracted from the JUPYTER_IMAGE or NEURODESKTOP_VERSION environment variables.

Last updated: 2026-05-14T06:46:04.326242+00:00

Python implementation: CPython
Python version       : 3.13.13
IPython version      : 9.12.0

Compiler    : GCC 14.3.0
OS          : Linux
Release     : 6.8.0-106-generic
Machine     : x86_64
Processor   : x86_64
CPU cores   : 16
Architecture: 64bit

IPython   : 9.12.0
ipyniivue : 2.4.4
ipywidgets: 7.8.5

Neurodesktop version: 2026-04-28
References
  1. Taylor Hanayik, Chris Rorden, Christopher Drake, Jens Ochsenmeier, Matt McCormick, Albert Armea, alexis, Anthony Androulakis, Kabilar Gunalan, Paul Wighton, John lee, Paul Taylor, Edwin Bennink, Shun, Nell Hardcastle, Yaroslav Halchenko, Florian Rupprecht, Korbinian Eckstein, Guilherme Povala, … Rahul Chaudhary. (2026). niivue/niivue: @niivue/niivue-v0.69.0. Zenodo. 10.5281/ZENODO.5786269