Compute and visualize ERDS maps

This example calculates and displays ERDS maps of event-related EEG data. ERDS (sometimes also written as ERD/ERS) is short for event-related desynchronization (ERD) and event-related synchronization (ERS) 1. Conceptually, ERD corresponds to a decrease in power in a specific frequency band relative to a baseline. Similarly, ERS corresponds to an increase in power. An ERDS map is a time/frequency representation of ERD/ERS over a range of frequencies 2. ERDS maps are also known as ERSP (event-related spectral perturbation) 3.

In this example, we use an EEG BCI data set containing two different motor imagery tasks (imagined hand and feet movement). Our goal is to generate ERDS maps for each of the two tasks.

First, we load the data and create epochs of 5s length. The data set contains multiple channels, but we will only consider C3, Cz, and C4. We compute maps containing frequencies ranging from 2 to 35Hz. We map ERD to red color and ERS to blue color, which is customary in many ERDS publications. Finally, we perform cluster-based permutation tests to estimate significant ERDS values (corrected for multiple comparisons within channels).

# Authors: Clemens Brunner <clemens.brunner@gmail.com>
#          Felix Klotzsche <klotzsche@cbs.mpg.de>
#
# License: BSD-3-Clause

As usual, we import everything we need.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import TwoSlopeNorm
import pandas as pd
import seaborn as sns
import mne
from mne.datasets import eegbci
from mne.io import concatenate_raws, read_raw_edf
from mne.time_frequency import tfr_multitaper
from mne.stats import permutation_cluster_1samp_test as pcluster_test

First, we load and preprocess the data. We use runs 6, 10, and 14 from subject 1 (these runs contains hand and feet motor imagery).

fnames = eegbci.load_data(subject=1, runs=(6, 10, 14))
raw = concatenate_raws([read_raw_edf(f, preload=True) for f in fnames])

raw.rename_channels(lambda x: x.strip('.'))  # remove dots from channel names

events, _ = mne.events_from_annotations(raw, event_id=dict(T1=2, T2=3))

Out:

Extracting EDF parameters from /home/circleci/mne_data/MNE-eegbci-data/files/eegmmidb/1.0.0/S001/S001R06.edf...
EDF file detected
Setting channel info structure...
Creating raw.info structure...
Reading 0 ... 19999  =      0.000 ...   124.994 secs...
Extracting EDF parameters from /home/circleci/mne_data/MNE-eegbci-data/files/eegmmidb/1.0.0/S001/S001R10.edf...
EDF file detected
Setting channel info structure...
Creating raw.info structure...
Reading 0 ... 19999  =      0.000 ...   124.994 secs...
Extracting EDF parameters from /home/circleci/mne_data/MNE-eegbci-data/files/eegmmidb/1.0.0/S001/S001R14.edf...
EDF file detected
Setting channel info structure...
Creating raw.info structure...
Reading 0 ... 19999  =      0.000 ...   124.994 secs...
Used Annotations descriptions: ['T1', 'T2']

Now we can create 5s epochs around events of interest.

tmin, tmax = -1, 4
event_ids = dict(hands=2, feet=3)  # map event IDs to tasks

epochs = mne.Epochs(raw, events, event_ids, tmin - 0.5, tmax + 0.5,
                    picks=('C3', 'Cz', 'C4'), baseline=None, preload=True)

Out:

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

Here we set suitable values for computing ERDS maps.

freqs = np.arange(2, 36)  # frequencies from 2-35Hz
vmin, vmax = -1, 1.5  # set min and max ERDS values in plot
baseline = [-1, 0]  # baseline interval (in s)
cnorm = TwoSlopeNorm(vmin=-1, vcenter=0, vmax=1.5)  # min, center, and max ERDS
kwargs = dict(n_permutations=100, step_down_p=0.05, seed=1,
              buffer_size=None, out_type='mask')  # for cluster test

Finally, we perform time/frequency decomposition over all epochs.

tfr = tfr_multitaper(epochs, freqs=freqs, n_cycles=freqs, use_fft=True,
                     return_itc=False, average=False, decim=2)
tfr.crop(tmin, tmax).apply_baseline(baseline, mode="percent")

for event in event_ids:
    # select desired epochs for visualization
    tfr_ev = tfr[event]
    fig, axes = plt.subplots(1, 4, figsize=(12, 4),
                             gridspec_kw={"width_ratios": [10, 10, 10, 1]})
    for ch, ax in enumerate(axes[:-1]):  # for each channel
        # positive clusters
        _, c1, p1, _ = pcluster_test(tfr_ev.data[:, ch], tail=1, **kwargs)
        # negative clusters
        _, c2, p2, _ = pcluster_test(tfr_ev.data[:, ch], tail=-1, **kwargs)

        # note that we keep clusters with p <= 0.05 from the combined clusters
        # of two independent tests; in this example, we do not correct for
        # these two comparisons
        c = np.stack(c1 + c2, axis=2)  # combined clusters
        p = np.concatenate((p1, p2))  # combined p-values
        mask = c[..., p <= 0.05].any(axis=-1)

        # plot TFR (ERDS map with masking)
        tfr_ev.average().plot([ch], cmap="RdBu", cnorm=cnorm, axes=ax,
                              colorbar=False, show=False, mask=mask,
                              mask_style="mask")

        ax.set_title(epochs.ch_names[ch], fontsize=10)
        ax.axvline(0, linewidth=1, color="black", linestyle=":")  # event
        if ch != 0:
            ax.set_ylabel("")
            ax.set_yticklabels("")
    fig.colorbar(axes[0].images[-1], cax=axes[-1])
    fig.suptitle(f"ERDS ({event})")
    plt.show()
  • ERDS (hands), C3, Cz, C4
  • ERDS (feet), C3, Cz, C4

Out:

Not setting metadata
Applying baseline correction (mode: percent)
Using a threshold of 1.724718
stat_fun(H1): min=-8.523637 max=3.197747
Running initial clustering
Found 78 clusters
Permuting 99 times...

  0%|          |  : 0/99 [00:00<?,       ?it/s]
 10%|#         |  : 10/99 [00:00<00:00,  290.62it/s]
 22%|##2       |  : 22/99 [00:00<00:00,  322.67it/s]
 34%|###4      |  : 34/99 [00:00<00:00,  333.60it/s]
 47%|####7     |  : 47/99 [00:00<00:00,  347.11it/s]
 62%|######1   |  : 61/99 [00:00<00:00,  361.74it/s]
 74%|#######3  |  : 73/99 [00:00<00:00,  360.55it/s]
 88%|########7 |  : 87/99 [00:00<00:00,  369.52it/s]
100%|##########|  : 99/99 [00:00<00:00,  368.39it/s]
100%|##########|  : 99/99 [00:00<00:00,  365.25it/s]
Computing cluster p-values
Step-down-in-jumps iteration #1 found 0 clusters to exclude from subsequent iterations
Done.
Using a threshold of -1.724718
stat_fun(H1): min=-8.523637 max=3.197747
Running initial clustering
Found 65 clusters
Permuting 99 times...

  0%|          |  : 0/99 [00:00<?,       ?it/s]
 11%|#1        |  : 11/99 [00:00<00:00,  324.87it/s]
 23%|##3       |  : 23/99 [00:00<00:00,  340.08it/s]
 36%|###6      |  : 36/99 [00:00<00:00,  355.66it/s]
 48%|####8     |  : 48/99 [00:00<00:00,  355.50it/s]
 61%|######    |  : 60/99 [00:00<00:00,  355.48it/s]
 69%|######8   |  : 68/99 [00:00<00:00,  333.08it/s]
 78%|#######7  |  : 77/99 [00:00<00:00,  321.95it/s]
 89%|########8 |  : 88/99 [00:00<00:00,  322.46it/s]
 99%|#########8|  : 98/99 [00:00<00:00,  318.87it/s]
100%|##########|  : 99/99 [00:00<00:00,  321.63it/s]
Computing cluster p-values
Step-down-in-jumps iteration #1 found 1 cluster to exclude from subsequent iterations
Permuting 99 times...

  0%|          |  : 0/99 [00:00<?,       ?it/s]
  8%|8         |  : 8/99 [00:00<00:00,  235.13it/s]
 17%|#7        |  : 17/99 [00:00<00:00,  250.98it/s]
 26%|##6       |  : 26/99 [00:00<00:00,  256.22it/s]
 34%|###4      |  : 34/99 [00:00<00:00,  250.91it/s]
 42%|####2     |  : 42/99 [00:00<00:00,  247.84it/s]
 51%|#####     |  : 50/99 [00:00<00:00,  245.68it/s]
 61%|######    |  : 60/99 [00:00<00:00,  253.97it/s]
 72%|#######1  |  : 71/99 [00:00<00:00,  264.59it/s]
 81%|########  |  : 80/99 [00:00<00:00,  264.60it/s]
 92%|#########1|  : 91/99 [00:00<00:00,  272.17it/s]
100%|##########|  : 99/99 [00:00<00:00,  278.12it/s]
100%|##########|  : 99/99 [00:00<00:00,  273.37it/s]
Computing cluster p-values
Step-down-in-jumps iteration #2 found 0 additional clusters to exclude from subsequent iterations
Done.
No baseline correction applied
Using a threshold of 1.724718
stat_fun(H1): min=-4.573067 max=3.687727
Running initial clustering
Found 85 clusters
Permuting 99 times...

  0%|          |  : 0/99 [00:00<?,       ?it/s]
  8%|8         |  : 8/99 [00:00<00:00,  235.10it/s]
 11%|#1        |  : 11/99 [00:00<00:00,  160.23it/s]
 20%|##        |  : 20/99 [00:00<00:00,  197.33it/s]
 31%|###1      |  : 31/99 [00:00<00:00,  231.66it/s]
 45%|####5     |  : 45/99 [00:00<00:00,  271.86it/s]
 59%|#####8    |  : 58/99 [00:00<00:00,  293.15it/s]
 71%|#######   |  : 70/99 [00:00<00:00,  303.40it/s]
 80%|#######9  |  : 79/99 [00:00<00:00,  297.89it/s]
 89%|########8 |  : 88/99 [00:00<00:00,  293.59it/s]
 99%|#########8|  : 98/99 [00:00<00:00,  293.90it/s]
100%|##########|  : 99/99 [00:00<00:00,  291.85it/s]
Computing cluster p-values
Step-down-in-jumps iteration #1 found 1 cluster to exclude from subsequent iterations
Permuting 99 times...

  0%|          |  : 0/99 [00:00<?,       ?it/s]
 12%|#2        |  : 12/99 [00:00<00:00,  354.24it/s]
 25%|##5       |  : 25/99 [00:00<00:00,  369.42it/s]
 37%|###7      |  : 37/99 [00:00<00:00,  364.29it/s]
 51%|#####     |  : 50/99 [00:00<00:00,  369.79it/s]
 64%|######3   |  : 63/99 [00:00<00:00,  372.84it/s]
 77%|#######6  |  : 76/99 [00:00<00:00,  374.86it/s]
 90%|########9 |  : 89/99 [00:00<00:00,  376.46it/s]
100%|##########|  : 99/99 [00:00<00:00,  379.74it/s]
100%|##########|  : 99/99 [00:00<00:00,  377.90it/s]
Computing cluster p-values
Step-down-in-jumps iteration #2 found 0 additional clusters to exclude from subsequent iterations
Done.
Using a threshold of -1.724718
stat_fun(H1): min=-4.573067 max=3.687727
Running initial clustering
Found 57 clusters
Permuting 99 times...

  0%|          |  : 0/99 [00:00<?,       ?it/s]
 12%|#2        |  : 12/99 [00:00<00:00,  354.69it/s]
 25%|##5       |  : 25/99 [00:00<00:00,  370.02it/s]
 37%|###7      |  : 37/99 [00:00<00:00,  364.93it/s]
 51%|#####     |  : 50/99 [00:00<00:00,  370.32it/s]
 62%|######1   |  : 61/99 [00:00<00:00,  360.41it/s]
 74%|#######3  |  : 73/99 [00:00<00:00,  359.44it/s]
 86%|########5 |  : 85/99 [00:00<00:00,  358.78it/s]
 97%|#########6|  : 96/99 [00:00<00:00,  353.78it/s]
100%|##########|  : 99/99 [00:00<00:00,  356.93it/s]
Computing cluster p-values
Step-down-in-jumps iteration #1 found 0 clusters to exclude from subsequent iterations
Done.
No baseline correction applied
Using a threshold of 1.724718
stat_fun(H1): min=-6.599131 max=3.329547
Running initial clustering
Found 64 clusters
Permuting 99 times...

  0%|          |  : 0/99 [00:00<?,       ?it/s]
 10%|#         |  : 10/99 [00:00<00:00,  294.63it/s]
 15%|#5        |  : 15/99 [00:00<00:00,  219.34it/s]
 27%|##7       |  : 27/99 [00:00<00:00,  266.68it/s]
 39%|###9      |  : 39/99 [00:00<00:00,  290.38it/s]
 53%|#####2    |  : 52/99 [00:00<00:00,  311.06it/s]
 67%|######6   |  : 66/99 [00:00<00:00,  330.39it/s]
 80%|#######9  |  : 79/99 [00:00<00:00,  339.31it/s]
 93%|#########2|  : 92/99 [00:00<00:00,  345.96it/s]
100%|##########|  : 99/99 [00:00<00:00,  349.55it/s]
100%|##########|  : 99/99 [00:00<00:00,  342.48it/s]
Computing cluster p-values
Step-down-in-jumps iteration #1 found 0 clusters to exclude from subsequent iterations
Done.
Using a threshold of -1.724718
stat_fun(H1): min=-6.599131 max=3.329547
Running initial clustering
Found 64 clusters
Permuting 99 times...

  0%|          |  : 0/99 [00:00<?,       ?it/s]
  8%|8         |  : 8/99 [00:00<00:00,  235.60it/s]
 18%|#8        |  : 18/99 [00:00<00:00,  266.53it/s]
 28%|##8       |  : 28/99 [00:00<00:00,  276.45it/s]
 39%|###9      |  : 39/99 [00:00<00:00,  289.51it/s]
 52%|#####1    |  : 51/99 [00:00<00:00,  303.81it/s]
 64%|######3   |  : 63/99 [00:00<00:00,  313.33it/s]
 75%|#######4  |  : 74/99 [00:00<00:00,  315.28it/s]
 86%|########5 |  : 85/99 [00:00<00:00,  316.77it/s]
 99%|#########8|  : 98/99 [00:00<00:00,  325.96it/s]
100%|##########|  : 99/99 [00:00<00:00,  322.65it/s]
Computing cluster p-values
Step-down-in-jumps iteration #1 found 0 clusters to exclude from subsequent iterations
Done.
No baseline correction applied
Using a threshold of 1.713872
stat_fun(H1): min=-3.687815 max=3.369164
Running initial clustering
Found 66 clusters
Permuting 99 times...

  0%|          |  : 0/99 [00:00<?,       ?it/s]
 11%|#1        |  : 11/99 [00:00<00:00,  324.89it/s]
 22%|##2       |  : 22/99 [00:00<00:00,  325.10it/s]
 34%|###4      |  : 34/99 [00:00<00:00,  335.37it/s]
 46%|####6     |  : 46/99 [00:00<00:00,  340.59it/s]
 59%|#####8    |  : 58/99 [00:00<00:00,  343.81it/s]
 69%|######8   |  : 68/99 [00:00<00:00,  334.74it/s]
 80%|#######9  |  : 79/99 [00:00<00:00,  332.96it/s]
 91%|######### |  : 90/99 [00:00<00:00,  331.57it/s]
100%|##########|  : 99/99 [00:00<00:00,  331.20it/s]
100%|##########|  : 99/99 [00:00<00:00,  331.46it/s]
Computing cluster p-values
Step-down-in-jumps iteration #1 found 0 clusters to exclude from subsequent iterations
Done.
Using a threshold of -1.713872
stat_fun(H1): min=-3.687815 max=3.369164
Running initial clustering
Found 75 clusters
Permuting 99 times...

  0%|          |  : 0/99 [00:00<?,       ?it/s]
 10%|#         |  : 10/99 [00:00<00:00,  295.25it/s]
 21%|##1       |  : 21/99 [00:00<00:00,  310.13it/s]
 32%|###2      |  : 32/99 [00:00<00:00,  315.58it/s]
 42%|####2     |  : 42/99 [00:00<00:00,  310.26it/s]
 54%|#####3    |  : 53/99 [00:00<00:00,  313.43it/s]
 64%|######3   |  : 63/99 [00:00<00:00,  310.08it/s]
 75%|#######4  |  : 74/99 [00:00<00:00,  312.65it/s]
 86%|########5 |  : 85/99 [00:00<00:00,  314.51it/s]
 97%|#########6|  : 96/99 [00:00<00:00,  315.80it/s]
100%|##########|  : 99/99 [00:00<00:00,  316.74it/s]
Computing cluster p-values
Step-down-in-jumps iteration #1 found 0 clusters to exclude from subsequent iterations
Done.
No baseline correction applied
Using a threshold of 1.713872
stat_fun(H1): min=-5.046259 max=5.406477
Running initial clustering
Found 98 clusters
Permuting 99 times...

  0%|          |  : 0/99 [00:00<?,       ?it/s]
 12%|#2        |  : 12/99 [00:00<00:00,  353.75it/s]
 18%|#8        |  : 18/99 [00:00<00:00,  262.01it/s]
 19%|#9        |  : 19/99 [00:00<00:00,  172.21it/s]
 30%|###       |  : 30/99 [00:00<00:00,  211.78it/s]
 40%|####      |  : 40/99 [00:00<00:00,  229.77it/s]
 49%|####9     |  : 49/99 [00:00<00:00,  236.34it/s]
 62%|######1   |  : 61/99 [00:00<00:00,  255.61it/s]
 72%|#######1  |  : 71/99 [00:00<00:00,  261.49it/s]
 81%|########  |  : 80/99 [00:00<00:00,  262.05it/s]
 90%|########9 |  : 89/99 [00:00<00:00,  262.53it/s]
100%|##########|  : 99/99 [00:00<00:00,  269.52it/s]
100%|##########|  : 99/99 [00:00<00:00,  264.41it/s]
Computing cluster p-values
Step-down-in-jumps iteration #1 found 1 cluster to exclude from subsequent iterations
Permuting 99 times...

  0%|          |  : 0/99 [00:00<?,       ?it/s]
  9%|9         |  : 9/99 [00:00<00:00,  265.66it/s]
 18%|#8        |  : 18/99 [00:00<00:00,  265.14it/s]
 25%|##5       |  : 25/99 [00:00<00:00,  243.97it/s]
 32%|###2      |  : 32/99 [00:00<00:00,  233.68it/s]
 42%|####2     |  : 42/99 [00:00<00:00,  247.09it/s]
 53%|#####2    |  : 52/99 [00:00<00:00,  256.17it/s]
 63%|######2   |  : 62/99 [00:00<00:00,  262.61it/s]
 70%|######9   |  : 69/99 [00:00<00:00,  254.17it/s]
 79%|#######8  |  : 78/99 [00:00<00:00,  255.63it/s]
 86%|########5 |  : 85/99 [00:00<00:00,  249.50it/s]
 93%|#########2|  : 92/99 [00:00<00:00,  244.59it/s]
100%|##########|  : 99/99 [00:00<00:00,  245.54it/s]
100%|##########|  : 99/99 [00:00<00:00,  246.65it/s]
Computing cluster p-values
Step-down-in-jumps iteration #2 found 0 additional clusters to exclude from subsequent iterations
Done.
Using a threshold of -1.713872
stat_fun(H1): min=-5.046259 max=5.406477
Running initial clustering
Found 66 clusters
Permuting 99 times...

  0%|          |  : 0/99 [00:00<?,       ?it/s]
  7%|7         |  : 7/99 [00:00<00:00,  206.15it/s]
 15%|#5        |  : 15/99 [00:00<00:00,  221.86it/s]
 23%|##3       |  : 23/99 [00:00<00:00,  226.94it/s]
 31%|###1      |  : 31/99 [00:00<00:00,  229.46it/s]
 40%|####      |  : 40/99 [00:00<00:00,  237.40it/s]
 49%|####9     |  : 49/99 [00:00<00:00,  242.75it/s]
 60%|#####9    |  : 59/99 [00:00<00:00,  251.49it/s]
 67%|######6   |  : 66/99 [00:00<00:00,  244.80it/s]
 77%|#######6  |  : 76/99 [00:00<00:00,  251.61it/s]
 86%|########5 |  : 85/99 [00:00<00:00,  253.47it/s]
 95%|#########4|  : 94/99 [00:00<00:00,  254.94it/s]
100%|##########|  : 99/99 [00:00<00:00,  255.17it/s]
Computing cluster p-values
Step-down-in-jumps iteration #1 found 0 clusters to exclude from subsequent iterations
Done.
No baseline correction applied
Using a threshold of 1.713872
stat_fun(H1): min=-5.964817 max=4.078953
Running initial clustering
Found 92 clusters
Permuting 99 times...

  0%|          |  : 0/99 [00:00<?,       ?it/s]
 10%|#         |  : 10/99 [00:00<00:00,  294.43it/s]
 13%|#3        |  : 13/99 [00:00<00:00,  189.14it/s]
 21%|##1       |  : 21/99 [00:00<00:00,  205.57it/s]
 29%|##9       |  : 29/99 [00:00<00:00,  213.77it/s]
 39%|###9      |  : 39/99 [00:00<00:00,  231.75it/s]
 48%|####8     |  : 48/99 [00:00<00:00,  238.16it/s]
 59%|#####8    |  : 58/99 [00:00<00:00,  247.64it/s]
 71%|#######   |  : 70/99 [00:00<00:00,  263.55it/s]
 81%|########  |  : 80/99 [00:00<00:00,  267.87it/s]
 92%|#########1|  : 91/99 [00:00<00:00,  275.01it/s]
100%|##########|  : 99/99 [00:00<00:00,  275.20it/s]
100%|##########|  : 99/99 [00:00<00:00,  269.07it/s]
Computing cluster p-values
Step-down-in-jumps iteration #1 found 1 cluster to exclude from subsequent iterations
Permuting 99 times...

  0%|          |  : 0/99 [00:00<?,       ?it/s]
 11%|#1        |  : 11/99 [00:00<00:00,  324.42it/s]
 20%|##        |  : 20/99 [00:00<00:00,  294.09it/s]
 31%|###1      |  : 31/99 [00:00<00:00,  304.97it/s]
 42%|####2     |  : 42/99 [00:00<00:00,  310.33it/s]
 54%|#####3    |  : 53/99 [00:00<00:00,  313.54it/s]
 66%|######5   |  : 65/99 [00:00<00:00,  321.43it/s]
 77%|#######6  |  : 76/99 [00:00<00:00,  322.10it/s]
 88%|########7 |  : 87/99 [00:00<00:00,  322.39it/s]
 99%|#########8|  : 98/99 [00:00<00:00,  322.68it/s]
100%|##########|  : 99/99 [00:00<00:00,  323.71it/s]
Computing cluster p-values
Step-down-in-jumps iteration #2 found 0 additional clusters to exclude from subsequent iterations
Done.
Using a threshold of -1.713872
stat_fun(H1): min=-5.964817 max=4.078953
Running initial clustering
Found 52 clusters
Permuting 99 times...

  0%|          |  : 0/99 [00:00<?,       ?it/s]
 10%|#         |  : 10/99 [00:00<00:00,  294.02it/s]
 22%|##2       |  : 22/99 [00:00<00:00,  325.24it/s]
 34%|###4      |  : 34/99 [00:00<00:00,  335.83it/s]
 44%|####4     |  : 44/99 [00:00<00:00,  325.01it/s]
 55%|#####4    |  : 54/99 [00:00<00:00,  318.65it/s]
 65%|######4   |  : 64/99 [00:00<00:00,  314.33it/s]
 75%|#######4  |  : 74/99 [00:00<00:00,  311.03it/s]
 84%|########3 |  : 83/99 [00:00<00:00,  304.27it/s]
 96%|#########5|  : 95/99 [00:00<00:00,  311.10it/s]
100%|##########|  : 99/99 [00:00<00:00,  312.54it/s]
Computing cluster p-values
Step-down-in-jumps iteration #1 found 0 clusters to exclude from subsequent iterations
Done.
No baseline correction applied

Similar to Epochs objects, we can also export data from EpochsTFR and AverageTFR objects to a Pandas DataFrame. By default, the time column of the exported data frame is in milliseconds. Here, to be consistent with the time-frequency plots, we want to keep it in seconds, which we can achieve by setting time_format=None:

df = tfr.to_data_frame(time_format=None)
df.head()
time freq epoch condition C3 Cz C4
0 -1.0000 2.0 0 feet -0.481570 -0.479232 -0.765757
1 -0.9875 2.0 0 feet -0.500555 -0.485289 -0.764209
2 -0.9750 2.0 0 feet -0.509981 -0.474900 -0.751235
3 -0.9625 2.0 0 feet -0.509883 -0.455039 -0.730498
4 -0.9500 2.0 0 feet -0.524450 -0.448783 -0.721221


This allows us to use additional plotting functions like seaborn.lineplot() to plot confidence bands:

df = tfr.to_data_frame(time_format=None, long_format=True)

# Map to frequency bands:
freq_bounds = {'_': 0,
               'delta': 3,
               'theta': 7,
               'alpha': 13,
               'beta': 35,
               'gamma': 140}
df['band'] = pd.cut(df['freq'], list(freq_bounds.values()),
                    labels=list(freq_bounds)[1:])

# Filter to retain only relevant frequency bands:
freq_bands_of_interest = ['delta', 'theta', 'alpha', 'beta']
df = df[df.band.isin(freq_bands_of_interest)]
df['band'] = df['band'].cat.remove_unused_categories()

# Order channels for plotting:
df['channel'] = df['channel'].cat.reorder_categories(('C3', 'Cz', 'C4'),
                                                     ordered=True)

g = sns.FacetGrid(df, row='band', col='channel', margin_titles=True)
g.map(sns.lineplot, 'time', 'value', 'condition', n_boot=10)
axline_kw = dict(color='black', linestyle='dashed', linewidth=0.5, alpha=0.5)
g.map(plt.axhline, y=0, **axline_kw)
g.map(plt.axvline, x=0, **axline_kw)
g.set(ylim=(None, 1.5))
g.set_axis_labels("Time (s)", "ERDS (%)")
g.set_titles(col_template="{col_name}", row_template="{row_name}")
g.add_legend(ncol=2, loc='lower center')
g.fig.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.08)
C3, Cz, C4

Out:

Converting "condition" to "category"...
Converting "epoch" to "category"...
Converting "freq" to "category"...
Converting "channel" to "category"...
Converting "ch_type" to "category"...

Having the data as a DataFrame also facilitates subsetting, grouping, and other transforms. Here, we use seaborn to plot the average ERDS in the motor imagery interval as a function of frequency band and imagery condition:

df_mean = (df.query('time > 1')
             .groupby(['condition', 'epoch', 'band', 'channel'])[['value']]
             .mean()
             .reset_index())

g = sns.FacetGrid(df_mean, col='condition', col_order=['hands', 'feet'],
                  margin_titles=True)
g = (g.map(sns.violinplot, 'channel', 'value', 'band', n_boot=10,
           palette='deep', order=['C3', 'Cz', 'C4'],
           hue_order=freq_bands_of_interest,
           linewidth=0.5)
      .add_legend(ncol=4, loc='lower center'))

g.map(plt.axhline, **axline_kw)
g.set_axis_labels("", "ERDS (%)")
g.set_titles(col_template="{col_name}", row_template="{row_name}")
g.fig.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.3)
hands, feet

References

1

Gert Pfurtscheller and Fernando H. Lopes da Silva. Event-related EEG/MEG synchronization and desynchronization: basic principles. Clinical Neurophysiology, 110(11):1842–1857, 1999. doi:10.1016/S1388-2457(99)00141-8.

2

Bernhard Graimann, Jane E. Huggins, Simon P. Levine, and Gert Pfurtscheller. Visualization of significant ERD/ERS patterns in multichannel EEG and ECoG data. Clinical Neurophysiology, 113(1):43–47, 2002. doi:10.1016/S1388-2457(01)00697-6.

3

Scott Makeig. Auditory event-related dynamics of the EEG spectrum and effects of exposure to tones. Electroencephalography and Clinical Neurophysiology, 86(4):283–293, 1993. doi:10.1016/0013-4694(93)90110-H.

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

Estimated memory usage: 138 MB

Gallery generated by Sphinx-Gallery