Note
Go to the end to download the full example code.
8.5. Extracting root sum of squares from edge-wise timeseries.¶
This example uses a RSSETSMarker to compute root sum of squares
of the edge-wise timeseries using the Schaefer parcellation
(100 rois and 200 rois, 17 Yeo networks) for a 4D nifti BOLD file.
Authors: Leonard Sasse, Sami Hamdan, Nicolas Nieto, Synchon Mandal
License: BSD 3 clause
import tempfile
import junifer.testing.registry # noqa: F401
from junifer.api import collect, run
from junifer.storage import SQLiteFeatureStorage
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:193: 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-01-29 11:01:30,483 - JUNIFER - INFO - ===== Lib Versions =====
2025-01-29 11:01:30,483 - JUNIFER - INFO - numpy: 1.26.4
2025-01-29 11:01:30,483 - JUNIFER - INFO - scipy: 1.15.0
2025-01-29 11:01:30,483 - JUNIFER - INFO - pandas: 2.1.4
2025-01-29 11:01:30,483 - JUNIFER - INFO - nilearn: 0.10.4
2025-01-29 11:01:30,483 - JUNIFER - INFO - nibabel: 5.3.2
2025-01-29 11:01:30,483 - JUNIFER - INFO - junifer: 0.0.6.dev429
2025-01-29 11:01:30,483 - JUNIFER - INFO - ========================
Define the DataGrabber interface
datagrabber = {
"kind": "SPMAuditoryTestingDataGrabber",
}
Define the markers interface
markers = [
{
"name": "Schaefer100x17_RSSETS",
"kind": "RSSETSMarker",
"parcellation": "Schaefer100x17",
},
{
"name": "Schaefer200x17_RSSETS",
"kind": "RSSETSMarker",
"parcellation": "Schaefer200x17",
},
]
Create a temporary directory for junifer feature extraction:
At the end you can read the extracted data into a pandas.DataFrame.
with tempfile.TemporaryDirectory() as tmpdir:
# Define the storage interface
storage = {
"kind": "SQLiteFeatureStorage",
"uri": f"{tmpdir}/test.sqlite",
}
# Run the defined junifer feature extraction pipeline
run(
workdir=tmpdir,
datagrabber=datagrabber,
markers=markers,
storage=storage,
elements=["sub001"], # we calculate for one subject only
)
# Collect extracted features data
collect(storage=storage)
# Create storage object to read in extracted features
db = SQLiteFeatureStorage(uri=storage["uri"])
# List all the features
print(db.list_features())
# Read extracted features
df_rssets = db.read_df(feature_name="BOLD_Schaefer200x17_RSSETS_rss_ets")
2025-01-29 11:01:30,485 - JUNIFER - INFO - Registering RSSETSMarker in marker
2025-01-29 11:01:30,485 - JUNIFER - INFO - Validating Marker Collection
2025-01-29 11:01:30,485 - JUNIFER - INFO - DataGrabber output type: ['BOLD', 'T1w']
2025-01-29 11:01:30,485 - JUNIFER - INFO - Validating Data Reader:
2025-01-29 11:01:30,485 - JUNIFER - INFO - Data Reader output type: ['BOLD', 'T1w']
2025-01-29 11:01:30,485 - JUNIFER - INFO - Validating Marker: Schaefer100x17_RSSETS
2025-01-29 11:01:30,485 - JUNIFER - INFO - Marker output type: ['timeseries']
2025-01-29 11:01:30,485 - JUNIFER - INFO - Validating storage for Schaefer100x17_RSSETS
2025-01-29 11:01:30,485 - JUNIFER - INFO - Validating Marker: Schaefer200x17_RSSETS
2025-01-29 11:01:30,485 - JUNIFER - INFO - Marker output type: ['timeseries']
2025-01-29 11:01:30,485 - JUNIFER - INFO - Validating storage for Schaefer200x17_RSSETS
2025-01-29 11:01:30,485 - JUNIFER - INFO - Getting element sub001
2025-01-29 11:01:32,549 - JUNIFER - INFO - Fitting pipeline
2025-01-29 11:01:32,549 - JUNIFER - INFO - Reading BOLD from /tmp/tmpgdu0af3z/sub001_bold.nii.gz
2025-01-29 11:01:32,550 - JUNIFER - INFO - BOLD is of type NIFTI
2025-01-29 11:01:32,550 - JUNIFER - INFO - Reading T1w from /tmp/tmpgdu0af3z/sub001_T1w.nii.gz
2025-01-29 11:01:32,551 - JUNIFER - INFO - T1w is of type NIFTI
2025-01-29 11:01:32,551 - JUNIFER - INFO - Fitting marker Schaefer100x17_RSSETS
2025-01-29 11:01:32,551 - JUNIFER - INFO - Computing BOLD
2025-01-29 11:01:32,551 - JUNIFER - INFO - Parcellation will be warped from MNI152NLin6Asym to MNI152Lin using highest resolution
2025-01-29 11:01:32,552 - JUNIFER - INFO - Fetching one of Schaefer parcellations.
2025-01-29 11:01:32,552 - JUNIFER - INFO - Parcellation parameters:
2025-01-29 11:01:32,552 - JUNIFER - INFO - resolution: None
2025-01-29 11:01:32,552 - JUNIFER - INFO - n_rois: 100
2025-01-29 11:01:32,552 - JUNIFER - INFO - yeo_networks: 17
2025-01-29 11:01:32,552 - JUNIFER - INFO - Resolution set to None, using highest resolution.
2025-01-29 11:01:32,552 - JUNIFER - INFO - At least one of the parcellation files are missing. Fetching using nilearn.
Downloading data from https://raw.githubusercontent.com/ThomasYeoLab/CBIG/v0.14.3-Update_Yeo2011_Schaefer2018_labelname/stable_projects/brain_parcellation/Schaefer2018_LocalGlobal/Parcellations/MNI/Schaefer2018_100Parcels_17Networks_order.txt ...
...done. (0 seconds, 0 min)
Downloading data from https://raw.githubusercontent.com/ThomasYeoLab/CBIG/v0.14.3-Update_Yeo2011_Schaefer2018_labelname/stable_projects/brain_parcellation/Schaefer2018_LocalGlobal/Parcellations/MNI/Schaefer2018_100Parcels_17Networks_order_FSLMNI152_1mm.nii.gz ...
...done. (0 seconds, 0 min)
2025-01-29 11:01:33,002 - JUNIFER - INFO - Loading parcellation: /github/home/junifer/data/parcellations/schaefer_2018/Schaefer2018_100Parcels_17Networks_order_FSLMNI152_1mm.nii.gz
2025-01-29 11:01:33,271 - JUNIFER - INFO - Found existing xfm file for MNI152NLin6Asym to MNI152Lin at /github/home/junifer/data/xfms/.git/annex/objects/JQ/Pp/SHA256E-s145422752--5091350b36f951d455dabd429ebe86c493c75a0217dca311ae355f1d62e080b0.h5/SHA256E-s145422752--5091350b36f951d455dabd429ebe86c493c75a0217dca311ae355f1d62e080b0.h5
2025-01-29 11:01:34,362 - JUNIFER - INFO - Downloading template MNI152Lin (T1w in resolution 1
2025-01-29 11:01:34,864 - JUNIFER - INFO - antsApplyTransforms command to be executed:
antsApplyTransforms -d 3 -e 3 -n 'GenericLabel[NearestNeighbor]' -i /tmp/junifer/tmpv6a7pc_7/ants_parcellation_warper_Schaefer100x17_from_MNI152NLin6Asym_to_MNI152Lin_66927adc-de30-11ef-acef-0242ac120002fdif3fng/prewarp_parcellation.nii.gz -r /tmp/junifer/tmpv6a7pc_7/ants_parcellation_warper_Schaefer100x17_from_MNI152NLin6Asym_to_MNI152Lin_66927adc-de30-11ef-acef-0242ac120002fdif3fng/MNI152Lin_T1w.nii.gz -t /github/home/junifer/data/xfms/.git/annex/objects/JQ/Pp/SHA256E-s145422752--5091350b36f951d455dabd429ebe86c493c75a0217dca311ae355f1d62e080b0.h5/SHA256E-s145422752--5091350b36f951d455dabd429ebe86c493c75a0217dca311ae355f1d62e080b0.h5 -o /tmp/junifer/tmpv6a7pc_7/ants_parcellation_warper_Schaefer100x17_from_MNI152NLin6Asym_to_MNI152Lin_66927adc-de30-11ef-acef-0242ac120002fdif3fng/parcellation_warped.nii.gz
2025-01-29 11:01:41,107 - JUNIFER - INFO - antsApplyTransforms command succeeded with the following output:
2025-01-29 11:01:43,942 - JUNIFER - INFO - Storing in <SQLiteFeatureStorage @ /tmp/tmpi1nhiqhp/test.sqlite (multiple output)>
2025-01-29 11:01:43,963 - JUNIFER - INFO - Fitting marker Schaefer200x17_RSSETS
2025-01-29 11:01:43,964 - JUNIFER - INFO - Computing BOLD
2025-01-29 11:01:43,964 - JUNIFER - INFO - Parcellation will be warped from MNI152NLin6Asym to MNI152Lin using highest resolution
2025-01-29 11:01:43,964 - JUNIFER - INFO - Fetching one of Schaefer parcellations.
2025-01-29 11:01:43,964 - JUNIFER - INFO - Parcellation parameters:
2025-01-29 11:01:43,964 - JUNIFER - INFO - resolution: None
2025-01-29 11:01:43,964 - JUNIFER - INFO - n_rois: 200
2025-01-29 11:01:43,964 - JUNIFER - INFO - yeo_networks: 17
2025-01-29 11:01:43,964 - JUNIFER - INFO - Resolution set to None, using highest resolution.
2025-01-29 11:01:43,964 - JUNIFER - INFO - At least one of the parcellation files are missing. Fetching using nilearn.
Downloading data from https://raw.githubusercontent.com/ThomasYeoLab/CBIG/v0.14.3-Update_Yeo2011_Schaefer2018_labelname/stable_projects/brain_parcellation/Schaefer2018_LocalGlobal/Parcellations/MNI/Schaefer2018_200Parcels_17Networks_order.txt ...
...done. (0 seconds, 0 min)
Downloading data from https://raw.githubusercontent.com/ThomasYeoLab/CBIG/v0.14.3-Update_Yeo2011_Schaefer2018_labelname/stable_projects/brain_parcellation/Schaefer2018_LocalGlobal/Parcellations/MNI/Schaefer2018_200Parcels_17Networks_order_FSLMNI152_1mm.nii.gz ...
...done. (0 seconds, 0 min)
2025-01-29 11:01:44,347 - JUNIFER - INFO - Loading parcellation: /github/home/junifer/data/parcellations/schaefer_2018/Schaefer2018_200Parcels_17Networks_order_FSLMNI152_1mm.nii.gz
2025-01-29 11:01:44,618 - JUNIFER - INFO - Found existing xfm file for MNI152NLin6Asym to MNI152Lin at /github/home/junifer/data/xfms/.git/annex/objects/JQ/Pp/SHA256E-s145422752--5091350b36f951d455dabd429ebe86c493c75a0217dca311ae355f1d62e080b0.h5/SHA256E-s145422752--5091350b36f951d455dabd429ebe86c493c75a0217dca311ae355f1d62e080b0.h5
2025-01-29 11:01:45,689 - JUNIFER - INFO - Downloading template MNI152Lin (T1w in resolution 1
2025-01-29 11:01:46,194 - JUNIFER - INFO - antsApplyTransforms command to be executed:
antsApplyTransforms -d 3 -e 3 -n 'GenericLabel[NearestNeighbor]' -i /tmp/junifer/tmpv6a7pc_7/ants_parcellation_warper_Schaefer200x17_from_MNI152NLin6Asym_to_MNI152Lin_6d56050a-de30-11ef-acef-0242ac120002soxvgwg5/prewarp_parcellation.nii.gz -r /tmp/junifer/tmpv6a7pc_7/ants_parcellation_warper_Schaefer200x17_from_MNI152NLin6Asym_to_MNI152Lin_6d56050a-de30-11ef-acef-0242ac120002soxvgwg5/MNI152Lin_T1w.nii.gz -t /github/home/junifer/data/xfms/.git/annex/objects/JQ/Pp/SHA256E-s145422752--5091350b36f951d455dabd429ebe86c493c75a0217dca311ae355f1d62e080b0.h5/SHA256E-s145422752--5091350b36f951d455dabd429ebe86c493c75a0217dca311ae355f1d62e080b0.h5 -o /tmp/junifer/tmpv6a7pc_7/ants_parcellation_warper_Schaefer200x17_from_MNI152NLin6Asym_to_MNI152Lin_6d56050a-de30-11ef-acef-0242ac120002soxvgwg5/parcellation_warped.nii.gz
2025-01-29 11:01:57,317 - JUNIFER - INFO - antsApplyTransforms command succeeded with the following output:
2025-01-29 11:01:59,660 - JUNIFER - INFO - Storing in <SQLiteFeatureStorage @ /tmp/tmpi1nhiqhp/test.sqlite (multiple output)>
2025-01-29 11:01:59,683 - JUNIFER - INFO - Marker collection fitting done
2025-01-29 11:01:59,693 - JUNIFER - INFO - Collecting data using SQLiteFeatureStorage
2025-01-29 11:01:59,693 - JUNIFER - INFO - Collecting data from /tmp/tmpi1nhiqhp/*test.sqlite
file: 0it [00:00, ?it/s]
feature: 0%| | 0/2 [00:00<?, ?it/s]
feature: 100%|██████████| 2/2 [00:00<00:00, 69.79it/s]
file: 1it [00:00, 23.87it/s]
2025-01-29 11:01:59,735 - JUNIFER - INFO - Collect done
{'de47d0513cf22df792d264ecc363085a': {'datagrabber': {'class': 'SPMAuditoryTestingDataGrabber', 'types': ['BOLD', 'T1w']}, 'dependencies': {'nilearn': '0.10.4'}, 'datareader': {'class': 'DefaultDataReader'}, 'type': 'BOLD', 'marker': {'agg_method': 'mean', 'agg_method_params': None, 'class': 'RSSETSMarker', 'masks': None, 'name': 'Schaefer100x17_RSSETS_rss_ets', 'parcellation': 'Schaefer100x17'}, '_element_keys': ['subject'], 'name': 'BOLD_Schaefer100x17_RSSETS_rss_ets'}, '0ff7a9e8f65fc25885bd0cd4936f87a4': {'datagrabber': {'class': 'SPMAuditoryTestingDataGrabber', 'types': ['BOLD', 'T1w']}, 'dependencies': {'nilearn': '0.10.4'}, 'datareader': {'class': 'DefaultDataReader'}, 'type': 'BOLD', 'marker': {'agg_method': 'mean', 'agg_method_params': None, 'class': 'RSSETSMarker', 'masks': None, 'name': 'Schaefer200x17_RSSETS_rss_ets', 'parcellation': 'Schaefer200x17'}, '_element_keys': ['subject'], 'name': 'BOLD_Schaefer200x17_RSSETS_rss_ets'}}
Now we take a look at the dataframe
Total running time of the script: (0 minutes 29.274 seconds)