Add event labels to events loaded from raw files


ds (Dataset) – A Dataset containing events (with variables as returned by


ds – Should return the modified events dataset.

Return type


See also


Change event order or timing


Override this method in subclasses to add event labels more flexibly than through the variables attribute. This method is applied after adding other variables.

The subject and session the events are from can be determined with['subject'] and['session'].

Calling the original (super-class) method is not necessary.


Add a label whenever trigger 2 follows trigger 1:

class Experiment(MneExperiment):

    def label_events(self, ds):
        # assign 'no' to all events
        ds[:, 'new'] = 'no'
        # assign 'yes' to events where trigger 2 follows trigger 1
        for i in range(1, ds.n_cases):
            if ds[i, 'trigger'] == 2 and ds[i-1, 'trigger'] == 1:
                ds[i, 'new'] = 'yes'
        return ds

Add events based on separate files. This assumes that the events in the recording only indicate trial onsets, and separate files contain events listed relative to these trial onsets:

class Experiment(MneExperiment):

    def label_events(self, ds):
        samplingrate =['sfreq']
        new_events = []
        # loop through trials
        for i_start, trigger in'i_start', 'trigger'):
            # load the event file, assuming that the trigger in the
            # data was used to indicate the trial ID
            trial_events = load.tsv(f'/files/trial_{trigger}.txt')
            # assuming trial_events has a column called 'time' (in
            # seconds), we infer the event's sample in the raw file
            trial_i_start = i_start + trial_events['time'] * samplingrate
            trial_events['i_start'] = Var(trial_i_start.astype(int))
            # events also need a trigger column
            trial_events[:, 'trigger'] = trigger
            # collect all trials
        # combine the trials to a single dataset
        return combine(new_events)