voxel-wise plotting for 3D volumes¶
in this tutorial, you will see how to use yabplot to visualize continuous 3D volumes. Unlike surface-based plotting, voxel-wise plotting renders the internal structure of the data using nested isosurfaces.
inputs and outputs¶
we start with:
- data volume (
.nii.gz): the 3D NIfTI file where each voxel contains continuous scalar data (e.g., an fMRI task activation map, t-statistics, or p-values).
the volume can be in any space, as yabplot places it using the affine in the NIfTI header. the built-in context brain is in MNI152NLin6Asym space, so for data in another space see section 5.
import pooch
import yabplot as yab
import numpy as np
1. basic voxel-wise plotting¶
the simplest way to plot is to pass the path to the NIfTI file. By default, yabplot will extract the top 5% of voxels and render them as nested shells.
# fetch an example 3D PET volume (D2 receptor density)
# [1] Hansen et al., Nat Neurosci (2022)
nii_url = "https://github.com/netneurolab/hansen_receptors/raw/main/data/PET_nifti_images/D2_flb457_hc55_sandiego.nii.gz"
nii_path = pooch.retrieve(url=nii_url, known_hash=None, path=pooch.os_cache("yabplot"),
fname="sandiego2015_pet.nii.gz")
# standard visualization using the 'Reds' colormap
ax = yab.plot_voxelwise(nii_path, cmap='Reds')
2. controlling the threshold¶
you can control which voxels are visible using the threshold parameter. This accepts either a direct numeric value or a percentile string (e.g., '99%' to see only the highest intensity peaks).
# show only the top 1% of highest intensity voxels
ax = yab.plot_voxelwise(nii_path, threshold='99%', cmap='magma')
3. refining the isosurfaces¶
you can adjust how many "layers" are shown and how smooth they appear.
n_levels: higher values create a smoother transition between intensity levels.blur_sigma: applies a slight Gaussian blur to the data before extracting shells, which helps remove voxel "jaggedness" in noisy data (but be careful in using this to not distort the true findings!).
ax = yab.plot_voxelwise(
nii_path,
n_levels=50, # more detailed shells
blur_sigma=1.0, # smooth out voxel edges
cmap='plasma',
threshold='99%'
)
ax = yab.plot_voxelwise(
nii_path,
n_levels=5, # less detailed shells (more cartoony)
blur_sigma=0.0, # no smoothing (default)
cmap='plasma',
threshold='99%'
)
4. changing how the nifti is displayed with context brain mesh¶
by default, the voxel data is rendered inside a light-gray brain mesh, which you can modify as with most plotting functions. however, the parameter ignore_bmesh is specifc to plot_voxelwise and is True by default, indicating that the image you are visualising is plotted "after" the context brain (but still inside of context brain). see the difference below.
ax = yab.plot_voxelwise(
nii_path,
bmesh='inflated', # use an inflated brain for context
ignore_bmesh=True # dont let the brain surface "hide" the image data
)
ax = yab.plot_voxelwise(
nii_path,
bmesh='inflated', # use an inflated brain for context
ignore_bmesh=False # let the brain surface obscure internal data
)
5. matching the context brain to the space of your data¶
plot_voxelwise places the image using the affine stored in the NIfTI header. nothing is resampled or registered, so the image and the context brain only line up if they are in the same space.
the built-in context meshes are group-average cortical surfaces from the Human Connectome Project (fs_LR 32k). they are in MNI152NLin6Asym space (the MNI152 template that comes with FSL) and cover the cortex only, so they have no cerebellum or brainstem.
if your data is in another space, for example MNI152NLin2009cAsym (the default output space of fMRIPrep) or a subject's native space, the image can reach a few millimetres past the edge of the context brain. the PET maps of [1] used in this tutorial are an example, as they are in MNI152NLin2009cAsym.
to get a context brain that fits, build one from a brain mask in the same space as your data with yab.load_nii_as_mesh and pass it as bmesh. here we use the brain mask of the template from TemplateFlow. with fMRIPrep outputs you can also use the desc-brain_mask file written next to your data.
# fetch a PET volume with more signal near the cortical surface (CB1 receptor density) [1]
cb1_url = "https://github.com/netneurolab/hansen_receptors/raw/main/data/PET_nifti_images/CB1_omar_hc77_normandin.nii.gz"
cb1_path = pooch.retrieve(url=cb1_url, known_hash=None, path=pooch.os_cache("yabplot"),
fname="normandin2015_pet.nii.gz")
# fetch the brain mask of the template this volume is in (MNI152NLin2009cAsym)
mask_url = "https://templateflow.s3.amazonaws.com/tpl-MNI152NLin2009cAsym/tpl-MNI152NLin2009cAsym_res-01_desc-brain_mask.nii.gz"
mask_path = pooch.retrieve(url=mask_url, known_hash=None, path=pooch.os_cache("yabplot"),
fname="tpl-MNI152NLin2009cAsym_res-01_desc-brain_mask.nii.gz")
# build a context brain from the mask
custom_bmesh = yab.load_nii_as_mesh(mask_path)
# default context brain: the image reaches past its edge
ax = yab.plot_voxelwise(cb1_path, cmap='Reds', views=['left_lateral', 'anterior', 'superior'])
# context brain in the same space as the image
ax = yab.plot_voxelwise(cb1_path, cmap='Reds', views=['left_lateral', 'anterior', 'superior'],
bmesh=custom_bmesh)
the image now sits inside the context brain, and the cerebellum and brainstem are part of it. see docs/tutorials/custom_background_mesh.ipynb for more on yab.load_nii_as_mesh, including data in native space.