FinGovBench / evaluate.py
redqueen77's picture
Add reproducible TF-IDF baseline and open-weight reference evaluation
f2ca97c verified
Raw History Blame Contribute Delete
2.64 kB
#!/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()