Events file for `nilearn.glm.first_level.FirstLevelModel`

I have a BIDS-compliant events.tsv file that includes onset and duration columns, as well as many columns specifying aspects of the presented stimuli, as well as the subjects’ responses (e.g., cue, fix, feedback, gain, loss, but NOT trial_type). I am trying to use nilearn.glm.first_level.FirstLevelModel to ultimately obtain the residuals from a GLM using all of the columns from my events.tsv file, considering my main goal is to use the residuals from the task fMRI data as pseudo resting state data.

When I run these commands:

avoid_model = FirstLevelModel(param_avoid_df['RepetitionTime'],
avoid_glm =, events_avoid_df) 

I get the following warning for each of the columns other than onset and duration:

/Users/flutist4129/Library/Python/3.8/lib/python/site-packages/nilearn/glm/first_level/ UserWarning: Unexpected column `fix2` in events data will be ignored.
  warnings.warn(("Unexpected column `{}` in events "

and the following warning (I think nilearn requires a trial_type column, even though BIDS does not):

/Users/flutist4129/Library/Python/3.8/lib/python/site-packages/nilearn/glm/first_level/ UserWarning: 'trial_type' column not found in the given events data.
  warnings.warn("'trial_type' column not found "

Is there a way to get nilearn.glm.first_level.FirstLevelModel to recognize that I can have columns other than trial_type that define the task? And that I do not need a trial_type column?

There might be a more straightforward way to do this, but creating a categorical variable out of all of my indicator columns (seemed to have) worked:

indic = events_avoid_df.iloc[:, 3:].idxmax(axis=1)
events_indic_avoid_df = events_avoid_df.iloc[:, 0:2]
events_indic_avoid_df['trial_type'] = indic

avoid_model = FirstLevelModel(param_avoid_df['RepetitionTime'],
                              hrf_model='spm + derivative + dispersion',
avoid_glm =, events_indic_avoid_df)

Yes. In that case, the issue seems to be fully pandas-related. Let us know if you need more help.

What version of pandas was the most recent version of nilearn tested using? Want to make sure that isn’t the source of my problem.

Hi @butellyn, the latest release of nilearn (0.9.2) supports pandas>=1.0

Okay. Good to know. Would it be possible to configure nilearn.glm.first_level.FirstLevelModel to work with any valid BIDS events.tsv file? Right now, nilearn requires there to only be onset, duration, and trial_type, but the BIDS specification only requires onset and duration, and allows for an arbitrarily large number of indicator columns specifying the task.

Just a note, I confirmed that my data is BIDS-valid by running the BIDS-validator.

Ahoi hoi @butellyn,

thx for bringing this up. Just wanted to make sure I understand this correctly: the idea would be to support a structure within which events.tsv has at least three columns including onset, duration, trial_type but could also have an arbitrary number of columns where the first two are onset & duration and whatever columns follow entail further task specifications, e.g. trial_type, reaction_time, intensity, etc.? Also looping in @tsalo as could also go into the direction of BIDS stats models, eh?

Cheers, Peer

Hi Peer,

Yes, but also column names that aren’t explicitly stated in the manual. I am having trouble finding this in the online rendering, but when you download the pdf of BIDS v1.7 you can see on page 34 an example of a task-specific column in the events.tsv file called stim_presentation_side, and on page 170 the specification states the following: “An arbitrary number of additional columns can be added. Those allow describing other properties of events that could be later referred in modelling and hypothesis extensions of BIDS. Note that the trial_type and any additional columns in a TSV file SHOULD be documented in an accompanying JSON sidecar file”. Here’s a screenshot of one of my files as an example (*.tsv files apparently can’t be attached):