forked from eBay/LatentScope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluator.py
166 lines (145 loc) · 6.96 KB
/
evaluator.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
import os
import json
import numpy as np
from loguru import logger
from models import CaseMetric, RootCause, RCC
from typing import *
class Evaluator:
def __init__(self, dataset: str, top_ks=[1, 5, 10], mrr_ks=[3, 5, 10]):
self.dataset = dataset
with open(os.path.join('data', dataset, 'labels', 'label.json'), 'rt') as f:
self.label = json.load(f)
self.top_ks = top_ks
self.mrr_ks = mrr_ks
self.result = {}
self.case_type: Dict[str, str] = {}
def evaluate_case(self, case: str, rank: List[RootCause], metrics: CaseMetric):
self.result[case] = {}
cur_rc = self.label[case]['rc']
self.case_type[case] = cur_rc[0]['type']
# Rank
min_rank = 999999999
min_ranks = [min_rank]
for rc in cur_rc:
if rc not in rank:
continue
rc_score = rank[rank.index(rc)].score
candidate_ranks = [(i + 1) for i in range(len(rank)) if abs(rank[i].score - rc_score) < 1e-3]
if candidate_ranks[0] < min_rank:
min_rank = int(np.mean(candidate_ranks) + 0.5)
min_ranks = candidate_ranks
self.result[case]['topk'] = []
for i, k in enumerate(self.top_ks):
cur_scores = []
for min_rank in min_ranks:
if min_rank <= k:
cur_scores.append(1)
else:
cur_scores.append(0)
self.result[case]['topk'].append(np.mean(cur_scores))
self.result[case]['rank'] = np.mean(min_ranks)
def print_result(self):
# Report the result
report: str = "--------------Report-------------\n"
for case in sorted(self.result):
report += f"Case {case}: "
for j, k in enumerate(self.top_ks):
report += f"Top {k}: {self.result[case]['topk'][j]}, "
report += f"Rank: {self.result[case]['rank']}, "
report += f"Type: {self.case_type[case]}"
report += '\n'
# Calculate avg value
valid_cases = [i for i in self.result.values() if 'rank' in i and 'topk' in i]
topk = np.mean(np.stack([i['topk'] for i in valid_cases]), axis=0)
report += f"============Micro ALL ({len(valid_cases)})==============\n"
for j, k in enumerate(self.top_ks):
report += f"Top {k}: {topk[j]:.4f}, "
report += "\n"
# Calculate MRR
overall_mrr = np.mean(1 / np.array([i['rank'] for i in valid_cases]))
report += f"MRR: {overall_mrr:.4f}"
for k in self.mrr_ks:
cur_mrr = float(np.mean([(1.0 / i['rank'] if i['rank'] < k else 0.0) for i in valid_cases]))
report += f", MRR@{k}: {cur_mrr:.4f}"
report += '\n'
# Calculate Type Value
per_type_values: Dict[str, List[float]] = {}
for cur_type in set(self.case_type.values()):
valid_cases = [self.result[case] for case in self.result if 'rank' in self.result[case] and self.case_type[case] == cur_type]
topk = np.mean(np.stack([i['topk'] for i in valid_cases]), axis=0)
report += f"============{cur_type} cases ({len(valid_cases)})==============\n"
for j, k in enumerate(self.top_ks):
per_type_values.setdefault(f"Top {k}", [])
per_type_values[f"Top {k}"].append(topk[j])
report += f"Top {k}: {topk[j]:.4f}, "
report += "\n"
mrr = np.mean(1 / np.array([i['rank'] for i in valid_cases]))
report += f"MRR: {mrr:.4f}"
per_type_values.setdefault(f"MRR", [])
per_type_values[f"MRR"].append(mrr)
for k in self.mrr_ks:
cur_mrr = float(np.mean([(1.0 / i['rank'] if i['rank'] < k else 0.0) for i in valid_cases]))
report += f", MRR@{k}: {cur_mrr:.4f}"
per_type_values.setdefault(f"MRR@{k}", [])
per_type_values[f"MRR@{k}"].append(cur_mrr)
report += '\n'
# Show Macro values
report += f"==================Macro Values======================\n"
for k in per_type_values:
report += f"{k}: {np.mean(per_type_values[k])}\t"
report += '\n'
logger.info(report)
return float(overall_mrr)
def save_result(self, save_path: str):
# Report the result
report: str = "--------------Report-------------\n"
for case in sorted(self.result):
report += f"Case {case}: "
for j, k in enumerate(self.top_ks):
report += f"Top {k}: {self.result[case]['topk'][j]}, "
report += f"Rank: {self.result[case]['rank']}, "
report += f"Type: {self.case_type[case]}"
report += '\n'
# Calculate avg value
valid_cases = [i for i in self.result.values() if 'rank' in i and 'topk' in i]
topk = np.mean(np.stack([i['topk'] for i in valid_cases]), axis=0)
report += f"============Micro ALL ({len(valid_cases)})==============\n"
for j, k in enumerate(self.top_ks):
report += f"Top {k}: {topk[j]:.4f}, "
report += "\n"
# Calculate MRR
mrr = np.mean(1 / np.array([i['rank'] for i in valid_cases]))
report += f"MRR: {mrr:.4f}"
for k in self.mrr_ks:
cur_mrr = float(np.mean([(1.0 / i['rank'] if i['rank'] < k else 0.0) for i in valid_cases]))
report += f", MRR@{k}: {cur_mrr:.4f}"
report += '\n'
# Calculate Type Value
per_type_values: Dict[str, List[float]] = {}
for cur_type in set(self.case_type.values()):
valid_cases = [self.result[case] for case in self.result if 'rank' in self.result[case] and self.case_type[case] == cur_type]
topk = np.mean(np.stack([i['topk'] for i in valid_cases]), axis=0)
report += f"============{cur_type} cases ({len(valid_cases)})==============\n"
for j, k in enumerate(self.top_ks):
per_type_values.setdefault(f"Top {k}", [])
per_type_values[f"Top {k}"].append(topk[j])
report += f"Top {k}: {topk[j]:.4f}, "
report += "\n"
mrr = np.mean(1 / np.array([i['rank'] for i in valid_cases]))
report += f"MRR: {mrr:.4f}"
per_type_values.setdefault(f"MRR", [])
per_type_values[f"MRR"].append(mrr)
for k in self.mrr_ks:
cur_mrr = float(np.mean([(1.0 / i['rank'] if i['rank'] < k else 0.0) for i in valid_cases]))
report += f", MRR@{k}: {cur_mrr:.4f}"
per_type_values.setdefault(f"MRR@{k}", [])
per_type_values[f"MRR@{k}"].append(cur_mrr)
report += '\n'
# Show Macro values
report += f"==================Macro Values======================\n"
for k in per_type_values:
report += f"{k}: {np.mean(per_type_values[k])}\t"
report += '\n'
# Save result
with open(save_path, 'wt') as f:
f.write(report)