-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmetrics_decision_based.py
174 lines (157 loc) · 7.21 KB
/
metrics_decision_based.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
import datasets
from sklearn.metrics import precision_score, recall_score, f1_score
import numpy as np
_DESCRIPTION_T12 = """\
Metrics for measuring the performance of prediction models for eRisk 2021 Task 2 and 3.
Include decision-based performance metrics: decision-based F1, lantency-weighted F1, ERDE score.
"""
_CITATION = ""
_KWARGS_DESCRIPTION_T12 = """
Calculates how good are predictions given some references, using certain scores.
Predictions and references are expected to be ordered chronologically (in order of
their appearance in the input stream).
Decision-based metrics consider predictions at the user level, where a decision for a
given user is considered positive if any prediction for that user is positive.
Args:
predictions: list of predictions to score. Each prediction
should be an integer in {0, 1}.
references: list of references for each prediction. Each
reference should be a dictionary with keys 'label' and 'user',
containing the true label for the given example (integer in {0, 1}),
and the user who authored the given datapoint (as a string).
Labels are expected to be consistent for the same user across references.
posts_per_datapoint: the number of user posts that are used to generate one prediction
and correspond to one label in the input.
Returns:
precision: decision-based precision
recall: decision-based recall
f1: decision-based f
latency_f1: f1 weighted by the median latency for positive alerts
erde: error score penalizing late alerts
Examples:
>>> erisk_metric = EriskScoresT1T2()
>>> results = erisk_metric.compute(
references=[{'user': 'subject14', 'label': 0},
{'user': 'subject15', label: 1}], predictions=[0, 1],
posts_per_datapoint=50)
>>> print(results)
{'f1': 1.0,
'f1_latency': 1.0,
'precision': 1.0,
'recall': 1.0,
'erde5': 0.0,
'erde50': 0.0}
"""
def _penalty(k, p=0.0078):
return -1 + 2 / (1 + np.exp(-p * (k - 1)))
def _lc(k, o):
return 1 - (1 / (1 + np.exp(k - o)))
class EriskScoresT1T2(datasets.Metric):
def _info(self):
return datasets.MetricInfo(
description=_DESCRIPTION_T12,
citation=_CITATION,
inputs_description=_KWARGS_DESCRIPTION_T12,
features=datasets.Features({
'predictions': datasets.Value('int64'),
'references': datasets.DatasetDict({'label': datasets.Value('int64'),
'user': datasets.Value('string')
})
}),
codebase_urls=[],
reference_urls=[],
)
def _latency(self, predictions, references, posts_per_datapoint):
assert(len(predictions)==len(references)), \
"Number of predictions not equal to number of references: %d vs %d." % (len(predictions), len(references))
predictions_per_user = {}
labels_per_user = {}
for i in range(len(predictions)):
u = references[i]['user']
l = references[i]['label']
p = predictions[i]
if u not in predictions_per_user:
predictions_per_user[u] = []
predictions_per_user[u].append(p)
if u in labels_per_user:
assert(labels_per_user[u] == l), "Inconsistent labels for same user: %s" % u
else:
labels_per_user[u] = l
users = list(labels_per_user.keys())
latencies = []
for u in users:
# Latency only relevant for true positives
if labels_per_user[u] != 1 or sum(predictions_per_user[u]) == 0:
continue
i = 0
p = predictions_per_user[u][i]
# Minimum latency has to be the number of posts used for the first prediction,
# assuming we predict 0s by default (before the model generated any predictions)
latency = posts_per_datapoint
while (p != 1) and (i < len(predictions_per_user[u])):
latency += posts_per_datapoint
p = predictions_per_user[u][i]
i += 1
latencies.append(latency)
median_penalty = _penalty(np.median(latencies))
print(latencies, median_penalty)
return median_penalty
def _erde(self, predictions, references, posts_per_datapoint, o):
assert(len(predictions)==len(references)), \
"Number of predictions not equal to number of references: %d vs %d." % (len(predictions), len(references))
predictions_per_user = {}
labels_per_user = {}
for i in range(len(predictions)):
u = references[i]['user']
l = references[i]['label']
p = predictions[i]
if u not in predictions_per_user:
predictions_per_user[u] = []
predictions_per_user[u].append(p)
if u in labels_per_user:
assert(labels_per_user[u] == l), "Inconsistent labels for same user: %s" % u
else:
labels_per_user[u] = l
users = list(labels_per_user.keys())
penalties = []
for u in users:
# Latency only relevant for true positives
if labels_per_user[u] != 1 or sum(predictions_per_user[u]) == 0:
continue
i = 0
p = predictions_per_user[u][i]
latency = posts_per_datapoint
while (p != 1) and (i < len(predictions_per_user[u])):
latency += posts_per_datapoint
p = predictions_per_user[u][i]
i += 1
penalties.append(latency)
erde = np.median([_lc(p, o) for p in penalties])
return erde
def _compute(self, predictions, references, posts_per_datapoint):
assert(len(predictions)==len(references)), \
"Number of predictions not equal to number of references: %d vs %d." % (len(predictions), len(references))
predictions_per_user = {}
labels_per_user = {}
for i in range(len(predictions)):
u = references[i]['user']
l = references[i]['label']
p = predictions[i]
if u not in predictions_per_user:
predictions_per_user[u] = p
# User-level prediction is 1 if any 1 was emitted, otherwise it's 0
predictions_per_user[u] = (p or predictions_per_user[u])
if u in labels_per_user:
assert(labels_per_user[u] == l), "Inconsistent labels for same user: %s" % u
else:
labels_per_user[u] = l
users = list(labels_per_user.keys())
y_true = [labels_per_user[u] for u in users]
y_pred = [predictions_per_user[u] for u in users]
penalty_score = self._latency(predictions, references, posts_per_datapoint)
return {"precision": precision_score(y_true, y_pred),
"recall": recall_score(y_true, y_pred),
"f1": f1_score(y_true, y_pred),
"latency_f1": f1_score(y_true, y_pred) * (1 - penalty_score),
"erde5": self._erde(predictions, references, posts_per_datapoint, 5),
"erde50": self._erde(predictions, references, posts_per_datapoint, 50)}