{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# EEG Example" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", " \n", "This example shows how to compute EEG frequency band power from EEG data acquired by the Muse Headband.\n", " \n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import and Helper Functions" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from pathlib import Path\n", "\n", "import re\n", "\n", "import pandas as pd\n", "import numpy as np\n", "\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "\n", "from fau_colors import cmaps\n", "import biopsykit as bp\n", "\n", "%matplotlib widget\n", "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "palette = sns.color_palette(cmaps.faculties)\n", "sns.set_theme(context=\"notebook\", style=\"ticks\", font=\"sans-serif\", palette=palette)\n", "\n", "plt.rcParams[\"figure.figsize\"] = (8, 4)\n", "plt.rcParams[\"pdf.fonttype\"] = 42\n", "plt.rcParams[\"mathtext.default\"] = \"regular\"\n", "\n", "palette" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dataset = bp.example_data.get_eeg_example()\n", "data = dataset.data_as_df(index=\"local_datetime\")\n", "# alternatively, load your own data:\n", "# dataset = bp.io.eeg.MuseDataset(\"\")\n", "# data = dataset.data_as_df(index=\")\n", "\n", "data.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a [EegProcessor](https://biopsykit.readthedocs.io/en/latest/api/biopsykit.signals.eeg.html#biopsykit.signals.eeg.EegProcessor) instance for processing EEG raw data." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Compute Relative EEG Frequency Band Energy\n", "eeg_processor = bp.signals.eeg.EegProcessor(data, dataset.sampling_rate_hz)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "eeg_processor.relative_band_energy()\n", "df_bands = eeg_processor.eeg_result[\"Data\"]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# For saving the frequency band data frame\n", "# bp.io.eeg.write_frequency_bands(df_bands, \"\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig, ax = plt.subplots(figsize=(10, 5))\n", "df_bands.plot(ax=ax)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "nbsphinx-thumbnail" ] }, "outputs": [], "source": [ "fig, ax = plt.subplots(figsize=(10, 5))\n", "# compute 20-point moving average and plot this\n", "df_bands.rolling(20).mean().plot(ax=ax)\n", "fig.tight_layout()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "biopsykit", "language": "python", "name": "biopsykit" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.13" } }, "nbformat": 4, "nbformat_minor": 4 }