.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials/tutorial_examples/plot_006_bids_layout.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_tutorials_tutorial_examples_plot_006_bids_layout.py: ==================== How pyAFQ uses BIDS ==================== The pyAFQ API relies heavily on the `Brain Imaging Data Standard (BIDS) `_, a widely used standard for organizing and describing neuroimaging data. This means that the software assumes that its inputs are organized according to the BIDS specification and its outputs conform where possible with BIDS. .. note:: Derivatives of processing diffusion MRI are not currently fully described in the existing BIDS specification, but describing these is part of an ongoing effort. Wherever possible, we conform with the draft implementation of the BIDS DWI derivatives available `here `_ In this example, we will explore the use of BIDS in pyAFQ and see how BIDS allows us to extend and provide flexibility to the users of the software. .. GENERATED FROM PYTHON SOURCE LINES 24-33 .. code-block:: Python import os import os.path as op import AFQ.api.bundle_dict as abd from AFQ.api.group import GroupAFQ import AFQ.data.fetch as afd .. GENERATED FROM PYTHON SOURCE LINES 34-36 To interact with and query BIDS datasets, we use `pyBIDS `_, which we import here: .. GENERATED FROM PYTHON SOURCE LINES 36-40 .. code-block:: Python import bids from bids.layout import BIDSLayout .. GENERATED FROM PYTHON SOURCE LINES 41-47 We start with some example data. The data we will use here is generated from the `Stanford HARDI dataset `_. The call below fetches this dataset and organized it within the `~/AFQ_data` folder in the BIDS format. .. GENERATED FROM PYTHON SOURCE LINES 47-50 .. code-block:: Python afd.organize_stanford_data(clear_previous_afq="all") .. GENERATED FROM PYTHON SOURCE LINES 51-93 After doing that, we should have a folder that looks like this: | stanford_hardi | ├── dataset_description.json | └── derivatives | ├── freesurfer | │   ├── dataset_description.json | │   └── sub-01 | │   └── ses-01 | │   └── anat | │   ├── sub-01_ses-01_T1w.nii.gz | │   └── sub-01_ses-01_seg.nii.gz | └── vistasoft | ├── dataset_description.json | └── sub-01 | └── ses-01 | └── dwi | ├── sub-01_ses-01_dwi.bvals | ├── sub-01_ses-01_dwi.bvecs | └── sub-01_ses-01_dwi.nii.gz The top level directory (`stanford_hardi`) is our overall BIDS dataset folder. In many cases, this folder will include folders with raw data for each subject in the dataset. In this case, we do not include the raw data folders and only have the outputs of pipelines that were used to preprocess the data (e.g., correct the data for subject motion, eddy currents, and so forth). In general, only the preprocessed diffusion data is required for pyAFQ to run. See the :doc:`"Organizing your data" ` section of the documentation for more details. In this case, one folder contains derivative of the Freesurfer software and another folder contains the DWI data that has been preprocessed with the Vistasoft software. pyAFQ provides facilities to segment tractography results obtained using other software as well. For example, we often use `qsiprep `_ to preprocess our data and reconstruct tractographies with software such as `MRTRIX `_. Here, we will demonstrate how to use these reconstructions in the pyAFQ segmentation and tractometry pipeline We fetch this data and add it as a separate pipeline The following code will download a previously-created tractography and organize it by adding it to the BIDS dataset folder and renaming them to be BIDS-compliant (e.g., `sub-01_ses_01_dwi_tractography.trk`). .. GENERATED FROM PYTHON SOURCE LINES 93-118 .. code-block:: Python afd.fetch_stanford_hardi_tractography() bids_path = op.join(op.expanduser('~'), 'AFQ_data', 'stanford_hardi') tractography_path = op.join(bids_path, 'derivatives', 'my_tractography') sub_path = op.join(tractography_path, 'sub-01', 'ses-01', 'dwi') os.makedirs(sub_path, exist_ok=True) os.rename( op.join( op.expanduser('~'), 'AFQ_data', 'stanford_hardi_tractography', 'full_segmented_cleaned_tractography.trk'), op.join( sub_path, 'sub-01_ses-01-dwi_tractography.trk')) afd.to_bids_description( tractography_path, **{"Name": "my_tractography", "PipelineDescription": {"Name": "my_tractography"}, "GeneratedBy": [{"Name": "my_tractography"}]}) .. rst-class:: sphx-glr-script-out .. code-block:: none 0%| | 0/11337 MB [00:00] 0%| | 5/11337 MB [00:00] 0%| | 10/11337 MB [00:00] 0%| | 16/11337 MB [00:00] 0%| | 24/11337 MB [00:00] 0%| | 32/11337 MB [00:00] 0%| | 41/11337 MB [00:00] 0%| | 50/11337 MB [00:00] 1%| | 61/11337 MB [00:01] 1%| | 73/11337 MB [00:01] 1%| | 85/11337 MB [00:01] 1%| | 100/11337 MB [00:01] 1%| | 116/11337 MB [00:01] 1%| | 133/11337 MB [00:01] 1%|▏ | 152/11337 MB [00:01] 2%|▏ | 174/11337 MB [00:02] 2%|▏ | 197/11337 MB [00:02] 2%|▏ | 223/11337 MB [00:02] 2%|▏ | 250/11337 MB [00:02] 2%|▏ | 282/11337 MB [00:02] 3%|▎ | 316/11337 MB [00:02] 3%|▎ | 354/11337 MB [00:02] 3%|▎ | 396/11337 MB [00:02] 4%|▍ | 442/11337 MB [00:03] 4%|▍ | 493/11337 MB [00:03] 5%|▍ | 549/11337 MB [00:03] 5%|▌ | 611/11337 MB [00:03] 6%|▌ | 679/11337 MB [00:03] 7%|▋ | 754/11337 MB [00:03] 7%|▋ | 837/11337 MB [00:03] 8%|▊ | 928/11337 MB [00:04] 9%|▉ | 1029/11337 MB [00:04] 10%|█ | 1140/11337 MB [00:04] 11%|█ | 1263/11337 MB [00:04] 12%|█▏ | 1398/11337 MB [00:04] 14%|█▎ | 1547/11337 MB [00:04] 15%|█▌ | 1711/11337 MB [00:04] 17%|█▋ | 1892/11337 MB [00:04] 18%|█▊ | 2091/11337 MB [00:05] 20%|██ | 2311/11337 MB [00:05] 23%|██▎ | 2554/11337 MB [00:05] 25%|██▍ | 2821/11337 MB [00:05] 27%|██▋ | 3116/11337 MB [00:05] 30%|███ | 3441/11337 MB [00:05] 33%|███▎ | 3795/11337 MB [00:05] 36%|███▌ | 4089/11337 MB [00:05] 38%|███▊ | 4361/11337 MB [00:06] 42%|████▏ | 4811/11337 MB [00:06] 46%|████▌ | 5229/11337 MB [00:06] 50%|█████ | 5710/11337 MB [00:06] 55%|█████▍ | 6194/11337 MB [00:06] 58%|█████▊ | 6587/11337 MB [00:06] 61%|██████ | 6928/11337 MB [00:06] 65%|██████▍ | 7324/11337 MB [00:06] 68%|██████▊ | 7654/11337 MB [00:07] 71%|███████ | 8061/11337 MB [00:07] 75%|███████▍ | 8468/11337 MB [00:07] 79%|███████▉ | 8960/11337 MB [00:07] 83%|████████▎ | 9446/11337 MB [00:07] 88%|████████▊ | 9931/11337 MB [00:07] 91%|█████████▏| 10371/11337 MB [00:07] 95%|█████████▍| 10741/11337 MB [00:08] 98%|█████████▊| 11142/11337 MB [00:08] 100%|██████████| 11337/11337 MB [00:08] 0%| | 0/14 MB [00:00] 36%|███▌ | 5/14 MB [00:00] 100%|██████████| 14/14 MB [00:00] 0%| | 0/1037 MB [00:00] 0%| | 5/1037 MB [00:00] 2%|▏ | 21/1037 MB [00:00] 8%|▊ | 82/1037 MB [00:00] 32%|███▏ | 329/1037 MB [00:00] 76%|███████▌ | 785/1037 MB [00:00] 100%|██████████| 1037/1037 MB [00:00] .. GENERATED FROM PYTHON SOURCE LINES 119-148 After we do that, our dataset folder should look like this: | stanford_hardi | ├── dataset_description.json | └── derivatives | ├── freesurfer | │   ├── dataset_description.json | │   └── sub-01 | │   └── ses-01 | │   └── anat | │   ├── sub-01_ses-01_T1w.nii.gz | │   └── sub-01_ses-01_seg.nii.gz | ├── my_tractography | | ├── dataset_description.json | │   └── sub-01 | │   └── ses-01 | │   └── dwi | │   └── sub-01_ses-01-dwi_tractography.trk | └── vistasoft | ├── dataset_description.json | └── sub-01 | └── ses-01 | └── dwi | ├── sub-01_ses-01_dwi.bvals | ├── sub-01_ses-01_dwi.bvecs | └── sub-01_ses-01_dwi.nii.gz To explore the layout of these derivatives, we will initialize a :class:`BIDSLayout` class instance to help us see what is in this dataset .. GENERATED FROM PYTHON SOURCE LINES 148-151 .. code-block:: Python layout = bids.BIDSLayout(bids_path, derivatives=True) .. GENERATED FROM PYTHON SOURCE LINES 152-154 Because there is no raw data in this BIDS layout (only derivatives), pybids will report that there are no subjects and sessions: .. GENERATED FROM PYTHON SOURCE LINES 154-157 .. code-block:: Python print(layout) .. rst-class:: sphx-glr-script-out .. code-block:: none BIDS Layout: ...runner/AFQ_data/stanford_hardi | Subjects: 0 | Sessions: 0 | Runs: 0 .. GENERATED FROM PYTHON SOURCE LINES 158-160 But a query on the derivatives will reveal the different derivatives that are stored here: .. GENERATED FROM PYTHON SOURCE LINES 160-163 .. code-block:: Python print(layout.derivatives) .. rst-class:: sphx-glr-script-out .. code-block:: none {'derivatives/my_tractography': BIDS Layout: ...di/derivatives/my_tractography | Subjects: 1 | Sessions: 1 | Runs: 0, 'derivatives/recobundles': BIDS Layout: ..._hardi/derivatives/recobundles | Subjects: 1 | Sessions: 1 | Runs: 0, 'derivatives/freesurfer': BIDS Layout: ...d_hardi/derivatives/freesurfer | Subjects: 1 | Sessions: 1 | Runs: 0, 'derivatives/vistasoft': BIDS Layout: ...rd_hardi/derivatives/vistasoft | Subjects: 1 | Sessions: 1 | Runs: 0} .. GENERATED FROM PYTHON SOURCE LINES 164-167 We can use a :class:`bids.BIDSValidator` object to make sure that the files within our data set are BIDS-compliant. For example, we can extract the tractography derivatives part of our layout using: .. GENERATED FROM PYTHON SOURCE LINES 167-170 .. code-block:: Python my_tractography = layout.derivatives["my_tractography"] .. GENERATED FROM PYTHON SOURCE LINES 171-175 This variable is also a BIDS layout object. This object has a ``get`` method, which allows us to query and find specific items within the layout. For example, we can ask for files that have a suffix consistent with tractography results: .. GENERATED FROM PYTHON SOURCE LINES 175-178 .. code-block:: Python tractography_files = my_tractography.get(suffix='tractography') .. GENERATED FROM PYTHON SOURCE LINES 179-180 Or ask for files that have a ``.trk`` extension: .. GENERATED FROM PYTHON SOURCE LINES 180-183 .. code-block:: Python tractography_files = my_tractography.get(extension='.trk') .. GENERATED FROM PYTHON SOURCE LINES 184-185 In this case, both of these would produce the same result. .. GENERATED FROM PYTHON SOURCE LINES 185-189 .. code-block:: Python tractography_file = tractography_files[0] print(tractography_file) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 190-191 We can also get some more structured information about this file: .. GENERATED FROM PYTHON SOURCE LINES 191-195 .. code-block:: Python print(tractography_file.get_entities()) .. rst-class:: sphx-glr-script-out .. code-block:: none {'datatype': 'dwi', 'extension': '.trk', 'session': '01', 'subject': '01', 'suffix': 'tractography'} .. GENERATED FROM PYTHON SOURCE LINES 196-202 We can use a :class:`bids.BIDSValidator` class instance to validate that this file is compliant with the specification. Note that the validator requires that the filename be provided relative to the root of the BIDS dataset, so we have to split the string that contains the full path of the tractography to extract only the part that is relative to the root of the entire BIDS ``layout`` object: .. GENERATED FROM PYTHON SOURCE LINES 202-209 .. code-block:: Python tractography_full_path = tractography_file.path tractography_relative_path = tractography_full_path.split(layout.root)[-1] validator = bids.BIDSValidator() print(validator.is_bids(tractography_relative_path)) .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 210-217 Next, we specify the information we need to define the bundles that we are interested in segmenting. In this case, we are going to use a list of bundle names for the bundle info. These names refer to bundles for which we already have clear definitions of the information needed to segment them (e.g., waypoint ROIs and probability maps). For an example that includes custom definition of bundle info, see the `plot_callosal_tract_profile example `_. .. GENERATED FROM PYTHON SOURCE LINES 217-227 .. code-block:: Python bundle_info = abd.default18_bd()[ "Left Superior Longitudinal", "Right Superior Longitudinal", "Left Arcuate", "Right Arcuate", "Left Corticospinal", "Right Corticospinal", "Forceps Minor"] .. GENERATED FROM PYTHON SOURCE LINES 228-244 Now, we can define our GroupAFQ object, pointing to the derivatives of the `'my_tractography'` pipeline as inputs. This is done by setting the `import_tract` key-word argument. We pass the `bundle_info` defined above. We also point to the preprocessed data that is in a `'dmriprep'` pipeline. Note that the pipeline name is not necessarily the name of the folder it is in; the pipeline name is defined in each pipeline's `dataset_description.json`. These data were preprocessed with 'vistasoft', so this is the pipeline we'll point to If we were using `'qsiprep'`, this is where we would pass that string instead. If we did that, AFQ would look for a derivatives folder called `'stanford_hardi/derivatives/qsiprep'` and find the preprocessed DWI data within it. Finally, to speed things up a bit, we also sub-sample the provided tractography. This is done by defining the segmentation_params dictionary input. To sub-sample to 10,000 streamlines, we define `'nb_streamlines' = 10000`. .. GENERATED FROM PYTHON SOURCE LINES 244-255 .. code-block:: Python my_afq = GroupAFQ( bids_path, preproc_pipeline='vistasoft', bundle_info=bundle_info, import_tract={ "suffix": "tractography", "scope": "my_tractography" }, segmentation_params={'nb_streamlines': 10000}) .. GENERATED FROM PYTHON SOURCE LINES 256-259 Finally, to run the segmentation and extract tract profiles, we call The `export_all` method. This creates all of the derivative outputs of AFQ within the 'stanford_hardi/derivatives/afq' folder. .. GENERATED FROM PYTHON SOURCE LINES 259-263 .. code-block:: Python my_afq.export_all() .. rst-class:: sphx-glr-script-out .. code-block:: none Optimizing level 2 [max iter: 10000] Optimizing level 1 [max iter: 1000] Optimizing level 0 [max iter: 100] Optimizing level 2 [max iter: 10000] Optimizing level 1 [max iter: 1000] Optimizing level 0 [max iter: 100] Optimizing level 2 [max iter: 10000] Optimizing level 1 [max iter: 1000] Optimizing level 0 [max iter: 100] /opt/hostedtoolcache/Python/3.12.11/x64/lib/python3.12/site-packages/AFQ/tasks/segmentation.py:61: UserWarning: Pass ['to_space'] as keyword args. From version 2.0.0 passing these as positional arguments will result in an error. 0%| | 0/7 [00:00` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_006_bids_layout.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_006_bids_layout.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_