-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresults_and_metrics.py
278 lines (204 loc) · 8.62 KB
/
results_and_metrics.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
import torch
import torch.nn as nn
import plot_outputs as po
import numpy as np
import sklearn.metrics
from torch.utils.data import DataLoader
import model_params as params
from importlib import reload
import data_management.vocabulary as vocab
from sklearn.metrics import (
precision_recall_curve,
matthews_corrcoef,
average_precision_score,
)
reload(params)
reload(po)
def test_results(output, target, results_dict, threshold):
output = output.cpu().detach().numpy()
target = target.cpu().detach().numpy()
predictions = output > threshold
for i, cat in enumerate(["replace", "insert", "delete"]):
cat_preds = predictions[:, :, i].reshape(-1).astype("bool")
cat_target = target[:, :, i].reshape(-1).astype("bool")
t_pos = (cat_preds & cat_target).sum()
results_dict[cat]["t_pos"].append(t_pos)
t_neg = (~cat_preds & ~cat_target).sum()
results_dict[cat]["t_neg"].append(t_neg)
f_pos = (cat_preds & ~cat_target).sum()
results_dict[cat]["f_pos"].append(f_pos)
f_neg = (~cat_preds & cat_target).sum()
results_dict[cat]["f_neg"].append(f_neg)
return results_dict
def precision_recall(inps, targets, threshold=0.5):
thresh_predictions = inps > threshold
tru_pos = np.logical_and(thresh_predictions, targets).sum()
pred_pos = thresh_predictions.sum()
targ_pos = targets.sum()
precision = tru_pos / pred_pos if pred_pos > 0 else 0
recall = tru_pos / targ_pos if targ_pos > 0 else 0
return precision, recall
def find_thresh_for_given_recall(output, target, num_trials=10000, target_recall=0.5):
if type(output) == torch.Tensor:
output = output.cpu().detach().numpy()
output = output.reshape(-1)
if type(target) == torch.Tensor:
target = target.cpu().detach().numpy()
target = target.reshape(-1)
thresholds = np.linspace(min(output), max(output), num_trials)
recalls = np.zeros(thresholds.shape)
for i, t in enumerate(thresholds):
_, recalls[i] = precision_recall(output, target, threshold=t)
closest_thresh = thresholds[np.argmin(np.abs(recalls - target_recall))]
return closest_thresh
def find_thresh_for_given_recalls(output, target, target_recalls, num_trials=1000):
return [
find_thresh_for_given_recall(output, target, num_trials, x)
for x in target_recalls
]
def f_measure(inps, targets, threshold=0.5, beta=1):
beta_squared = beta * beta
thresh_predictions = inps > threshold
tru_pos = np.logical_and(thresh_predictions, targets).sum()
pred_pos = thresh_predictions.sum()
targ_pos = targets.sum()
if pred_pos > 0 and targ_pos > 0 and tru_pos > 0:
precision = tru_pos / pred_pos
recall = tru_pos / targ_pos
F1 = (
(1 + beta_squared)
* (precision * recall)
/ ((precision * beta_squared) + recall)
)
else:
F1 = 0
return F1
def matthews_correlation(output, target, threshold=0.5):
if type(output) is torch.Tensor:
output = output.cpu().detach().numpy()
target = target.cpu().detach().numpy()
output = output.reshape(-1)
target = target.reshape(-1)
thresh_predictions = output > threshold
mcc = matthews_corrcoef(thresh_predictions, target)
return mcc
def multilabel_thresholding(output, target, num_trials=2000):
if type(output) is torch.Tensor:
output = output.cpu().detach().numpy()
target = target.cpu().detach().numpy()
output = output.reshape(-1)
target = target.reshape(-1)
upper_positive = np.max(output[target == 1])
lower_positive = np.min(output[target == 1])
thresholds = np.linspace(lower_positive, upper_positive, num_trials)
mccs = np.zeros(thresholds.shape)
for i, t in enumerate(thresholds):
mccs[i] = matthews_correlation(output, target, threshold=t)
best_thresh = thresholds[np.argmax(mccs)]
best_score = np.max(mccs)
return best_score, best_thresh
def normalized_recall(output, target):
if type(output) is torch.Tensor:
output = output.cpu().detach().numpy()
target = target.cpu().detach().numpy()
output = output.reshape(-1)
target = target.reshape(-1)
num_positive = np.sum(target)
n = output.shape[0]
order = np.argsort(output * -1)
ranks = np.argsort(order)
positive_locs = np.nonzero(target)
sum_rank = np.sum(ranks[positive_locs])
sum_i = (num_positive * (num_positive - 1)) // 2
res = 1 - ((sum_rank - sum_i) / (num_positive * (n - num_positive)))
return res
class TestResults(object):
def __init__(self, threshes, target_recalls):
self.threshes = threshes
self.target_recalls = target_recalls + [0]
self.outputs = np.array([])
self.targets = np.array([])
self.results_dict = {"t_pos": [], "t_neg": [], "f_pos": [], "f_neg": []}
def update_threshes_for_given_recalls(self):
self.threshes = find_thresh_for_given_recalls(
self.outputs, self.targets, self.target_recalls
)
def update(self, output, target):
if type(output) == torch.Tensor:
output = output.cpu().detach().numpy()
if type(target) == torch.Tensor:
target = target.cpu().detach().numpy()
self.outputs = np.concatenate([self.outputs, output.reshape(-1)])
self.targets = np.concatenate([self.targets, target.reshape(-1).astype(int)])
def calculate_stats(self):
categories = [
"precision",
"recall",
"true negative rate",
"prop_positive_predictions",
"prop_positive_targets",
"mcc",
]
r = {x: {} for x in categories}
for i, t in enumerate(self.threshes):
thresh_res = self.calculate_stats_for_thresh(t)
for k in thresh_res.keys():
target_recall = self.target_recalls[i]
r[k][target_recall] = thresh_res[k]
return r
def sigmoid_outputs(self):
sigmoid = lambda x: 1 / (1 + np.exp(-x))
return sigmoid(self.outputs)
def calculate_stats_for_thresh(self, thresh):
predictions = self.sigmoid_outputs() > thresh
cat_preds = predictions.reshape(-1).astype("bool")
cat_target = self.targets.reshape(-1).astype("bool")
t_pos = (cat_preds & cat_target).sum()
t_neg = (~cat_preds & ~cat_target).sum()
f_pos = (cat_preds & ~cat_target).sum()
f_neg = (~cat_preds & cat_target).sum()
prop_positive_predictions = np.sum(cat_preds) / len(cat_preds)
prop_positive_targets = np.sum(cat_target) / len(cat_target)
r = {}
r["precision"] = t_pos / (t_pos + f_pos) if (t_pos + f_pos) > 0 else 0
r["recall"] = t_pos / (t_pos + f_neg) if (t_pos + f_neg) > 0 else 0
r["true negative rate"] = t_neg / (t_neg + f_pos) if (t_neg + f_pos) > 0 else 0
r["prop_positive_predictions"] = prop_positive_predictions
r["prop_positive_targets"] = prop_positive_targets
r["mcc"] = matthews_correlation(cat_preds, cat_target, thresh)
return r
def make_pr_curve(self):
# necessary to downsample PR graphs because wandb only does graphs of 10000 pts
precision, recall, thresholds = precision_recall_curve(
self.targets, self.outputs
)
# downsample_amt = (len(precision) // 10001) + 1
# precision = precision[::downsample_amt]
# recall = recall[::downsample_amt]
# thresholds = thresholds[::downsample_amt]
return precision, recall, thresholds
def average_precision(self):
return average_precision_score(self.targets, self.outputs)
def normalized_recall(self):
return normalized_recall(self.targets, self.outputs)
if __name__ == "__main__":
num_trials = 100
from sklearn.metrics import PrecisionRecallDisplay
import matplotlib.pyplot as plt
tr = TestResults(threshes=[0.1, 0.5, 0.9], target_recalls=[0.1, 0.5, 0.9])
for x in range(10):
targets = torch.tensor(np.random.choice([0, 1], num_trials, p=[0.9, 0.1]))
outputs = torch.linspace(0, 1, num_trials) + (0.25 * targets)
# targets = torch.round(outputs)
tr.update(outputs, targets)
asdf = find_thresh_for_given_recall(
torch.Tensor(tr.outputs), torch.Tensor(tr.targets), target_recall=0.33
)
res = tr.calculate_stats()
p, r, t = tr.make_pr_curve()
display = PrecisionRecallDisplay.from_predictions(
tr.targets, tr.outputs, name="TestData"
)
_ = display.ax_.set_title("2-class Precision-Recall curve")
# plt.show()
# 1 - mccloss(torch.tensor(tr.outputs > asdf), torch.tensor(tr.targets))