Find events in a raw fileΒΆ

Find events from the stimulation/trigger channel in the raw data. The plot them to get an idea of the paradigm.

../_images/sphx_glr_plot_extract_events_from_raw_001.png

Script output:

At sample 27977 stim channel went from 0 to 2
At sample 28345 stim channel went from 0 to 3
At sample 28771 stim channel went from 0 to 1
At sample 29219 stim channel went from 0 to 4
At sample 29652 stim channel went from 0 to 2
# Author: Alexandre Gramfort <alexandre.gramfort@telecom-paristech.fr>
#
# License: BSD (3-clause)

import mne
from mne.datasets import sample
from mne.io import Raw

print(__doc__)

data_path = sample.data_path()
fname = data_path + '/MEG/sample/sample_audvis_raw.fif'

# Reading events
raw = Raw(fname)

events = mne.find_events(raw, stim_channel='STI 014')

# Writing events
mne.write_events('sample_audvis_raw-eve.fif', events)

for ind, before, after in events[:5]:
    print("At sample %d stim channel went from %d to %d"
          % (ind, before, after))

# Plot the events to get an idea of the paradigm
# Specify colors and an event_id dictionary for the legend.
event_id = {'aud_l': 1, 'aud_r': 2, 'vis_l': 3, 'vis_r': 4, 'smiley': 5,
            'button': 32}
color = {1: 'green', 2: 'yellow', 3: 'red', 4: 'c', 5: 'black', 32: 'blue'}

mne.viz.plot_events(events, raw.info['sfreq'], raw.first_samp, color=color,
                    event_id=event_id)

Total running time of the script: (0 minutes 1.079 seconds)

Download Python source code: plot_extract_events_from_raw.py