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
# Copyright the MNE-Python contributors.

As usual, we import everything we need.

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from matplotlib.colors import TwoSlopeNorm

import mne
from mne.datasets import eegbci
from mne.io import concatenate_raws, read_raw_edf
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(subjects=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
# rename descriptions to be more easily interpretable
raw.annotations.rename(dict(T1="hands", T2="feet"))
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...

Now we can create 5-second 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,
    event_id=["hands", "feet"],
    tmin=tmin - 0.5,
    tmax=tmax + 0.5,
    picks=("C3", "Cz", "C4"),
    baseline=None,
    preload=True,
)
Used Annotations descriptions: [np.str_('T0'), np.str_('feet'), np.str_('hands')]
Ignoring annotation durations and creating fixed-duration epochs around annotation onsets.
Not setting metadata
45 matching events found
No baseline correction applied
0 projection items activated
Using data from preloaded Raw for 45 events and 961 original time points ...
0 bad epochs dropped

Here we set suitable values for computing ERDS maps. Note especially the cnorm variable, which sets up an asymmetric colormap where the middle color is mapped to zero, even though zero is not the middle value of the colormap range. This does two things: it ensures that zero values will be plotted in white (given that below we select the RdBu colormap), and it makes synchronization and desynchronization look equally prominent in the plots, even though their extreme values are of different magnitudes.

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=vmin, vcenter=0, vmax=vmax)  # min, center & 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 = epochs.compute_tfr(
    method="multitaper",
    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]).ax.set_yscale("linear")
    fig.suptitle(f"ERDS ({event})")
    plt.show()
  • ERDS (hands), C3, Cz, C4
  • ERDS (feet), C3, Cz, C4
Applying baseline correction (mode: percent)
Using a threshold of 1.724718
stat_fun(H1): min=-8.55207615547708 max=3.1832308002458687
Running initial clustering …
Found 80 clusters

  0%|          | Permuting : 0/99 [00:00<?,       ?it/s]
  9%|▉         | Permuting : 9/99 [00:00<00:00,  250.69it/s]
 18%|█▊        | Permuting : 18/99 [00:00<00:00,  258.87it/s]
 26%|██▋       | Permuting : 26/99 [00:00<00:00,  251.25it/s]
 34%|███▍      | Permuting : 34/99 [00:00<00:00,  247.65it/s]
 42%|████▏     | Permuting : 42/99 [00:00<00:00,  245.55it/s]
 52%|█████▏    | Permuting : 51/99 [00:00<00:00,  249.63it/s]
 60%|█████▉    | Permuting : 59/99 [00:00<00:00,  247.68it/s]
 69%|██████▊   | Permuting : 68/99 [00:00<00:00,  247.06it/s]
 78%|███████▊  | Permuting : 77/99 [00:00<00:00,  249.76it/s]
 86%|████████▌ | Permuting : 85/99 [00:00<00:00,  248.27it/s]
 94%|█████████▍| Permuting : 93/99 [00:00<00:00,  247.04it/s]
100%|██████████| Permuting : 99/99 [00:00<00:00,  248.84it/s]
100%|██████████| Permuting : 99/99 [00:00<00:00,  248.58it/s]
Step-down-in-jumps iteration #1 found 0 clusters to exclude from subsequent iterations
Using a threshold of -1.724718
stat_fun(H1): min=-8.55207615547708 max=3.1832308002458687
Running initial clustering …
Found 67 clusters

  0%|          | Permuting : 0/99 [00:00<?,       ?it/s]
  9%|▉         | Permuting : 9/99 [00:00<00:00,  246.78it/s]
 16%|█▌        | Permuting : 16/99 [00:00<00:00,  227.48it/s]
 24%|██▍       | Permuting : 24/99 [00:00<00:00,  230.88it/s]
 33%|███▎      | Permuting : 33/99 [00:00<00:00,  240.43it/s]
 41%|████▏     | Permuting : 41/99 [00:00<00:00,  239.84it/s]
 49%|████▉     | Permuting : 49/99 [00:00<00:00,  239.39it/s]
 58%|█████▊    | Permuting : 57/99 [00:00<00:00,  239.11it/s]
 66%|██████▌   | Permuting : 65/99 [00:00<00:00,  238.89it/s]
 73%|███████▎  | Permuting : 72/99 [00:00<00:00,  234.71it/s]
 83%|████████▎ | Permuting : 82/99 [00:00<00:00,  239.92it/s]
 91%|█████████ | Permuting : 90/99 [00:00<00:00,  239.66it/s]
 98%|█████████▊| Permuting : 97/99 [00:00<00:00,  236.23it/s]
100%|██████████| Permuting : 99/99 [00:00<00:00,  237.83it/s]
Step-down-in-jumps iteration #1 found 1 cluster to exclude from subsequent iterations

  0%|          | Permuting : 0/99 [00:00<?,       ?it/s]
  9%|▉         | Permuting : 9/99 [00:00<00:00,  244.07it/s]
 17%|█▋        | Permuting : 17/99 [00:00<00:00,  240.75it/s]
 27%|██▋       | Permuting : 27/99 [00:00<00:00,  259.89it/s]
 35%|███▌      | Permuting : 35/99 [00:00<00:00,  253.99it/s]
 43%|████▎     | Permuting : 43/99 [00:00<00:00,  250.42it/s]
 51%|█████     | Permuting : 50/99 [00:00<00:00,  242.45it/s]
 59%|█████▊    | Permuting : 58/99 [00:00<00:00,  241.65it/s]
 67%|██████▋   | Permuting : 66/99 [00:00<00:00,  241.03it/s]
 74%|███████▎  | Permuting : 73/99 [00:00<00:00,  236.31it/s]
 83%|████████▎ | Permuting : 82/99 [00:00<00:00,  237.91it/s]
 92%|█████████▏| Permuting : 91/99 [00:00<00:00,  241.28it/s]
 99%|█████████▉| Permuting : 98/99 [00:00<00:00,  237.70it/s]
100%|██████████| Permuting : 99/99 [00:00<00:00,  238.75it/s]
Step-down-in-jumps iteration #2 found 0 additional clusters to exclude from subsequent iterations
No baseline correction applied
Using a threshold of 1.724718
stat_fun(H1): min=-4.528367198129923 max=3.7064219374431917
Running initial clustering …
Found 88 clusters

  0%|          | Permuting : 0/99 [00:00<?,       ?it/s]
  7%|▋         | Permuting : 7/99 [00:00<00:00,  205.45it/s]
 16%|█▌        | Permuting : 16/99 [00:00<00:00,  227.19it/s]
 24%|██▍       | Permuting : 24/99 [00:00<00:00,  230.65it/s]
 32%|███▏      | Permuting : 32/99 [00:00<00:00,  232.47it/s]
 41%|████▏     | Permuting : 41/99 [00:00<00:00,  240.00it/s]
 46%|████▋     | Permuting : 46/99 [00:00<00:00,  222.97it/s]
 55%|█████▍    | Permuting : 54/99 [00:00<00:00,  225.31it/s]
 65%|██████▍   | Permuting : 64/99 [00:00<00:00,  234.95it/s]
 75%|███████▍  | Permuting : 74/99 [00:00<00:00,  243.24it/s]
 86%|████████▌ | Permuting : 85/99 [00:00<00:00,  251.12it/s]
 97%|█████████▋| Permuting : 96/99 [00:00<00:00,  259.68it/s]
100%|██████████| Permuting : 99/99 [00:00<00:00,  255.94it/s]
Step-down-in-jumps iteration #1 found 1 cluster to exclude from subsequent iterations

  0%|          | Permuting : 0/99 [00:00<?,       ?it/s]
  9%|▉         | Permuting : 9/99 [00:00<00:00,  259.12it/s]
 19%|█▉        | Permuting : 19/99 [00:00<00:00,  278.30it/s]
 29%|██▉       | Permuting : 29/99 [00:00<00:00,  284.86it/s]
 40%|████      | Permuting : 40/99 [00:00<00:00,  296.10it/s]
 52%|█████▏    | Permuting : 51/99 [00:00<00:00,  300.13it/s]
 62%|██████▏   | Permuting : 61/99 [00:00<00:00,  299.55it/s]
 70%|██████▉   | Permuting : 69/99 [00:00<00:00,  289.35it/s]
 81%|████████  | Permuting : 80/99 [00:00<00:00,  294.91it/s]
 94%|█████████▍| Permuting : 93/99 [00:00<00:00,  307.20it/s]
100%|██████████| Permuting : 99/99 [00:00<00:00,  308.92it/s]
100%|██████████| Permuting : 99/99 [00:00<00:00,  305.78it/s]
Step-down-in-jumps iteration #2 found 0 additional clusters to exclude from subsequent iterations
Using a threshold of -1.724718
stat_fun(H1): min=-4.528367198129923 max=3.7064219374431917
Running initial clustering …
Found 58 clusters

  0%|          | Permuting : 0/99 [00:00<?,       ?it/s]
  8%|▊         | Permuting : 8/99 [00:00<00:00,  232.81it/s]
 19%|█▉        | Permuting : 19/99 [00:00<00:00,  280.60it/s]
 30%|███       | Permuting : 30/99 [00:00<00:00,  296.79it/s]
 41%|████▏     | Permuting : 41/99 [00:00<00:00,  303.11it/s]
 52%|█████▏    | Permuting : 51/99 [00:00<00:00,  301.79it/s]
 63%|██████▎   | Permuting : 62/99 [00:00<00:00,  306.53it/s]
 74%|███████▎  | Permuting : 73/99 [00:00<00:00,  306.78it/s]
 82%|████████▏ | Permuting : 81/99 [00:00<00:00,  296.65it/s]
 92%|█████████▏| Permuting : 91/99 [00:00<00:00,  296.71it/s]
100%|██████████| Permuting : 99/99 [00:00<00:00,  299.98it/s]
100%|██████████| Permuting : 99/99 [00:00<00:00,  299.44it/s]
Step-down-in-jumps iteration #1 found 0 clusters to exclude from subsequent iterations
No baseline correction applied
Using a threshold of 1.724718
stat_fun(H1): min=-6.581589289696407 max=3.346448151996899
Running initial clustering …
Found 67 clusters

  0%|          | Permuting : 0/99 [00:00<?,       ?it/s]
  9%|▉         | Permuting : 9/99 [00:00<00:00,  254.97it/s]
 17%|█▋        | Permuting : 17/99 [00:00<00:00,  246.06it/s]
 28%|██▊       | Permuting : 28/99 [00:00<00:00,  273.93it/s]
 38%|███▊      | Permuting : 38/99 [00:00<00:00,  280.21it/s]
 49%|████▉     | Permuting : 49/99 [00:00<00:00,  290.50it/s]
 61%|██████    | Permuting : 60/99 [00:00<00:00,  297.37it/s]
 72%|███████▏  | Permuting : 71/99 [00:00<00:00,  300.62it/s]
 83%|████████▎ | Permuting : 82/99 [00:00<00:00,  304.48it/s]
 94%|█████████▍| Permuting : 93/99 [00:00<00:00,  307.54it/s]
100%|██████████| Permuting : 99/99 [00:00<00:00,  307.46it/s]
100%|██████████| Permuting : 99/99 [00:00<00:00,  304.27it/s]
Step-down-in-jumps iteration #1 found 0 clusters to exclude from subsequent iterations
Using a threshold of -1.724718
stat_fun(H1): min=-6.581589289696407 max=3.346448151996899
Running initial clustering …
Found 69 clusters

  0%|          | Permuting : 0/99 [00:00<?,       ?it/s]
  9%|▉         | Permuting : 9/99 [00:00<00:00,  250.20it/s]
 21%|██        | Permuting : 21/99 [00:00<00:00,  302.91it/s]
 32%|███▏      | Permuting : 32/99 [00:00<00:00,  311.19it/s]
 42%|████▏     | Permuting : 42/99 [00:00<00:00,  307.57it/s]
 51%|█████     | Permuting : 50/99 [00:00<00:00,  292.30it/s]
 62%|██████▏   | Permuting : 61/99 [00:00<00:00,  298.78it/s]
 72%|███████▏  | Permuting : 71/99 [00:00<00:00,  296.88it/s]
 83%|████████▎ | Permuting : 82/99 [00:00<00:00,  301.29it/s]
 94%|█████████▍| Permuting : 93/99 [00:00<00:00,  304.73it/s]
100%|██████████| Permuting : 99/99 [00:00<00:00,  308.80it/s]
100%|██████████| Permuting : 99/99 [00:00<00:00,  306.73it/s]
Step-down-in-jumps iteration #1 found 0 clusters to exclude from subsequent iterations
No baseline correction applied
Using a threshold of 1.713872
stat_fun(H1): min=-3.754759498497248 max=3.3607039428313787
Running initial clustering …
Found 71 clusters

  0%|          | Permuting : 0/99 [00:00<?,       ?it/s]
 10%|█         | Permuting : 10/99 [00:00<00:00,  282.88it/s]
 20%|██        | Permuting : 20/99 [00:00<00:00,  289.94it/s]
 30%|███       | Permuting : 30/99 [00:00<00:00,  292.47it/s]
 38%|███▊      | Permuting : 38/99 [00:00<00:00,  277.86it/s]
 47%|████▋     | Permuting : 47/99 [00:00<00:00,  275.57it/s]
 58%|█████▊    | Permuting : 57/99 [00:00<00:00,  279.70it/s]
 68%|██████▊   | Permuting : 67/99 [00:00<00:00,  279.69it/s]
 78%|███████▊  | Permuting : 77/99 [00:00<00:00,  282.29it/s]
 88%|████████▊ | Permuting : 87/99 [00:00<00:00,  284.30it/s]
 97%|█████████▋| Permuting : 96/99 [00:00<00:00,  282.24it/s]
100%|██████████| Permuting : 99/99 [00:00<00:00,  284.37it/s]
Step-down-in-jumps iteration #1 found 0 clusters to exclude from subsequent iterations
Using a threshold of -1.713872
stat_fun(H1): min=-3.754759498497248 max=3.3607039428313787
Running initial clustering …
Found 80 clusters

  0%|          | Permuting : 0/99 [00:00<?,       ?it/s]
 10%|█         | Permuting : 10/99 [00:00<00:00,  274.57it/s]
 14%|█▍        | Permuting : 14/99 [00:00<00:00,  185.87it/s]
 19%|█▉        | Permuting : 19/99 [00:00<00:00,  173.64it/s]
 25%|██▌       | Permuting : 25/99 [00:00<00:00,  174.85it/s]
 34%|███▍      | Permuting : 34/99 [00:00<00:00,  194.54it/s]
 44%|████▍     | Permuting : 44/99 [00:00<00:00,  211.55it/s]
 55%|█████▍    | Permuting : 54/99 [00:00<00:00,  222.96it/s]
 59%|█████▊    | Permuting : 58/99 [00:00<00:00,  208.08it/s]
 64%|██████▎   | Permuting : 63/99 [00:00<00:00,  200.28it/s]
 69%|██████▊   | Permuting : 68/99 [00:00<00:00,  193.99it/s]
 79%|███████▉  | Permuting : 78/99 [00:00<00:00,  205.64it/s]
 88%|████████▊ | Permuting : 87/99 [00:00<00:00,  212.12it/s]
 98%|█████████▊| Permuting : 97/99 [00:00<00:00,  219.41it/s]
100%|██████████| Permuting : 99/99 [00:00<00:00,  215.00it/s]
Step-down-in-jumps iteration #1 found 0 clusters to exclude from subsequent iterations
No baseline correction applied
Using a threshold of 1.713872
stat_fun(H1): min=-4.992503139561789 max=5.416450269990024
Running initial clustering …
Found 103 clusters

  0%|          | Permuting : 0/99 [00:00<?,       ?it/s]
  4%|▍         | Permuting : 4/99 [00:00<00:00,  110.46it/s]
 11%|█         | Permuting : 11/99 [00:00<00:00,  158.62it/s]
 20%|██        | Permuting : 20/99 [00:00<00:00,  195.83it/s]
 31%|███▏      | Permuting : 31/99 [00:00<00:00,  230.56it/s]
 39%|███▉      | Permuting : 39/99 [00:00<00:00,  232.07it/s]
 44%|████▍     | Permuting : 44/99 [00:00<00:00,  216.46it/s]
 49%|████▉     | Permuting : 49/99 [00:00<00:00,  205.28it/s]
 54%|█████▎    | Permuting : 53/99 [00:00<00:00,  191.08it/s]
 60%|█████▉    | Permuting : 59/99 [00:00<00:00,  187.62it/s]
 65%|██████▍   | Permuting : 64/99 [00:00<00:00,  182.86it/s]
 70%|██████▉   | Permuting : 69/99 [00:00<00:00,  177.93it/s]
 78%|███████▊  | Permuting : 77/99 [00:00<00:00,  184.32it/s]
 87%|████████▋ | Permuting : 86/99 [00:00<00:00,  192.70it/s]
 96%|█████████▌| Permuting : 95/99 [00:00<00:00,  199.86it/s]
100%|██████████| Permuting : 99/99 [00:00<00:00,  199.46it/s]
100%|██████████| Permuting : 99/99 [00:00<00:00,  197.77it/s]
Step-down-in-jumps iteration #1 found 1 cluster to exclude from subsequent iterations

  0%|          | Permuting : 0/99 [00:00<?,       ?it/s]
  6%|▌         | Permuting : 6/99 [00:00<00:00,  156.49it/s]
 12%|█▏        | Permuting : 12/99 [00:00<00:00,  166.96it/s]
 18%|█▊        | Permuting : 18/99 [00:00<00:00,  170.81it/s]
 27%|██▋       | Permuting : 27/99 [00:00<00:00,  196.09it/s]
 35%|███▌      | Permuting : 35/99 [00:00<00:00,  205.08it/s]
 40%|████      | Permuting : 40/99 [00:00<00:00,  194.64it/s]
 44%|████▍     | Permuting : 44/99 [00:00<00:00,  182.30it/s]
 48%|████▊     | Permuting : 48/99 [00:00<00:00,  173.01it/s]
 53%|█████▎    | Permuting : 52/99 [00:00<00:00,  165.21it/s]
 61%|██████    | Permuting : 60/99 [00:00<00:00,  173.27it/s]
 65%|██████▍   | Permuting : 64/99 [00:00<00:00,  165.74it/s]
 70%|██████▉   | Permuting : 69/99 [00:00<00:00,  162.71it/s]
 75%|███████▍  | Permuting : 74/99 [00:00<00:00,  161.30it/s]
 81%|████████  | Permuting : 80/99 [00:00<00:00,  162.92it/s]
 90%|████████▉ | Permuting : 89/99 [00:00<00:00,  172.46it/s]
100%|██████████| Permuting : 99/99 [00:00<00:00,  184.95it/s]
100%|██████████| Permuting : 99/99 [00:00<00:00,  181.02it/s]
Step-down-in-jumps iteration #2 found 0 additional clusters to exclude from subsequent iterations
Using a threshold of -1.713872
stat_fun(H1): min=-4.992503139561789 max=5.416450269990024
Running initial clustering …
Found 67 clusters

  0%|          | Permuting : 0/99 [00:00<?,       ?it/s]
  9%|▉         | Permuting : 9/99 [00:00<00:00,  245.84it/s]
 19%|█▉        | Permuting : 19/99 [00:00<00:00,  271.00it/s]
 22%|██▏       | Permuting : 22/99 [00:00<00:00,  208.80it/s]
 29%|██▉       | Permuting : 29/99 [00:00<00:00,  208.57it/s]
 36%|███▋      | Permuting : 36/99 [00:00<00:00,  208.44it/s]
 44%|████▍     | Permuting : 44/99 [00:00<00:00,  213.88it/s]
 55%|█████▍    | Permuting : 54/99 [00:00<00:00,  227.54it/s]
 64%|██████▎   | Permuting : 63/99 [00:00<00:00,  233.43it/s]
 72%|███████▏  | Permuting : 71/99 [00:00<00:00,  234.01it/s]
 80%|███████▉  | Permuting : 79/99 [00:00<00:00,  234.41it/s]
 90%|████████▉ | Permuting : 89/99 [00:00<00:00,  240.59it/s]
 99%|█████████▉| Permuting : 98/99 [00:00<00:00,  243.46it/s]
100%|██████████| Permuting : 99/99 [00:00<00:00,  241.64it/s]
Step-down-in-jumps iteration #1 found 0 clusters to exclude from subsequent iterations
No baseline correction applied
Using a threshold of 1.713872
stat_fun(H1): min=-6.044339991978302 max=4.070443782271568
Running initial clustering …
Found 92 clusters

  0%|          | Permuting : 0/99 [00:00<?,       ?it/s]
  9%|▉         | Permuting : 9/99 [00:00<00:00,  252.08it/s]
 18%|█▊        | Permuting : 18/99 [00:00<00:00,  259.72it/s]
 27%|██▋       | Permuting : 27/99 [00:00<00:00,  262.41it/s]
 37%|███▋      | Permuting : 37/99 [00:00<00:00,  271.69it/s]
 46%|████▋     | Permuting : 46/99 [00:00<00:00,  270.79it/s]
 58%|█████▊    | Permuting : 57/99 [00:00<00:00,  277.01it/s]
 67%|██████▋   | Permuting : 66/99 [00:00<00:00,  275.46it/s]
 77%|███████▋  | Permuting : 76/99 [00:00<00:00,  278.66it/s]
 86%|████████▌ | Permuting : 85/99 [00:00<00:00,  277.20it/s]
 96%|█████████▌| Permuting : 95/99 [00:00<00:00,  279.66it/s]
100%|██████████| Permuting : 99/99 [00:00<00:00,  278.32it/s]
Step-down-in-jumps iteration #1 found 1 cluster to exclude from subsequent iterations

  0%|          | Permuting : 0/99 [00:00<?,       ?it/s]
  9%|▉         | Permuting : 9/99 [00:00<00:00,  250.26it/s]
 19%|█▉        | Permuting : 19/99 [00:00<00:00,  273.56it/s]
 28%|██▊       | Permuting : 28/99 [00:00<00:00,  271.56it/s]
 38%|███▊      | Permuting : 38/99 [00:00<00:00,  278.42it/s]
 47%|████▋     | Permuting : 47/99 [00:00<00:00,  276.03it/s]
 58%|█████▊    | Permuting : 57/99 [00:00<00:00,  280.02it/s]
 67%|██████▋   | Permuting : 66/99 [00:00<00:00,  277.95it/s]
 77%|███████▋  | Permuting : 76/99 [00:00<00:00,  280.78it/s]
 87%|████████▋ | Permuting : 86/99 [00:00<00:00,  283.02it/s]
 96%|█████████▌| Permuting : 95/99 [00:00<00:00,  278.78it/s]
100%|██████████| Permuting : 99/99 [00:00<00:00,  281.23it/s]
Step-down-in-jumps iteration #2 found 0 additional clusters to exclude from subsequent iterations
Using a threshold of -1.713872
stat_fun(H1): min=-6.044339991978302 max=4.070443782271568
Running initial clustering …
Found 51 clusters

  0%|          | Permuting : 0/99 [00:00<?,       ?it/s]
  9%|▉         | Permuting : 9/99 [00:00<00:00,  249.76it/s]
 19%|█▉        | Permuting : 19/99 [00:00<00:00,  273.27it/s]
 29%|██▉       | Permuting : 29/99 [00:00<00:00,  281.54it/s]
 38%|███▊      | Permuting : 38/99 [00:00<00:00,  277.78it/s]
 48%|████▊     | Permuting : 48/99 [00:00<00:00,  282.06it/s]
 58%|█████▊    | Permuting : 57/99 [00:00<00:00,  279.37it/s]
 67%|██████▋   | Permuting : 66/99 [00:00<00:00,  277.46it/s]
 76%|███████▌  | Permuting : 75/99 [00:00<00:00,  275.95it/s]
 86%|████████▌ | Permuting : 85/99 [00:00<00:00,  278.81it/s]
 95%|█████████▍| Permuting : 94/99 [00:00<00:00,  277.42it/s]
100%|██████████| Permuting : 99/99 [00:00<00:00,  278.49it/s]
100%|██████████| Permuting : 99/99 [00:00<00:00,  278.08it/s]
Step-down-in-jumps iteration #1 found 0 clusters to exclude from subsequent iterations
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 1 feet -0.473093 -0.474571 -0.764021
1 -0.9875 2.0 1 feet -0.491659 -0.480088 -0.762445
2 -0.9750 2.0 1 feet -0.500872 -0.469376 -0.749421
3 -0.9625 2.0 1 feet -0.500605 -0.449373 -0.728686
4 -0.9500 2.0 1 feet -0.515911 -0.443439 -0.719536


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
Converting "condition" to "category"...
Converting "epoch" 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"], observed=False)[["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",
    cut=0,
    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#

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

Gallery generated by Sphinx-Gallery