Commit d68beefe authored by Can Pervane's avatar Can Pervane

added a a new feature extraction method called rms: it calculates the root...

added a a new feature extraction method called rms: it calculates the root mean square value of a time series.
edited gen_embedding such that it support python3 and python2
if window column is present in the raw data, the wavesextract will use that information to add a window columns to its generated dataframe.
in get examples: the return type of dict values is still a dictionary in python 3, thus needed to be converted to list before indexin it.
parent e698bb71
......@@ -117,14 +117,14 @@ class EEGSession():
def plot_dataframe(self, df_name, channels=""):
pass
def get_examples(self, feature_args, epoch_size="all", channels="all", filtered_waves="true"):
def get_examples(self, feature_args, epoch_size="all", channels="all", filtered_waves=True):
if channels == "all":
channels = [col for col in self.raw.columns if col not in plot_ignore_columns]
if filtered_waves:
extractWaves(self)
if epoch_size == "all":
epoch_size = self.waves.values()[0].shape[0]
n_epochs = int(self.waves.values()[0].shape[0] / epoch_size)
epoch_size = list(self.waves.values())[0].shape[0]
n_epochs = int(list(self.waves.values())[0].shape[0] / epoch_size)
examples = []
wave_matrices = {k: v.as_matrix() for k, v in self.waves.items()}
for i in range(n_epochs):
......
......@@ -14,7 +14,18 @@ def coherence(raw_matrix):
coherences.append(sp.signal.coherence(raw_matrix[:, i], raw_matrix[:, j])[0])
return np.hstack(coherences)
def rms(raw_matrix):
"""
Calculates the root mean square value of a time series
"""
rmsValues = []
for i in range(raw_matrix.shape[1]):
x = raw_matrix[:,i]
rmsValues.append(np.sqrt(np.mean(x**2)))
return np.hstack(rmsValues)
extractors = {
"correlation": correlation,
"coherence": coherence,
"rms" : rms,
}
......@@ -58,6 +58,8 @@ def extractWaves(session, n=4001, samplingRate=256, wave='all'):
# apply filter, via convolution
s = pd.Series(np.convolve(session.raw[col], b[key], mode='valid'))
df['_'.join([col,key])] = s
if 'window' in session.raw.columns:
df['window'] = session.raw['window'][chop:-chop].reset_index(drop=True)
df['time'] = session.raw['time'][chop:-chop].reset_index(drop=True)
session.waves[key] = df
return 0
......
import numpy as np
from matplotlib import pyplot as plt
from itertools import cycle
import sys
from config import pid_noConcussion, pid_3stepProtocol, pid_testRetest, pid_concussion, feature_functions, epoch_size, \
embedding_args, pid_testlist
from patient import Patient
from embedding import Embedding
def centroid(data):
length = len(data)
x_sum = np.sum(data[:, 0])
......@@ -38,7 +38,6 @@ for lst, pat_list in zip([pid_testlist], [noCon_pats]):
# create training data
train_data = np.vstack([p.pre_test.examples for p in noCon_pats if p.pre_test is not None] +
[p.post_test.examples for p in noCon_pats if p.post_test is not None])
# create and train embedding
emb = Embedding(**embedding_args)
emb.train(train_data)
......@@ -48,7 +47,13 @@ emb.train(train_data)
colors = cycle(['r', 'b', 'g', 'y'])
pre_post_distances = []
for p in noCon_pats:
color = colors.next()
if (sys.version_info < (3,0)):
# for python2 use
color = colors.next()
else:
# for python3 use
color = next(colors)
pre_emb = emb.embed(p.pre_test.examples)
post_emb = emb.embed(p.post_test.examples)
plt.plot(pre_emb[:, 0], pre_emb[:, 1], linestyle='None', marker="x", color=color, label=p.pid + "_pre")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment