In [1]:
%matplotlib inline
from matplotlib import pylab as pl
import cPickle as pickle
import pandas as pd
import numpy as np
import os
import random

In [2]:
import sys
sys.path.append('..')

In [3]:
!ls -l /vol2/data/


total 512
drwxrwxr-x 2 ubuntu ubuntu  69632 Aug 21 20:57 Dog_1
drwxrwxr-x 2 ubuntu ubuntu  90112 Aug 21 19:16 Dog_2
drwxrwxr-x 2 ubuntu ubuntu 139264 Aug 22 18:14 Dog_3
drwxrwxr-x 2 ubuntu ubuntu 126976 Aug 22 18:10 Dog_4
drwxrwxr-x 2 ubuntu ubuntu  40960 Aug 21 17:30 Dog_5
drwxrwxr-x 2 ubuntu ubuntu  20480 Aug 21 20:09 Patient_1
drwxrwxr-x 2 ubuntu ubuntu  20480 Aug 21 20:50 Patient_2

In [4]:
targets = ['Dog_1', 'Dog_2', 'Dog_3', 'Dog_4', 'Dog_5', 'Patient_1', 'Patient_2']

In [5]:
data_types = ['preictal', 'interictal', 'test']

In [6]:
target = targets[-1]
data_type = data_types[-1]

In [7]:
import scipy.io

ld = []
start = 1
for segment in range(start,1000000):
    print segment
    fname = '/vol2/data/%s/%s_%s_segment_%04d.mat'%(target,target,data_type,segment)
    try:
        data = scipy.io.loadmat(fname)
        k = '%s_segment_%d'%(data_type,segment)
        d = data[k]['data'][0,0]
        try:
            sequence = data[k]['sequence'][0,0][0,0]
        except:
            sequence = -1 # test data

        if str(d.dtype)=='int32':
            mask = np.abs(d) < 5000
        print sequence, str(d.dtype), np.std(d,axis=-1)
        break
        ld.append(d)
#         if segment == start+6:
#             break
    except:
        break


1
-1 int32 [ 1838.1697136   1856.4808117   1707.65446307  2073.54280569  2122.28749349
  2069.87491901  1914.98810589  1893.14812928  2673.19547638  2710.01879429
  2630.47860132  2662.1464223   2668.09612018  2659.46453313  2646.50684396
  2671.61148503  2645.36988517  2640.86556984  2671.97818948  2639.4673828
  2652.83860578  2662.35483781  2638.88633509  2614.89188297]

In [8]:
mask = np.abs(d) < 5000

In [9]:
pl.hist(d[3,mask[3,:]],bins=50);



In [11]:
pl.plot(d[3,:])


Out[11]:
[<matplotlib.lines.Line2D at 0x7fa59ec7e190>]

In [10]:
pl.plot((d[3,:])[367619:367630])


Out[10]:
[<matplotlib.lines.Line2D at 0x7fa59ec113d0>]

In [12]:
x = d[3,:].copy()
len(x)


Out[12]:
3000000

In [14]:
s = np.zeros(len(x)-1)
dif = x[1:] - x[:-1]
pl.hist(np.clip(dif,-20,20),bins=500);



In [112]:
def ExpMovingAverage(values, window):
    weights = np.exp(np.linspace(-1., 0., window))
    weights /= weights.sum()

    # Here, we will just allow the default since it is an EMA
    a =  np.convolve(values, weights)
    a[:window] = a[window]
    w = (window+1)//2
    a = a[w:w+len(values)]
    return a #again, as a numpy array.
sdif = ExpMovingAverage(dif,50)
# mask = (dif > 50) |(dif < -50)
# sdif[mask] = dif[mask]

In [211]:
lasts = x[0]
N = len(dif)
y = np.empty(N+1)
weights = np.exp(np.clip(-np.abs(dif)/20.,-5,-0.05))
y[0] = lasts
for i in xrange(N):
    w = weights[i]
    lasts = lasts*w + (1.-w)*x[i+1]
    y[i+1] = lasts

In [212]:
pl.plot((x-y)[367619-10000:367630+50000])
pl.plot(x[367619-10000:367630+50000]/1000.)
#pl.plot(sdif)


Out[212]:
[<matplotlib.lines.Line2D at 0x7fa543673550>]

In [207]:
np.std((x-y))


Out[207]:
12.424809454869132

In [ ]: