-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_model.py
executable file
·188 lines (154 loc) · 8.24 KB
/
train_model.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import numpy as np
import scipy.signal as signal
import os
import math
import pandas as pd
import json
import SWS_utils
import sys
def train_first_model(epochlen, fsd, emg_flag, animal, model_dir, mod_name):
# Data available for training for the first time ("#acquisition:#hr")
raw_data = {
1:0,
2:0,
4:0,
22:0,
22:1,
# 23:0,
# 24:0,
31:0,
31:1,
31:2
}
# Using EMG data by default. (No video for now)
final_features = ['Animal_Name', 'animal_num', 'State', 'delta_pre', 'delta_pre2',
'delta_pre3', 'delta_post', 'delta_post2', 'delta_post3', 'EEGdelta', 'theta_pre',
'theta_pre2', 'theta_pre3',
'theta_post', 'theta_post2', 'theta_post3', 'EEGtheta', 'EEGalpha', 'EEGbeta',
'EEGgamma', 'EEGnarrow', 'nb_pre', 'delta/theta', 'EEGfire', 'EEGamp', 'EEGmax',
'EEGmean', 'EMG']
data = np.empty((len(final_features),0))
i = 0
for a in raw_data:
print(i)
i = i+1
h = raw_data[a]
print('Handling acquisition '+str(a)+' hour '+str(h))
print('Loading EEG and EMG....')
data_dir = os.path.join(model_dir, 'initial_data/')
downsampEEG = np.load(os.path.join(data_dir, 'downsampEEG_Acq' + str(a) + '_hr' + str(h) + '.npy'))
if emg_flag:
downsampEMG = np.load(os.path.join(data_dir, 'downsampEMG_Acq' + str(a) + '_hr' + str(h) + '.npy'))
acq_len = np.size(downsampEEG) / fsd # fs: sampling rate, fsd: downsampled sampling rate
hour_segs = math.ceil(acq_len / 3600) # acq_len in seconds, convert to hours
print('This acquisition has ' + str(hour_segs) + ' segments.')
for h in np.arange(hour_segs):
this_eeg = np.load(os.path.join(data_dir, 'downsampEEG_Acq' + str(a) + '_hr' + str(h) + '.npy'))
if emg_flag:
this_emg = np.load(os.path.join(data_dir, 'downsampEMG_Acq' + str(a) + '_hr' + str(h) + '.npy'))
# chop off the remainder that does not fit into the 4s epoch
seg_len = np.size(this_eeg) / fsd
nearest_epoch = math.floor(seg_len / epochlen)
new_length = int(nearest_epoch * epochlen * fsd)
this_eeg = this_eeg[0:new_length]
# os.chdir(extracted_dir)
if emg_flag:
EMGamp, EMGmax, EMGmean = SWS_utils.generate_signal(this_emg, epochlen, fsd)
else:
EMGamp = False
EEGamp, EEGmax, EEGmean = SWS_utils.generate_signal(this_eeg, epochlen, fsd)
EEGdelta, idx_delta = SWS_utils.bandPower(0.5, 4, this_eeg, epochlen, fsd)
EEGtheta, idx_theta = SWS_utils.bandPower(4, 8, this_eeg, epochlen, fsd)
EEGalpha, idx_alpha = SWS_utils.bandPower(8, 12, this_eeg, epochlen, fsd)
EEGbeta, idx_beta = SWS_utils.bandPower(12, 30, this_eeg, epochlen, fsd)
EEGgamma, idx_gamma = SWS_utils.bandPower(30, 80, this_eeg, epochlen, fsd)
EEG_broadtheta, idx_broadtheta = SWS_utils.bandPower(2, 16, this_eeg, epochlen, fsd)
EEGfire, idx_fire = SWS_utils.bandPower(4, 20, this_eeg, epochlen, fsd)
EEGnb = EEGtheta / EEG_broadtheta # narrow-band theta
delt_thet = EEGdelta / EEGtheta # ratio; esp. important
EEGdelta = SWS_utils.normalize(EEGdelta)
EEGalpha = SWS_utils.normalize(EEGalpha)
EEGbeta = SWS_utils.normalize(EEGbeta)
EEGgamma = SWS_utils.normalize(EEGbeta)
EEGnb = SWS_utils.normalize(EEGnb)
EEGtheta = SWS_utils.normalize(EEGtheta)
EEGfire = SWS_utils.normalize(EEGfire)
delt_thet = SWS_utils.normalize(delt_thet)
# frame shifting
delta_post, delta_pre = SWS_utils.post_pre(EEGdelta, EEGdelta)
theta_post, theta_pre = SWS_utils.post_pre(EEGtheta, EEGtheta)
delta_post2, delta_pre2 = SWS_utils.post_pre(delta_post, delta_pre)
theta_post2, theta_pre2 = SWS_utils.post_pre(theta_post, theta_pre)
delta_post3, delta_pre3 = SWS_utils.post_pre(delta_post2, delta_pre2)
theta_post3, theta_pre3 = SWS_utils.post_pre(theta_post2, theta_pre2)
nb_post, nb_pre = SWS_utils.post_pre(EEGnb, EEGnb)
animal_name = np.full(np.size(delta_pre), animal)
# Note: The second parameter depends on the actual animal name. For example, if the animal is "KNR00004", we
# should use "animal[3:]" for "00004"; if the animal is "jaLC_FLiPAKAREEGEMG004", we should use "animal[19:]"
# for "004".
animal_num = np.full(np.shape(animal_name), int(animal[19:]))
# os.chdir(model_dir)
# if emg_flag:
# clf = load(mod_name + '_EMG.joblib')
# else:
# clf = load(mod_name + '_no_EMG.joblib')
# feature list
FeatureList = []
nans = np.full(np.shape(animal_name), np.nan)
if emg_flag:
FeatureList = [animal_num, delta_pre, delta_pre2, delta_pre3, delta_post, delta_post2, delta_post3,
EEGdelta,
theta_pre, theta_pre2, theta_pre3, theta_post, theta_post2, theta_post3,
EEGtheta, EEGalpha, EEGbeta, EEGgamma, EEGnb, nb_pre, delt_thet, EEGfire, EEGamp, EEGmax,
EEGmean, EMGamp]
else:
FeatureList = [animal_num, delta_pre, delta_pre2, delta_pre3, delta_post, delta_post2, delta_post3,
EEGdelta,
theta_pre, theta_pre2, theta_pre3, theta_post, theta_post2, theta_post3,
EEGtheta, EEGalpha, EEGbeta, EEGgamma, EEGnb, nb_pre, delt_thet, EEGfire, EEGamp, EEGmax,
EEGmean, nans]
FeatureList_smoothed = []
for f in FeatureList:
FeatureList_smoothed.append(signal.medfilt(f, 5))
Features = np.column_stack((FeatureList_smoothed))
Features = np.nan_to_num(Features)
inital_data_dir = model_dir+"initial_data/"
State = np.load(os.path.join(inital_data_dir, 'StatesAcq' + str(a) + '_hr' + str(h) + '.npy'))
print(len(State))
data_addition = np.vstack(
[animal_name, animal_num, State, delta_pre, delta_pre2, delta_pre3, delta_post,
delta_post2, delta_post3, EEGdelta, theta_pre, theta_pre2, theta_pre3, theta_post, theta_post2,
theta_post3,
EEGtheta, EEGalpha, EEGbeta, EEGgamma, EEGnb, nb_pre, delt_thet, EEGfire, EEGamp, EEGmax,
EEGmean])
if np.size(np.where(pd.isnull(EMGamp))[0]) > 0:
EMGamp[np.isnan(EMGamp)] = 0
data_addition = np.vstack([data_addition, EMGamp])
data = np.hstack([data, data_addition])
df = pd.DataFrame(columns=final_features, data=data.T)
Sleep_Model = SWS_utils.update_sleep_model(model_dir, mod_name, df)
jobname, x_features = SWS_utils.load_joblib(final_features, emg_flag, mod_name)
Sleep_Model = Sleep_Model.drop(index=np.where(Sleep_Model['EMG'].isin(['nan']))[0])
SWS_utils.retrain_model(Sleep_Model, x_features, model_dir, jobname)
if __name__ == "__main__":
# execute only if run as a script
args = sys.argv
assert args[0] == 'train_model.py'
if len(args) < 2:
print(
"You need to specify the path of your Score_Settings.json. For instance, run `python train_model.py /home/ChenLab_Sleep_Scoring/Score_Settings.json`.")
elif len(args) > 2:
print(
"You only need to specify the path of your Score_Settings.json. For instance, run `python train_model.py /home/ChenLab_Sleep_Scoring/Score_Settings.json`.")
else:
with open(args[1], 'r') as f:
d = json.load(f)
extracted_dir = str(d['savedir'])
epochlen = int(d['epochlen'])
fsd = int(d['fsd'])
emg_flag = int(d['emg'])
vid_flag = int(d['vid'])
model_dir = str(d['model_dir'])
animal = str(d['animal'])
mod_name = str(d['mod_name'])
train_first_model(epochlen, fsd, emg_flag, animal, model_dir, mod_name)