Pybids: Extracting Metadata

Fairly straightforward question here, but as a novice I could use some clarification: When extracting metadata with Pybids, what does the number in brackets do?

f = layout.get(task=‘nback’, run=1, extensions=‘nii.gz’)[0].filename

In subsequent examples written by Chris to conduct first level analyses on fmriprep data, I get an error and this number changes to 5:
source_epi = layout.get(type=“bold”, task=“fingerfootlips”, session=“test”, extensions=“nii.gz”)[5]

Error: List index out of range.

What does the 5 specify?

layout.get() returns a list - in python you can use bracket notation to index; [0] will return the first element, [5] will return the 6th.

Thank you @mgxd that makes a lot of sense! Appreciate the quick response. I was able to troubleshoot my issue immediately.

Hello,

I have a similar issue/question. I am also following the 1st level examples Chris wrote (https://github.com/poldrack/fmri-analysis-vm/blob/master/analysis/postFMRIPREPmodelling/First%20and%20Second%20Level%20Modeling%20(FSL).ipynb) and have an issue with ‘BIDSLayout’. Which layer of fmriprep output do I apply ‘BIDSLayout’ to? Currently I have something like this:

datadir = 'B:\InProcess\3T\NABM\fMRI\fmriprep-1.3.2\out\fmriprep'
layout = BIDSLayout(datadir)
file = layout.get(suffix='bold', task='cue', session = '2a', extensions = 'nii.gz')[0].filename

But when I index ‘layout’ it shows up as: Subjects: 0 | Sessions: 0 | Runs: 0, even though there are subjects, sessions, runs in that folder. Consequently, ‘file’ comes up empty. Datadir contains folders for 2 subjects, HTML outputs, dataset_description.json, and a logs folder, so I think it’s the correct directory to work with pybids.

When I change ‘layout’ to a few levels up (B:\InProcess\3T\NABM\fMRI\fmriprep-1.3.2\out, or B:\InProcess\3T\NABM\fMRI\fmriprep-1.3.2), I get an error about ‘dataset_description.json’ missing.

Thanks.

@atersakyan, solution that worked when I tested this is following:

  1. create somewhere your root BIDS directory, e.g. B:\InProcess\3T\NABM\fMRI\MyStudy
  2. in this root BIDS directory create minimal dataset_descripton.json file (following specification) and B:\InProcess\3T\NABM\fMRI\MyStudy\derivatives directory
  3. copy entire fmriprep directory to B:\InProcess\3T\NABM\fMRI\MyStudy\derivatives\
  4. run following code
datadir = 'B:\InProcess\3T\NABM\fMRI\MyStudy'
layout = BIDSLayout(datadir, derivatives=True)
files = layout.get()

I believe that main argument of BIDSLayout object is path to root BIDS directory and not derived one. Specification says:

Args:
    root (str): The root directory of the BIDS dataset.

dataset_description.json for derived BIDS structure has slightly different mandatory fields than for unprocessed, original data. I am not sure if there is another solution, that allows to treat fmriprep directory as valid root directory of the BIDS dataset.

1 Like

Thank you for the help. I organized the data in the way you suggested and it seems things are working well.

I know this is a bit of an old thread, but just want to add another option.
You can leave the structure as is and just add “validate=False” to the BIDSlayout.
So, instead of what you originally wrote, just use:

datadir = ‘B:\InProcess\3T\NABM\fMRI\fmriprep-1.3.2\out\fmriprep’
layout = BIDSLayout(datadir, validate=False)
file = layout.get(suffix=‘bold’, task=‘cue’, session = ‘2a’, extensions = ‘nii.gz’)[0].filename