Time Domain Analysis with Plotly

Preamble

In [2]:
import mne
import numpy as np                 # for multi-dimensional containers
import pandas as pd                # for DataFrames
import plotly.graph_objects as go  # for data visualisation
import plotly.io as pio            # to set shahin plot layout

pio.templates['shahin'] = pio.to_templated(go.Figure().update_layout(yaxis=dict(autorange = "reversed"),margin=dict(t=0,r=0,b=40,l=40))).layout.template
pio.templates.default = 'shahin'

Dataset

Download sample data from Mike Cohen.

In [3]:
import warnings
warnings.filterwarnings("ignore")

def load_data(tmin, tmax):
    epochs = mne.io.read_epochs_eeglab('sampleEEGdata.mat').crop(tmin, tmax)
    return epochs

Butterfly Plot of ERP from all sensors (all channels/all trials in time domain)

With MNE.Epochs.to_data_frame()

In [8]:
epochs = load_data(-0.2,1)
values = epochs.to_data_frame()
values = values.groupby("time").mean()
values.head()
Extracting parameters from sampleEEGdata.mat...
99 matching events found
No baseline correction applied
Not setting metadata
0 projection items activated
Ready.
Converting "time" to "<class 'numpy.int64'>"...
Out[8]:
channel Fp1 AF7 AF3 F1 F3 F5 F7 FT7 FC5 FC3 ... CP4 CP2 P2 P4 P6 P8 P10 PO8 PO4 O2
time
-199 -0.891870 -0.701918 -1.706986 -1.024161 -0.976764 -1.014164 -1.002770 -1.402991 -1.496777 -1.314428 ... -1.511028 -1.477739 -1.723046 -1.574034 -1.598564 -1.779448 -0.940347 -1.883280 -1.808410 -2.185663
-195 0.233510 0.506950 -0.445840 0.333534 0.195593 0.212527 0.163632 -0.052681 -0.010986 0.129063 ... 0.125678 0.130709 0.035723 0.130725 0.134243 0.357075 0.358627 -0.038384 -0.170933 -0.259101
-191 -0.507979 -1.007955 -0.986916 -0.328037 -0.593872 -0.573180 -0.562565 -1.356027 -0.634322 -0.383950 ... 0.207923 0.279907 0.127276 0.382046 0.444502 0.647713 0.310199 0.538165 0.387237 0.784565
-188 -0.371972 -0.792418 0.033578 -0.341501 -0.652673 -0.620372 -0.657750 -1.051308 -0.668186 -0.351760 ... -0.173513 -0.230984 -0.433561 -0.238758 -0.352948 -0.364883 0.579337 -0.249470 -0.402957 -0.453058
-184 -0.152894 -0.505432 1.171427 0.201709 -0.251452 -0.286700 -0.341288 -0.404997 -0.500198 -0.006877 ... 0.178264 0.226898 -0.141822 0.106690 0.067396 -0.146313 -0.080451 0.000498 0.049574 0.088095

5 rows × 64 columns

In [4]:
fig = go.Figure(layout=dict(xaxis=dict(title='time'),yaxis=dict(title='voltage')))

for ch in epochs.info['ch_names']:
    fig.add_scatter(x=epochs.times, y=values[ch], name=ch)

fig.show()