-
Notifications
You must be signed in to change notification settings - Fork 25
/
nn_evaluate.py
executable file
·164 lines (117 loc) · 4.77 KB
/
nn_evaluate.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Autoencoders evaluation.
Usage:
nn_evaluate.py [--whole] [--male] [--threshold] [--leave-site-out] [<derivative> ...]
nn_evaluate.py (-h | --help)
Options:
-h --help Show this screen
--whole Run model for the whole dataset
--male Run model for male subjects
--threshold Run model for thresholded subjects
--leave-site-out Prepare data using leave-site-out method
derivative Derivatives to process
"""
import numpy as np
import pandas as pd
import tensorflow as tf
from docopt import docopt
from nn import nn
from utils import (load_phenotypes, format_config, hdf5_handler,
reset, to_softmax, load_ae_encoder, load_fold)
from sklearn.metrics import confusion_matrix
def nn_results(hdf5, experiment, code_size_1, code_size_2):
exp_storage = hdf5["experiments"][experiment]
n_classes = 2
results = []
for fold in exp_storage:
experiment_cv = format_config("{experiment}_{fold}", {
"experiment": experiment,
"fold": fold,
})
X_train, y_train, \
X_valid, y_valid, \
X_test, y_test = load_fold(hdf5["patients"], exp_storage, fold)
y_test = np.array([to_softmax(n_classes, y) for y in y_test])
ae1_model_path = format_config("./data/models/{experiment}_autoencoder-1.ckpt", {
"experiment": experiment_cv,
})
ae2_model_path = format_config("./data/models/{experiment}_autoencoder-2.ckpt", {
"experiment": experiment_cv,
})
nn_model_path = format_config("./data/models/{experiment}_mlp.ckpt", {
"experiment": experiment_cv,
})
try:
model = nn(X_test.shape[1], n_classes, [
{"size": 1000, "actv": tf.nn.tanh},
{"size": 600, "actv": tf.nn.tanh},
])
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
saver = tf.train.Saver(model["params"])
saver.restore(sess, nn_model_path)
output = sess.run(
model["output"],
feed_dict={
model["input"]: X_test,
model["dropouts"][0]: 1.0,
model["dropouts"][1]: 1.0,
}
)
print(output)
y_pred = np.argmax(output, axis=1)
y_true = np.argmax(y_test, axis=1)
[[TN, FP], [FN, TP]] = confusion_matrix(y_true, y_pred, labels=[0, 1]).astype(float)
accuracy = (TP+TN)/(TP+TN+FP+FN)
specificity = TN/(FP+TN)
precision = TP/(TP+FP)
sensivity = recall = TP/(TP+FN)
fscore = 2*TP/(2*TP+FP+FN)
results.append([accuracy, precision, recall, fscore, sensivity, specificity])
finally:
reset()
return [experiment] + np.mean(results, axis=0).tolist()
if __name__ == "__main__":
reset()
arguments = docopt(__doc__)
pd.set_option("display.expand_frame_repr", False)
pheno_path = "./data/phenotypes/Phenotypic_V1_0b_preprocessed1.csv"
pheno = load_phenotypes(pheno_path)
hdf5 = hdf5_handler("./data/abide.hdf5", "a")
valid_derivatives = ["cc200", "aal", "ez", "ho", "tt", "dosenbach160"]
derivatives = [derivative for derivative
in arguments["<derivative>"]
if derivative in valid_derivatives]
experiments = []
for derivative in derivatives:
config = {"derivative": derivative}
if arguments["--whole"]:
experiments += [format_config("{derivative}_whole", config)],
if arguments["--male"]:
experiments += [format_config("{derivative}_male", config)]
if arguments["--threshold"]:
experiments += [format_config("{derivative}_threshold", config)]
if arguments["--leave-site-out"]:
for site in pheno["SITE_ID"].unique():
site_config = {"site": site}
experiments += [
format_config("{derivative}_leavesiteout-{site}",
config, site_config)
]
# First autoencoder bottleneck
code_size_1 = 1000
# Second autoencoder bottleneck
code_size_2 = 600
results = []
experiments = sorted(experiments)
for experiment in experiments:
results.append(nn_results(hdf5, experiment, code_size_1, code_size_2))
cols = ["Exp", "Accuracy", "Precision", "Recall", "F-score",
"Sensivity", "Specificity"]
df = pd.DataFrame(results, columns=cols)
print df[cols] \
.sort_values(["Exp"]) \
.reset_index()