Repository navigation
Expand file tree
/
Copy pathevaluate.py
More file actions
50 lines (38 loc) · 1.98 KB
/
Copy pathevaluate.py
File metadata and controls
50 lines (38 loc) · 1.98 KB
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
"""Score Page Split's predictions on PoliTax-Split, or anyone's in the same format.
python evaluate.py --annotations path/to/annotations.jsonl [--predictions predictions/]
annotations.jsonl is PoliTax-Split's answer: https://huggingface.co/datasets/Extend-AI/PoliTax-Split
Each prediction is a JSON file {"file_name", "documents": [{"start_page", "end_page", "type"}]}.
Document F1: a predicted document counts when its first page, last page and type all match one in the answer.
Boundary F1: first pages only. Every score is computed for each PDF, then averaged over the PDFs (macro).
"""
import argparse
import json
from pathlib import Path
def f1(pred: set, gold: set):
tp = len(pred & gold)
p = tp / len(pred) if pred else 0.0
r = tp / len(gold) if gold else 0.0
return tp, p, r, (2 * p * r / (p + r) if p + r else 0.0)
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--annotations", required=True)
ap.add_argument("--predictions", default=str(Path(__file__).parent / "predictions"))
a = ap.parse_args()
gold = {}
for line in open(a.annotations):
row = json.loads(line)
gold[row["file_name"]] = row["sub_documents"]
key = lambda d: (d["start_page"], d["end_page"], d["type"])
rows = []
for path in sorted(Path(a.predictions).glob("*.json")):
pred = json.loads(path.read_text())
g = gold[pred["file_name"]]
_, p, r, doc_f1 = f1({key(d) for d in pred["documents"]}, {key(d) for d in g})
*_, b_f1 = f1({d["start_page"] for d in pred["documents"]}, {d["start_page"] for d in g})
rows.append((pred["file_name"], p, r, doc_f1, b_f1))
print(f"{pred['file_name'][:60]:60s} document F1 {100 * doc_f1:5.1f} boundary F1 {100 * b_f1:5.1f}")
mean = lambda i: 100 * sum(row[i] for row in rows) / len(rows)
print(f"\n{len(rows)} PDFs: document F1 {mean(3):.1f} (precision {mean(1):.1f}, recall {mean(2):.1f}), "
f"boundary F1 {mean(4):.1f}")
if __name__ == "__main__":
main()