Note
Go to the end to download the full example code.
8.4. 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.datareader import DefaultDataReader
from junifer.markers import ParcelAggregation
from junifer.utils import configure_logging
Set the logging level to info to see extra information
configure_logging(level="INFO")
/__w/junifer/junifer/junifer/utils/logging.py:155: UserWarning: The '__version__' attribute is deprecated and will be removed in MarkupSafe 3.1. Use feature detection, or `importlib.metadata.version("markupsafe")`, instead.
vstring = str(getattr(module, "__version__", None))
2025-05-09 12:39:24,958 - JUNIFER - INFO - ===== Lib Versions =====
2025-05-09 12:39:24,958 - JUNIFER - INFO - click: 8.1.8
2025-05-09 12:39:24,958 - JUNIFER - INFO - numpy: 1.26.4
2025-05-09 12:39:24,958 - JUNIFER - INFO - scipy: 1.15.0
2025-05-09 12:39:24,958 - JUNIFER - INFO - datalad: 1.1.5
2025-05-09 12:39:24,958 - JUNIFER - INFO - pandas: 2.1.4
2025-05-09 12:39:24,958 - JUNIFER - INFO - nibabel: 5.3.2
2025-05-09 12:39:24,958 - JUNIFER - INFO - nilearn: 0.10.4
2025-05-09 12:39:24,958 - JUNIFER - INFO - sqlalchemy: 2.0.40
2025-05-09 12:39:24,959 - JUNIFER - INFO - ruamel.yaml: 0.18.10
2025-05-09 12:39:24,959 - JUNIFER - INFO - tqdm: 4.66.6
2025-05-09 12:39:24,959 - JUNIFER - INFO - templateflow: 24.2.2
2025-05-09 12:39:24,959 - JUNIFER - INFO - junifer_data: None
2025-05-09 12:39:24,959 - JUNIFER - INFO - junifer: 0.0.7.dev83
2025-05-09 12:39:24,959 - JUNIFER - INFO - ========================
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="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-05-09 12:39:24,964 - JUNIFER - INFO - Getting element sub-01
2025-05-09 12:39:24,965 - JUNIFER - INFO - Reading VBM_GM from /github/home/nilearn_data/oasis1/OAS1_0001_MR1/mwrc1OAS1_0001_MR1_mpr_anon_fslswapdim_bet.nii.gz
2025-05-09 12:39:24,965 - JUNIFER - INFO - VBM_GM is of type NIFTI
2025-05-09 12:39:24,966 - JUNIFER - INFO - Computing VBM_GM
2025-05-09 12:39:24,966 - JUNIFER - INFO - Parcellation will be warped from MNI152NLin6Asym to MNI152Lin using highest resolution
2025-05-09 12:39:24,966 - JUNIFER - INFO - Parcellation parameters:
2025-05-09 12:39:24,966 - JUNIFER - INFO - resolution: None
2025-05-09 12:39:24,966 - JUNIFER - INFO - n_rois: 100
2025-05-09 12:39:24,966 - JUNIFER - INFO - yeo_networks: 7
2025-05-09 12:39:24,966 - JUNIFER - INFO - Resolution set to None, using highest resolution.
2025-05-09 12:39:25,040 - JUNIFER - INFO - Loading parcellation: /github/home/junifer_data/v2/parcellations/Schaefer2018/Yeo2011/Schaefer2018_100Parcels_7Networks_order_FSLMNI152_1mm.nii.gz
2025-05-09 12:39:27,334 - JUNIFER - INFO - Downloading template MNI152Lin (T1w in resolution 1)
2025-05-09 12:39:27,840 - JUNIFER - INFO - antsApplyTransforms command to be executed:
antsApplyTransforms -d 3 -e 3 -n 'GenericLabel[NearestNeighbor]' -i /tmp/junifer/tmpxe1wwcwb/ants_parcellation_warper_Schaefer100x7_from_MNI152NLin6Asym_to_MNI152Lin_a3e40428-2cd2-11f0-b407-763dc8eb2af0bhd7b19r/prewarp_parcellation.nii.gz -r /tmp/junifer/tmpxe1wwcwb/ants_parcellation_warper_Schaefer100x7_from_MNI152NLin6Asym_to_MNI152Lin_a3e40428-2cd2-11f0-b407-763dc8eb2af0bhd7b19r/MNI152Lin_T1w.nii.gz -t /github/home/junifer_data/v2/.git/annex/objects/JQ/Pp/SHA256E-s145422752--5091350b36f951d455dabd429ebe86c493c75a0217dca311ae355f1d62e080b0.h5/SHA256E-s145422752--5091350b36f951d455dabd429ebe86c493c75a0217dca311ae355f1d62e080b0.h5 -o /tmp/junifer/tmpxe1wwcwb/ants_parcellation_warper_Schaefer100x7_from_MNI152NLin6Asym_to_MNI152Lin_a3e40428-2cd2-11f0-b407-763dc8eb2af0bhd7b19r/parcellation_warped.nii.gz
2025-05-09 12:39:34,072 - JUNIFER - INFO - antsApplyTransforms command succeeded with the following output:
2025-05-09 12:39:35,397 - JUNIFER - INFO - No storage specified, returning dictionary
dict_keys(['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="mean", on="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-05-09 12:39:35,398 - JUNIFER - INFO - Getting element sub001
Dataset created in /github/home/nilearn_data/spm_auditory
Data absent, downloading...
Downloading data from https://www.fil.ion.ucl.ac.uk/spm/download/data/MoAEpilot/MoAEpilot.zip ...
Downloaded 1310720 of 34212021 bytes (3.8%, 25.9s remaining)
Downloaded 6447104 of 34212021 bytes (18.8%, 8.8s remaining)
Downloaded 11329536 of 34212021 bytes (33.1%, 6.2s remaining)
Downloaded 16490496 of 34212021 bytes (48.2%, 4.4s remaining)
Downloaded 21209088 of 34212021 bytes (62.0%, 3.1s remaining)
Downloaded 24002560 of 34212021 bytes (70.2%, 2.6s remaining)
Downloaded 27017216 of 34212021 bytes (79.0%, 1.9s remaining)
Downloaded 30162944 of 34212021 bytes (88.2%, 1.1s remaining)
Downloaded 33374208 of 34212021 bytes (97.6%, 0.2s remaining) ...done. (11 seconds, 0 min)
Extracting data from /github/home/nilearn_data/spm_auditory/sub001/MoAEpilot.zip..... done.
2025-05-09 12:39:48,989 - JUNIFER - INFO - Reading BOLD from /tmp/tmpv0_2dc8x/sub001_bold.nii.gz
2025-05-09 12:39:48,989 - JUNIFER - INFO - BOLD is of type NIFTI
2025-05-09 12:39:48,990 - JUNIFER - INFO - Reading T1w from /tmp/tmpv0_2dc8x/sub001_T1w.nii.gz
2025-05-09 12:39:48,990 - JUNIFER - INFO - T1w is of type NIFTI
2025-05-09 12:39:48,991 - JUNIFER - INFO - Computing BOLD
2025-05-09 12:39:48,991 - JUNIFER - INFO - Parcellation will be warped from MNI152NLin6Asym to MNI152Lin using highest resolution
2025-05-09 12:39:48,991 - JUNIFER - INFO - Parcellation parameters:
2025-05-09 12:39:48,991 - JUNIFER - INFO - resolution: None
2025-05-09 12:39:48,991 - JUNIFER - INFO - n_rois: 100
2025-05-09 12:39:48,991 - JUNIFER - INFO - yeo_networks: 7
2025-05-09 12:39:48,991 - JUNIFER - INFO - Resolution set to None, using highest resolution.
2025-05-09 12:39:49,065 - JUNIFER - INFO - Loading parcellation: /github/home/junifer_data/v2/parcellations/Schaefer2018/Yeo2011/Schaefer2018_100Parcels_7Networks_order_FSLMNI152_1mm.nii.gz
2025-05-09 12:39:51,338 - JUNIFER - INFO - Downloading template MNI152Lin (T1w in resolution 1)
2025-05-09 12:39:51,842 - JUNIFER - INFO - antsApplyTransforms command to be executed:
antsApplyTransforms -d 3 -e 3 -n 'GenericLabel[NearestNeighbor]' -i /tmp/junifer/tmpxe1wwcwb/ants_parcellation_warper_Schaefer100x7_from_MNI152NLin6Asym_to_MNI152Lin_b235b044-2cd2-11f0-b407-763dc8eb2af0nxbbrjem/prewarp_parcellation.nii.gz -r /tmp/junifer/tmpxe1wwcwb/ants_parcellation_warper_Schaefer100x7_from_MNI152NLin6Asym_to_MNI152Lin_b235b044-2cd2-11f0-b407-763dc8eb2af0nxbbrjem/MNI152Lin_T1w.nii.gz -t /github/home/junifer_data/v2/.git/annex/objects/JQ/Pp/SHA256E-s145422752--5091350b36f951d455dabd429ebe86c493c75a0217dca311ae355f1d62e080b0.h5/SHA256E-s145422752--5091350b36f951d455dabd429ebe86c493c75a0217dca311ae355f1d62e080b0.h5 -o /tmp/junifer/tmpxe1wwcwb/ants_parcellation_warper_Schaefer100x7_from_MNI152NLin6Asym_to_MNI152Lin_b235b044-2cd2-11f0-b407-763dc8eb2af0nxbbrjem/parcellation_warped.nii.gz
2025-05-09 12:39:58,134 - JUNIFER - INFO - antsApplyTransforms command succeeded with the following output:
2025-05-09 12:40:01,105 - JUNIFER - INFO - No storage specified, returning dictionary
dict_keys(['BOLD'])
(96, 100)
Total running time of the script: (0 minutes 36.152 seconds)