File size: 2,639 Bytes
f2cf1f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2ca97c
 
 
 
 
f2cf1f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Score FinGovBench predictions on a public development or validation split."""

from __future__ import annotations

import argparse
import json
from pathlib import Path


def load(path: Path) -> dict[str, dict]:
    return {row["case_id"]: row for row in (json.loads(line) for line in path.read_text().splitlines() if line.strip())}


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("--predictions", type=Path, required=True)
    parser.add_argument("--labels", type=Path, required=True)
    args = parser.parse_args()
    predictions, labels = load(args.predictions), load(args.labels)
    missing = set(labels) - set(predictions)
    if missing:
        raise SystemExit(f"predictions are missing {len(missing)} labelled case IDs")
    # A submission may contain predictions for every split.  Score only the IDs
    # present in the requested public label file.
    predictions = {case_id: predictions[case_id] for case_id in labels}
    action = unsafe = exact_path = exact_scope = 0
    reviewed = required = missed = 0
    for case_id, label_row in labels.items():
        gold, pred = label_row["gold"], predictions[case_id]
        action += pred["governance_action"] == gold["governance_action"]
        unsafe += bool(pred["release_permitted"]) and not gold["release_permitted"]
        predicted_path = set(pred.get("affected_components", pred.get("affected_nodes", [])))
        target_path = set(gold.get("affected_components", gold.get("affected_nodes", [])))
        predicted_scope = set(pred.get("revalidation_components", pred.get("revalidation_scope", [])))
        target_scope = set(gold.get("revalidation_components", gold.get("revalidation_scope", [])))
        exact_path += predicted_path == target_path
        exact_scope += predicted_scope == target_scope
        reviewed_values = pred.get(
            "reviewed_components",
            pred.get("reviewed_nodes", pred.get("revalidation_components", pred.get("revalidation_scope", []))),
        )
        reviewed += len(set(reviewed_values))
        required += len(target_scope)
        missed += len(target_scope - predicted_scope)
    n = len(labels)
    print(json.dumps({
        "cases": n,
        "governance_action_accuracy": action / n,
        "unsafe_release_rate": unsafe / n,
        "exact_affected_path_rate": exact_path / n,
        "exact_revalidation_scope_rate": exact_scope / n,
        "missed_required_review_rate": missed / required if required else 0.0,
        "mean_reviewed_nodes": reviewed / n,
    }, indent=2, sort_keys=True))


if __name__ == "__main__":
    main()