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('..')
In [120]:
target = 'Dog_4'
data_type = 'test' # preictal interictal, test
In [121]:
channel = 0
get the first segment chain
In [122]:
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]
try:
sequence = data[k]['sequence'][0,0][0,0]
except:
sequence = 1
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:
if Fs > 400:
data_offset = d[:,:1].mean(axis=-1) - all_data[:,-1:].mean(axis=-1)
d -= data_offset.reshape(-1,1)
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[122]:
In [123]:
print 'next segment', segment, last_sequence
In [124]:
int(all_data.shape[1]*399.61/Fs + 0.5)
Out[124]:
In [125]:
all_data.shape
Out[125]:
In [126]:
5000/399.61
Out[126]:
In [127]:
Nchannels = 4 # d.shape[0]
for i in range(Nchannels):
pl.subplot(Nchannels,1,i+1)
pl.plot(all_data[i,:int(12.5*40000)])
pl.title(channels[i])
pl.gcf().set_size_inches(14,8);
10 min windw
In [128]:
Nfs = int(Fs*data_length_sec+0.5)
N = int(399.61*data_length_sec)
Nfs, N
Out[128]:
In [129]:
data = all_data[:,:Nfs]
data.shape, data[:10]
Out[129]:
In [130]:
print data.mean(axis=-1)
data = data - data.mean(axis=-1).reshape(-1,1)
In [131]:
e = np.zeros(data.shape[0])
q = int(Fs/60.+0.5)
for i in range(q):
s = np.mean(data[:,(np.arange(i,Nfs,Fs/60.)).astype(int)], axis=-1)
e += s*s
e = np.mean(np.sqrt(e/q))
e
Out[131]:
In [132]:
from scipy.signal import resample, hann
if Fs > 410:
if e > 2:
notchwidth = 8.
notchwidths = notchwidth/2./Fs
notchfreq = 60.
notchfreqs = notchfreq/Fs
import numpy.fft
fftfreq = np.abs(numpy.fft.fftfreq(data.shape[-1]))
notch = np.ones(Nfs)
for h in range(1,int((Fs/2.)/notchfreq+0.5)):
notch[np.abs(fftfreq - h*notchfreqs) < notchwidths] = 0.
else:
notch = None
data = resample(data, 239766, axis=-1, window=notch)
F = 399.61
else:
F = Fs
In [133]:
if notch:
pl.plot(fftfreq, notch)
pl.gca().set_xlim(0,4*notchfreq/Fs)
In [134]:
data.shape, data[:10]
Out[134]:
In [135]:
WINDOW=int(F*1.) # 10 sec window
# power of 2 FFT
FFT_SIZE=1024
while FFT_SIZE < WINDOW*2:
FFT_SIZE*=2
Pxx, freqs, bins, im = pl.specgram(data[channel,:].astype(float), NFFT=WINDOW, Fs=F, noverlap=WINDOW/2, pad_to=FFT_SIZE)# , detrend='linear'
#pl.gca().set_ylim((0,200.))
pl.gcf().set_size_inches(18,8);
#pl.tight_layout();
pl.autoscale(tight=True);
data.shape, WINDOW
Out[135]:
In [136]:
pl.plot(freqs[20:1500],Pxx.mean(axis=-1)[20:1500])
Out[136]:
In [197]:
Pxx.shape
Out[197]:
In [ ]: