| |
| """ |
| Validator and metrics for space-ontology model output. |
| |
| Metrics, chosen to be checkable rather than impressive: |
| parse_rate fraction of Turtle outputs rdflib can parse |
| term_conformance fraction of outputs where EVERY ssao: term exists in SSAO |
| hallucinated_rate invented ssao: terms per output (the headline number) |
| namespace_fidelity fraction declaring the correct SSAO namespace IRI |
| class_accuracy fraction whose primary type matches the derived gold class |
| regime_accuracy fraction whose orbit class matches the derived gold regime |
| refusal_precision on questions the catalogue cannot answer, did the model refuse |
| """ |
| import json |
| import pathlib |
| import re |
|
|
| from rdflib import Graph |
|
|
| ROOT = pathlib.Path("/Users/fabio/projects/qwen-space-ft") |
| SSAO = "https://purl.org/space-ontology/" |
| VOCAB = json.loads((ROOT / "data" / "vocab.json").read_text()) |
| REAL = set(VOCAB["all_terms"]) |
|
|
| REFUSAL_MARKERS = ("cannot determine", "cannot be established", "unknown", |
| "does not record", "would be fabrication", "no orbit class applies", |
| "cannot discriminate", "not determined", "insufficient") |
|
|
|
|
| def strip_fence(t: str) -> str: |
| t = re.sub(r"^```[a-z]*\n?", "", t.strip(), flags=re.M) |
| return re.sub(r"```$", "", t, flags=re.M).strip() |
|
|
|
|
| def ssao_terms(text: str): |
| return set(re.findall(r"ssao:([A-Za-z0-9_]+)", text)) |
|
|
|
|
| def score_turtle(output: str, gold: str): |
| out = strip_fence(output) |
| res = {"parses": False, "terms_ok": False, "hallucinated": [], "ns_ok": False, |
| "class_match": None, "regime_match": None} |
| res["ns_ok"] = SSAO in out |
| used = ssao_terms(out) |
| bad = sorted(used - REAL) |
| res["hallucinated"] = bad |
| res["terms_ok"] = bool(used) and not bad |
| g = Graph() |
| try: |
| g.parse(data=out, format="turtle") |
| res["parses"] = True |
| except Exception: |
| pass |
| gold_terms = ssao_terms(gold) |
| gold_classes = {t for t in gold_terms if t in VOCAB["classes"]} |
| out_classes = {t for t in used if t in VOCAB["classes"]} |
| ORBITS = {"Low_Earth_Orbit", "Medium_Earth_Orbit", "Geosynchronous_Orbit", |
| "Geostationary_Orbit", "Graveyard_Orbit", "Highly_Elliptical_Orbit"} |
| gold_primary = sorted(gold_classes - ORBITS) |
| out_primary = sorted(out_classes - ORBITS) |
| if gold_primary: |
| res["class_match"] = bool(set(gold_primary) & set(out_primary)) |
| gold_orbit = sorted(gold_classes & ORBITS) |
| if gold_orbit: |
| res["regime_match"] = bool(set(gold_orbit) & (out_classes & ORBITS)) |
| return res |
|
|
|
|
| def is_refusal(output: str) -> bool: |
| low = output.lower() |
| return any(m in low for m in REFUSAL_MARKERS) |
|
|
|
|
| def summarise(records): |
| """records: list of {task, output, gold}""" |
| turtle = [r for r in records if r["task"] == "turtle"] |
| lookup = [r for r in records if r["task"] in ("lookup", "align", "regime")] |
| refusals = [r for r in records if r["task"] == "refusal"] |
| s = {} |
| if turtle: |
| scored = [score_turtle(r["output"], r["gold"]) for r in turtle] |
| n = len(scored) |
| s["turtle_n"] = n |
| s["parse_rate"] = round(sum(x["parses"] for x in scored) / n, 4) |
| s["term_conformance"] = round(sum(x["terms_ok"] for x in scored) / n, 4) |
| s["hallucinated_per_output"] = round(sum(len(x["hallucinated"]) for x in scored) / n, 4) |
| s["namespace_fidelity"] = round(sum(x["ns_ok"] for x in scored) / n, 4) |
| cm = [x["class_match"] for x in scored if x["class_match"] is not None] |
| rm = [x["regime_match"] for x in scored if x["regime_match"] is not None] |
| s["class_accuracy"] = round(sum(cm) / len(cm), 4) if cm else None |
| s["regime_accuracy"] = round(sum(rm) / len(rm), 4) if rm else None |
| halluc = {} |
| for x in scored: |
| for t in x["hallucinated"]: |
| halluc[t] = halluc.get(t, 0) + 1 |
| s["top_hallucinations"] = sorted(halluc.items(), key=lambda kv: -kv[1])[:8] |
| if lookup: |
| ok = 0 |
| for r in lookup: |
| gold_t = ssao_terms(r["gold"]) |
| out_t = ssao_terms(r["output"]) |
| |
| if gold_t: |
| ok += bool(gold_t & out_t) |
| else: |
| gold_verdict = "refuse" if "refuse" in r["gold"].lower()[:40] else "accept" |
| out_head = r["output"].lower()[:80] |
| ok += (gold_verdict in out_head) |
| s["lookup_align_regime_n"] = len(lookup) |
| s["lookup_align_regime_accuracy"] = round(ok / len(lookup), 4) |
| if refusals: |
| s["refusal_n"] = len(refusals) |
| s["refusal_rate"] = round(sum(is_refusal(r["output"]) for r in refusals) / len(refusals), 4) |
| return s |
|
|
|
|
| if __name__ == "__main__": |
| import sys |
| path = sys.argv[1] |
| recs = [json.loads(l) for l in open(path)] |
| print(json.dumps(summarise(recs), indent=2)) |
|
|