Nilearn Firstlevelmodel from BIDS unable to find bold images

Hi all,

I am learning how to use Nilearn for MRI analysis, and I am running into the following error when trying to run a first-level GLM using first_level_from_bids:

ValueError: 2 events.tsv files found for 0 bold files. Same number of event files as the number of runs is expected

I have been following the User Guide: First level analysis of a complete BIDS dataset from openneuro - Nilearn

Here is my command:

from nilearn.glm.first_level import first_level_from_bids
data_dir = '/Volumes/Pegasus/nilearn/rawdata'
task_label = 'card'
space_label = 'MNI152NLin2009cAsym'
derivatives_folder = '/Volumes/Pegasus/nilearn/derivatives/fmriprep'
models, models_run_imgs, models_events, models_confounds = \
first_level_from_bids(data_dir, task_label, space_label, smoothing_fwhm=6,
                      img_filters=[('desc','preproc'),('dir','AP'),('dir','PA')],
                      derivatives_folder=derivatives_folder)

The data I am trying to analyze consists of two runs of task data, one run in AP and one run in PA. The full paths to my fmriprep preprocessed data files are:

'/Volumes/Pegasus/nilearn/derivatives/fmriprep/sub-XXXX/ses-01/func/sub-XXXX_ses-01_task-card_dir-AP_space-MNI152NLin2009cAsym_desc-preproc_bold.nii'

'/Volumes/Pegasus/nilearn/derivatives/fmriprep/sub-XXXX/ses-01/func/sub-XXXX_ses-01_task-card_dir-PA_space-MNI152NLin2009cAsym_desc-preproc_bold.nii'

If anybody has any insight into what might be causing this error, I’d greatly appreciate it.

Thanks!
Hannah

Hi, sorry for that.
What if you remove the (‘dir’, ‘AP’), (‘dir’, ‘PA’) from img_filters ?
Best,
Bertrand

Removing (‘dir’,‘AP’),(‘dir’,‘PA’) from img_filters results in the following error:

ValueError: More than one nifti image found for the same ses 01, while no additional run specification present. Please verify that the desc_label and space_label labels corresponding to the BIDS spec were correctly specified.

The entire traceback:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/var/folders/5s/c4kkvs2j0glc78dbm7n6d75h0000gp/T/ipykernel_77929/2981758697.py in <module>
      5 derivatives_folder = '/Volumes/Pegasus/nilearn/derivatives/fmriprep'
      6 models, models_run_imgs, models_events, models_confounds = \
----> 7 first_level_from_bids(data_dir, task_label, space_label, smoothing_fwhm=6,
      8                       img_filters=[('desc','preproc')],
      9                       derivatives_folder=derivatives_folder)

~/opt/anaconda3/lib/python3.9/site-packages/nilearn/glm/first_level/first_level.py in first_level_from_bids(dataset_path, task_label, space_label, img_filters, t_r, slice_time_ref, hrf_model, drift_model, high_pass, drift_order, fir_delays, min_onset, mask_img, target_affine, target_shape, smoothing_fwhm, memory, memory_level, standardize, signal_scaling, noise_model, verbose, n_jobs, minimize_memory, derivatives_folder)
    998                 elif '_ses-' in img_dict['file_basename']:
    999                     if img_dict['ses'] in run_check_list:
-> 1000                         raise ValueError(
   1001                             'More than one nifti image '
   1002                             'found for the same ses %s, while '

ValueError: More than one nifti image found for the same ses 01, while no additional run specification present. Please verify that the desc_label and space_label labels corresponding to the BIDS spec were correctly specified.

Hm. Then, with only(‘dir’, ‘AP’), does it work ?

It’s very strange. If I set img_filters=[('dir','AP')], the error I receive is:

ValueError: 2 events.tsv files found for 1 bold files. Same number of event files as the number of runs is expected

But if I set img_filters=[('dir','AP'), ('dir','PA')], the error is:

ValueError: 2 events.tsv files found for 0 bold files. Same number of event files as the number of runs is expected

Not setting any image filters results in:

ValueError: More than one nifti image found for the same ses 01, while no additional run specification present. Please verify that the desc_label and space_label labels corresponding to the BIDS spec were correctly specified.

I see in the doc “Only one filter per field allowed.”, which may explain the odd behavior.
I’m afraid that you hit a deep bug. I’ll try to look more deeply…
Best,
Bertrand

Interesting. Thanks for looking into this.

We have other task data in folder (_task-rest & _task-ssrt) both in AP and PA, so perhaps the task_label parameter isn’t being detected properly?

I suspect that this kind of bug should be fixed with latest version of Nilearn but if not feel free to open an issue on the repository