8.5. Compute Parcel Aggregation.

This example uses the ParcelAggregation marker to compute the mean of each parcel using the Tian subcortical parcellation (scale I, 16 ROIs) for a 3D NIfTI and the Schaefer parcellation (100 rois, 7 Yeo networks) for a 4D NIfTI.

Authors: Federico Raimondo, Synchon Mandal

License: BSD 3 clause

from junifer.testing.datagrabbers import (
    ADHDTestingDataGrabber,
    UCLACNPVBMTestingDataGrabber,
)
from junifer.datagrabber import DataType
from junifer.datareader import DefaultDataReader
from junifer.markers import ParcelAggregation
from junifer.stats import AggFunc
from junifer.utils import configure_logging

Set the logging level to info to see extra information

configure_logging(level="INFO")
2026-10-19 21:14:34 [info     ] ===== Lib Versions =====       [junifer] pkg=utils
2026-10-19 21:14:34 [info     ] click: 8.5.0                   [junifer] pkg=utils
2026-10-19 21:14:34 [info     ] numpy: 2.4.6                   [junifer] pkg=utils
2026-10-19 21:14:34 [info     ] scipy: 1.17.1                  [junifer] pkg=utils
2026-10-19 21:14:34 [info     ] datalad: 1.7.1                 [junifer] pkg=utils
2026-10-19 21:14:34 [info     ] pandas: 3.0.6                  [junifer] pkg=utils
2026-10-19 21:14:34 [info     ] nibabel: 5.4.2                 [junifer] pkg=utils
2026-10-19 21:14:34 [info     ] nilearn: 0.14.1                [junifer] pkg=utils
2026-10-19 21:14:34 [info     ] sqlalchemy: 2.1.5              [junifer] pkg=utils
2026-10-19 21:14:34 [info     ] ruamel.yaml: 0.19.1            [junifer] pkg=utils
2026-10-19 21:14:34 [info     ] h5py: 3.16.0                   [junifer] pkg=utils
2026-10-19 21:14:34 [info     ] tqdm: 4.70.1                   [junifer] pkg=utils
2026-10-19 21:14:34 [info     ] templateflow: 25.1.2           [junifer] pkg=utils
2026-10-19 21:14:34 [info     ] lapy: 1.7.0                    [junifer] pkg=utils
2026-10-19 21:14:34 [info     ] junifer_data: 1.6.1            [junifer] pkg=utils
2026-10-19 21:14:34 [info     ] junifer: 0.0.8                 [junifer] pkg=utils
2026-10-19 21:14:34 [info     ] ========================       [junifer] pkg=utils

Perform parcel aggregation on VBM GM data (3D) from the UCLA CNP dataset

with UCLACNPVBMTestingDataGrabber() as dg:
    # Get the first element
    element = dg.get_elements()[0]
    # Read the element
    element_data = DefaultDataReader().fit_transform(dg[element])
    # Initialize marker
    marker = ParcelAggregation(
        parcellation="TianxS1x3TxMNInonlinear2009cAsym",
        method=AggFunc.Mean,
    )
    # Compute feature
    feature = marker.fit_transform(element_data)
    # Print the output
    print(feature.keys())
    print(feature["VBM_GM"]["aggregation"]["data"].shape)  # Shape is (1 x parcels)
2026-10-19 21:14:34 [info     ] Parameters: {'types': ['VBM_GM'], 'datadir': '/tmp/tmpmc101b16', 'resolution': 2.0} [junifer] component=UCLACNPVBMTestingDataGrabber pkg=datagrabber step=datagrabber
2026-10-19 21:14:34 [info     ] Getting element ('sub-10206',) [junifer] pkg=datagrabber step=datagrabber
2026-10-19 21:14:34 [info     ] Reading VBM_GM from /home/docker/nilearn_data/ds000030/ds000030_R1.0.4/uncompressed/derivatives/fmriprep/sub-10206/anat/sub-10206_T1w_space-MNI152NLin2009cAsym_class-GM_probtissue_res-2mm.nii [junifer] pkg=datareader step=datareader
2026-10-19 21:14:34 [info     ] VBM_GM is of type NIFTI        [junifer] pkg=datareader step=datareader
2026-10-19 21:14:34 [info     ] Parameters: {'on': ['T1w', 'T2w', 'BOLD', 'VBM_GM', 'VBM_WM', 'VBM_CSF', 'fALFF', 'GCOR', 'LCOR'], 'name': 'ParcelAggregation', 'parcellation': ['TianxS1x3TxMNInonlinear2009cAsym'], 'method': 'mean', 'method_params': None, 'time_method': None, 'time_method_params': None, 'masks': None} [junifer] component=ParcelAggregation pkg=markers step=marker
2026-10-19 21:14:34 [info     ] Computing DataType.VBM_GM      [junifer] pkg=markers step=marker
2026-10-19 21:14:34 [info     ] Parcellation parameters:       [junifer] pkg=data
2026-10-19 21:14:34 [info     ]         resolution: 2.0               [junifer] pkg=data
2026-10-19 21:14:34 [info     ]         scale: 1                      [junifer] pkg=data
2026-10-19 21:14:34 [info     ]         space: MNI152NLin2009cAsym    [junifer] pkg=data
2026-10-19 21:14:34 [info     ]         magneticfield: 3T             [junifer] pkg=data
2026-10-19 21:14:34 [info     ] Loading parcellation: /home/docker/junifer_data/v8/parcellations/Melbourne/v1.4/3T/Subcortex-Only/Tian_Subcortex_S1_3T_2009cAsym.nii.gz [junifer] pkg=data
2026-10-19 21:14:35 [info     ] No storage specified, returning dictionary [junifer] pkg=markers step=marker
dict_keys([<DataType.VBM_GM: 'VBM_GM'>])
(1, 16)

Perform parcel aggregation on BOLD data (4D) from ADHD dataset

with ADHDTestingDataGrabber() as dg:
    # Get the first element
    element = dg.get_elements()[0]
    # Read the element
    element_data = DefaultDataReader().fit_transform(dg[element])
    # Initialize marker
    marker = ParcelAggregation(
        parcellation="Schaefer100x7",
        method=AggFunc.Mean,
        on=[DataType.BOLD],
    )
    # Compute feature
    feature = marker.fit_transform(element_data)
    # Print the output
    print(feature.keys())
    print(feature["BOLD"]["aggregation"]["data"].shape)  # Shape is (timepoints x parcels)
2026-10-19 21:14:35 [info     ] Parameters: {'types': ['BOLD'], 'datadir': '/tmp/tmp5u84on0q', 'resolution': 3.0, 'n_timepoints': 50} [junifer] component=ADHDTestingDataGrabber pkg=datagrabber step=datagrabber
[fetch_adhd] Dataset directory found: /home/docker/nilearn_data/adhd
2026-10-19 21:14:35 [info     ] Getting element ('sub-01',)    [junifer] pkg=datagrabber step=datagrabber
2026-10-19 21:14:35 [info     ] Reading BOLD from /home/docker/nilearn_data/adhd/data/0010042/0010042_rest_tshift_RPI_voreg_mni_res-3mm_tp-50.nii [junifer] pkg=datareader step=datareader
2026-10-19 21:14:35 [info     ] BOLD is of type NIFTI          [junifer] pkg=datareader step=datareader
2026-10-19 21:14:35 [info     ] Parameters: {'on': ['BOLD'], 'name': 'ParcelAggregation', 'parcellation': ['Schaefer100x7'], 'method': 'mean', 'method_params': None, 'time_method': None, 'time_method_params': None, 'masks': None} [junifer] component=ParcelAggregation pkg=markers step=marker
2026-10-19 21:14:35 [info     ] Computing DataType.BOLD        [junifer] pkg=markers step=marker
2026-10-19 21:14:35 [info     ] Parcellation parameters:       [junifer] pkg=data
2026-10-19 21:14:35 [info     ]         resolution: 3.0               [junifer] pkg=data
2026-10-19 21:14:35 [info     ]         n_rois: 100                   [junifer] pkg=data
2026-10-19 21:14:35 [info     ]         yeo_networks: 7               [junifer] pkg=data
2026-10-19 21:14:35 [info     ] Loading parcellation: /home/docker/junifer_data/v8/parcellations/Schaefer2018/Yeo2011/Schaefer2018_100Parcels_7Networks_order_FSLMNI152_2mm.nii.gz [junifer] pkg=data
2026-10-19 21:14:35 [info     ] No storage specified, returning dictionary [junifer] pkg=markers step=marker
dict_keys([<DataType.BOLD: 'BOLD'>])
(50, 100)

Total running time of the script: (0 minutes 2.169 seconds)

Gallery generated by Sphinx-Gallery