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
from collections import defaultdict

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

Compute AUC for each target separatly


In [3]:
import scipy.io
target2metainfo = defaultdict(dict)
for target in ['Dog_1', 'Dog_2', 'Dog_3', 'Dog_4', 'Dog_5', 'Patient_1', 'Patient_2']:
    print target
    
    last_sequence = last_data_length_sec = last_Fs = last_channels = last_d_shape = None
    for data_type in ['preictal', 'interictal', 'test']:
        print data_type
        
        first_sequence = last_sequence = last_data_length_sec = last_Fs = last_channels = last_d_shape = None
        sequences = []
        for segment in range(1,10000):
            fname = '../seizure-data/%s/%s_%s_segment_%04d.mat'%(target,target,data_type,segment)
            try:
                data = scipy.io.loadmat(fname)
            except:
                break
            k = '%s_segment_%d'%(data_type, segment)
            data_length_sec = data[k]['data_length_sec'][0,0][0,0]
            target2metainfo[target]['data_length_sec'] = data_length_sec
            try:
                sequence = data[k]['sequence'][0,0][0,0]
            except:
                sequence = 1 # test data
            Fs = float(data[k]['sampling_frequency'][0,0][0,0])
            target2metainfo[target]['Fs'] = Fs
            channels = [t[0] for t in data[k]['channels'][0,0][0]]
            target2metainfo[target]['channels'] = channels
            d = data[k]['data'][0,0]
#             print segment,data_length_sec,sequence,Fs,d.shape

            assert len(channels) == d.shape[0]
            assert int(Fs*data_length_sec + 0.5) == d.shape[1],int(Fs*data_length_sec + 0.5)
            assert last_data_length_sec is None or last_data_length_sec == data_length_sec
            last_data_length_sec = data_length_sec
            assert last_Fs is None or last_Fs == Fs
            last_Fs = Fs
            assert last_channels is None or all(c1==c2 for c1,c2 in zip(last_channels, channels))
            last_channels = channels
            assert last_d_shape is None or last_d_shape == d.shape
            last_d_shape = d.shape

            if last_sequence is None:
                first_sequence = sequence
            elif last_sequence+1 != sequence:
                sequences.append((first_sequence, last_sequence))
                first_sequence = sequence
            last_sequence = sequence
        sequences.append((first_sequence, last_sequence))
        target2metainfo[target][data_type + '_nsegments'] = segment-1
        target2metainfo[target][data_type + '_sequences'] = sequences
        print segment-1, len(sequences)


Dog_1
preictal
24 4
interictal
480 80
test
502 502
Dog_2
preictal
42 7
interictal
500 84
test
1000 1000
Dog_3
preictal
72 12
interictal
1440 240
test
907 907
Dog_4
preictal
97 17
interictal
804 134
test
990 990
Dog_5
preictal
30 5
interictal
450 75
test
191 191
Patient_1
preictal
18 3
interictal
50 9
test
195 195
Patient_2
preictal
18 3
interictal
42 7
test
150 150

In [3]:


In [5]:
with open('../data-cache/140925-metainfo.pkl','wb') as fp:
    pickle.dump(target2metainfo, fp, -1)

In [9]:
dict((t,len(target2metainfo[t]['channels'])) for t in target2metainfo.iterkeys())


Out[9]:
{'Dog_1': 16,
 'Dog_2': 16,
 'Dog_3': 16,
 'Dog_4': 16,
 'Dog_5': 15,
 'Patient_1': 15,
 'Patient_2': 24}

In [11]:
for target, metainfo in target2metainfo.iteritems():
    pnsegments = metainfo['preictal_nsegments']
    nsegments = pnsegments + metainfo['interictal_nsegments']
    pnsequences = len(metainfo['preictal_sequences'])
    nsequences = pnsequences + len(metainfo['interictal_sequences'])
    print target,100.*pnsegments/nsegments,100.*pnsequences/nsequences


Dog_2 7.74907749077 7.69230769231
Dog_3 4.7619047619 4.7619047619
Dog_1 4.7619047619 4.7619047619
Dog_4 10.7658157603 11.2582781457
Dog_5 6.25 6.25
Patient_2 30.0 30.0
Patient_1 26.4705882353 25.0

In [ ]: