EEG Example¶
This example shows how to compute EEG frequency band power from EEG data acquired by the Muse Headband.
Import and Helper Functions¶
[1]:
from pathlib import Path
import re
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from fau_colors import cmaps
import biopsykit as bp
%matplotlib inline
%load_ext autoreload
%autoreload 2
[2]:
palette = sns.color_palette(cmaps.faculties)
sns.set_theme(context="notebook", style="ticks", font="sans-serif", palette=palette)
plt.rcParams["figure.figsize"] = (8, 4)
plt.rcParams["pdf.fonttype"] = 42
plt.rcParams["mathtext.default"] = "regular"
palette
[2]:
[3]:
dataset = bp.example_data.get_eeg_example()
data = dataset.data_as_df(index="local_datetime")
# alternatively, load your own data:
# dataset = bp.io.eeg.MuseDataset("<path-to-muse-eeg-data>")
# data = dataset.data_as_df(index="<specify-your-index-type">)
data.head()
[3]:
TP9 | AF7 | AF8 | TP10 | |
---|---|---|---|---|
date (Europe/Berlin) | ||||
2020-11-26 14:45:09.680000+01:00 | -34.668 | -31.738 | -34.668 | -23.438 |
2020-11-26 14:45:09.684000+01:00 | -41.504 | -34.180 | -30.762 | -31.738 |
2020-11-26 14:45:09.688000+01:00 | -35.156 | -38.574 | -37.598 | -30.762 |
2020-11-26 14:45:09.692000+01:00 | -24.414 | -44.434 | -47.852 | -24.902 |
2020-11-26 14:45:09.696000+01:00 | -28.809 | -34.668 | -41.016 | -30.273 |
Create a EegProcessor instance for processing EEG raw data.
[4]:
# Compute Relative EEG Frequency Band Energy
eeg_processor = bp.signals.eeg.EegProcessor(data, dataset.sampling_rate_hz)
[5]:
eeg_processor.relative_band_energy()
df_bands = eeg_processor.eeg_result["Data"]
[6]:
# For saving the frequency band data frame
# bp.io.eeg.write_frequency_bands(df_bands, "<path-to-save.csv>")
[7]:
fig, ax = plt.subplots(figsize=(10, 5))
df_bands.plot(ax=ax)
[7]:
<Axes: xlabel='timestamp'>
[8]:
fig, ax = plt.subplots(figsize=(10, 5))
# compute 20-point moving average and plot this
df_bands.rolling(20).mean().plot(ax=ax)
fig.tight_layout()
[ ]: