In [44]:
import pandas as pd
import numpy as np
import scipy.stats as stats
import scipy.signal as signal
import matplotlib.pyplot as plt
import sklearn as sk
import tensorflow as tf
from tensorflow.contrib import learn
EPOCH_LENGTH = 440
VARIANCE_THRESHOLD = 550
In [45]:
# Data has been collected, let's import it
open_data = pd.read_csv("../Muse Data/DanoThursdayOpenRawEEG0.csv", header=0, index_col=False)
closed_data = pd.read_csv("../Muse Data/DanoThursdayClosedRawEEG1.csv", header=0, index_col=False)
In [46]:
# Drop difficulty, timestamp, and performance columns since we're not using them
open_data = open_data.drop(['Difficulty', 'Performance', 'Timestamp (ms)'], axis=1)
closed_data = closed_data.drop(['Difficulty', 'Performance', 'Timestamp (ms)'], axis=1)
# Prune rows from tail of datasets so that they are all divisible by 440 (the desired size of our epochs)
open_overflow = open_data.shape[0] % EPOCH_LENGTH
open_data = open_data[0:-open_overflow]
closed_overflow = closed_data.shape[0] % EPOCH_LENGTH
closed_data = closed_data[0:-closed_overflow]
In [47]:
# Split DataFrames into many different dataframes 440 samples long
split_open_data = np.stack(np.array_split(open_data, EPOCH_LENGTH), axis=1)
split_closed_data = np.stack(np.array_split(closed_data, EPOCH_LENGTH), axis=1)
# Transform data into a 3D pandas Panel ( n epochs x 4 channels x 440 samples )
open_panel = pd.Panel(split_open_data)
closed_panel = pd.Panel(split_closed_data)
open_panel.shape
Out[47]:
In [48]:
# Remove epochs with too much variance
def removeNoise(panel):
for frameIndex in panel:
for columnIndex in panel[frameIndex]:
if np.var(panel[frameIndex][columnIndex]) > VARIANCE_THRESHOLD:
print('variance ', np.var(panel[frameIndex][columnIndex]), ' at electrode ', columnIndex, ' frame ', frameIndex)
panel = panel.drop(frameIndex)
break
return panel
closed_panel = removeNoise(closed_panel)
open_panel = removeNoise(open_panel)
In [51]:
plt.figure()
plt.subplot(2,2,1)
plt.specgram(open_panel[20][0], NFFT=256, Fs=220, noverlap=198)
plt.ylim(0,55)
plt.subplot(2,2,2)
plt.specgram(open_panel[20][1], NFFT=256, Fs=220, noverlap=198)
plt.ylim(0,55)
plt.subplot(2,2,3)
plt.specgram(open_panel[20][2], NFFT=256, Fs=220, noverlap=198)
plt.ylim(0,55)
plt.subplot(2,2,4)
plt.specgram(open_panel[20][3], NFFT=256, Fs=220, noverlap=198)
plt.ylim(0,55)
plt.show
Out[51]:
In [7]:
# Plot test spectograms of all 4 channels
def plotAndSave(frame, filename):
plt.figure()
plt.subplot(2,2,1)
plt.specgram(frame[0], NFFT=256, Fs=220, noverlap=198)
plt.ylim(0,55)
plt.subplot(2,2,2)
plt.specgram(frame[2], NFFT=256, Fs=220, noverlap=198)
plt.ylim(0,55)
plt.subplot(2,2,3)
plt.specgram(frame[3], NFFT=256, Fs=220, noverlap=198)
plt.ylim(0,55)
plt.savefig('%s.jpg' % filename, pad_inches=0, bbox_inches='tight')
for frameIndex in open_panel:
plotAndSave(open_panel[frameIndex], 'open%s' % frameIndex)
for frameIndex in closed_panel:
plotAndSave(closed_panel[frameIndex], 'closed%s' % frameIndex)
In [231]:
Questions to answer before continuing: