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-12-05 13:12:18 [info     ] ===== Lib Versions =====       [junifer]
2025-12-05 13:12:18 [info     ] click: 8.1.8                   [junifer]
2025-12-05 13:12:18 [info     ] numpy: 1.26.4                  [junifer]
2025-12-05 13:12:18 [info     ] scipy: 1.15.0                  [junifer]
2025-12-05 13:12:18 [info     ] datalad: 1.1.6                 [junifer]
2025-12-05 13:12:18 [info     ] pandas: 2.1.4                  [junifer]
2025-12-05 13:12:18 [info     ] nibabel: 5.3.2                 [junifer]
2025-12-05 13:12:18 [info     ] nilearn: 0.10.4                [junifer]
2025-12-05 13:12:18 [info     ] sqlalchemy: 2.0.44             [junifer]
2025-12-05 13:12:18 [info     ] ruamel.yaml: 0.18.16           [junifer]
2025-12-05 13:12:18 [info     ] tqdm: 4.66.6                   [junifer]
2025-12-05 13:12:18 [info     ] templateflow: 24.2.2           [junifer]
2025-12-05 13:12:18 [info     ] junifer_data: None             [junifer]
2025-12-05 13:12:18 [info     ] junifer: 0.0.7.dev430          [junifer]
2025-12-05 13:12:18 [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-12-05 13:12:18 [info     ] Getting element ('sub-01',)    [junifer]
2025-12-05 13:12:18 [info     ] Reading VBM_GM from /github/home/nilearn_data/oasis1/OAS1_0001_MR1/mwrc1OAS1_0001_MR1_mpr_anon_fslswapdim_bet.nii.gz [junifer]
2025-12-05 13:12:18 [info     ] VBM_GM is of type NIFTI        [junifer]
2025-12-05 13:12:18 [info     ] Computing DataType.VBM_GM      [junifer]
2025-12-05 13:12:18 [info     ] Parcellation will be warped from MNI152NLin6Asym to MNI152Lin using highest resolution [junifer]
2025-12-05 13:12:18 [info     ] Parcellation parameters:       [junifer]
2025-12-05 13:12:18 [info     ]         resolution: None              [junifer]
2025-12-05 13:12:18 [info     ]         n_rois: 100                   [junifer]
2025-12-05 13:12:18 [info     ]         yeo_networks: 7               [junifer]
2025-12-05 13:12:18 [info     ] Resolution set to None, using highest resolution. [junifer]
2025-12-05 13:12:18 [info     ] Start annex operation          [datalad.annex] dlm_progress=annexprogress-140610731763824 dlm_progress_label=Total dlm_progress_noninteractive_level=5 dlm_progress_total=233605 dlm_progress_unit=' Bytes'
2025-12-05 13:12:20 [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-140610731763824-5480809050933991960 dlm_progress_label='Get parcella .. 2_1mm.nii.gz' dlm_progress_noninteractive_level=5 dlm_progress_total=233605.0 dlm_progress_unit=' Bytes'
2025-12-05 13:12:20 [info     ] 38.57%                         [datalad.annex] dlm_progress=annexprogress-140610731763824-5480809050933991960 dlm_progress_noninteractive_level=5 dlm_progress_update=90112.0
2025-12-05 13:12:20 [info     ] Finished annex action: None    [datalad.annex] dlm_progress=annexprogress-140610731763824-5480809050933991960 dlm_progress_noninteractive_level=5
2025-12-05 13:12:20 [info     ] Finished annex get             [datalad.annex] dlm_progress=annexprogress-140610731763824 dlm_progress_noninteractive_level=5
2025-12-05 13:12:20 [info     ] Loading parcellation: /github/home/junifer_data/v5/parcellations/Schaefer2018/Yeo2011/Schaefer2018_100Parcels_7Networks_order_FSLMNI152_1mm.nii.gz [junifer]
2025-12-05 13:12:23 [info     ] Downloading template MNI152Lin (T1w in resolution 1) [junifer]
2025-12-05 13:12:23 [info     ] antsApplyTransforms command to be executed:
antsApplyTransforms -d 3 -e 3 -n 'GenericLabel[NearestNeighbor]' -i /tmp/junifer/tmp5v6qhb8l/ants_parcellation_warper_Schaefer100x7_from_MNI152NLin6Asym_to_MNI152Lin_085e4162-d1dc-11f0-85d5-6ae627a7b3dcmiyg41qa/prewarp_parcellation.nii.gz -r /tmp/junifer/tmp5v6qhb8l/ants_parcellation_warper_Schaefer100x7_from_MNI152NLin6Asym_to_MNI152Lin_085e4162-d1dc-11f0-85d5-6ae627a7b3dcmiyg41qa/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/junifer/tmp5v6qhb8l/ants_parcellation_warper_Schaefer100x7_from_MNI152NLin6Asym_to_MNI152Lin_085e4162-d1dc-11f0-85d5-6ae627a7b3dcmiyg41qa/parcellation_warped.nii.gz [junifer]
2025-12-05 13:12:30 [info     ] antsApplyTransforms command succeeded with the following output:
 [junifer]
2025-12-05 13:12:31 [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-12-05 13:12:31 [info     ] Getting element ('sub001',)    [junifer]
2025-12-05 13:12:33 [info     ] Reading BOLD from /tmp/tmpjbwi87md/sub001_bold.nii.gz [junifer]
2025-12-05 13:12:33 [info     ] BOLD is of type NIFTI          [junifer]
2025-12-05 13:12:33 [info     ] Reading T1w from /tmp/tmpjbwi87md/sub001_T1w.nii.gz [junifer]
2025-12-05 13:12:33 [info     ] T1w is of type NIFTI           [junifer]
2025-12-05 13:12:33 [info     ] Computing DataType.BOLD        [junifer]
2025-12-05 13:12:33 [info     ] Parcellation will be warped from MNI152NLin6Asym to MNI152Lin using highest resolution [junifer]
2025-12-05 13:12:33 [info     ] Parcellation parameters:       [junifer]
2025-12-05 13:12:33 [info     ]         resolution: None              [junifer]
2025-12-05 13:12:33 [info     ]         n_rois: 100                   [junifer]
2025-12-05 13:12:33 [info     ]         yeo_networks: 7               [junifer]
2025-12-05 13:12:33 [info     ] Resolution set to None, using highest resolution. [junifer]
2025-12-05 13:12:33 [info     ] Loading parcellation: /github/home/junifer_data/v5/parcellations/Schaefer2018/Yeo2011/Schaefer2018_100Parcels_7Networks_order_FSLMNI152_1mm.nii.gz [junifer]
2025-12-05 13:12:36 [info     ] Downloading template MNI152Lin (T1w in resolution 1) [junifer]
2025-12-05 13:12:36 [info     ] antsApplyTransforms command to be executed:
antsApplyTransforms -d 3 -e 3 -n 'GenericLabel[NearestNeighbor]' -i /tmp/junifer/tmp5v6qhb8l/ants_parcellation_warper_Schaefer100x7_from_MNI152NLin6Asym_to_MNI152Lin_1012dd50-d1dc-11f0-85d5-6ae627a7b3dcp1vb68pp/prewarp_parcellation.nii.gz -r /tmp/junifer/tmp5v6qhb8l/ants_parcellation_warper_Schaefer100x7_from_MNI152NLin6Asym_to_MNI152Lin_1012dd50-d1dc-11f0-85d5-6ae627a7b3dcp1vb68pp/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/junifer/tmp5v6qhb8l/ants_parcellation_warper_Schaefer100x7_from_MNI152NLin6Asym_to_MNI152Lin_1012dd50-d1dc-11f0-85d5-6ae627a7b3dcp1vb68pp/parcellation_warped.nii.gz [junifer]
2025-12-05 13:12:43 [info     ] antsApplyTransforms command succeeded with the following output:
 [junifer]
2025-12-05 13:12:46 [info     ] No storage specified, returning dictionary [junifer]
dict_keys([<DataType.BOLD: 'BOLD'>])
(96, 100)

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

Gallery generated by Sphinx-Gallery