8.2. Generic BIDS DataGrabber for datalad.

This example uses a generic BIDS DataGraber to get the data from a BIDS dataset store in a datalad remote sibling.

Authors: Federico Raimondo

License: BSD 3 clause

from junifer.datagrabber import PatternDataladDataGrabber
from junifer.utils import configure_logging

Set the logging level to info to see extra information

configure_logging(level="INFO")
2024-08-14 13:18:39,911 - JUNIFER - INFO - ===== Lib Versions =====
2024-08-14 13:18:39,911 - JUNIFER - INFO - numpy: 1.26.4
2024-08-14 13:18:39,911 - JUNIFER - INFO - scipy: 1.14.0
2024-08-14 13:18:39,911 - JUNIFER - INFO - pandas: 2.1.4
2024-08-14 13:18:39,911 - JUNIFER - INFO - nilearn: 0.10.4
2024-08-14 13:18:39,911 - JUNIFER - INFO - nibabel: 5.2.1
2024-08-14 13:18:39,911 - JUNIFER - INFO - junifer: 0.0.6.dev23
2024-08-14 13:18:39,911 - JUNIFER - INFO - ========================

The BIDS DataGrabber requires three parameters: the types of data we want, the specific pattern that matches each type, and the variables that will be replaced in the patterns.

types = ["T1w", "BOLD"]
patterns = {
    "T1w": {
        "pattern": "{subject}/anat/{subject}_T1w.nii.gz",
        "space": "native",
    },
    "BOLD": {
        "pattern": "{subject}/func/{subject}_task-rest_bold.nii.gz",
        "space": "MNI152NLin6Asym",
    },
}
replacements = ["subject"]

Additionally, a datalad-based DataGrabber requires the URI of the remote sibling and the location of the dataset within the remote sibling.

repo_uri = "https://gin.g-node.org/juaml/datalad-example-bids"
rootdir = "example_bids"

Now we can use the DataGrabber within a with context. One thing we can do with any DataGrabber is iterate over the elements. In this case, each element of the DataGrabber is one session.

2024-08-14 13:18:39,912 - JUNIFER - INFO - `datadir` is None, creating a temporary directory
2024-08-14 13:18:39,913 - JUNIFER - INFO - `datadir` set to /tmp/tmp2y_p2ph0/datadir
sub-05
sub-03
sub-08
sub-07
sub-02
sub-06
sub-09
sub-04
sub-01

Another feature of the DataGrabber is the ability to get a specific element by its name. In this case, we index sub-01 and we get the file paths for the two types of data we want (T1w and BOLD).

2024-08-14 13:18:43,578 - JUNIFER - INFO - `datadir` is None, creating a temporary directory
2024-08-14 13:18:43,579 - JUNIFER - INFO - `datadir` set to /tmp/tmpzapkubz5/datadir
2024-08-14 13:18:47,084 - JUNIFER - INFO - Getting element sub-01
2024-08-14 13:18:47,084 - JUNIFER - INFO - Resolving path from pattern for T1w
2024-08-14 13:18:47,084 - JUNIFER - INFO - Resolving path from pattern for BOLD
{'T1w': {'space': 'native', 'path': PosixPath('/tmp/tmpzapkubz5/datadir/example_bids/sub-01/anat/sub-01_T1w.nii.gz'), 'meta': {'datagrabber': {'class': 'PatternDataladDataGrabber', 'replacements': ['subject'], 'patterns': {'T1w': {'pattern': '{subject}/anat/{subject}_T1w.nii.gz', 'space': 'native'}, 'BOLD': {'pattern': '{subject}/func/{subject}_task-rest_bold.nii.gz', 'space': 'MNI152NLin6Asym'}}, 'partial_pattern_ok': False, 'confounds_format': None, 'types': ['T1w', 'BOLD'], 'uri': 'https://gin.g-node.org/juaml/datalad-example-bids', 'datalad_dirty': False, 'datalad_commit_id': 'b87897cbe51bf0ee5514becaa5c7dd76491db5ad', 'datalad_id': '8fddff30-6993-420a-9d1e-b5b028c59468'}, 'dependencies': set(), 'element': {'subject': 'sub-01'}}}, 'BOLD': {'space': 'MNI152NLin6Asym', 'path': PosixPath('/tmp/tmpzapkubz5/datadir/example_bids/sub-01/func/sub-01_task-rest_bold.nii.gz'), 'meta': {'datagrabber': {'class': 'PatternDataladDataGrabber', 'replacements': ['subject'], 'patterns': {'T1w': {'pattern': '{subject}/anat/{subject}_T1w.nii.gz', 'space': 'native'}, 'BOLD': {'pattern': '{subject}/func/{subject}_task-rest_bold.nii.gz', 'space': 'MNI152NLin6Asym'}}, 'partial_pattern_ok': False, 'confounds_format': None, 'types': ['T1w', 'BOLD'], 'uri': 'https://gin.g-node.org/juaml/datalad-example-bids', 'datalad_dirty': False, 'datalad_commit_id': 'b87897cbe51bf0ee5514becaa5c7dd76491db5ad', 'datalad_id': '8fddff30-6993-420a-9d1e-b5b028c59468'}, 'dependencies': set(), 'element': {'subject': 'sub-01'}}}}

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

Gallery generated by Sphinx-Gallery