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

Read precomputed features

uncommoent the relevant pipeline in ../seizure_detection.py and run

cd ..
./doall data

or

./doall td
./doall tt

In [3]:
FEATURES = 'gen-8_allbands2-usf-w60-b0.2-b4-b8-b12-b30-b70'

In [4]:
nbands = 0
nwindows = 0
for p in FEATURES.split('-'):
    if p[0] == 'b':
        nbands += 1
    elif p[0] == 'w':
        nwindows = int(p[1:])

nbands -= 1
nbands, nwindows


Out[4]:
(5, 60)

In [5]:
NUNITS = 1

In [6]:
from common.data import CachedDataLoader
cached_data_loader = CachedDataLoader('../data-cache')

In [7]:
def read_data(target, data_type):
    fname = 'data_%s_%s_%s'%(data_type,target,FEATURES)
    print fname
    return cached_data_loader.load(fname,None)

In [43]:
def process(X, percentile=None,nunits=NUNITS,band=4):
    N, Nf = X.shape
    print '# samples',N,'# power points', Nf
    nchannels = Nf / (nbands*nwindows)
    print '# channels', nchannels
    
    newX = []
    for i in range(N):
        nw = nwindows//nunits
        windows = X[i,:].reshape((nunits,nw,-1))
        if band is not None:
            windows = windows[:,:,range(band,windows.shape[2],nbands)]
        if percentile is not None:
            windows = np.sort(windows, axis=1)
            windows = np.concatenate([windows[:,int(p*nw),:] for p in percentile], axis=-1)
        newX.append(windows.ravel())
    newX = np.array(newX)

    return newX

In [44]:
from sklearn import preprocessing
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

scale = StandardScaler()

min_max_scaler = preprocessing.MinMaxScaler() # scale features to be [0..1] which is DBN requirement

In [45]:
for itarget, target in enumerate(['Dog_1', 'Dog_2', 'Dog_3', 'Dog_4', 'Dog_5', 'Patient_1', 'Patient_2']):
    pdata = read_data(target, 'preictal') # positive examples
    ndata = read_data(target, 'interictal') # negative examples
    tdata = read_data(target, 'test') # test examples
    Xp = pdata.X
    Np, NF = Xp.shape
    Xp = process(Xp)
    print 'P',Xp.mean(),Xp.std()
    
    Xn = ndata.X
    Nn = Xn.shape[0]
    Xn = process(Xn)
    print 'N',Xn.mean(),Xn.std()
    
    Xt = tdata.X
    Nt = Xt.shape[0]
    Xt = process(Xt)
    print 'T',Xt.mean(),Xt.std()
   
    # normalize train and test together (allowed)
    X = scale.fit_transform(np.concatenate((Xp, Xn, Xt)))
    X = np.clip(X,-3,3)
    X = min_max_scaler.fit_transform(X)
    Xp = X[:Np,:]
    Xn = X[Np:(Np+Nn),:]
    Xt = X[(Np+Nn):,:]
        
    pl.subplot(4,2,itarget+1)
    pl.hist(Xp.ravel(),bins=50,normed=True)
    pl.hist(Xn.ravel(),bins=50,alpha=0.5,normed=True)
    pl.hist(Xt.ravel(),bins=50,alpha=0.3,normed=True);


data_preictal_Dog_1_gen-8_allbands2-usf-w60-b0.2-b4-b8-b12-b30-b70
data_interictal_Dog_1_gen-8_allbands2-usf-w60-b0.2-b4-b8-b12-b30-b70
data_test_Dog_1_gen-8_allbands2-usf-w60-b0.2-b4-b8-b12-b30-b70
# samples 184 # power points 4800
# channels 16
P 6.02509709696 0.331752199925
# samples 480 # power points 4800
# channels 16
N 5.98886455133 0.314228578872
# samples 502 # power points 4800
# channels 16
T 6.03814863311 0.336207912619
data_preictal_Dog_2_gen-8_allbands2-usf-w60-b0.2-b4-b8-b12-b30-b70
data_interictal_Dog_2_gen-8_allbands2-usf-w60-b0.2-b4-b8-b12-b30-b70
data_test_Dog_2_gen-8_allbands2-usf-w60-b0.2-b4-b8-b12-b30-b70
# samples 322 # power points 4800
# channels 16
P 6.04860279883 0.301324429127
# samples 500 # power points 4800
# channels 16
N 5.87524987884 0.336356837952
# samples 1000 # power points 4800
# channels 16
T 5.88568475309 0.343104860578
data_preictal_Dog_3_gen-8_allbands2-usf-w60-b0.2-b4-b8-b12-b30-b70
data_interictal_Dog_3_gen-8_allbands2-usf-w60-b0.2-b4-b8-b12-b30-b70
data_test_Dog_3_gen-8_allbands2-usf-w60-b0.2-b4-b8-b12-b30-b70
# samples 552 # power points 4800
# channels 16
P 6.02050505522 0.259973583548
# samples 1440 # power points 4800
# channels 16
N 5.96504608162 0.411540362205
# samples 907 # power points 4800
# channels 16
T 5.95505777921 0.446728152071
data_preictal_Dog_4_gen-8_allbands2-usf-w60-b0.2-b4-b8-b12-b30-b70
data_interictal_Dog_4_gen-8_allbands2-usf-w60-b0.2-b4-b8-b12-b30-b70
data_test_Dog_4_gen-8_allbands2-usf-w60-b0.2-b4-b8-b12-b30-b70
# samples 737 # power points 4800
# channels 16
P 5.57427877297 0.251072149032
# samples 804 # power points 4800
# channels 16
N 5.56952219729 0.274401018881
# samples 990 # power points 4800
# channels 16
T 5.6000850071 0.250126109339
data_preictal_Dog_5_gen-8_allbands2-usf-w60-b0.2-b4-b8-b12-b30-b70
data_interictal_Dog_5_gen-8_allbands2-usf-w60-b0.2-b4-b8-b12-b30-b70
data_test_Dog_5_gen-8_allbands2-usf-w60-b0.2-b4-b8-b12-b30-b70
# samples 230 # power points 4500
# channels 15
P 5.92826831464 0.278521132295
# samples 450 # power points 4500
# channels 15
N 5.9480261183 0.32075653254
# samples 191 # power points 4500
# channels 15
T 5.94585753847 0.435330438824
data_preictal_Patient_1_gen-8_allbands2-usf-w60-b0.2-b4-b8-b12-b30-b70
data_interictal_Patient_1_gen-8_allbands2-usf-w60-b0.2-b4-b8-b12-b30-b70
data_test_Patient_1_gen-8_allbands2-usf-w60-b0.2-b4-b8-b12-b30-b70
# samples 138 # power points 4500
# channels 15
P 3.61719368674 0.496425998609
# samples 50 # power points 4500
# channels 15
N 3.43675866827 0.600261020395
# samples 195 # power points 4500
# channels 15
T 3.88938284754 0.49122969672
data_preictal_Patient_2_gen-8_allbands2-usf-w60-b0.2-b4-b8-b12-b30-b70
data_interictal_Patient_2_gen-8_allbands2-usf-w60-b0.2-b4-b8-b12-b30-b70
data_test_Patient_2_gen-8_allbands2-usf-w60-b0.2-b4-b8-b12-b30-b70
# samples 138 # power points 7200
# channels 24
P 3.82128485251 0.499804941626
# samples 42 # power points 7200
# channels 24
N 3.68356459524 0.543461590136
# samples 150 # power points 7200
# channels 24
T 4.06574033709 0.525487586722

In [37]:


In [ ]: