-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmuse_dl_model.py
430 lines (280 loc) · 11.4 KB
/
muse_dl_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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import json
import logging
import mlflow
import numpy as np
import os
import pandas as pd
import pickle
import random
import shutil
import sys
import time
from fastapi import FastAPI, Request
from mlflow.tracking import MlflowClient
from mlflow.tracking.artifact_utils import _download_artifact_from_uri
from numbers_parser import Document
from pydantic import BaseModel
from tensorflow.keras.utils import to_categorical
########## Default parameters ##########
logFormatter = logging.Formatter("%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] %(message)s")
rootLogger = logging.getLogger()
# fileHandler = logging.FileHandler("{0}/{1}.log".format(os.getcwd(), "ML"))
# fileHandler.setFormatter(logFormatter)
# rootLogger.addHandler(fileHandler)
consoleHandler = logging.StreamHandler(sys.stdout)
consoleHandler.setFormatter(logFormatter)
rootLogger.addHandler(consoleHandler)
rootLogger.setLevel(logging.INFO)
is_replace_run_g = False # By default, we do not delete the old runs.
female_code_g = 0
male_code_g = 1
########## Tweaked parameters ##########
is_replace_run_g = True
########## MLFlow ##########
def link_mlflow_local():
rootLogger.info("Set tracking url")
mlflow.set_tracking_uri("http://localhost:5000")
def link_mlflow_databricks():
rootLogger.info("Logging Databricks Community Edition")
mlflow.login()
rootLogger.info("Set tracking url")
mlflow.set_tracking_uri("databricks")
def set_mlflow_exp(exp_name):
rootLogger.info("Set experiment to " + exp_name)
mlflow.set_experiment(exp_name)
def set_mlflow_run(exp_name, run_name):
if is_replace_run_g:
del_run_id_by_name(exp_name, run_name)
rootLogger.info("Set run name to " + run_name)
mlflow.set_tag("mlflow.runName", run_name)
def del_run_id_by_name(exp_name, run_name):
run_ids = get_run_id_by_name(exp_name, run_name)
if run_ids is not None:
# print(run_ids)
for run_id in run_ids:
# print(run_id)
mlflow.delete_run(run_id)
rootLogger.info("Delete run success.")
else:
rootLogger.info("Not found run \"" + run_name + "\"")
def get_run_id_by_name(exp_name, run_name):
exps = mlflow.search_experiments()
# print(mlflow.search_experiments())
exp_id = None
for exp in exps:
if exp.name == ("/" + exp_name):
exp_id = exp.experiment_id
break
if exp_id is not None:
try:
run_pds = mlflow.search_runs(experiment_ids=[exp_id])
# print(run_pds.columns.tolist())
# print(run_pds.loc[:,"tags.mlflow.runName"])
matching_rows = run_pds[run_pds["tags.mlflow.runName"] == run_name]
matching_run_ids = matching_rows["run_id"]
return matching_run_ids.tolist()
except:
return None
else:
rootLogger.info(f"Not found {exp_name}.")
return None
def get_model_path(run_id_p, artifact_path_p="model"):
## Initialize the MLflow client
client_l = MlflowClient()
## Construct the full model path
model_path_l = client_l.download_artifacts(run_id_p, artifact_path_p) # This method downloads the model into the Temp directory.
rootLogger.info(f"Model Path: {model_path_l}")
return model_path_l
def get_model(run_id_p):
## Download the model from MLflow server to temp directory.
model_path_l = get_model_path(run_id_p)
rootLogger.info(f"Model was downloaded to {model_path_l}")
## Copy the model to the "model" of the current working directory.
save_model_dir_l = "model"
if os.path.exists("./" + save_model_dir_l):
shutil.rmtree("./" + save_model_dir_l)
rootLogger.info("Delete the old model.")
else:
rootLogger.info(f"Couldn't find ./{save_model_dir_l}.")
shutil.copytree(model_path_l, "./" + save_model_dir_l)
## Restore the model from the path.
model_l = mlflow.tensorflow.load_model("./" + save_model_dir_l)
## Get test accuracy of the model.
sex_dict_l = loadSex()
EEG_dict_l = loadEEGs()
sorted_names_l = sorted(EEG_dict_l.keys())
nb_classes_l = 2 # The number of classs to be predicted.
valid_percentage_l = 20 # The percentage of subjects to be used as a validation set.
test_percentage_l = 20 # The percentage of subjects to be used as a test set.
n_test_l = int(np.floor(len(sorted_names_l)*test_percentage_l / 100))
n_valid_l = int(np.floor(len(sorted_names_l)*valid_percentage_l / 100))
n_train_l = len(sorted_names_l) - (n_valid_l + n_test_l)
rootLogger.info(f"#Train: {n_train_l}, #Valid: {n_valid_l}, #Test: {n_test_l}")
np.random.seed(0)
random.seed(0)
test_names_l = random.sample(sorted_names_l, n_test_l)
train_valid_names_l = sorted([subj for subj in sorted_names_l if subj not in test_names_l])
valid_names_l = random.sample(train_valid_names_l, n_valid_l)
train_names_l = sorted([subj for subj in train_valid_names_l if subj not in valid_names_l])
x_test_l, y_test_l = gen_test(EEG_dict_l, sex_dict_l, test_names_l)
Y_test_l = to_categorical(y_test_l, nb_classes_l)
score_valid_l = model_l.evaluate(x_test_l, Y_test_l, verbose=0)
valid_acc = score_valid_l[1]
rootLogger.info(f"Validate accuracy: {valid_acc}")
return model_l
########## Train ##########
def gen_test(EEG_dict_p, sex_dict_p, test_names_p):
x_tests_l = None
y_tests_l = None
## Generate the train data
rootLogger.info("Generatte the test data.")
for name in test_names_p:
eeg_l = EEG_dict_p[name]
sex_l = sex_dict_p[name]
n_batch_l = np.shape(eeg_l)[1]
sex_l = [sex_l] * n_batch_l
if x_tests_l is None:
x_tests_l = eeg_l
else:
x_tests_l = np.concatenate((x_tests_l, eeg_l), axis=1)
if y_tests_l is None:
y_tests_l = sex_l
else:
y_tests_l = y_tests_l + sex_l
x_tests_l = np.swapaxes(x_tests_l, 0, 1) # Make [batch, sample points, electrodes]
indices_l = np.random.permutation(x_tests_l.shape[0])
x_tests_l = np.take(x_tests_l, indices_l, axis=0)
y_tests_l = np.take(y_tests_l, indices_l, axis=0)
rootLogger.info("Done.")
return x_tests_l, y_tests_l
def gen_train_valid(EEG_dict_p, sex_dict_p, train_names_p, valid_names_p):
x_trains_l = None
y_trains_l = None
x_valids_l = None
y_valids_l = None
## Generate the train data
rootLogger.info("Generatte the train data.")
for name in train_names_p:
eeg_l = EEG_dict_p[name]
sex_l = sex_dict_p[name]
n_batch_l = np.shape(eeg_l)[1]
sex_l = [sex_l] * n_batch_l
if x_trains_l is None:
x_trains_l = eeg_l
else:
x_trains_l = np.concatenate((x_trains_l, eeg_l), axis=1)
if y_trains_l is None:
y_trains_l = sex_l
else:
y_trains_l = y_trains_l + sex_l
x_trains_l = np.swapaxes(x_trains_l, 0, 1) # Make [batch, sample points, electrodes]
indices_l = np.random.permutation(x_trains_l.shape[0])
x_trains_l = np.take(x_trains_l, indices_l, axis=0)
y_trains_l = np.take(y_trains_l, indices_l, axis=0)
if valid_names_p is not None:
## Generate the validation data
rootLogger.info("Generatte the validation data.")
for name in valid_names_p:
eeg_l = EEG_dict_p[name]
sex_l = sex_dict_p[name]
n_batch_l = np.shape(eeg_l)[1]
sex_l = [sex_l] * n_batch_l
if x_valids_l is None:
x_valids_l = eeg_l
else:
x_valids_l = np.concatenate((x_valids_l, eeg_l), axis=1)
if y_valids_l is None:
y_valids_l = sex_l
else:
y_valids_l = y_valids_l + sex_l
x_valids_l = np.swapaxes(x_valids_l, 0, 1) # Make [batch, sample points, electrodes]
indices_l = np.random.permutation(x_valids_l.shape[0])
x_valids_l = np.take(x_valids_l, indices_l, axis=0)
y_valids_l = np.take(y_valids_l, indices_l, axis=0)
rootLogger.info("Done.")
return x_trains_l, y_trains_l, x_valids_l, y_valids_l
def prep_train_valid(EEG_dict_p, valid_name_p):
valid_eeg_l = EEG_dict_p[valid_name_p]
sorted_names_l = sorted(EEG_dict_p.keys())
# print(sorted_names_l)
train_eegs_l = None
for train_name in sorted_names_l:
if (train_name != valid_name_p):
# print("Add: ", train_name)
tmp_eeg_l = EEG_dict_p[train_name]
if train_eegs_l is None:
train_eegs_l = tmp_eeg_l
else:
train_eegs_l = np.concatenate((train_eegs_l, EEG_dict_p[train_name]), axis=1)
else:
print("Valid: ", valid_name_p)
return train_eegs_l, valid_eeg_l
def loadSex():
def _sex_code(txt):
if txt == "หญิง":
return female_code_g
elif txt == "ชาย":
return male_code_g
else:
raise Exception("Unmatched sex")
## Load data from MW project.
dat_fn_l =
df_l = pd.read_excel(dat_fn_l, usecols="F,BC")
df_l = df_l[df_l["Directory name"].notna()]
# print(df)
tmp_dict_sex_l = df_l.set_index("Directory name").to_dict(orient="index")
dict_sex_l = {}
for key, value in tmp_dict_sex_l.items():
dict_sex_l[key] = _sex_code(value["ขอมลทวไป_เพศโดยกำเนด"])
## Load data from depression project.
dat_fn_l =
doc_update_l = Document(dat_fn_l)
sheet = doc_update_l.sheets[0]
table = sheet.tables[0]
rows_update_l = table.rows()
line_count = 0
for row in rows_update_l:
if line_count > 0: # Skip the table header
id_l = (table.cell(line_count, 1).value).strip()
sex_l = (table.cell(line_count, 5).value).strip()
dict_sex_l[id_l] = _sex_code(sex_l)
line_count += 1
return dict_sex_l
def loadEEGs():
## Load data from MW project.
MW_exc_list_l =
dat_dir_l =
dir_list_l = os.listdir(dat_dir_l)
EEG_dict_l = {}
for dir in dir_list_l:
if dir not in MW_exc_list_l:
eeg_path_l = os.path.join(dat_dir_l, dir, "SexPred", "EEG_1sec_epochs")
if os.path.isfile(eeg_path_l):
# rootLogger.info("Loading..." + eeg_path_l)
with open(eeg_path_l, "rb") as fp:
input_l = pickle.load(fp)
fp.close()
# print(np.shape(input_l))
EEG_dict_l[dir] = input_l
## Load data from depression project.
dat_dir_l =
dir_list_l = os.listdir(dat_dir_l)
for dir in dir_list_l:
eeg_path_l = os.path.join(dat_dir_l, dir, "SexPred", "EEG_1sec_epochs")
if os.path.isfile(eeg_path_l):
# rootLogger.info("Loading..." + eeg_path_l)
with open(eeg_path_l, "rb") as fp:
input_l = pickle.load(fp)
fp.close()
EEG_dict_l[dir] = input_l
return EEG_dict_l
########## Test ##########
if __name__ == "__main__":
## This commemt is added for testing dagshub.
start_time_l = time.ctime()
rootLogger.info("Start")
# link_mlflow_databricks()
link_mlflow_local()
run_id_l = "30787f6373ae4b59917bf0c3388db868"
get_model(run_id_l)
rootLogger.info("Stop")