{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Looking at EEG power spectra" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we examine a number of ways in which the frequency content of EEG data can be assessed.\n", "\n", "We use the N170 dataset as an example, but the following can be used with minimal adjustments with any eeg or time series data." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setup" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import sys\n", "eegnb_dir = \"C:\\\\Users\\\\john_griffiths\\\\Code\\\\libraries_of_mine\\\\github\\\\eeg-notebooks\"\n", "sys.path.append(eegnb_dir)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Generic imports\n", "import os,sys,glob,numpy as np, pandas as pd\n", "\n", "# we're currently in sandbox (i.e. test / dev area.)\n", "# but we want the behaviour to be as if we're in one dir up; so move there now\n", "os.chdir('../')\n", "\n", "# eeg-notebooks utils\n", "from utils import utils \n", "\n", "# mne functions\n", "from mne import Epochs, find_events\n", "from mne.time_frequency import psd_welch\n", "\n", "# visualization stuff\n", "%matplotlib inline \n", "from matplotlib import pyplot as plt\n", "import seaborn as sns\n", "\n", "# others\n", "from scipy.signal import welch" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load the data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We will use session 1, subject 1 from the eeg notebooks example N170 dataset" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Creating RawArray with float64 data, n_channels=5, n_times=30732\n", " Range : 0 ... 30731 = 0.000 ... 120.043 secs\n", "Ready.\n", "Creating RawArray with float64 data, n_channels=5, n_times=30732\n", " Range : 0 ... 30731 = 0.000 ... 120.043 secs\n", "Ready.\n", "Creating RawArray with float64 data, n_channels=5, n_times=30732\n", " Range : 0 ... 30731 = 0.000 ... 120.043 secs\n", "Ready.\n", "Creating RawArray with float64 data, n_channels=5, n_times=30732\n", " Range : 0 ... 30731 = 0.000 ... 120.043 secs\n", "Ready.\n", "Creating RawArray with float64 data, n_channels=5, n_times=30720\n", " Range : 0 ... 30719 = 0.000 ... 119.996 secs\n", "Ready.\n", "Creating RawArray with float64 data, n_channels=5, n_times=30732\n", " Range : 0 ... 30731 = 0.000 ... 120.043 secs\n", "Ready.\n" ] } ], "source": [ "subject = 1\n", "session = 1\n", "raw = utils.load_data('visual/N170', sfreq=256., \n", " subject_nb=subject, session_nb=session)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Calculate PSD for a raw (non-epoched) recording" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is the simplest option." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Effective window size : 1.000 (s)\n" ] } ], "source": [ "# compute the poweer spectral density (PSD) using \n", "# the MNE psd_welch function\n", "# (this is simply a wrapper on scipy.signal.welch\n", "# that adds compatbility for MNE data types)\n", "\n", "psd,freqs = psd_welch(raw)\n", "\n", "# Place in a pandas dataframe for convenience\n", "\n", "df_psd = pd.DataFrame(psd, columns=freqs).T\n", "df_psd.columns = raw.ch_names[:4]\n", "df_psd.index.names = ['freq']\n", "df_psd.columns.names = ['chan']" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| chan | \n", "TP9 | \n", "AF7 | \n", "AF8 | \n", "TP10 | \n", "
|---|---|---|---|---|
| freq | \n", "\n", " | \n", " | \n", " | \n", " |
| 0.0 | \n", "7.857495e-12 | \n", "8.728770e-13 | \n", "9.292256e-13 | \n", "1.154006e-11 | \n", "
| 1.0 | \n", "4.834027e-11 | \n", "7.087248e-12 | \n", "5.778365e-12 | \n", "6.940097e-11 | \n", "
| 2.0 | \n", "7.097305e-11 | \n", "4.820972e-12 | \n", "2.761128e-12 | \n", "7.803431e-11 | \n", "
| 3.0 | \n", "6.414010e-11 | \n", "3.632046e-12 | \n", "1.375676e-12 | \n", "6.852216e-11 | \n", "
| 4.0 | \n", "4.564189e-11 | \n", "2.447174e-12 | \n", "1.015633e-12 | \n", "4.741013e-11 | \n", "
| 5.0 | \n", "2.399177e-11 | \n", "1.383455e-12 | \n", "8.356244e-13 | \n", "2.572205e-11 | \n", "
| 6.0 | \n", "1.053377e-11 | \n", "8.235806e-13 | \n", "6.736721e-13 | \n", "1.132447e-11 | \n", "
| 7.0 | \n", "5.375513e-12 | \n", "6.215222e-13 | \n", "5.176218e-13 | \n", "5.815875e-12 | \n", "
| 8.0 | \n", "3.451319e-12 | \n", "4.510599e-13 | \n", "4.286293e-13 | \n", "3.447529e-12 | \n", "
| 9.0 | \n", "2.929786e-12 | \n", "3.904274e-13 | \n", "4.247347e-13 | \n", "2.907828e-12 | \n", "