In [1]:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
In [2]:
%matplotlib inline
plt.rcParams['figure.figsize'] = 6, 4.5
plt.rcParams['axes.grid'] = True
plt.gray()
In [3]:
cd ~/repositories/neuroglycerin-data/Dog_1/
In [4]:
import glob
In [5]:
# grab all the filenames
fnames = glob.glob("*")
In [6]:
# test file
t = fnames[0]
print(t)
In [7]:
import scipy.io
In [8]:
matest = scipy.io.loadmat(t)
In [9]:
matest.keys()
Out[9]:
In [10]:
matest['__version__']
Out[10]:
In [11]:
type(matest['test_segment_353'])
Out[11]:
In [12]:
type(matest['test_segment_353'][0,0])
Out[12]:
In [13]:
# weird, try making it an array
data = np.array(matest['test_segment_353'][0,0])
So, now I'm in the situation this guy was in(looks like a decent blog) and apparently I need to just know the right key. Matlab can fuck off so much right now.
In [14]:
data.dtype
Out[14]:
In [15]:
data['data']
Out[15]:
In [16]:
data['sampling_frequency']
Out[16]:
In [17]:
data['channels']
Out[17]:
Writing a function to do the above for arbitrary files:
In [18]:
for k in matest.keys():
print(k)
In [19]:
[k for k in matest.keys() if "segment" in k][0]
Out[19]:
In [27]:
def fuckyoumatlab(fname):
mat = scipy.io.loadmat(fname)
# assuming they'll always have segment in them
# I apologise for this
k = [k for k in mat.keys() if "segment" in k][0]
data = mat[k][0,0]
# I'm turning this into a dictionary
# just try and stop me
datadict = {}
for i in ['data','sampling_frequency']:
datadict[i] = data[i]
return datadict
In [28]:
data = fuckyoumatlab(t)
In [29]:
data.keys()
Out[29]:
In [31]:
data['data'].shape
Out[31]:
In [33]:
data['data']
Out[33]:
In [32]:
plt.plot(data['data'][0,0:100])
Out[32]:
In [36]:
import pylab
In [83]:
x = np.linspace(0,2*np.pi,1000)
In [87]:
s = np.sin(x*100)
In [88]:
w=np.hamming(len(s))
In [89]:
dpsd=pylab.psd(w*s,Fs=1000)
In [71]:
pylab.psd?
In [50]:
data['data'][0,:].astype(np.float)
Out[50]:
In [57]:
fs = data['sampling_frequency'].item()
In [58]:
fs
Out[58]:
In [94]:
w = np.hanning(len(data['data'][0,:].astype(np.float)))
In [97]:
dpsd=pylab.psd(data['data'][0,:].astype(np.float)*w,Fs=fs)
In [99]:
data = fuckyoumatlab(fnames[2])
In [100]:
fs = data['sampling_frequency'].item()
w = np.hanning(len(data['data'][0,:].astype(np.float)))
dpsd=pylab.psd(data['data'][0,:].astype(np.float)*w,Fs=fs)
In [102]:
prek = [k for k in fnames if "preictal" in k][0]
In [104]:
data = fuckyoumatlab(prek)
In [105]:
fs = data['sampling_frequency'].item()
w = np.hanning(len(data['data'][0,:].astype(np.float)))
dpsd=pylab.psd(data['data'][0,:].astype(np.float)*w,Fs=fs)
In [122]:
bandedges = np.linspace(0,data['sampling_frequency'].item()/2,num=20)
In [123]:
bandedges
Out[123]:
In [155]:
import scipy.integrate
In [131]:
d = data['data'][0,:].astype(np.float)
In [152]:
ps = np.abs(np.fft.fft(d*w))**2
In [153]:
freq = np.fft.fftfreq(len(d),d=1/fs)
In [154]:
plt.plot(freq,ps)
Out[154]:
In [166]:
dfreq = {}
for f,p in zip(freq,ps):
dfreq[f] = p
In [167]:
sfreq = np.sort(freq)
In [175]:
r = [f for f in sfreq if f > 10 and f < 100]
scipy.integrate.trapz([dfreq[f] for f in r],r)
Out[175]:
In [ ]:
for l,h in zip(bandedges):