-
Notifications
You must be signed in to change notification settings - Fork 27
/
main.py
149 lines (125 loc) · 6.32 KB
/
main.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
from model_wrappers import Multask_Wrapper
from nonImg_model_wrappers import NonImg_Model_Wrapper, Fusion_Model_Wrapper
from utils import read_json, plot_shap_bar, plot_shap_heatmap, plot_shap_beeswarm
from performance_eval import whole_eval_package
from multiprocessing import Process
def multi_process(main_config, task_config, tid_gpu, tasks):
"""
:param tid_gpu: python dictionary {task id : GPU id} for example run cross0 on gpu 1, run cross1 on gpu2 {0:1, 1:2}
"""
model_name = main_config['model_name']
csv_dir = main_config['csv_dir']
processes = []
for i in tid_gpu:
main_config['csv_dir'] = csv_dir + 'cross{}/'.format(i)
main_config['model_name'] = model_name + '_cross{}'.format(i)
p = Process(target=main, args=(main_config, task_config, tid_gpu[i], tasks))
p.start()
processes.append(p)
for p in processes:
p.join()
def crossValid(main_config, task_config, device, tasks):
model_name = main_config['model_name']
csv_dir = main_config['csv_dir']
for i in range(5):
main_config['csv_dir'] = csv_dir + 'cross{}/'.format(i)
main_config['model_name'] = model_name + '_cross{}'.format(i)
model = Multask_Wrapper(tasks=tasks,
device=device,
main_config=main_config,
task_config=task_config,
seed=1000)
# model.train()
model.gen_embd(['test'], layer='mid')
model.gen_embd(['exter_test'], layer='mid')
# model.gen_score(['valid'], thres)
# model.shap_mid(task_idx=1, path='/data_2/NACC_ALL/ADNI_neuroPath/', file='ADNI_NP.csv')
# whole_eval_package(model_name, 'test', 'tb_log/{}_cross0/test_perform'.format(model_name))
# whole_eval_package(model_name, 'valid') # validation set performance
# whole_eval_package(model_name, 'OASIS', 'tb_log/{}_cross0/OASIS_perform'.format(model_name))
# whole_eval_package(model_name, 'exter_test', 'tb_log/{}_cross0/exter_test_perform'.format(model_name))
def fusion_CNN_main(main_config, task_config, tasks):
model_name = main_config['model_name']
csv_dir = main_config['csv_dir']
for i in range(5):
processes = []
for j in range(4):
main_config['csv_dir'] = csv_dir + 'cross{}/'.format(i) + 'fold{}/'.format(j)
main_config['model_name'] = model_name + '_cross{}_fold{}'.format(i, j)
p = Process(target=main, args=(main_config, task_config, j, tasks))
p.start()
processes.append(p)
for p in processes:
p.join()
def crossValid_nonImg(main_config, task_config, tasks, shap_analysis=True):
model_name = main_config['model_name']
csv_dir = main_config['csv_dir']
shap, data = [], []
for i in range(5):
main_config['csv_dir'] = csv_dir + 'cross{}/'.format(i)
main_config['model_name'] = model_name + '_cross{}'.format(i)
model = NonImg_Model_Wrapper(tasks=tasks,
main_config=main_config,
task_config=task_config,
seed=1000)
model.train()
thres = model.get_optimal_thres()
model.gen_score(['test', 'OASIS'], thres)
if shap_analysis:
shap_values, features_values = model.shap('test_shap')
shap.append(shap_values)
data.append(features_values)
whole_eval_package(model_name, 'test')
whole_eval_package(model_name, 'OASIS')
if shap_analysis:
plot_shap_bar('tb_log/' + model_name + '_cross0/', model_name, 'test_shap', tasks, top=15)
plot_shap_beeswarm('tb_log/' + model_name + '_cross0/', shap, data, tasks, 'test_shap')
def crossValid_Fusion(main_config, task_config, tasks, shap_analysis=True):
model_name = main_config['model_name']
csv_dir = main_config['csv_dir']
shap, data = [], []
for i in range(5):
main_config['csv_dir'] = csv_dir + 'cross{}/'.format(i)
main_config['model_name'] = model_name + '_cross{}'.format(i)
model = Fusion_Model_Wrapper(tasks=tasks,
main_config=main_config,
task_config=task_config,
seed=1000)
model.train()
thres = model.get_optimal_thres(csv_name='valid_mri')
# model.gen_score(['test_mri', 'OASIS_mri'], thres)
model.gen_score(['neuro_test_mri'], thres)
if shap_analysis:
shap_values, features_values = model.shap('test_shap')
shap.append(shap_values)
data.append(features_values)
# whole_eval_package(model_name, 'test_mri', 'tb_log/{}_cross0/test_perform'.format(model_name))
# whole_eval_package(model_name, 'OASIS_mri', 'tb_log/{}_cross0/OASIS_perform'.format(model_name))
whole_eval_package(model_name, 'neuro_test_mri', 'tb_log/{}_cross0/neuro_test_perform'.format(model_name))
if shap_analysis:
plot_shap_bar('tb_log/'+model_name+'_cross0/', model_name, 'test_shap', tasks, top=15)
plot_shap_beeswarm('tb_log/'+model_name+'_cross0/', shap, data, tasks, 'test_shap')
if __name__ == "__main__":
# main(tasks = ['COG', 'ADD'],
# device = 2,
# main_config = read_json('config.json'),
# task_config = read_json('task_config.json'))
crossValid(tasks = ['COG', 'ADD'],
device = 2,
main_config = read_json('config.json'),
task_config = read_json('task_config.json'))
# multi_process(tasks = ['COG', 'ADD'],
# tid_gpu = {0:0, 1:1, 2:2},
# main_config = read_json('config.json'),
# task_config = read_json('task_config.json'))
# fusion_CNN_main(main_config = read_json('config.json'),
# task_config = read_json('task_config.json'),
# tasks = ['COG', 'ADD'])
# crossValid_nonImg(tasks=['COG', 'ADD'],
# main_config=read_json('config.json'),
# task_config=read_json('nonImg_task_config.json'))
# plot_shap_heatmap(['CatBoost', 'XGBoost', 'RandomForest', 'DecisionTree', 'SupportVector', 'NearestNeighbor', 'Perceptron'],
# ['COG', 'ADD'], 'test_shap')
"""
ssh -L 16005:127.0.0.1:6006 [email protected]
"""