-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_rocauc.py
More file actions
317 lines (283 loc) · 8.31 KB
/
Copy pathtest_rocauc.py
File metadata and controls
317 lines (283 loc) · 8.31 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
from valor_lite.classification import Classification, MetricType
from valor_lite.classification.loader import Loader
def test_rocauc_with_animal_example(
loader: Loader,
classifications_animal_example: list[Classification],
):
"""
Test ROC auc computation. This agrees with scikit-learn: the code (whose data
comes from classification_test_data)
animal_gts = ["bird", "dog", "bird", "bird", "cat", "dog"]
animal_preds = [
{"bird": 0.6, "dog": 0.2, "cat": 0.2},
{"cat": 0.9, "dog": 0.1, "bird": 0.0},
{"cat": 0.8, "dog": 0.05, "bird": 0.15},
{"dog": 0.75, "cat": 0.1, "bird": 0.15},
{"cat": 1.0, "dog": 0.0, "bird": 0.0},
{"cat": 0.4, "dog": 0.4, "bird": 0.2},
]
```
from sklearn.metrics import roc_auc_score
# for the "animal" label key
y_true = [0, 2, 0, 0, 1, 2]
y_score = [
[0.6, 0.2, 0.2],
[0.0, 0.9, 0.1],
[0.15, 0.8, 0.05],
[0.15, 0.1, 0.75],
[0.0, 1.0, 0.0],
[0.2, 0.4, 0.4],
]
print(roc_auc_score(y_true, y_score, multi_class="ovr"))
```
outputs ==> 0.8009259259259259
"""
loader.add_data(classifications_animal_example)
evaluator = loader.finalize()
metrics = evaluator.compute_rocauc()
# test ROCAUC
actual_metrics = [m.to_dict() for m in metrics[MetricType.ROCAUC]]
expected_metrics = [
{
"type": "ROCAUC",
"value": 0.7777777777777779,
"parameters": {
"label": "bird",
},
},
{
"type": "ROCAUC",
"value": 0.625,
"parameters": {
"label": "dog",
},
},
{
"type": "ROCAUC",
"value": 1.0,
"parameters": {
"label": "cat",
},
},
]
for m in actual_metrics:
assert m in expected_metrics
for m in expected_metrics:
assert m in actual_metrics
# test mROCAUC
actual_metrics = [m.to_dict() for m in metrics[MetricType.mROCAUC]]
expected_metrics = [
{"type": "mROCAUC", "value": 0.8009259259259259, "parameters": {}},
]
for m in actual_metrics:
assert m in expected_metrics
for m in expected_metrics:
assert m in actual_metrics
def test_rocauc_with_color_example(
loader: Loader,
classifications_color_example: list[Classification],
):
"""
Test ROC auc computation. This agrees with scikit-learn: the code (whose data
comes from classification_test_data)
color_gts = ["white", "white", "red", "blue", "black", "red"]
color_preds = [
{"white": 0.65, "red": 0.1, "blue": 0.2, "black": 0.05},
{"blue": 0.5, "white": 0.3, "red": 0.0, "black": 0.2},
{"red": 0.4, "white": 0.2, "blue": 0.1, "black": 0.3},
{"white": 1.0, "red": 0.0, "blue": 0.0, "black": 0.0},
{"red": 0.8, "white": 0.0, "blue": 0.2, "black": 0.0},
{"red": 0.9, "white": 0.06, "blue": 0.01, "black": 0.03},
]
```
from sklearn.metrics import roc_auc_score
# for the "color" label key
y_true = [3, 3, 2, 1, 0, 2]
y_score = [
[0.05, 0.2, 0.1, 0.65],
[0.2, 0.5, 0.0, 0.3],
[0.3, 0.1, 0.4, 0.2],
[0.0, 0.0, 0.0, 1.0],
[0.0, 0.2, 0.8, 0.0],
[0.03, 0.01, 0.9, 0.06],
]
print(roc_auc_score(y_true, y_score, multi_class="ovr"))
```
outputs:
```
0.43125
```
"""
loader.add_data(classifications_color_example)
evaluator = loader.finalize()
metrics = evaluator.compute_rocauc()
actual_metrics = [m.to_dict() for m in metrics[MetricType.ROCAUC]]
expected_metrics = [
{
"type": "ROCAUC",
"value": 0.75,
"parameters": {
"label": "white",
},
},
{
"type": "ROCAUC",
"value": 0.875,
"parameters": {
"label": "red",
},
},
{
"type": "ROCAUC",
"value": 0.0,
"parameters": {
"label": "blue",
},
},
{
"type": "ROCAUC",
"value": 0.09999999999999998,
"parameters": {
"label": "black",
},
},
]
for m in actual_metrics:
assert m in expected_metrics
for m in expected_metrics:
assert m in actual_metrics
# test mROCAUC
actual_metrics = [m.to_dict() for m in metrics[MetricType.mROCAUC]]
expected_metrics = [
{"type": "mROCAUC", "value": 0.43125, "parameters": {}},
]
for m in actual_metrics:
assert m in expected_metrics
for m in expected_metrics:
assert m in actual_metrics
def test_rocauc_with_image_example(
loader: Loader,
classifications_image_example: list[Classification],
):
loader.add_data(classifications_image_example)
evaluator = loader.finalize()
metrics = evaluator.compute_rocauc()
actual_metrics = [m.to_dict() for m in metrics[MetricType.ROCAUC]]
expected_metrics = [
{
"type": "ROCAUC",
"value": 0.0,
"parameters": {"label": "v1"},
},
{
"type": "ROCAUC",
"value": 0.0,
"parameters": {"label": "v4"},
},
{
"type": "ROCAUC",
"value": 0.0,
"parameters": {"label": "v5"},
},
{
"type": "ROCAUC",
"value": 0.0,
"parameters": {"label": "v8"},
},
]
for m in actual_metrics:
assert m in expected_metrics
for m in expected_metrics:
assert m in actual_metrics
actual_metrics = [m.to_dict() for m in metrics[MetricType.mROCAUC]]
expected_metrics = [
{"type": "mROCAUC", "value": 0.0, "parameters": {}},
]
for m in actual_metrics:
assert m in expected_metrics
for m in expected_metrics:
assert m in actual_metrics
def test_rocauc_with_tabular_example(
loader: Loader,
classifications_tabular_example: list[Classification],
):
loader.add_data(classifications_tabular_example)
evaluator = loader.finalize()
metrics = evaluator.compute_rocauc()
actual_metrics = [m.to_dict() for m in metrics[MetricType.ROCAUC]]
expected_metrics = [
{
"type": "ROCAUC",
"value": 0.75,
"parameters": {"label": "1"},
},
{
"type": "ROCAUC",
"value": 1.0,
"parameters": {"label": "0"},
},
{
"type": "ROCAUC",
"value": 0.5555555555555556,
"parameters": {"label": "2"},
},
]
for m in actual_metrics:
assert m in expected_metrics
for m in expected_metrics:
assert m in actual_metrics
actual_metrics = [m.to_dict() for m in metrics[MetricType.mROCAUC]]
expected_metrics = [
{
"type": "mROCAUC",
"value": 0.7685185185185185,
"parameters": {},
}
]
for m in actual_metrics:
assert m in expected_metrics
for m in expected_metrics:
assert m in actual_metrics
def test_rocauc_single_classification(loader: Loader):
data = [
Classification(
uid="uid",
groundtruth="dog",
predictions=["dog", "cat"],
scores=[1.0, 0.0],
)
]
loader.add_data(data)
evaluator = loader.finalize()
metrics = evaluator.compute_rocauc()
# test ROCAUC
actual_metrics = [m.to_dict() for m in metrics[MetricType.ROCAUC]]
expected_metrics = [
{
"type": "ROCAUC",
"value": 0.0,
"parameters": {
"label": "dog",
},
},
{
"type": "ROCAUC",
"value": 0.0,
"parameters": {
"label": "cat",
},
},
]
for m in actual_metrics:
assert m in expected_metrics
for m in expected_metrics:
assert m in actual_metrics
# test mROCAUC
actual_metrics = [m.to_dict() for m in metrics[MetricType.mROCAUC]]
expected_metrics = [
{"type": "mROCAUC", "value": 0.0, "parameters": {}},
]
for m in actual_metrics:
assert m in expected_metrics
for m in expected_metrics:
assert m in actual_metrics