In [1]:
%pylab inline
In [2]:
import pyrem as pr
import numpy as np
import pylab as pl
import pandas as pd
from scipy import signal
In [3]:
seq = np.genfromtxt("/home/quentin/Desktop/rem_chunking/test_eeg_no_header_small.txt")
#seq = np.genfromtxt("/home/quentin/Desktop/rem_chunking/test_eeg_no_header.txt")
to_remove =1 * int(seq.size / 100.0)
seq = seq[to_remove: -to_remove]
# seq = np.sin(np.linspace(0, 200, 100000)) *10
# seq = seq + np.sin(np.linspace(0, 20, 100000)) * 2
# seq = seq + np.random.normal(0,10,100000)
sig = pr.Signal(seq, 200.0)
In [4]:
sig = sig.resample(256)
print sig.duration
sig[0:300000]
Out[4]:
In [5]:
n = 25
c1 = sig[sig.sampling_freq * 20 * n:sig.sampling_freq * 20 * (n+1)]
# f, pxx = signal.welch(c1, sig.sampling_freq, window="blackmanharris")
# pl.plot(f, pxx)
n = 20
c2 = sig[sig.sampling_freq * 20 * n:sig.sampling_freq * 20 * (n+1)]
# #boxcar, triang, blackman, hamming, hann, bartlett, flattop, parzen, bohman, blackmanharris, nuttall, barthann,
# f, pxx = signal.welch(c2, sig.sampling_freq, window="bartlett",nperseg=256)
# pl.plot(f, pxx)
# pl.xscale("log")
# print f[np.argmax(pxx)]
In [8]:
ff = pr.features.FeatureFactory()
df = pd.concat([ff(c1,1), ff(c2,2)])
df
Out[8]:
In [11]:
ff = pr.features.FeatureFactory()
features = ff.make_features_for_epochs(sig,30,2.0/3.0, 2)
# dfs = [ff(t,s) for t, s in sig.embed_seq(30,2.0/3.0 )]
# features = pd.concat(dfs)
features.shape
In [8]:
plot(features["welch_median"], features["power_sd"],".")
pl.figure()
plot(features["hjorth_activity"],"-")
pl.figure()
plot(features["nl_hurstExp_fast"], features["welch_median"],".")
pl.figure()
plot(features["entropy_spectral"], features["entropy_svd"],".")
pl.figure()
plot(features["welch_skew"], features["welch_median"],".")
pl.figure()
plot(features["welch_median"], features["welch_mean"],".")
pl.figure()
In [ ]:
pl.hist(np.array(features["welch_median"]), bins=100)
pass
In [ ]:
from sklearn import cluster
In [ ]:
f = np.array(features[["welch_median","welch_sd","welch_kurtosis","welch_skew"]] )
km = cluster.KMeans(2)
fit = km.fit(f)
In [ ]:
fit
In [ ]:
p = km.predict(f)
color_map = ["k","b","r","g"]
pl.figure()
for i, (c, x, y) in enumerate(zip(p, features["welch_median"], features["welch_sd"])):
pl.plot(x, y,'.',color=color_map[c])
pl.figure()
for i, (c, x, y) in enumerate(zip(p, features["welch_median"], features["welch_kurtosis"])):
pl.plot(x, y,'.',color=color_map[c])
pl.figure()
for i, (c, x, y) in enumerate(zip(p, features["welch_skew"], features["welch_kurtosis"])):
pl.plot(x, y,'.',color=color_map[c])
In [ ]:
In [ ]: