-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
341 lines (320 loc) · 22.2 KB
/
tools.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
from sklearn.metrics import roc_curve, auc
import torch
def to_numpy(tensor_or_array):
"""Helper function to convert a possible torch tensor to a numpy array."""
if isinstance(tensor_or_array, torch.Tensor):
return tensor_or_array.detach().cpu().numpy()
return tensor_or_array
def evaluate_accuracy_list(datasetNameList, Y_test, pred_out, toPrint=True):
from sklearn.metrics import roc_curve, auc
normalized_pred_out = pred_out
num_wrong_pred = 0
num_wrong_pred_each_dataset = [0] * len(pred_out)
num_total_each_dataset = [0] * len(pred_out)
auroc_list = []
if len(datasetNameList) > 1:
for i, predict_out_i in enumerate(pred_out): # i means i-th dataset
true_labels = []
predicted_scores = []
for j, pred_out_i_j in enumerate(predict_out_i): # j means j-th input dimension
if Y_test.iloc[i, j] != 0.5: # when the label is 0.5, it's not considered.
true_labels.append(to_numpy(Y_test.iloc[i, j]))
predicted_scores.append(to_numpy(pred_out[i][j]))
num_total_each_dataset[i] += 1
normalized_pred_out[i][j] = 1.0 if pred_out[i][j] >= 0.5 else 0.0
num_wrong_pred += 1.0 if (abs(Y_test.iloc[i, j].item() - normalized_pred_out[i][j])) >= 0.5 else 0.0
num_wrong_pred_each_dataset[i] += 1.0 if (abs(Y_test.iloc[i, j].item() - normalized_pred_out[i][j])) >= 0.5 else 0.0
if true_labels:
true_labels = to_numpy(true_labels)
predicted_scores = to_numpy(predicted_scores)
fpr, tpr, thresholds = roc_curve(true_labels, predicted_scores)
auroc_list.append(auc(fpr, tpr))
else:
auroc_list.append(None)
elif len(datasetNameList) == 1:
true_labels = [Y_test[0].iloc[i] for i, item in enumerate(pred_out) if Y_test[0].iloc[i] != 0.5]
predicted_scores = [item for i, item in enumerate(pred_out) if Y_test[0].iloc[i] != 0.5]
for i, item in enumerate(pred_out):
normalized_pred_out[i] = 1.0 if item >= 0.5 else 0.0
num_wrong_pred += 1.0 if (abs(Y_test[0].iloc[i] - normalized_pred_out[i])) >= 0.5 else 0.0
true_labels = to_numpy(true_labels)
predicted_scores = to_numpy(predicted_scores)
fpr, tpr, thresholds = roc_curve(true_labels, predicted_scores)
auroc_list.append(auc(fpr, tpr))
split_accuracy_dict = [0.0] * len(pred_out)
for i in range(len(pred_out)):
split_accuracy_dict[i] = (1.0 - num_wrong_pred_each_dataset[i] / num_total_each_dataset[i])
if toPrint:
print("Y_test")
print(Y_test.shape)
print(Y_test)
print("prediction")
print(len(pred_out[0]))
print("for each dataset")
for i in range(len(pred_out)):
print("dataset %s" % datasetNameList[i])
print("total case %d" % num_total_each_dataset[i])
print("wrong prediction %d" % num_wrong_pred_each_dataset[i])
print("accuracy for this dataset=%f" % (1.0 - num_wrong_pred_each_dataset[i] / num_total_each_dataset[i]))
print("AUROC for this dataset=%f" % auroc_list[i])
print("num of wrong prediction")
print(num_wrong_pred)
print("num of test total")
print(len(Y_test.iloc[0]))
print("accuracy")
print(1.0 - num_wrong_pred / len(Y_test.iloc[0]))
return normalized_pred_out, num_wrong_pred, 1.0 - num_wrong_pred / len(Y_test.iloc[0]), split_accuracy_dict, auroc_list
# def evaluate_accuracy_list(datasetNameList,Y_test,pred_out,toPrint=True):
# normalized_pred_out = pred_out#[[0] * len(datasetNameList) for i in range(len(pred_out))]
# num_wrong_pred = 0
# num_wrong_pred_each_dataset=[0]*len(pred_out)
# num_total_each_dataset = [0] * len(pred_out)
# if len(datasetNameList) > 1:
# for i, predict_out_i in enumerate(pred_out):#i means i-th dataset
# for j, pred_out_i_j in enumerate(predict_out_i):# j means j-th input dimension
# if Y_test.iloc[i,j]!=0.5:#when the label is 0.5, it's not considered.
# num_total_each_dataset[i]+=1
# '''
# print("DEBUG:Y_test.iloc[%d,%d]!=0.5"%(i,j))
# print("DEBUG:Y_test.iloc[%d,%d]=%f"%(i,j,Y_test.iloc[i,j].item()))
# print("DEBUG:pred_out[%d,%d]=%f"%(i,j,pred_out[i][j].item()))
# '''
# normalized_pred_out[i][j] = 1.0 if pred_out[i][j]>=0.5 else 0.0
# num_wrong_pred += 1.0 if (abs(Y_test.iloc[i, j].item() - normalized_pred_out[i][j]))>=0.5 else 0.0
# num_wrong_pred_each_dataset[i] += 1.0 if (abs(Y_test.iloc[i, j].item() - normalized_pred_out[i][j])) else 0.0
# '''
# if pred_out_i_j >= 0.5:
# normalized_pred_out[i][j] = 1
# num_wrong_pred += round(abs(Y_test.iloc[i,j] - 1.0))
# num_wrong_pred_each_dataset[i] += round(abs(Y_test.iloc[i,j] - 1.0))
# elif pred_out_i_j < 0.5:
# normalized_pred_out[i][j] = 0
# num_wrong_pred += round(abs(Y_test.iloc[i,j] - 0.0))
# num_wrong_pred_each_dataset[i] += round(abs(Y_test.iloc[i, j] - 0.0))'''
# elif len(datasetNameList) == 1:
# for i, item in enumerate(pred_out):
# if item >= 0.5:
# normalized_pred_out.append(1)
# num_wrong_pred += round(abs(Y_test[0].iloc[i] - 1.0))
# elif item < 0.5:
# normalized_pred_out.append(0)
# num_wrong_pred += round(abs(Y_test[0].iloc[i] - 0.0))
# split_accuracy_dict = [0.0]*6
# for i in range(len(pred_out)):
# split_accuracy_dict[i] = (1.0 - num_wrong_pred_each_dataset[i] / num_total_each_dataset[i]) # [datasetNameList[i]]
# if toPrint:
# print("Y_test")
# print(Y_test.shape)
# print(Y_test)
# print("prediction")
# #print(pred_out.shape)
# #print(pred_out)
# print(len(pred_out[0]))
# print("for each dataset")
# for i in range(len(pred_out)):
# print("dataset %s"% datasetNameList[i])
# print("total case %d"%num_total_each_dataset[i])
# print("wrong prediction %d"%num_wrong_pred_each_dataset[i])
# print("accuracy for this dataset=%f"% (1.0-num_wrong_pred_each_dataset[i]/num_total_each_dataset[i]))
# #split_accuracy_dict[i]=(1.0-num_wrong_pred_each_dataset[i]/num_total_each_dataset[i])#[datasetNameList[i]]
# print("num of wrong prediction")
# print(num_wrong_pred)
# print("num of test total")
# print(len(Y_test.iloc[0]))
# print("accuracy")
# print(1.0 - num_wrong_pred / len(Y_test.iloc[0]))
# return normalized_pred_out, num_wrong_pred, 1.0 - num_wrong_pred / len(Y_test.iloc[0]),split_accuracy_dict
def evaluate_accuracy_list_single(datasetNameList,Y_test,pred_out,toPrint=True):
#normalized_pred_out = pred_out#[[0] * len(datasetNameList) for i in range(len(pred_out))]#commented
normalized_pred_out = pred_out.clone()
num_wrong_pred = 0
num_wrong_pred_each_dataset=[0]*len(pred_out)
num_total_each_dataset = [0] * len(pred_out)
if len(datasetNameList) > 1:
for i, predict_out_i in enumerate(pred_out):#i means i-th dataset
#for j, pred_out_i_j in enumerate(predict_out_i):# j means j-th input dimension
for j, pred_out_i_j in enumerate(predict_out_i.clone()):# j means j-th input dimension
if Y_test.iloc[i,j]!=0.5:#when the label is 0.5, it's not considered.
num_total_each_dataset[i]+=1
'''
print("DEBUG:Y_test.iloc[%d,%d]!=0.5"%(i,j))
print("DEBUG:Y_test.iloc[%d,%d]=%f"%(i,j,Y_test.iloc[i,j].item()))
print("DEBUG:pred_out[%d,%d]=%f"%(i,j,pred_out[i][j].item()))
'''
normalized_pred_out[i][j] = 1.0 if pred_out[i][j]>=0.5 else 0.0
num_wrong_pred += 1.0 if (abs(Y_test.iloc[i, j].item() - normalized_pred_out[i][j]))>=0.5 else 0.0
num_wrong_pred_each_dataset[i] += 1.0 if (abs(Y_test.iloc[i, j].item() - normalized_pred_out[i][j])) else 0.0
'''
if pred_out_i_j >= 0.5:
normalized_pred_out[i][j] = 1
num_wrong_pred += round(abs(Y_test.iloc[i,j] - 1.0))
num_wrong_pred_each_dataset[i] += round(abs(Y_test.iloc[i,j] - 1.0))
elif pred_out_i_j < 0.5:
normalized_pred_out[i][j] = 0
num_wrong_pred += round(abs(Y_test.iloc[i,j] - 0.0))
num_wrong_pred_each_dataset[i] += round(abs(Y_test.iloc[i, j] - 0.0))'''
elif len(datasetNameList) == 1:
for i, item in enumerate(pred_out):
if item >= 0.5:
normalized_pred_out.append(1)
num_wrong_pred += round(abs(Y_test[0].iloc[i] - 1.0))
elif item < 0.5:
normalized_pred_out.append(0)
num_wrong_pred += round(abs(Y_test[0].iloc[i] - 0.0))
split_accuracy_dict = [0.0]*6
for i in range(len(pred_out)):
split_accuracy_dict[i] = (1.0 - num_wrong_pred_each_dataset[i] / num_total_each_dataset[i]) # [datasetNameList[i]]
if toPrint:
print("Y_test")
print(Y_test.shape)
print(Y_test)
print("prediction")
#print(pred_out.shape)
#print(pred_out)
print(len(pred_out[0]))
print("for each dataset")
for i in range(len(pred_out)):
print("dataset %s"% datasetNameList[i])
print("total case %d"%num_total_each_dataset[i])
print("wrong prediction %d"%num_wrong_pred_each_dataset[i])
print("accuracy for this dataset=%f"% (1.0-num_wrong_pred_each_dataset[i]/num_total_each_dataset[i]))
#split_accuracy_dict[i]=(1.0-num_wrong_pred_each_dataset[i]/num_total_each_dataset[i])#[datasetNameList[i]]
print("num of wrong prediction")
print(num_wrong_pred)
print("num of test total")
print(len(Y_test.iloc[0]))
print("accuracy")
print(1.0 - num_wrong_pred / len(Y_test.iloc[0]))
return normalized_pred_out, num_wrong_pred, 1.0 - num_wrong_pred / len(Y_test.iloc[0]),split_accuracy_dict
def print_parameters_settings(code, date, h_dim, toTrainMeiNN, toAddGenePathway, toAddGeneSite, multiDatasetMode,
datasetNameList, num_of_selected_residue, lossMode, selectNumPathwayMode,
num_of_selected_pathway, AE_epoch, NN_epoch, batch_size_mode, batch_size_ratio,
separatelyTrainAE_NN, toMask, framework, skip_connection_mode, split_accuracy_list,
total_accuracy, split_accuracy_list2, total_accuracy2, split_accuracy_list3,
total_accuracy3, auroc_list, total_auroc, auroc_list2, total_auroc2, auroc_list3,
total_auroc3, toValidate, multi_task_training_policy, learning_rate_list, preprocess_time,
train_time, predict_time):
print("code, date, h_dim, toTrainMeiNN, toAddGenePathway, toAddGeneSite, multiDatasetMode,"
"datasetNameList, num_of_selected_residue, lossMode, selectNumPathwayMode, num_of_selected_pathway,"
"AE_epoch, NN_epoch, batch_size_mode, batch_size_ratio, separatelyTrainAE_NN, toMask, framework,"
"skip_connection_mode, split_accuracy_list, total_accuracy, split_accuracy_list2, total_accuracy2,"
"split_accuracy_list3, total_accuracy3, auroc_list, total_auroc, auroc_list2, total_auroc2, auroc_list3,"
"total_auroc3, toValidate, multi_task_training_policy, learning_rate_list, preprocess_time, train_time,"
"predict_time")
print(code, date, h_dim, toTrainMeiNN, toAddGenePathway, toAddGeneSite, multiDatasetMode,
datasetNameList, num_of_selected_residue, lossMode, selectNumPathwayMode, num_of_selected_pathway,
AE_epoch, NN_epoch, batch_size_mode, batch_size_ratio, separatelyTrainAE_NN, toMask, framework,
skip_connection_mode, split_accuracy_list, total_accuracy, split_accuracy_list2, total_accuracy2,
split_accuracy_list3, total_accuracy3, auroc_list, total_auroc, auroc_list2, total_auroc2, auroc_list3,
total_auroc3, toValidate, multi_task_training_policy, learning_rate_list, preprocess_time, train_time,
predict_time)
# def print_parameters_settings(code,date,h_dim,toTrainMeiNN, toAddGenePathway,toAddGeneSite, multiDatasetMode,
# datasetNameList,
# num_of_selected_residue,
# lossMode, selectNumPathwayMode,
# num_of_selected_pathway,
# AE_epoch, NN_epoch,
# batch_size_mode,batch_size_ratio,
# separatelyTrainAE_NN, toMask,
# framework, skip_connection_mode,
# split_accuracy_list,total_accuracy,
# split_accuracy_list2,total_accuracy2,
# split_accuracy_list3,total_accuracy3,
# toValidate,multi_task_training_policy,learning_rate_list,preprocess_time,train_time,predict_time):
# print("code,date,h_dim,toTrainMeiNN, toAddGenePathway,toAddGeneSite, multiDatasetMode,\
# datasetNameList,\
# num_of_selected_residue,\
# lossMode, selectNumPathwayMode,\
# num_of_selected_pathway,\
# AE_epoch, NN_epoch,\
# batch_size_mode,batch_size_ratio,\
# separatelyTrainAE_NN, toMask,\
# framework, skip_connection_mode,\
# split_accuracy_list,total_accuracy,\
# split_accuracy_list2,total_accuracy2,\
# split_accuracy_list3,total_accuracy3\
# toValidate,multi_task_training_policy,learning_rate_list,\
# preprocess_time,train_time,predict_time")
# print(code,date,h_dim,toTrainMeiNN, toAddGenePathway,toAddGeneSite, multiDatasetMode,
# datasetNameList,
# num_of_selected_residue,
# lossMode, selectNumPathwayMode,
# num_of_selected_pathway,
# AE_epoch, NN_epoch,
# batch_size_mode,batch_size_ratio,
# separatelyTrainAE_NN, toMask,
# framework, skip_connection_mode,
# split_accuracy_list,total_accuracy,
# split_accuracy_list2,total_accuracy2,
# split_accuracy_list3,total_accuracy3,
# toValidate,multi_task_training_policy,learning_rate_list,
# preprocess_time,train_time,predict_time)
def add_to_result_csv(code,date,h_dim,toTrainMeiNN, toAddGenePathway,toAddGeneSite, multiDatasetMode,
datasetNameList,
num_of_selected_residue,
lossMode, selectNumPathwayMode,
num_of_selected_pathway,
AE_epoch, NN_epoch,
batch_size_mode,batch_size_ratio,
separatelyTrainAE_NN, toMask,
framework, skip_connection_mode,
split_accuracy_list,total_accuracy,
split_accuracy_list2,total_accuracy2,
split_accuracy_list3,total_accuracy3,
auroc_list,total_auroc,
auroc_list2, total_auroc2,
auroc_list3, total_auroc3,
toValidate,multi_task_training_policy,learning_rate_list,
preprocess_time,train_time,predict_time):
import csv
input_path ="./result-all/" # campaign file path
output_file_name="1-10results-together.csv"
input_csv = open(input_path + output_file_name, 'a')#original 'ab'
IBD,MS,Psoriasis,RA,SLE,diabetes1=split_accuracy_list
single_IBD, single_MS, single_Psoriasis, single_RA, single_SLE,single_diabetes1 = split_accuracy_list2
whole_IBD, whole_MS, whole_Psoriasis, whole_RA, whole_SLE,whole_diabetes1 = split_accuracy_list3
IBD_AUROC,MS_AUROC,Psoriasis_AUROC,RA_AUROC,SLE_AUROC,diabetes1_AUROC=auroc_list
single_IBD_AUROC, single_MS_AUROC, single_Psoriasis_AUROC, single_RA_AUROC, single_SLE_AUROC,single_diabetes1_AUROC = auroc_list2
whole_IBD_AUROC, whole_MS_AUROC, whole_Psoriasis_AUROC, whole_RA_AUROC, whole_SLE_AUROC,whole_diabetes1_AUROC =auroc_list3
total_AUROC=total_auroc
single_total_AUROC=total_auroc2
whole_total_AUROC=total_auroc3
a0=["code","date","h_dim","toTrainMeiNN", "toAddGenePathway","toAddGeneSite", "multiDatasetMode",
"datasetNameList",
"num_of_selected_residue",
"lossMode", "selectNumPathwayMode",
"num_of_selected_pathway",
"AE_epoch", "NN_epoch",
"batch_size_mode","batch_size_ratio",
"separatelyTrainAE_NN", "toMask",
"framework", "skip_connection_mode",
"diabetes1","IBD","MS","Psoriasis","RA","SLE","total_accuracy",
"single_diabetes1", "single_IBD", "single_MS", "single_Psoriasis", "single_RA", "single_SLE","single_total_accuracy",
"whole_diabetes1", "whole_IBD", "whole_MS", "whole_Psoriasis", "whole_RA", "whole_SLE",
"whole_total_accuracy",
"diabetes1_AUROC", "IBD_AUROC", "MS_AUROC", "Psoriasis_AUROC", "RA_AUROC", "SLE_AUROC", "total_AUROC",
"single_diabetes1_AUROC", "single_IBD_AUROC", "single_MS_AUROC", "single_Psoriasis_AUROC",
"single_RA_AUROC", "single_SLE_AUROC", "single_total_AUROC", "whole_diabetes1_AUROC", "whole_IBD_AUROC",
"whole_MS_AUROC", "whole_Psoriasis_AUROC", "whole_RA_AUROC", "whole_SLE_AUROC", "whole_total_AUROC",
"toValidate","multi_task_training_policy","learning_rate_list","preprocess_time","train_time","predict_time"]
a = [code,date,h_dim,toTrainMeiNN, toAddGenePathway,toAddGeneSite, multiDatasetMode,
datasetNameList,
num_of_selected_residue,
lossMode, selectNumPathwayMode,
num_of_selected_pathway,
AE_epoch, NN_epoch,
batch_size_mode,batch_size_ratio,
separatelyTrainAE_NN, toMask,
framework, skip_connection_mode,
diabetes1,IBD,MS,Psoriasis,RA,SLE,total_accuracy,
single_diabetes1, single_IBD, single_MS, single_Psoriasis, single_RA, single_SLE,total_accuracy2,
whole_diabetes1, whole_IBD, whole_MS, whole_Psoriasis, whole_RA, whole_SLE,total_accuracy3,
diabetes1_AUROC, IBD_AUROC, MS_AUROC, Psoriasis_AUROC, RA_AUROC, SLE_AUROC, total_AUROC,
single_diabetes1_AUROC, single_IBD_AUROC, single_MS_AUROC, single_Psoriasis_AUROC,
single_RA_AUROC, single_SLE_AUROC, single_total_AUROC, whole_diabetes1_AUROC, whole_IBD_AUROC,
whole_MS_AUROC, whole_Psoriasis_AUROC, whole_RA_AUROC, whole_SLE_AUROC, whole_total_AUROC,
toValidate,multi_task_training_policy,learning_rate_list,preprocess_time,train_time,predict_time]
csv_write = csv.writer(input_csv, dialect='excel')
csv_write.writerow(a0)
csv_write.writerow(a)
#csv_write.writerow(b)