In [33]:
%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 [34]:
import sys
sys.path.append('..')

In [35]:
target = 'Dog_1'
data_type = 'interictal' # preictal interictal, test

according to 140905-feature-importance the most important cannel is


In [36]:
channel = 3

get the first segment chain


In [46]:
import scipy.io

nchain = 0 # which chain you want

all_data = last_sequence = last_data_length_sec = last_Fs = last_channels = last_d_shape = None
for segment in range(10000):
    fname = '../seizure-data/%s/%s_%s_segment_%04d.mat'%(target,target,data_type,segment+1)
    try:
        data = scipy.io.loadmat(fname)
    except:
        break
    k = '%s_segment_%d'%(data_type,segment+1)
    data_length_sec = data[k]['data_length_sec'][0,0][0,0]
    sequence = data[k]['sequence'][0,0][0,0]
    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]
    
    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:
        all_data = d.astype(float)
    elif last_sequence < sequence:
        all_data = np.hstack((all_data,d.astype(float)))
    else:
        if nchain == 0:
            break
        nchain -= 1
        all_data = d.astype(float)
    last_sequence = sequence
all_data = all_data.copy()
data_length_sec, sequence, Fs, all_data.shape


Out[46]:
(600, 1, 399.609756097561, (16, 1438596))

In [47]:
print 'next segment', segment, last_sequence


next segment 6 6

In [48]:
Nchannels = 4 # d.shape[0]
for i in range(Nchannels):
    pl.subplot(Nchannels,1,i+1)
    pl.plot(all_data[i,:500000])
    pl.title(channels[i])
pl.gcf().set_size_inches(14,8);



In [49]:
Y = np.abs(np.fft.rfft(all_data[channel,:]))

In [50]:
Y[60*data_length_sec]/np.std(all_data[0,:])


Out[50]:
1376.594627613784

In [51]:
Fmax = Fs/2
pl.plot(np.linspace(0,Fmax,Fmax*data_length_sec),Y[:Fmax*data_length_sec])


Out[51]:
[<matplotlib.lines.Line2D at 0x12ccfa090>]

In [60]:
NFFT=1024
S = 1438596//6
Pxx, freqs, bins, im = pl.specgram(all_data[0,:], NFFT=NFFT, Fs=Fs, pad_to=NFFT*2, noverlap=NFFT/2)
#pl.gca().set_ylim((0,200.))
pl.gcf().set_size_inches(18,8);
#pl.tight_layout();
pl.autoscale(tight=True);
all_data.shape


Out[60]:
(16, 1438596)

In [58]:
pl.plot(freqs,Pxx.mean(axis=-1))


Out[58]:
[<matplotlib.lines.Line2D at 0x12a974250>]

In [44]: