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/
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
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]:
In [10]:
pl.plot((d[3,:])[367619:367630])
Out[10]:
In [12]:
x = d[3,:].copy()
len(x)
Out[12]:
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]:
In [207]:
np.std((x-y))
Out[207]:
In [ ]: