Visualize Epochs data

# sphinx_gallery_thumbnail_number = 7

import os.path as op

import mne

data_path = op.join(mne.datasets.sample.data_path(), 'MEG', 'sample')
raw = mne.io.read_raw_fif(
    op.join(data_path, 'sample_audvis_raw.fif'), preload=True)
raw.load_data().filter(None, 9, fir_design='firwin')
raw.set_eeg_reference('average', projection=True)  # set EEG average reference
event_id = {'auditory/left': 1, 'auditory/right': 2, 'visual/left': 3,
            'visual/right': 4, 'smiley': 5, 'button': 32}
events = mne.read_events(op.join(data_path, 'sample_audvis_raw-eve.fif'))
epochs = mne.Epochs(raw, events, event_id=event_id, tmin=-0.2, tmax=.5)

Out:

Opening raw data file /home/circleci/mne_data/MNE-sample-data/MEG/sample/sample_audvis_raw.fif...
    Read a total of 3 projection items:
        PCA-v1 (1 x 102)  idle
        PCA-v2 (1 x 102)  idle
        PCA-v3 (1 x 102)  idle
    Range : 25800 ... 192599 =     42.956 ...   320.670 secs
Ready.
Current compensation grade : 0
Reading 0 ... 166799  =      0.000 ...   277.714 secs...
Filtering raw data in 1 contiguous segment
Setting up low-pass filter at 9 Hz

FIR filter parameters
---------------------
Designing a one-pass, zero-phase, non-causal lowpass filter:
- Windowed time-domain design (firwin) method
- Hamming window with 0.0194 passband ripple and 53 dB stopband attenuation
- Upper passband edge: 9.00 Hz
- Upper transition bandwidth: 2.25 Hz (-6 dB cutoff frequency: 10.12 Hz)
- Filter length: 881 samples (1.467 sec)

Adding average EEG reference projection.
1 projection items deactivated
Average reference projection was added, but has not been applied yet. Use the apply_proj method to apply it.
320 matching events found
Applying baseline correction (mode: mean)
Not setting metadata
Created an SSP operator (subspace dimension = 4)
4 projection items activated

This tutorial focuses on visualization of epoched data. All of the functions introduced here are basically high level matplotlib functions with built in intelligence to work with epoched data. All the methods return a handle to matplotlib figure instance.

Events used for constructing the epochs here are the triggers for subject being presented a smiley face at the center of the visual field. More of the paradigm at Trigger codes for the sample data set..

All plotting functions start with plot. Let’s start with the most obvious. mne.Epochs.plot() offers an interactive browser that allows rejection by hand when called in combination with a keyword block=True. This blocks the execution of the script until the browser window is closed.

epochs.plot(block=True)
../../_images/sphx_glr_plot_visualize_epochs_001.png

Out:

Loading data for 320 events and 421 original time points ...
0 bad epochs dropped
Loading data for 78 events and 421 original time points ...
Loading data for 20 events and 421 original time points ...

The numbers at the top refer to the event id of the epoch. The number at the bottom is the running numbering for the epochs.

Since we did no artifact correction or rejection, there are epochs contaminated with blinks and saccades. For instance, epoch number 1 seems to be contaminated by a blink (scroll to the bottom to view the EOG channel). This epoch can be marked for rejection by clicking on top of the browser window. The epoch should turn red when you click it. This means that it will be dropped as the browser window is closed.

It is possible to plot event markers on epoched data by passing events keyword to the epochs plotter. The events are plotted as vertical lines and they follow the same coloring scheme as mne.viz.plot_events(). The events plotter gives you all the events with a rough idea of the timing. Since the colors are the same, the event plotter can also function as a legend for the epochs plotter events. It is also possible to pass your own colors via event_colors keyword. Here we can plot the reaction times between seeing the smiley face and the button press (event 32).

When events are passed, the epoch numbering at the bottom is switched off by default to avoid overlaps. You can turn it back on via settings dialog by pressing o key. You should check out help at the lower left corner of the window for more information about the interactive features.

events = mne.pick_events(events, include=[5, 32])
mne.viz.plot_events(events)
epochs['smiley'].plot(events=events)
  • ../../_images/sphx_glr_plot_visualize_epochs_002.png
  • ../../_images/sphx_glr_plot_visualize_epochs_003.png

Out:

Loading data for 15 events and 421 original time points ...
Loading data for 15 events and 421 original time points ...

To plot individual channels as an image, where you see all the epochs at one glance, you can use function mne.Epochs.plot_image(). It shows the amplitude of the signal over all the epochs plus an average (evoked response) of the activation. We explicitly set interactive colorbar on (it is also on by default for plotting functions with a colorbar except the topo plots). In interactive mode you can scale and change the colormap with mouse scroll and up/down arrow keys. You can also drag the colorbar with left/right mouse button. Hitting space bar resets the scale.

epochs.plot_image(278, cmap='interactive', sigma=1., vmin=-250, vmax=250)
../../_images/sphx_glr_plot_visualize_epochs_004.png

Out:

Loading data for 320 events and 421 original time points ...

We can also give an overview of all channels by calculating the global field power (or other other aggregation methods). However, combining multiple channel types (e.g., MEG and EEG) in this way is not sensible. Instead, we can use the group_by parameter. Setting group_by to ‘type’ combines channels by type. group_by can also be used to group channels into arbitrary groups, e.g. regions of interests, by providing a dictionary containing group name -> channel indices mappings.

epochs.plot_image(combine='gfp', group_by='type', sigma=2., cmap="YlGnBu_r")
  • ../../_images/sphx_glr_plot_visualize_epochs_005.png
  • ../../_images/sphx_glr_plot_visualize_epochs_006.png
  • ../../_images/sphx_glr_plot_visualize_epochs_007.png

Out:

Loading data for 320 events and 421 original time points ...
320 matching events found
No baseline correction applied
Not setting metadata
0 projection items activated
0 bad epochs dropped
320 matching events found
No baseline correction applied
Not setting metadata
0 projection items activated
0 bad epochs dropped
320 matching events found
No baseline correction applied
Not setting metadata
0 projection items activated
0 bad epochs dropped

You also have functions for plotting channelwise information arranged into a shape of the channel array. The image plotting uses automatic scaling by default, but noisy channels and different channel types can cause the scaling to be a bit off. Here we define the limits by hand.

epochs.plot_topo_image(vmin=-250, vmax=250, title='ERF images', sigma=2.,
                       fig_facecolor='w', font_color='k')
../../_images/sphx_glr_plot_visualize_epochs_008.png

Out:

Loading data for 320 events and 421 original time points ...

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

Estimated memory usage: 1312 MB

Gallery generated by Sphinx-Gallery