8.5. Computer Parcel Aggregation.

This example uses the ParcelAggregation marker to compute the mean of each parcel using the Schaefer parcellations (100 rois, 7 Yeo networks) for both 3D and 4D NIfTI.

Authors: Federico Raimondo, Synchon Mandal

License: BSD 3 clause

from junifer.testing.datagrabbers import (
    OasisVBMTestingDataGrabber,
    SPMAuditoryTestingDataGrabber,
)
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")
2025-11-18 17:37:04 [info     ] ===== Lib Versions =====       [junifer]
2025-11-18 17:37:04 [info     ] click: 8.1.8                   [junifer]
2025-11-18 17:37:04 [info     ] numpy: 1.26.4                  [junifer]
2025-11-18 17:37:04 [info     ] scipy: 1.15.0                  [junifer]
2025-11-18 17:37:04 [info     ] datalad: 1.1.6                 [junifer]
2025-11-18 17:37:04 [info     ] pandas: 2.1.4                  [junifer]
2025-11-18 17:37:04 [info     ] nibabel: 5.3.2                 [junifer]
2025-11-18 17:37:04 [info     ] nilearn: 0.10.4                [junifer]
2025-11-18 17:37:04 [info     ] sqlalchemy: 2.0.44             [junifer]
2025-11-18 17:37:04 [info     ] ruamel.yaml: 0.18.16           [junifer]
2025-11-18 17:37:04 [info     ] tqdm: 4.66.6                   [junifer]
2025-11-18 17:37:04 [info     ] templateflow: 24.2.2           [junifer]
2025-11-18 17:37:04 [info     ] junifer_data: None             [junifer]
2025-11-18 17:37:04 [info     ] junifer: 0.0.7.dev401          [junifer]
2025-11-18 17:37:04 [info     ] ========================       [junifer]

Perform parcel aggregation on VBM GM data (3D) from OASIS dataset

with OasisVBMTestingDataGrabber() 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,
    )
    # 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)
2025-11-18 17:37:04 [info     ] Getting element ('sub-01',)    [junifer]
2025-11-18 17:37:04 [info     ] Reading VBM_GM from /github/home/nilearn_data/oasis1/OAS1_0001_MR1/mwrc1OAS1_0001_MR1_mpr_anon_fslswapdim_bet.nii.gz [junifer]
2025-11-18 17:37:04 [info     ] VBM_GM is of type NIFTI        [junifer]
2025-11-18 17:37:04 [info     ] Computing DataType.VBM_GM      [junifer]
2025-11-18 17:37:04 [info     ] Parcellation will be warped from MNI152NLin6Asym to MNI152Lin using highest resolution [junifer]
2025-11-18 17:37:04 [info     ] Parcellation parameters:       [junifer]
2025-11-18 17:37:04 [info     ]         resolution: None              [junifer]
2025-11-18 17:37:04 [info     ]         n_rois: 100                   [junifer]
2025-11-18 17:37:04 [info     ]         yeo_networks: 7               [junifer]
2025-11-18 17:37:04 [info     ] Resolution set to None, using highest resolution. [junifer]
2025-11-18 17:37:04 [info     ] Start annex operation          [datalad.annex] dlm_progress=annexprogress-139689200586000 dlm_progress_label=Total dlm_progress_noninteractive_level=5 dlm_progress_total=233605 dlm_progress_unit=' Bytes'
2025-11-18 17:37:05 [info     ] Start annex action: {'command': 'get', 'file': 'parcellations/Schaefer2018/Yeo2011/Schaefer2018_100Parcels_7Networks_order_FSLMNI152_1mm.nii.gz', 'input': ['parcellations/Schaefer2018/Yeo2011/Schaefer2018_100Parcels_7Networks_order_FSLMNI152_1mm.nii.gz'], 'key': 'MD5E-s233605--0709e73f84a0cd60687ff4add7f8fd05.nii.gz', 'note': 'from gin-src...'} [datalad.annex] dlm_progress=annexprogress-139689200586000-5135629851891593778 dlm_progress_label='Get parcella .. 2_1mm.nii.gz' dlm_progress_noninteractive_level=5 dlm_progress_total=233605.0 dlm_progress_unit=' Bytes'
2025-11-18 17:37:05 [info     ] 14.03%                         [datalad.annex] dlm_progress=annexprogress-139689200586000-5135629851891593778 dlm_progress_noninteractive_level=5 dlm_progress_update=32768.0
2025-11-18 17:37:06 [info     ] 63.12%                         [datalad.annex] dlm_progress=annexprogress-139689200586000-5135629851891593778 dlm_progress_noninteractive_level=5 dlm_progress_update=147456.0
2025-11-18 17:37:06 [info     ] Finished annex action: None    [datalad.annex] dlm_progress=annexprogress-139689200586000-5135629851891593778 dlm_progress_noninteractive_level=5
2025-11-18 17:37:06 [info     ] Finished annex get             [datalad.annex] dlm_progress=annexprogress-139689200586000 dlm_progress_noninteractive_level=5
2025-11-18 17:37:06 [info     ] Loading parcellation: /github/home/junifer_data/v5/parcellations/Schaefer2018/Yeo2011/Schaefer2018_100Parcels_7Networks_order_FSLMNI152_1mm.nii.gz [junifer]
2025-11-18 17:37:08 [info     ] Downloading template MNI152Lin (T1w in resolution 1) [junifer]
2025-11-18 17:37:09 [info     ] antsApplyTransforms command to be executed:
antsApplyTransforms -d 3 -e 3 -n 'GenericLabel[NearestNeighbor]' -i /tmp/tmpuuq7ljtr/ants_parcellation_warper_Schaefer100x7_from_MNI152NLin6Asym_to_MNI152Lin_33da1a4e-c4a5-11f0-a26b-be2f509f929d_80esxv9/prewarp_parcellation.nii.gz -r /tmp/tmpuuq7ljtr/ants_parcellation_warper_Schaefer100x7_from_MNI152NLin6Asym_to_MNI152Lin_33da1a4e-c4a5-11f0-a26b-be2f509f929d_80esxv9/MNI152Lin_T1w.nii.gz -t /github/home/junifer_data/v5/.git/annex/objects/JQ/Pp/SHA256E-s145422752--5091350b36f951d455dabd429ebe86c493c75a0217dca311ae355f1d62e080b0.h5/SHA256E-s145422752--5091350b36f951d455dabd429ebe86c493c75a0217dca311ae355f1d62e080b0.h5 -o /tmp/tmpuuq7ljtr/ants_parcellation_warper_Schaefer100x7_from_MNI152NLin6Asym_to_MNI152Lin_33da1a4e-c4a5-11f0-a26b-be2f509f929d_80esxv9/parcellation_warped.nii.gz [junifer]
2025-11-18 17:37:15 [info     ] antsApplyTransforms command succeeded with the following output:
 [junifer]
2025-11-18 17:37:17 [info     ] No storage specified, returning dictionary [junifer]
dict_keys([<DataType.VBM_GM: 'VBM_GM'>])
(1, 100)

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

with SPMAuditoryTestingDataGrabber() 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)
2025-11-18 17:37:17 [info     ] Getting element ('sub001',)    [junifer]
2025-11-18 17:37:19 [info     ] Reading BOLD from /tmp/tmpf7c8v3r_/sub001_bold.nii.gz [junifer]
2025-11-18 17:37:19 [info     ] BOLD is of type NIFTI          [junifer]
2025-11-18 17:37:19 [info     ] Reading T1w from /tmp/tmpf7c8v3r_/sub001_T1w.nii.gz [junifer]
2025-11-18 17:37:19 [info     ] T1w is of type NIFTI           [junifer]
2025-11-18 17:37:19 [info     ] Computing DataType.BOLD        [junifer]
2025-11-18 17:37:19 [info     ] Parcellation will be warped from MNI152NLin6Asym to MNI152Lin using highest resolution [junifer]
2025-11-18 17:37:19 [info     ] Parcellation parameters:       [junifer]
2025-11-18 17:37:19 [info     ]         resolution: None              [junifer]
2025-11-18 17:37:19 [info     ]         n_rois: 100                   [junifer]
2025-11-18 17:37:19 [info     ]         yeo_networks: 7               [junifer]
2025-11-18 17:37:19 [info     ] Resolution set to None, using highest resolution. [junifer]
2025-11-18 17:37:19 [info     ] Loading parcellation: /github/home/junifer_data/v5/parcellations/Schaefer2018/Yeo2011/Schaefer2018_100Parcels_7Networks_order_FSLMNI152_1mm.nii.gz [junifer]
2025-11-18 17:37:21 [info     ] Downloading template MNI152Lin (T1w in resolution 1) [junifer]
2025-11-18 17:37:22 [info     ] antsApplyTransforms command to be executed:
antsApplyTransforms -d 3 -e 3 -n 'GenericLabel[NearestNeighbor]' -i /tmp/tmpuuq7ljtr/ants_parcellation_warper_Schaefer100x7_from_MNI152NLin6Asym_to_MNI152Lin_3b71d224-c4a5-11f0-a26b-be2f509f929d8zvtpnyc/prewarp_parcellation.nii.gz -r /tmp/tmpuuq7ljtr/ants_parcellation_warper_Schaefer100x7_from_MNI152NLin6Asym_to_MNI152Lin_3b71d224-c4a5-11f0-a26b-be2f509f929d8zvtpnyc/MNI152Lin_T1w.nii.gz -t /github/home/junifer_data/v5/.git/annex/objects/JQ/Pp/SHA256E-s145422752--5091350b36f951d455dabd429ebe86c493c75a0217dca311ae355f1d62e080b0.h5/SHA256E-s145422752--5091350b36f951d455dabd429ebe86c493c75a0217dca311ae355f1d62e080b0.h5 -o /tmp/tmpuuq7ljtr/ants_parcellation_warper_Schaefer100x7_from_MNI152NLin6Asym_to_MNI152Lin_3b71d224-c4a5-11f0-a26b-be2f509f929d8zvtpnyc/parcellation_warped.nii.gz [junifer]
2025-11-18 17:37:28 [info     ] antsApplyTransforms command succeeded with the following output:
 [junifer]
2025-11-18 17:37:31 [info     ] No storage specified, returning dictionary [junifer]
dict_keys([<DataType.BOLD: 'BOLD'>])
(96, 100)

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

Gallery generated by Sphinx-Gallery