-
Notifications
You must be signed in to change notification settings - Fork 1
/
evaluation.py
157 lines (132 loc) · 4.86 KB
/
evaluation.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
import sys
import numpy as np
from itertools import chain
from sklearn import metrics
from tree_util import salzburg_ted, tree_similarity_ratio
def precision_recall_f1(true_clusters, pred_clusters):
true_entries = set(chain(*true_clusters))
pred_entries = set(chain(*pred_clusters))
intersect = true_entries.intersection(pred_entries)
if len(pred_entries) == 0:
sys.stderr.write("len(pred_entries) is 0")
p = 0
else:
p = float(len(intersect)) / len(pred_entries)
r = float(len(intersect)) / len(true_entries)
f1 = 2 * p * r / (p + r)
return p, r, f1
def convert_to_cluster_assignment_array(
true_clusters, pred_clusters, all_entry_ids, true_only=True):
true_id2label = {}
for i, clst in enumerate(true_clusters):
true_id2label.update({elem: i+1
for elem in clst})
pred_id2label = {}
for i, clst in enumerate(pred_clusters):
pred_id2label.update({elem: i+1
for elem in clst})
if true_only:
true_ids = sorted(chain(*true_clusters))
true_labels = map(lambda i: true_id2label[i],
true_ids)
pred_labels = map(lambda i: pred_id2label.get(i, 0),
true_ids)
else:
all_entry_ids = sorted(all_entry_ids)
true_labels = map(lambda i: true_id2label.get(i, 0),
all_entry_ids)
pred_labels = map(lambda i: pred_id2label.get(i, 0),
all_entry_ids)
return true_labels, pred_labels
def evaluate_clustering_result(
true_clusters, pred_clusters,
all_entry_ids,
metric, true_only=True):
"""
pred_clusters, true_clusters: array of list[int]
all_entry_ids: list[int]
"""
true_labels, pred_labels = convert_to_cluster_assignment_array(
true_clusters, pred_clusters, all_entry_ids,
true_only
)
return metric(true_labels, pred_labels)
def events2clusters(events):
return [[i['message_id'] for i in e]for e in events]
def trees2clusters(trees):
return [t.nodes() for t in trees]
def evaluate_meta_tree_result(
true_events, pred_events, all_entry_ids, methods):
setcover_obj = len(set([n for t in pred_events for n in t.nodes_iter()]))
scores = {
'set_cover_obj': setcover_obj
}
true_clusters = trees2clusters(true_events)
pred_clusters = trees2clusters(pred_events)
for m in methods:
for true_only in (True, False):
name = m.__name__
if not true_only:
name += "(all)"
scores[name] = evaluate_clustering_result(
true_clusters, pred_clusters,
all_entry_ids,
m,
true_only)
p, r, f1 = precision_recall_f1(true_clusters, pred_clusters)
scores['precision'] = p
scores['recall'] = r
scores['f1'] = f1
if 'calculation_time' in pred_events[0].graph:
scores['log(running_time)'] = np.log(np.mean(
[t.graph['calculation_time'] for t in pred_events]
))
# mean of tree edit distance across all (true, pred) pairs
# weighted mean can be added
# scores['tree_similarity'] = np.mean(
# [tree_similarity_ratio(
# salzburg_ted(true, pred),
# true, pred
# )
# for true, pred in zip(true_events, pred_events)]
# )
return scores
def main():
import os
import cPickle as pkl
import pandas as pd
from util import json_load
from max_cover import k_best_trees
import argparse
parser = argparse.ArgumentParser('Evaluate the events')
parser.add_argument('-c', '--cand_trees_path', required=True, nargs='+')
parser.add_argument('--interactions_path', required=True)
parser.add_argument('--events_path', required=True)
args = parser.parse_args()
interactions = json_load(args.interactions_path)
true_events = json_load(args.events_path)
methods = [metrics.adjusted_rand_score,
metrics.adjusted_mutual_info_score,
metrics.homogeneity_score,
metrics.completeness_score,
metrics.v_measure_score]
K = 10
indexes = []
scores = []
for p in args.cand_trees_path:
cand_trees = pkl.load(open(p))
pred_trees = k_best_trees(cand_trees, K)
indexes.append(os.path.basename(p))
scores.append(evaluate_meta_tree_result(
true_events,
pred_trees,
[i['message_id'] for i in interactions],
methods
))
df = pd.DataFrame(scores, index=indexes,
columns=[m.__name__ for m in methods] +
[m.__name__ + "(all)" for m in methods] +
['precision', 'recall', 'f1'])
df.to_csv('tmp/evaluation.csv')
if __name__ == '__main__':
main()