This lesson is in the early stages of development (Alpha version)

Exploring Preprocessed fMRI Data from fMRIPREP

Overview

Teaching: 20 min
Exercises: 5 min
Questions
  • How does fMRIPrep store preprocessed neuroimaging data

  • How do I access preprocessed neuroimaging data

Objectives
  • Learn about fMRIPrep derivatives

  • Understand how preprocessed data is stored and how you can access key files for analysis

Exploring Preprocessed fMRI Data from fMRIPREP

BIDS applications such as fMRIPREP output data into a full data structure with strong similarity to BIDS organization principals. In fact, there is a specification for derivatives (outputs derived from) BIDS datasets; although this is a current work in progress, details can be found in: BIDS Derivatives.

In this tutorial, we’ll explore the outputs generated by fMRIPREP and get a handle of how the data is organized from this preprocessing pipeline

Luckily the semi-standardized output for fMRIPrep is organized in such a way that the data is easily accessible using pyBIDS! We’ll first show what the full data structure looks like, then we will provide you with methods on how you can pull specific types of outputs using pyBIDS.

The fMRIPrep Derivative Data Structure

First let’s take a quick look at the fMRIPrep data structure:

tree -L 1 '../data/ds000030/derivatives/fmriprep'

../data/ds000030/derivatives/fmriprep/
├── sub-10171
├── sub-10292
├── sub-10365
├── sub-10438
├── sub-10565
├── sub-10788
├── sub-11106
├── sub-11108
├── sub-11122
├── sub-11131
├── sub-50010
├── sub-50035
├── sub-50047
├── sub-50048
├── sub-50052
├── sub-50067
├── sub-50075
├── sub-50077
├── sub-50081
└── sub-50083

First note that inside the fMRIPrep folder, we have a folder per-subject. Let’s take a quick look at a single subject folder:

tree '../data/ds000030/derivatives/fmriprep/sub-10788'
../data/ds000030/derivatives/fmriprep/sub-10788/
├── anat
│   ├── sub-10788_T1w_brainmask.nii.gz
│   ├── sub-10788_T1w_preproc.nii.gz
│   ├── sub-10788_T1w_space-MNI152NLin2009cAsym_brainmask.nii.gz
│   └── sub-10788_T1w_space-MNI152NLin2009cAsym_preproc.nii.gz
└── func
    ├── sub-10788_task-rest_bold_confounds.tsv
    ├── sub-10788_task-rest_bold_space-fsaverage5.L.func.gii
    ├── sub-10788_task-rest_bold_space-fsaverage5.R.func.gii
    ├── sub-10788_task-rest_bold_space-MNI152NLin2009cAsym_brainmask.nii.gz
    ├── sub-10788_task-rest_bold_space-MNI152NLin2009cAsym_preproc.nii.gz
    ├── sub-10788_task-rest_bold_space-T1w_brainmask.nii.gz
    └── sub-10788_task-rest_bold_space-T1w_preproc.nii.gz

2 directories, 11 files

As you can see above, each subject folder is organized into an anat and func sub-folder.

Specifically:

This data is single-session, so a session folder is missing here - but with multiple sessions you will see anat and ses-[insert_session_here] folders where each session folder contain a func folder.

Hopefully you’re now convinced that the outputs of fMRIPREP roughly follows BIDS organization principles and is, in fact, quite simple. The filenames themselves give you a full description of what each file is (check the slides to get an idea of what each file means!

Now let’s see how we can pull data in using pyBIDS!

Let’s import pyBIDS through the bids module first:

import bids

We can make a bids.BIDSLayout object as usual by just feeding in the fmriprep directory! However, one caveat is that since the fmriprep outputs are not really BIDS but BIDS-like, we have to turn off bids validation:

layout = bids.BIDSLayout('../data/ds000030/derivatives/fmriprep/',validate=False)

Now that we have a layout object, we can pretend like we’re working with a BIDS dataset! Let’s try some common commands that you would’ve used with a BIDS dataset:

First, we’ll demonstrate that we can grab a list of pre-processed subjects much like in the way we would grab subjects from a raw BIDS dataset:

layout.get_subjects()
['10171',
 '10292',
 '10365',
 '10438',
 '10565',
 '10788',
 '11106',
 '11108',
 '11122',
 '11131',
 '50010',
 '50035',
 '50047',
 '50048',
 '50052',
 '50067',
 '50075',
 '50077',
 '50081',
 '50083']

We can also do the same for tasks

 layout.get_tasks()
 ['rest']

Now let’s try fetching specific files. Similar to how you would fetch BIDS data using pyBIDS, the exact same syntax will work for fMRIPREP derivatives. Let’s try pulling just the preprocessed anatomical data.

Recall that the anatomical folder is named as follows:

tree '../data/ds000030/derivatives/fmriprep/sub-10788/anat'^
../data/ds000030/derivatives/fmriprep/sub-10788/anat
├── sub-10788_T1w_brainmask.nii.gz
├── sub-10788_T1w_preproc.nii.gz
├── sub-10788_T1w_space-MNI152NLin2009cAsym_brainmask.nii.gz
└── sub-10788_T1w_space-MNI152NLin2009cAsym_preproc.nii.gz

0 directories, 4 files

The file that we’re interested in is of form sub-[subject]_T1w_preproc.nii.gz. Now we can construct a pyBIDS call to pull these types of files specifically:

preproc_T1 = layout.get(datatype='anat',suffix='preproc',extension='.nii.gz')
preproc_T1
[<BIDSImageFile filename='/mnt/tigrlab/projects/jjeyachandra/scwg2018_python_neuroimaging/data/ds000030/derivatives/fmriprep/sub-10171/anat/sub-10171_T1w_preproc.nii.gz'>,
 <BIDSImageFile filename='/mnt/tigrlab/projects/jjeyachandra/scwg2018_python_neuroimaging/data/ds000030/derivatives/fmriprep/sub-10171/anat/sub-10171_T1w_space-MNI152NLin2009cAsym_preproc.nii.gz'>,
 <BIDSImageFile filename='/mnt/tigrlab/projects/jjeyachandra/scwg2018_python_neuroimaging/data/ds000030/derivatives/fmriprep/sub-10292/anat/sub-10292_T1w_preproc.nii.gz'>,
 ...

Note that we also pulled in MNI152NLin2009cAsym_preproc.nii.gz data as well. This is data that has been transformed into MNI152NLin2009cAsym template space. We can pull this data out by further specifying our layout.get using the space argument:

mni_preproc_T1 = layout.get(datatype='anat',suffix='preproc',extension='.nii.gz',space='MNI152NLin2009cAsym')
mni_preproc_T1
[<BIDSImageFile filename='/mnt/tigrlab/projects/jjeyachandra/scwg2018_python_neuroimaging/data/ds000030/derivatives/fmriprep/sub-10171/anat/sub-10171_T1w_space-MNI152NLin2009cAsym_preproc.nii.gz'>,
 <BIDSImageFile filename='/mnt/tigrlab/projects/jjeyachandra/scwg2018_python_neuroimaging/data/ds000030/derivatives/fmriprep/sub-10292/anat/sub-10292_T1w_space-MNI152NLin2009cAsym_preproc.nii.gz'>,
 ...

What if we wanted to pull out the data in T1 “native space” (it really is a template space, since it is merged T1s)? Unfortunately for this specific version of fMRIPREP this isn’t directly possible using layout.get. Instead we’ll use a bit of python magic to pull the data that we want:

In newer versions of fMRIPREP space is included in the native T1w file filename as space-T1w - in this case you can pull the data by using layout.get(..., space='T1w')

native_preproc_T1 = [f for f in preproc_T1 if f not in mni_preproc_T1]
native_preproc_T1

Similarily fMRI data can be pulled by specifying datatype='func' and using the suffix argument as appropriate:

Exercise 1

  1. Get the list of all preprocessed functional data
  2. Get the list of functional data in MNI152NLin2009cAsym space
  3. Get the list of functional data in T1w space (native)

    ## Solution All the functional data

    func_data = layout.get(datatype='func', suffix='preproc')
    

    MNI152NLin2009cAsym Functional Data

    mni_func_data = layout.get(datatype='func', suffix='preproc', space='MNI152NLin2009cAsym')
    mni_func_data
    

    Native/T1w space functional data

    native_func_data = [b for b in func_data if b not in mni_func_data]
    native_func_data
    

    Now that we have a handle on how fMRIPREP preprocessed data is organized and how we can pull this data. Let’s start working with the actual data itself!

Key Points

  • fMRIPrep stores preprocessed data in a ‘BIDS-like’ fashion

  • You can pull files using pyBIDS much like how you can navigate raw BIDS data