How do I get a list of subjects?

In many of the exercises, you will need to loop over subjects. A question that many students often have is, “How do I get a list of subjects?”

There are multiple ways to do this:

1. Manually create a list.

sub_list = ['S01', 'S02', 'S04', 'S05', 'S06', 'S07', 'S08', 'S09', 'S10']

2. Use PyBids - layout.get_subjects(). Make sure you specifically look within derivatives folder. You may need to exclude missing data like ‘S03’.

[x for x in layout.get_subjects(scope='derivatives') if x != 'S03']

3. Use Glob. glob is a way to search for files using pattern matching

file_list = glob.glob(os.path.join('../data', 'localizer', 'derivatives', 'fmriprep', '*', 'func','*betas*nii.gz'))

for f in file_list:
    print(os.path.basename(f).split('_')[0].split('-')[1])
1 Like