-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathevaluate.py
More file actions
326 lines (268 loc) · 8.86 KB
/
evaluate.py
File metadata and controls
326 lines (268 loc) · 8.86 KB
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
import os
from math import log
def logit(p):
return log(p/(1-p))
'''
evaluate_sequences
Inputs:
model - a pre-trained model RNN built from model.py
sequences - a list of sequence data read in by fasta.py
output - the name of an output file to print to.
max_batch_size - the maximum size of a batch
Returns:
Nothing. Prints a file of predicted probabilities and scores
'''
def evaluate_sequences(model, sequences, output, max_batch_size=16):
print "Preparing batches for prediction..."
batches = prepare_batches(sequences,max_batch_size)
predictions = get_batch_predictions(model, batches)
OUT = open(output,'w')
for result in predictions:
name,prob,score = result
OUT.write("%s\t%f\t%f\n" % (name.strip(),prob,score))
OUT.close()
'''
ensemble_evaluate_sequences
Inputs:
models - a list of pre-trained models mRNN built from model.py
sequences - a list of sequence data read in by fasta.py
output - the name of an output file to print to.
max_batch_size - the maximum size of a batch
Returns:
Nothing. Prints a file of predicted probabilities and scores
'''
def ensemble_evaluate_sequences(models, sequences, output, max_batch_size=16):
print "Preparing batches for prediction..."
batches = prepare_batches(sequences,max_batch_size)
predictions = get_batch_ensemble_predictions(models, batches)
OUT = open(output,'w')
for result in predictions:
name,prob,score = result
OUT.write("%s\t%f\t%f\n" % (name.strip(),prob,score))
OUT.close()
'''
evaluate_model
Inputs:
model - a pre-trained model RNN built from model.py
test_data - a tuple of positives,negatives
max_batch_size - the maximum size of a batch
Returns:
A confusion matrix of the predictions
'''
def evaluate_model(model, test_data, max_batch_size=16):
positive, negative = test_data
print "Preparing batches for prediction..."
pos_batches = prepare_batches(positive,max_batch_size)
neg_batches = prepare_batches(negative,max_batch_size)
p_mat = batch_predict(model, pos_batches)
n_mat = batch_predict(model, neg_batches)
return [n_mat, p_mat]
'''
prepare_batches
'''
def prepare_batches(data,batch_size):
length_dictionary = {}
data = sorted(data, key=lambda tup: len(tup[0]), reverse=False)
for d in data:
length = len(d[0])
if length not in length_dictionary:
length_dictionary[length] = []
length_dictionary[length].append(d)
batches = []
for length in length_dictionary:
datas = length_dictionary[length]
for b in range(0, len(datas), batch_size):
batches.append(datas[b:b+batch_size])
return batches
"""
get_batch_predictions
Inputs:
model - Model to use for prediction
data - Data set to generate predictions for
batch_size - Size of each prediction batch
Note:
Each batch of data in batches is a list of tuples of dna, name
Returns:
An array of the counts of negative, positive predictions
"""
def get_batch_predictions(model, batches):
print "Making predictions..."
results = []
for b in batches:
dna, name = zip(*b)
r = model.predict(dna)
probs = []
scores = []
for pred in r:
prob = pred[0]
probs.append(prob)
score = logit(prob)
scores.append(score)
batch_results = zip(name,probs,scores)
results.extend(batch_results)
return results
"""
get_batch_ensemble_predictions
Inputs:
models - a list of models to use for prediction
batches - pre-batched sequences
Note:
Each batch of data in batches is a list of tuples of dna, name
Returns:
An array of the counts of negative, positive predictions
"""
def get_batch_ensemble_predictions(models, batches):
results = []
print "Making predictions..."
for b in batches:
dna, name = zip(*b)
probs = []
scores = []
multi_preds = []
for model in models:
preds = []
r = model.predict(dna)
for pred in r:
preds.append(pred[0])
multi_preds.append(preds)
pred_list = zip(*multi_preds)
for p in pred_list:
predictions = list(p)
prob = sum(predictions)/len(predictions)
probs.append(prob)
score = logit(prob)
scores.append(score)
batch_results = zip(name,probs,scores)
results.extend(batch_results)
return results
"""
batch_predict
Inputs:
model - Model to use for prediction
data - Data set to generate predictions for
batch_size - Size of each prediction batch
Note:
Each batch of data in batches is a list of tuples of dna, name
Returns:
An array of the counts of negative, positive predictions
"""
def batch_predict(model, batches):
result = [0.0,0.0] # negative, positive
print "Making predictions..."
for b in batches:
dna, name = zip(*b)
r = model.predict(dna)
for pred in r:
round_pred = int(round(pred[0]))
result[round_pred] += 1
return result
'''
evaluate_multi_model
Inputs:
models - a list of pre-trained model RNNs built from model.py
test_data - a tuple of positives,negatives
max_batch_size - the maximum size of a batch
Returns:
A confusion matrix of the predictions
'''
def evaluate_multi_model(models, test_data, max_batch_size=16):
positive, negative = test_data
print "Preparing batches for prediction..."
pos_batches = prepare_batches(positive,max_batch_size)
neg_batches = prepare_batches(negative,max_batch_size)
p_mat = batch_multi_predict(models, pos_batches)
n_mat = batch_multi_predict(models, neg_batches)
return [n_mat, p_mat]
"""
batch_multi_predict
Inputs:
models - a list of models to use for prediction
data - Data set to generate predictions for
batch_size - Size of each prediction batch
Note:
Each batch of data in batches is a list of tuples of dna, name
Returns:
An array of the counts of negative, positive predictions
"""
def batch_multi_predict(models, batches):
result = [0.0,0.0] # negative, positive
print "Making predictions..."
for b in batches:
dna, name = zip(*b)
multi_preds = []
for model in models:
preds = []
r = model.predict(dna)
for pred in r:
#round_pred = int(round(pred[0]))
#preds.append(round_pred)
preds.append(pred[0])
multi_preds.append(preds)
pred_list = zip(*multi_preds)
for p in pred_list:
predictions = list(p)
thisPred = int(round(sum(predictions)/len(predictions)))
result[thisPred] += 1
#pred_sum = sum(predictions)
#if pred_sum >= len(predictions)/float(2):
# result[1] += 1
#else:
# result[0] += 1
return result
'''
get_model_errors
Inputs:
model - a pre-trained model RNN built from model.py
test_data - a tuple of positives,negatives
max_batch_size - the maximum size of a batch
Returns:
A confusion matrix of the predictions
'''
def get_model_errors(model, test_data, max_batch_size=16):
positive, negative = test_data
print "Preparing batches for prediction..."
pos_batches = prepare_batches(positive,max_batch_size)
neg_batches = prepare_batches(negative,max_batch_size)
p_err = batch_get_errors(model, pos_batches, 1)
n_err = batch_get_errors(model, neg_batches, 0)
return p_err, n_err
"""
batch_get_errors
Inputs:
model - Model to use for prediction
data - Data set to generate predictions for
batch_size - Size of each prediction batch
Note:
Each batch of data in batches is a list of tuples of dna, name
Returns:
An array of the counts of negative, positive predictions
"""
def batch_get_errors(model, batches, label):
print "Making predictions..."
err = []
for b in batches:
dna, name = zip(*b)
r = model.predict(dna)
for result in zip(r, dna, name):
pred,thisDNA,thisName = result
round_pred = int(round(pred[0]))
if round_pred != label:
err.append((thisDNA,thisName))
return err
def process_results(conf_mat,parameters):
[[TN, FP], [FN, TP]] = conf_mat
acc = (TP+TN)/(TP+TN+FP+FN)
sens = TP/(TP+FN)
spec = TN/(TN+FP)
baseName = os.path.basename(parameters['weights'])
outFile = baseName + "_acc.txt"
if parameters['file_label']:
outFile = baseName+ "_" + parameters['file_label'] + "_acc.txt"
print "Writing accuracy information to " + outFile
F = open(outFile,'w')
F.write("%s\tACC\t%.4f\n" % (parameters['weights'],acc))
F.write("%s\tSPEC\t%.4f\n" % (parameters['weights'],spec))
F.write("%s\tSENS\t%.4f\n" % (parameters['weights'],sens))
F.write("%d\t%d\n%d\t%d\n" % (TN,FP,FN,TP))
F.close()
return acc