Note
Click here to download the full example code
7.1. 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")
2022-12-20 14:32:53,389 - JUNIFER - INFO - ===== Lib Versions =====
2022-12-20 14:32:53,389 - JUNIFER - INFO - numpy: 1.23.5
2022-12-20 14:32:53,389 - JUNIFER - INFO - scipy: 1.9.3
2022-12-20 14:32:53,389 - JUNIFER - INFO - pandas: 1.5.2
2022-12-20 14:32:53,389 - JUNIFER - INFO - nilearn: 0.9.2
2022-12-20 14:32:53,389 - JUNIFER - INFO - nibabel: 4.0.2
2022-12-20 14:32:53,389 - JUNIFER - INFO - junifer: 0.0.1
2022-12-20 14:32:53,389 - 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 int he patterns.
types = ["T1w", "BOLD"]
patterns = {
"T1w": "{subject}/anat/{subject}_T1w.nii.gz",
"BOLD": "{subject}/func/{subject}_task-rest_bold.nii.gz",
}
replacements = ["subject"]
Additionally, a datalad datagrabber requires the URI of the remote sibling and the location of the dataset within the remote sibling.
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.
with PatternDataladDataGrabber(
rootdir=rootdir,
types=types,
patterns=patterns,
uri=repo_uri,
replacements=replacements,
) as dg:
for elem in dg:
print(elem)
2022-12-20 14:32:53,390 - JUNIFER - WARNING - `datadir` is None, creating a temporary directory
2022-12-20 14:32:53,390 - JUNIFER - INFO - `datadir` set to /tmp/tmp16xpgdqr
sub-08
sub-05
sub-03
sub-01
sub-02
sub-06
sub-04
sub-09
sub-07
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).
with PatternDataladDataGrabber(
rootdir=rootdir,
types=types,
patterns=patterns,
uri=repo_uri,
replacements=replacements,
) as dg:
sub01 = dg["sub-01"]
print(sub01)
2022-12-20 14:32:56,548 - JUNIFER - WARNING - `datadir` is None, creating a temporary directory
2022-12-20 14:32:56,548 - JUNIFER - INFO - `datadir` set to /tmp/tmp5os9btdc
2022-12-20 14:32:59,617 - JUNIFER - INFO - Getting element sub-01
{'T1w': {'path': PosixPath('/tmp/tmp5os9btdc/example_bids/sub-01/anat/sub-01_T1w.nii.gz'), 'meta': {'datagrabber': {'class': 'PatternDataladDataGrabber', 'types': ['T1w', 'BOLD'], 'patterns': {'T1w': '{subject}/anat/{subject}_T1w.nii.gz', 'BOLD': '{subject}/func/{subject}_task-rest_bold.nii.gz'}, 'replacements': ['subject'], 'uri': 'https://gin.g-node.org/juaml/datalad-example-bids', 'datalad_dirty': False, 'datalad_commit_id': '522dfb203afcd2cd55799bf347f9b211919a7338', 'datalad_id': 'fec92475-d9c0-4409-92ba-f041b6a12c40'}, 'dependencies': set(), 'element': {'subject': 'sub-01'}}}, 'BOLD': {'path': PosixPath('/tmp/tmp5os9btdc/example_bids/sub-01/func/sub-01_task-rest_bold.nii.gz'), 'meta': {'datagrabber': {'class': 'PatternDataladDataGrabber', 'types': ['T1w', 'BOLD'], 'patterns': {'T1w': '{subject}/anat/{subject}_T1w.nii.gz', 'BOLD': '{subject}/func/{subject}_task-rest_bold.nii.gz'}, 'replacements': ['subject'], 'uri': 'https://gin.g-node.org/juaml/datalad-example-bids', 'datalad_dirty': False, 'datalad_commit_id': '522dfb203afcd2cd55799bf347f9b211919a7338', 'datalad_id': 'fec92475-d9c0-4409-92ba-f041b6a12c40'}, 'dependencies': set(), 'element': {'subject': 'sub-01'}}}}
Total running time of the script: ( 0 minutes 7.374 seconds)