In [1]:
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [2]:
import pyrem as pr
import numpy as np
import pylab as pl
import pandas as pd
from scipy import signal


/home/quentin/.virtualenvs/pyrem/lib/python2.7/site-packages/pandas/io/excel.py:626: UserWarning: Installed openpyxl is not supported at this time. Use >=1.6.1 and <2.0.0.
  .format(openpyxl_compat.start_ver, openpyxl_compat.stop_ver))

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]


1:21:39.996094
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]:
power_kurtosis power_mean power_median power_sd power_skew entropy_spectral entropy_svd nl_hurst hjorth_activity hjorth_complexity hjorth_morbidity welch_kurtosis welch_mean welch_median welch_mode welch_sd welch_skew
1 54.330183 1.049947 0.415976 1.774463 5.431699 0.920754 2.804202 0.407921 1.097359 1.053565 0.143370 2739.321245 11.940402 4.216177 7 23.899414 540.523089
2 101.411483 0.704216 0.244177 1.599462 8.221428 0.935683 2.850287 0.324840 0.495138 1.054220 0.155354 549.769663 11.749631 4.185779 6 17.482154 287.567544

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


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-11-b25a44866c6a> in <module>()
      1 ff = pr.features.FeatureFactory()
----> 2 features = ff.make_features_for_epochs(sig,30,2.0/3.0, 2)
      3 # dfs = [ff(t,s) for t, s in sig.embed_seq(30,2.0/3.0 )]
      4 # features = pd.concat(dfs)
      5 features.shape

/home/quentin/comput/pyrem-git/src/pyrem/features/feature_factory.py in make_features_for_epochs(self, signal, length, lag, processes)
     38         else:
     39             pool = mp.Pool(processes)
---> 40             pool.map(_make_features, time_signals)
     41 
     42         features = pd.concat(dfs)

/usr/lib/python2.7/multiprocessing/pool.pyc in map(self, func, iterable, chunksize)
    249         '''
    250         assert self._state == RUN
--> 251         return self.map_async(func, iterable, chunksize).get()
    252 
    253     def imap(self, func, iterable, chunksize=1):

/usr/lib/python2.7/multiprocessing/pool.pyc in get(self, timeout)
    556             return self._value
    557         else:
--> 558             raise self._value
    559 
    560     def _set(self, i, obj):

AttributeError: 'Signal' object has no attribute '_Signal__sampling_freq'

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()


---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-8-d5b1b96ce28a> in <module>()
      3 plot(features["hjorth_activity"],"-")
      4 pl.figure()
----> 5 plot(features["nl_hurstExp_fast"], features["welch_median"],".")
      6 
      7 pl.figure()

/home/quentin/.virtualenvs/pyrem/lib/python2.7/site-packages/pandas/core/frame.pyc in __getitem__(self, key)
   1682             return self._getitem_multilevel(key)
   1683         else:
-> 1684             return self._getitem_column(key)
   1685 
   1686     def _getitem_column(self, key):

/home/quentin/.virtualenvs/pyrem/lib/python2.7/site-packages/pandas/core/frame.pyc in _getitem_column(self, key)
   1689         # get column
   1690         if self.columns.is_unique:
-> 1691             return self._get_item_cache(key)
   1692 
   1693         # duplicate columns & possible reduce dimensionaility

/home/quentin/.virtualenvs/pyrem/lib/python2.7/site-packages/pandas/core/generic.pyc in _get_item_cache(self, item)
   1050         res = cache.get(item)
   1051         if res is None:
-> 1052             values = self._data.get(item)
   1053             res = self._box_item_values(item, values)
   1054             cache[item] = res

/home/quentin/.virtualenvs/pyrem/lib/python2.7/site-packages/pandas/core/internals.pyc in get(self, item)
   2535 
   2536             if not isnull(item):
-> 2537                 loc = self.items.get_loc(item)
   2538             else:
   2539                 indexer = np.arange(len(self.items))[isnull(self.items)]

/home/quentin/.virtualenvs/pyrem/lib/python2.7/site-packages/pandas/core/index.pyc in get_loc(self, key)
   1154         loc : int if unique index, possibly slice or mask if not
   1155         """
-> 1156         return self._engine.get_loc(_values_from_object(key))
   1157 
   1158     def get_value(self, series, key):

/home/quentin/.virtualenvs/pyrem/lib/python2.7/site-packages/pandas/index.so in pandas.index.IndexEngine.get_loc (pandas/index.c:3353)()

/home/quentin/.virtualenvs/pyrem/lib/python2.7/site-packages/pandas/index.so in pandas.index.IndexEngine.get_loc (pandas/index.c:3233)()

/home/quentin/.virtualenvs/pyrem/lib/python2.7/site-packages/pandas/hashtable.so in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:11148)()

/home/quentin/.virtualenvs/pyrem/lib/python2.7/site-packages/pandas/hashtable.so in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:11101)()

KeyError: 'nl_hurstExp_fast'
<matplotlib.figure.Figure at 0x7f6c52fe8550>

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 [ ]: