Time Domain Analysis with Plotly
Preamble¶
In [1]:
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'
pio.renderers.default = "notebook_connected"
Dataset¶
Download sample data from Mike Cohen.
In [2]:
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 [3]:
epochs = load_data(-0.2,1)
values = epochs.to_data_frame()
values = values.groupby("time").mean()
values.head()
Out[3]:
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()
With MNE.Epochs.get_data()¶
In [5]:
epochs = load_data(-0.2,1)
values = epochs.get_data().mean(axis=0)
values = pd.DataFrame(np.transpose(values), columns=epochs.info['ch_names'])
values.head()
Out[5]:
In [6]:
fig = go.Figure(layout=dict(xaxis=dict(title='time')))
for ch in epochs.info['ch_names']:
fig.add_scatter(x=epochs.times, y=values[ch], name=ch)
fig.show()