-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcompute_statistics.py
297 lines (264 loc) · 10.5 KB
/
compute_statistics.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
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
""" Measure the performance of AFP algorithms.
Compute statistics and metrics to measure the performance of an AFP algorithm.
This code has been used in the publication 'BAF: An Audio Fingerprinting Dataset
for Broadcast Monitoring' by Guillem Cortès, Alex Ciurana, Emilio Molina, Marius
Miron, Owen Meyers, Joren Six and Xavier Serra.
"""
import argparse
import pprint
import pandas as pd
import intervaltree as itree
def create_tree(groundtruths, results, threshold=None):
"""Creates intervaltree with Groundtruth and Tech results.
Args:
results (DataFrame): Tech results pandas DataFrame.
groundtruths (DataFrame): Groundtruth (annotations) pandas DataFrame.
Returns:
tree (intervaltree): tree.
"""
def _add_to_tree(tree, segments, tag):
"""Add relevant segments to the tree.
Args:
tree (intervaltree): tree.
segments (DataFrame): Segments desired to be added to the tree.
tag (str): "GT" or "RS". Segment tag.
Returns:
tree (intervaltree): updated tree.
"""
for idx, seg in segments.iterrows():
if (seg["query_end"] - seg["query_start"]) <= 0:
# intervaltree doesn't allow 0-length segments
continue
if threshold is not None and tag == "RS" and seg["score"] < threshold:
continue
interval = itree.Interval(
seg["query_start"], seg["query_end"], [f"{tag} {idx}"]
)
if seg["query"] not in tree:
tree[seg["query"]] = {}
if seg["reference"] not in tree[seg["query"]]:
tree[seg["query"]][seg["reference"]] = itree.IntervalTree()
tree[seg["query"]][seg["reference"]].add(interval)
return tree
tree = {}
tree = _add_to_tree(tree, groundtruths, "GT")
tree = _add_to_tree(tree, results, "RS")
return tree
def extract_ids_from_segments(segments, status, tag):
"""Extract ids from segments with specific status and type (tag).
Args:
segments (DataFrame): DataFrame
status (str): "TP" True Positive, "FP" False Positive,
"TN" True Negative, "FN" False Negative.
tag (str): "GT" or "RS". Segment tag.
Returns:
ids_unique (list): list with unique ids.
"""
ids = []
for _, row in segments.query(f'status == "{status}"').iterrows():
ids += [int(v.split(" ")[1]) for v in row["debug"] if v.startswith(tag)]
ids_unique = set(ids)
return ids_unique
def sum_seconds_from_segments(segments, status, tag):
"""Sum seconds from segments.
Filtering segments by status and type (tag). This method counts duplicate
matches.
Args:
segments (DataFrame): DataFrame
status (str): "TP" True Positive, "FP" False Positive,
"TN" True Negative, "FN" False Negative.
tag (str): "GT" or "RS". Segment tag.
Returns:
seconds (float): Summed seconds from segments.
"""
seconds = 0
for _, row in segments.query(f'status == "{status}"').iterrows():
number_segments = len([l for l in row["debug"] if l.startswith(tag)])
seconds += number_segments * (row.end - row.begin)
return seconds
def compute_statistics(results, groundtruths, threshold=None):
"""Compute statistics.
Args:
results (DataFrame): Tech results pandas DataFrame.
groundtruths (DataFrame): Groundtruth (annotations) pandas DataFrame.
Returns:
out_stats (dict): Dictionary with stats values.
"""
def _merge_segments_data(data1, data2):
"""Merge overlapping segments in intervaltree."""
return data1 + data2
out_stats = {}
# MERGE RESULTS AND GROUNDTRUTH
mixed_tree = create_tree(groundtruths, results, threshold)
for query, ref_trees in mixed_tree.items():
for ref, tree in ref_trees.items():
tree.split_overlaps()
tree.merge_overlaps(_merge_segments_data)
# CLASSIFY MERGED SEGMENTS INTO TP, FP, FN...
mixed_segments = []
for query, ref_trees in mixed_tree.items():
for ref, tree in ref_trees.items():
for interval in sorted(tree.items()):
status = "UNK"
types_found = sorted(
list(set([l.split(" ")[0] for l in interval.data]))
)
if types_found == ["GT"]:
status = "FN"
elif types_found == ["RS"]:
status = "FP"
elif types_found == ["GT", "RS"]:
status = "TP"
mixed_segments.append(
{
"query": query,
"ref": ref,
"begin": interval.begin,
"end": interval.end,
"status": status,
"debug": interval.data,
}
)
mixed_df = pd.DataFrame(mixed_segments)
mixed_df.sort_values(by=["query", "ref", "begin", "end"], inplace=True)
# UNIQUES
tps_rs_unique = extract_ids_from_segments(mixed_df, "TP", "RS")
fps_rs_unique = extract_ids_from_segments(mixed_df, "FP", "RS")
fps_rs_unique -= tps_rs_unique
tps_gt_unique = extract_ids_from_segments(mixed_df, "TP", "GT")
fns_gt_unique = extract_ids_from_segments(mixed_df, "FN", "GT")
fns_gt_unique -= tps_gt_unique
# Results segments precision
precision = len(tps_rs_unique) / (len(tps_rs_unique) + len(fps_rs_unique))
# GroundTruth segments recall
recall_gt = len(tps_gt_unique) / (len(tps_gt_unique) + len(fns_gt_unique))
out_stats["uniques"] = {
"TP_results": len(tps_rs_unique),
"TP_groundtruth": len(tps_gt_unique),
"FP_results": len(fps_rs_unique),
"FN_groundtruth": len(fns_gt_unique),
"TP_match_ratio": len(tps_rs_unique) / len(tps_gt_unique),
"metrics": {"precision": precision, "recall_GT": recall_gt},
"check": {
"TP_plus_FP_results": len(tps_rs_unique) + len(fps_rs_unique), # results
"TP_plus_FN_groundtruth": len(tps_gt_unique) + len(fns_gt_unique), # GT
},
}
# SECONDS
tp_seconds_with_duplicates = sum_seconds_from_segments(mixed_df, "TP", "RS")
fp_seconds_with_duplicates = sum_seconds_from_segments(mixed_df, "FP", "RS")
fn_seconds_with_duplicates = sum_seconds_from_segments(mixed_df, "FN", "GT")
precision_sec = tp_seconds_with_duplicates / (
tp_seconds_with_duplicates + fp_seconds_with_duplicates
)
recall_sec = tp_seconds_with_duplicates / (
tp_seconds_with_duplicates + fn_seconds_with_duplicates
)
f1_score_sec = 2 * precision_sec * recall_sec / (precision_sec + recall_sec)
out_stats["seconds"] = {
"TPs": tp_seconds_with_duplicates,
"FPs": fp_seconds_with_duplicates,
"FNs": fn_seconds_with_duplicates,
"metrics": {
"precision": precision_sec,
"recall": recall_sec,
"f1-score": f1_score_sec,
},
"check": {
"TP_plus_FP": (tp_seconds_with_duplicates + fp_seconds_with_duplicates),
"TP_plus_FN": (tp_seconds_with_duplicates + fn_seconds_with_duplicates),
},
}
tp_seconds = sum(
[row.end - row.begin for _, row in mixed_df.query('status == "TP"').iterrows()]
)
fp_seconds = sum(
[row.end - row.begin for _, row in mixed_df.query('status == "FP"').iterrows()]
)
fn_seconds = sum(
[row.end - row.begin for _, row in mixed_df.query('status == "FN"').iterrows()]
)
overlength_seconds = 0
for _, row in mixed_df.query('status == "FP"').iterrows():
rs_ids = set([int(v.split(" ")[1]) for v in row["debug"] if v.startswith("RS")])
rs_ids -= tps_rs_unique
if len(rs_ids) > 0:
overlength_seconds += row.end - row.begin
precision_sec_uniq = tp_seconds / (tp_seconds + fp_seconds)
recall_sec_uniq = tp_seconds / (tp_seconds + fn_seconds)
f1_score_sec_uniq = (
2
* precision_sec_uniq
* recall_sec_uniq
/ (precision_sec_uniq + recall_sec_uniq)
)
out_stats["seconds_without_duplicates"] = {
"TPs": tp_seconds,
"FPs": fp_seconds,
"FPs_subtypes": {
"FPs_overlength": overlength_seconds,
"FPs_other": fp_seconds - overlength_seconds,
},
"FNs": fn_seconds,
"metrics": {
"precision": precision_sec_uniq,
"recall": recall_sec_uniq,
"f1-score": f1_score_sec_uniq,
},
"check": {
"TP_plus_FP": tp_seconds + fp_seconds,
"TP_plus_FN": tp_seconds + fn_seconds,
},
}
return out_stats
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=(
"Compute statistics and metrics to measure the performance "
"of an AFP algorithm."
)
)
parser.add_argument(
"results_file",
help=(
"Results file path. It has to be as csv with at least the following "
"fields in the header: {query, reference, query_start, query_end,"
"ref_start, ref_end, score}"
),
)
parser.add_argument("annotations_file", help="Annotations groundtruth file path.")
parser.add_argument(
"-t", "--xtag", default="unanimity", help="Cross-Annotations tag."
)
parser.add_argument(
"-th", "--threshold", default=None, type=float, help="Custom score threshold."
)
args = parser.parse_args()
stats = {}
stats["input"] = {
"results_file": args.results_file,
"groundtruth_file": args.annotations_file,
"cross_annotation_tag": args.xtag,
}
# LOAD RESULTS
res = pd.read_csv(args.results_file)
stats["results"] = {
"num": len(res),
"seconds": sum([r.query_end - r.query_start for _, r in res.iterrows()]),
}
# LOAD GROUNDTRUTH
gts = pd.read_csv(args.annotations_file)
if args.xtag == "unanimity":
gts = gts[gts["x_tag"].isin(["unanimity"])]
elif args.xtag == "majority":
gts = gts[gts["x_tag"].isin(["unanimity", "majority"])]
elif args.xtag == "single":
gts = gts[gts["x_tag"].isin(["unanimity", "majority", "single"])]
gts.reset_index(inplace=True, drop=True)
stats["groundtruth"] = {
"num": len(gts),
"seconds": sum([gt.query_end - gt.query_start for _, gt in gts.iterrows()]),
}
# COMPUTE STATISTICS
stats.update(compute_statistics(res, gts, args.threshold))
pprint.pprint(stats)