-
Notifications
You must be signed in to change notification settings - Fork 1
/
symbolic_metrics.py
229 lines (172 loc) · 10.6 KB
/
symbolic_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
import numpy as np
import data_preprocessing
import utils
import label
import granularity
def get_l_detection_rule_support(edcr,
test: bool,
l: data_preprocessing.label) -> float:
if l not in edcr.error_detection_rules:
return 0
test_or_train = 'test' if test else 'train'
N_l = np.sum(edcr.get_where_label_is_l(pred=True, test=test, l=l))
r_l = edcr.error_detection_rules[l]
where_l_detection_rule_body_is_satisfied = (
r_l.get_where_body_is_satisfied(
pred_fine_data=edcr.pred_data[test_or_train]['original'][edcr.preprocessor.granularities['fine']],
pred_coarse_data=edcr.pred_data[test_or_train]['original'][edcr.preprocessor.granularities['coarse']]))
num_predicted_l_and_any_conditions_satisfied = np.sum(where_l_detection_rule_body_is_satisfied)
s_l = num_predicted_l_and_any_conditions_satisfied / N_l
assert s_l <= 1
return s_l
def get_l_detection_rule_confidence(edcr,
test: bool,
l: label.Label) -> float:
if l not in edcr.error_detection_rules:
return 0
test_or_train = 'test' if test else 'train'
r_l = edcr.error_detection_rules[l]
where_l_detection_rule_body_is_satisfied = (
r_l.get_where_body_is_satisfied(
pred_fine_data=edcr.pred_data[test_or_train]['original'][edcr.preprocessor.granularities['fine']],
pred_coarse_data=edcr.pred_data[test_or_train]['original'][edcr.preprocessor.granularities['coarse']]))
where_l_fp = edcr.get_where_fp_l(test=test, l=l)
where_head_and_body_is_satisfied = where_l_detection_rule_body_is_satisfied * where_l_fp
num_where_l_detection_rule_body_is_satisfied = np.sum(where_l_detection_rule_body_is_satisfied)
if num_where_l_detection_rule_body_is_satisfied == 0:
return 0
c_l = np.sum(where_head_and_body_is_satisfied) / num_where_l_detection_rule_body_is_satisfied
return c_l
def get_l_detection_rule_theoretical_precision_increase(edcr,
test: bool,
l: label.Label) -> float:
s_l = edcr.get_l_detection_rule_support(test=test, l=l)
if s_l == 0:
return 0
c_l = edcr.get_l_detection_rule_confidence(test=test, l=l)
p_l = edcr.get_l_precision_and_recall(l=l, test=test, stage='original')[0]
return s_l / (1 - s_l) * (c_l + p_l - 1)
def get_g_detection_rule_theoretical_precision_increase(edcr,
test: bool,
g: granularity.Granularity):
precision_increases = [edcr.get_l_detection_rule_theoretical_precision_increase(test=test, l=l)
for l in edcr.preprocessor.get_labels(g).values()]
return np.mean(precision_increases)
def get_l_detection_rule_theoretical_recall_decrease(edcr,
test: bool,
l: label.Label) -> float:
c_l = edcr.get_l_detection_rule_confidence(test=test, l=l)
s_l = edcr.get_l_detection_rule_support(test=test, l=l)
p_l, r_l = edcr.get_l_precision_and_recall(l=l, test=test, stage='original')
return (1 - c_l) * s_l * r_l / p_l
def get_g_detection_rule_theoretical_recall_decrease(edcr,
test: bool,
g: granularity.Granularity):
recall_decreases = [edcr.get_l_detection_rule_theoretical_recall_decrease(test=test, l=l)
for l in edcr.preprocessor.get_labels(g).values()]
return np.mean(recall_decreases)
def get_l_correction_rule_confidence(edcr,
test: bool,
l: label.Label,
pred_fine_data: np.array = None,
pred_coarse_data: np.array = None
) -> float:
if l not in edcr.error_correction_rules:
return 0
test_or_train = 'test' if test else 'train'
r_l = edcr.error_correction_rules[l]
where_l_correction_rule_body_is_satisfied = (
r_l.get_where_body_is_satisfied(
fine_data=edcr.pred_data[test_or_train]['post_correction'][edcr.preprocessor.granularities['fine']]
if pred_fine_data is None else pred_fine_data,
coarse_data=edcr.pred_data[test_or_train]['post_correction'][edcr.preprocessor.granularities['coarse']]
if pred_coarse_data is None else pred_coarse_data))
where_l_gt = edcr.get_where_label_is_l(pred=False, test=test, l=l)
where_head_and_body_is_satisfied = where_l_correction_rule_body_is_satisfied * where_l_gt
num_where_l_correction_rule_body_is_satisfied = np.sum(where_l_correction_rule_body_is_satisfied)
if num_where_l_correction_rule_body_is_satisfied == 0:
return 0
c_l = np.sum(where_head_and_body_is_satisfied) / num_where_l_correction_rule_body_is_satisfied
return c_l
def get_l_correction_rule_support(edcr,
test: bool,
l: label.Label,
pred_fine_data: np.array = None,
pred_coarse_data: np.array = None
) -> float:
if l not in edcr.error_correction_rules:
return 0
test_or_train = 'test' if test else 'train'
N_l = np.sum(edcr.get_where_label_is_l(pred=True, test=test, l=l, stage='post_correction')
if (pred_fine_data is None and pred_coarse_data is None)
else edcr.get_where_label_is_l_in_data(l=l,
test_pred_fine_data=pred_fine_data,
test_pred_coarse_data=pred_coarse_data))
if N_l == 0:
return 0
r_l = edcr.error_correction_rules[l]
where_rule_body_is_satisfied = (
r_l.get_where_body_is_satisfied(
fine_data=edcr.pred_data[test_or_train]['post_correction'][edcr.preprocessor.granularities['fine']]
if pred_fine_data is None else pred_fine_data,
coarse_data=edcr.pred_data[test_or_train]['post_correction'][
edcr.preprocessor.granularities['coarse']]
if pred_coarse_data is None else pred_coarse_data))
s_l = np.sum(where_rule_body_is_satisfied) / N_l
return s_l
def get_l_correction_rule_theoretical_precision_increase(edcr,
test: bool,
l: label.Label) -> float:
c_l = edcr.get_l_correction_rule_confidence(test=test, l=l)
s_l = edcr.get_l_correction_rule_support(test=test, l=l)
p_l_prior_correction = edcr.get_l_precision_and_recall(l=l,
test=test,
stage='post_correction')[0]
return s_l * (c_l - p_l_prior_correction) / (1 + s_l)
def get_g_correction_rule_theoretical_precision_increase(edcr,
test: bool,
g: granularity.Granularity):
precision_increases = [edcr.get_l_correction_rule_theoretical_precision_increase(test=test, l=l)
for l in edcr.preprocessor.get_labels(g).values()]
return np.mean(precision_increases)
def evaluate_and_print_g_detection_rule_precision_increase(edcr,
test: bool,
g: granularity.Granularity,
threshold: float = 1e-5):
original_g_precisions = edcr.get_g_precision_and_recall(g=g, test=test, stage='original')[0]
post_detection_g_precisions = edcr.get_g_precision_and_recall(g=g, test=test, stage='post_detection')[0]
original_g_mean_precision = np.mean(list(original_g_precisions.values()))
post_detection_mean_precision = np.mean(list(post_detection_g_precisions.values()))
precision_diff = post_detection_mean_precision - original_g_mean_precision
detection_rule_theoretical_precision_increase = (
edcr.get_g_detection_rule_theoretical_precision_increase(test=test, g=g))
precision_theory_holds = abs(detection_rule_theoretical_precision_increase - precision_diff) < threshold
precision_theory_holds_str = utils.green_text('The theory holds!') if precision_theory_holds else (
utils.red_text('The theory does not hold!'))
print('\n' + '#' * 20 + f'post detection {g}-grain precision results' + '#' * 20)
print(f'{g}-grain new mean precision: {post_detection_mean_precision}, '
f'{g}-grain old mean precision: {original_g_mean_precision}, '
f'diff: {utils.blue_text(precision_diff)}\n'
f'theoretical precision increase: {utils.blue_text(detection_rule_theoretical_precision_increase)}\n'
f'{precision_theory_holds_str}'
)
def evaluate_and_print_g_detection_rule_recall_decrease(edcr,
test: bool,
g: granularity.Granularity,
threshold: float = 1e-5):
original_g_recalls = edcr.get_g_precision_and_recall(g=g, test=test, stage='original')[1]
post_detection_recalls = edcr.get_g_precision_and_recall(g=g, test=test, stage='post_detection')[1]
original_g_mean_recall = np.mean(list(original_g_recalls.values()))
post_detection_g_mean_recall = np.mean(list(post_detection_recalls.values()))
recall_diff = post_detection_g_mean_recall - original_g_mean_recall
detection_rule_theoretical_recall_decrease = (
edcr.get_g_detection_rule_theoretical_recall_decrease(test=test, g=g))
recall_theory_holds = abs(abs(detection_rule_theoretical_recall_decrease) - abs(recall_diff)) < threshold
recall_theory_holds_str = utils.green_text('The theory holds!') if recall_theory_holds else (
utils.red_text('The theory does not hold!'))
print('\n' + '#' * 20 + f'post detection {g}-grain recall results' + '#' * 20)
print(f'{g}-grain new mean recall: {post_detection_g_mean_recall}, '
f'{g}-grain old mean recall: {original_g_mean_recall}, '
f'diff: {utils.blue_text(recall_diff)}\n'
f'theoretical recall decrease: -{utils.blue_text(detection_rule_theoretical_recall_decrease)}\n'
f'{recall_theory_holds_str}')