In [3]:
%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 [4]:
import sys
sys.path.append('..')
In [54]:
import scipy.io
means = None
stds = None
jumps = None
for target in ['Dog_1']: #['Dog_1', 'Dog_2', 'Dog_3', 'Dog_4', 'Dog_5', 'Patient_1', 'Patient_2']:
for data_type in ['preictal', 'interictal', 'test']:
print target, data_type
first = True
segment = 1 # start segment
last_data = last_sequence = last_data_length_sec = last_Fs = last_channels = last_d_shape = None
nsegments = 0
while True:
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)
# for k1 in data.keys():
# if k1 != k and data[k1]:
# print data[k1],
# print
data_length_sec = data[k]['data_length_sec'][0,0][0,0]
try:
sequence = data[k]['sequence'][0,0][0,0]
except:
sequence = 1 # test data
Fs = float(data[k]['sampling_frequency'][0,0][0,0])
channels = [t[0] for t in data[k]['channels'][0,0][0]]
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
segment_mean = d.mean(axis=-1)
means = segment_mean if means is None else np.hstack((means, segment_mean))
segment_std = d.std(axis=-1)
stds = segment_std if stds is None else np.hstack((stds, segment_std))
if nsegments == 0:
last_data = d
last_sequence = sequence
N = d.shape[1]
nsegments = 1
elif last_sequence+1 == sequence:
assert N == d.shape[1]
jump = d[:,0] - last_data[:,-1]
jumps = jump if jumps is None else np.hstack((jumps, jump))
# break
last_data = d
last_sequence = sequence
nsegments += 1
else:
nsegments = 0
segment += 1
if first:
print data_length_sec, sequence, Fs,nsegments
first = False
In [55]:
d.shape
Out[55]:
In [49]:
len(jumps)
Out[49]:
note that the means over all segments and channels are between $-0.5$ to $+0.5$ which looks like an integer mean was removed
In [50]:
pl.hist(means);
In [51]:
pl.hist(stds,bins=50);
In [52]:
pl.hist(jumps,bins=50);
In [53]:
jumps.min(),jumps.max()
Out[53]:
In [ ]: