In [ ]:
%pylab inline
import numpy as np
from seizures.features.FeatureExtractBase import FeatureExtractBase
from statsmodels.tsa.vector_ar.var_model import VAR
from seizures.features.ARFeatures import ARFeatures
import scipy.signal
from itertools import tee, izip
from matplotlib import pyplot as plt

from seizures.data.DataLoader import DataLoader, EEGData

In [ ]:
data_path = "/nfs/data3/kaggle_seizure/scratch/Stiched_data/Dog_1/"
feature_extractor = ARFeatures()
eeg_data = EEGData(data_path+'Dog_1_ictal_segment_1')
instances = eeg_data.get_instances()
X = instances[1].eeg_data
print X

plt.plot(X.T)

In [ ]:
data = X
q = 2 # ds facto		
data_ds	= scipy.signal.decimate(data, q)
print data_ds.shape
def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return izip(a, b)

#building pairwise cross correlations
N_time = data_ds.shape[1]
N_channel=data_ds.shape[0]
print N_channel, N_time
M = np.zeros( (N_channel*(N_channel-1)/2, 2*N_time-1) )
i = 0
for v in range(N_channel):
    for w in range(0,v):
        M[i,:] = np.correlate(data_ds[v],data_ds[w],mode="full")
        i+=1

print M.shape
#compution SVD
U, s, V = np.linalg.svd(M, full_matrices=True)

print V.shape
print np.dot(V[0,:],V[1,:])
print np.dot(V[0,:],V[1,:])

fig = plt.figure(figsize=(10,20))
plt.imshow(data_ds)
plt.show()
plt.plot(s)
plt.show()
plt.imshow(M)
plt.show()
plt.imshow(V)
plt.show()

N_pairs = M.shape[0]

S=np.zeros((N_pairs,M.shape[1]))

S[0:N_pairs,0:N_pairs]=np.diag(s)
plt.imshow( np.dot(np.dot(U,S),V))
plt.show()


N_singular_vec = 10
N_fft = 30

sgn=sign(V[:,(V.shape[1]-1)/2])
V_sgn=np.dot(np.diag(sgn),V)
V_fft = np.real(np.fft.fft(V_sgn[0:N_singular_vec-1,:]))
#V_fft = np.real(np.fft.fft(V[0:N_singular_vec-1,:]))
#[0:N_fft-1,:]

plt.plot(V_sgn[0:N_singular_vec-1,:].T)
plt.show()

features = V_fft[:,0:N_fft-1]

plt.plot(V_fft[:,0:N_fft-1].T)
plt.show()

plt.plot(np.flipud(V_fft[:,-15:-1].T))
plt.show()

#final extraction

In [ ]:
N_singular_vec = 10
N_fft = 30

data = X
q = 2 # ds facto		
data_ds	= scipy.signal.decimate(data, q)
#building pairwise cross correlations
N_time = data_ds.shape[1]
N_channel=data_ds.shape[0]
M = np.zeros( (N_channel*(N_channel-1)/2, 2*N_time-1) )
i = 0
for v in range(N_channel):
    for w in range(0,v):
        M[i,:] = np.correlate(data_ds[v],data_ds[w],mode="full")
        i+=1
print M.shape
#compution SVD
U, s, V = np.linalg.svd(M, full_matrices=True)

sgn=sign(V[:,(V.shape[1]-1)/2])
V_sgn=np.dot(np.diag(sgn),V)
V_fft = np.real(np.fft.fft(V_sgn[0:N_singular_vec-1,:]))
features = V_fft[:,0:N_fft-1]

In [75]:
2*N_time-1


Out[75]:
399

In [ ]: