ModuleMap-Bench / scripts /evaluate_roles.py
alejandrosalastexido's picture
ModuleMap-Bench 1.0.0
e00f999 verified
Raw History Blame Contribute Delete
1.63 kB
#!/usr/bin/env python3
"""Precision and recall per role of a role predictor against the labels.
python3 scripts/evaluate_roles.py predictions.jsonl [--projects kiwi,mall-swarm]
predictions.jsonl holds one {"id": "<project>/<qualified name>", "role": "<role>"} per line;
a missing id counts as "none". "none" predicted is no prediction: a miss counts against
the labelled role, not as a false positive of "none". See results/agentkit-roles.predictions.jsonl.
"""
import argparse
from common import ROLES, data, read_jsonl
p = argparse.ArgumentParser()
p.add_argument("predictions")
p.add_argument("--projects", default="")
a = p.parse_args()
only = {x for x in a.projects.split(",") if x}
labels = [l for l in read_jsonl(data("roles", "labels.jsonl")) if not only or l["project"] in only]
pred = {r["id"]: r["role"] for r in read_jsonl(a.predictions)}
c = {r: {"tp": 0, "fp": 0, "fn": 0, "n": 0} for r in ROLES}
for l in labels:
got = pred.get(l["id"], "none")
c[l["role"]]["n"] += 1
if got == l["role"]:
c[l["role"]]["tp"] += 1
else:
c[l["role"]]["fn"] += 1
if got != "none":
c[got]["fp"] += 1
pct = lambda n, d: f"{100 * n / d:5.1f} %" if d else " — "
print(f"{'role':<12} {'precision':>10} {'recall':>10} {'labelled':>9}")
for r in ROLES:
x = c[r]
if x["n"] or x["fp"]:
prec = " — " if r == "none" else pct(x["tp"], x["tp"] + x["fp"])
print(f"{r:<12} {prec:>10} {pct(x['tp'], x['tp'] + x['fn']):>10} {x['n']:>9}")
right = sum(x["tp"] for x in c.values())
print(f"accuracy {pct(right, len(labels)).strip()} ({right}/{len(labels)})")