-
Notifications
You must be signed in to change notification settings - Fork 3
/
feature_selection.py
93 lines (87 loc) · 2.92 KB
/
feature_selection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#
# NAME
# feature_selection.py
#
# DESCRIPTION
# see https://github.com/END-team/final-project/wiki
# datasets should be located in the same folder
#
from __future__ import division
import gc
import numpy as np
import matplotlib.pylab as plt
def load_REM_from_file(filename):
"""
load_file takes the file name and reads in the data. It returns an
array containing only REM sleep stage
"""
gc.collect()
data = np.load(filename)
a = data['stages']
d = data['DATA']
srate = data['srate']
j=0
it = np.nditer(a, flags=['f_index'])
while not it.finished:
if( it[0]==5 ):
eeg = pull_epoch(d, srate, it.index)
if(j==0):
samples = eeg
else:
samples = np.hstack((samples,eeg))
j=j+1
it.iternext()
return samples, srate
def pull_epoch(data, srate, epoch_number):
elength = srate * 30.0
startpos = epoch_number * elength
endpos = startpos + elength
eegdata = data[0:,int(startpos):int(endpos)]
return eegdata
def plot_spectrograms(bsl,rec,rate,title):
plt.close()
plt.rc('font', family='Arial')
fig, ax = plt.subplots(nrows=9, ncols=2, sharex='col', sharey='row')
fig.suptitle(title + " - REM stage", fontsize=20)
plt.subplots_adjust(wspace = .05,hspace = 0.4 )
ny_nfft=1024
i=0
plt.tick_params(axis='both', labelsize=8)
while i<9:
Pxx, freq, bins, im = ax[i,0].specgram(bsl[i],NFFT=ny_nfft,Fs=rate)
ax[i,0].set_yticks(np.arange(0, 50, 10))
ax[i,0].set_ylim([0, 40])
if(i==8):
ax[i,0].set_xlabel("Time, seconds", fontsize=10)
ax[i,0].set_ylabel("Freq, Hz", fontsize=8)
ax[i,0].set_title('Baseline, channel:'+str(i+1), fontsize=10)
for label in (ax[i,0].get_xticklabels() + ax[i,0].get_yticklabels()):
label.set_fontname('Arial')
label.set_fontsize(8)
i=i+1
i=0
while i<9:
Pxx, freq, bins, im = ax[i,1].specgram(rec[i],NFFT=ny_nfft,Fs=rate)
ax[i,0].set_yticks(np.arange(0, 50, 10))
ax[i,1].set_ylim([0, 40])
#ax[i,1].set_xlim([0, 10000]) #13000])
if(i==8):
ax[i,1].set_xlabel("Time, seconds", fontsize=10)
#ax[i,1].set_ylabel("Freq, Hz")
ax[i,1].set_title('Recovery, channel:'+str(i+1), fontsize=10)
for label in (ax[i,0].get_xticklabels() + ax[i,0].get_yticklabels()):
label.set_fontname('Arial')
label.set_fontsize(8)
i=i+1
plt.show()
return
##########################
#You can put the code that calls the above functions down here
if __name__ == "__main__":
plt.close('all') #Closes old plots.
j = 1
while j<5:
BSL_REM, srate = load_REM_from_file('S'+str(j)+'_BSL.npz')
REC_REM, srate = load_REM_from_file('S'+str(j)+'_REC.npz')
plot_spectrograms(BSL_REM,REC_REM,srate,'Subject: '+str(j))
j=j+1