if the testing data was taking from small number of events then we can find the segments by measuring the distance between first samples of one segment and last samples of other segment and find the best assignment. We can then combine the scores given to each segment and even use overlap segments.


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

Extarct the first and last W data samples of each test segment


In [3]:
W = 10

In [83]:
data_type = 'test' # preictal interictal, test

In [126]:
import scipy.io

edges = []
for target in ['Patient_2']: #'Dog_1', 'Dog_2', 'Dog_3', 'Dog_4', 'Dog_5', 'Patient_1', 'Patient_2']:
    for segment in range(1,1000000):
        fname = '../seizure-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]
            edges.append((d[:,:W].astype(float),d[:,-W:].astype(float)))
        except:
            break

find the closest pair of segments in the test data


In [127]:
def mydist(j1, j2):
    return -2.*np.dot(j1,j2)/(np.dot(j1,j1) + np.dot(j2,j2))
#     return -np.dot(j1,j2)/np.sqrt(np.dot(j1,j1)*np.dot(j2,j2))

In [128]:
N = len(edges)
d = np.zeros((N,N))
for i in range(N):
    ei = edges[i]
    for j in range(i+1,N):
        ej = edges[j]
        d[i,j] = mydist(ei[1][:,-1],ej[0][:,0])
        d[j,i] = mydist(ej[1][:,-1],ei[0][:,0])

In [129]:
for i in range(N):
    d[i,i] = 1

In [130]:
np.save('../data-cache/%s-test-jump-distance'%target, d)

In [131]:
ord = np.unravel_index(d.ravel().argsort(),d.shape)

In [132]:
N/6


Out[132]:
25

In [133]:
pl.hist(d[ord],bins=50);



In [134]:
m = 0
s1,s2=np.array(ord).T[m]
s1,s2,d[s1,s2]


Out[134]:
(102, 24, -0.99972249873083607)

In [135]:
fuse = np.concatenate((edges[s1][1][:,:], edges[s2][0][:,:]),axis=-1)

In [136]:
fuse = fuse.squeeze()
fuse.shape


Out[136]:
(24, 20)

In [137]:
Nbase = 0
Nchannels = fuse.shape[0]

for i in range(Nchannels):
    pl.subplot(Nchannels,1,i+1)
    channel = i+Nbase
    pl.plot(fuse[channel,:])
pl.gcf().set_size_inches(14,16);



In [138]:
N/6


Out[138]:
25

In [139]:
next_segment = [-1]*N
previous_segment = [-1]*N
for i,(s1,s2) in enumerate(np.array(ord).T):
    dist = d[s1,s2]
    if next_segment[s1] != -1:
        print i,'right conflict',dist
        break
    if previous_segment[s2] != -1:
        print i,'left conflict',dist
        break
    next_segment[s1] = s2
    previous_segment[s2] = s1


1 right conflict -0.999516626264

In [46]:
s1,s2


Out[46]:
(98, 153)

In [47]:
s22 = next_segment[s1]
s1,s22


Out[47]:
(98, -1)

In [48]:
from scipy import signal
def plfuse(s1,s2,channel=0):
    fuse = np.concatenate((edges[s1][1][:,:], edges[s2][0][:,:]),axis=-1).squeeze()

    pl.subplot(3,1,1)
    f, Pxx_den = signal.periodogram(edges[s1][1][channel,:])
    pl.plot(f, Pxx_den);

    pl.subplot(3,1,2)
    f, Pxx_den = signal.periodogram(edges[s2][0][channel,:])
    pl.plot(f, Pxx_den);

    f, Pxx_den = signal.periodogram(fuse[channel,:])
    pl.subplot(3,1,3)
    pl.plot(f, Pxx_den);

In [49]:
plfuse(s1,s2)



In [50]:
plfuse(s1,s22)



In [ ]: