#!/usr/bin/env python3 """M5 error taxonomy by question family and RQ4 (EGS against the automatic score) under protocol v4. scripts/19_m5_errors.py read the complete T4 runs, write outputs/m5/errors/ Refuses to run unless configs/protocol_v4.yaml matches its checksum and every checkpoint carries the current M3-M4 input digests. Correctness is the automatic score against the XML answer; no human rater exists. """ from __future__ import annotations import json from pathlib import Path import duckdb import yaml from volve_dia import m34, m5_errors as me from volve_dia.m34_qa import qa_universe from volve_dia.m5_judge import sha256_file ROOT = Path(__file__).resolve().parents[1] PROTOCOL = ROOT / "configs/protocol_v4.yaml" PRIMARY = "vai-deepseek-v4-1-flash" RESAMPLES, SEED = 2000, 20261117 SHARE_COLUMNS = ("n",) + me.CLASSES def main(): expected = (PROTOCOL.with_suffix(".yaml.sha256")).read_text().split()[0] if sha256_file(PROTOCOL) != expected: raise SystemExit("configs/protocol_v4.yaml does not match its checksum") v4 = yaml.safe_load(PROTOCOL.read_text()) protocol, _ = m34.configuration() root, digests = m34.inputs(protocol) out = root / "m34" questions, paired, _, blocked = qa_universe(root, protocol) by_id = {q["id"]: q for q in questions} paired_ids = {q["id"] for q in paired} sets = {"c4": paired_ids, "c4_clean": paired_ids - blocked} all_ids = set(by_id) with duckdb.connect(str(root / "ddr/volve_ddr.duckdb"), read_only=True) as con: known = {r[0] for r in con.execute("SELECT report_id FROM reports").fetchall()} sources = [(db, PRIMARY) for db in sorted((out / f"v2/qa/{PRIMARY}").glob("rep*.sqlite"))] sources.append((out / "qa/qwen.sqlite", "qwen")) frame = me.load_predictions(sources, digests, by_id, known) frame, coverage = me.complete_runs(frame, lambda mode: sets.get(mode, all_ids)) frame = frame[frame["scope"].ne("field")].reset_index(drop=True) dest = root / "m5/errors" dest.mkdir(parents=True, exist_ok=True) tax = me.taxonomy(frame) tax.to_csv(dest / "taxonomy_by_run.csv", index=False) summary = me.mean_over_seeds(tax, ["model", "mode", "modality", "family"], SHARE_COLUMNS) summary.to_csv(dest / "taxonomy.csv", index=False) me.by_answer_type(frame).to_csv(dest / "by_answer_type.csv", index=False) rows = me.rq4(frame, RESAMPLES, SEED) egs = frame.groupby(["model", "mode", "modality", "seed"]).agg( n=("egs", "size"), mean_egs=("egs", "mean"), measurement=("measurement", "mean"), measurement_ext=("measurement_ext", "mean"), ddr_reference=("ddr_reference", "mean"), accuracy=("correct", "mean")).reset_index() egs.to_csv(dest / "egs_by_run.csv", index=False) frame.drop(columns=["answerable"]).to_parquet(dest / "predictions.parquet", index=False) macro = summary[summary["family"].eq("macro")] report = { "protocol": {"file": str(PROTOCOL.relative_to(ROOT)), "sha256": expected, "version": v4["version"]}, "inputs": digests, "reference": "automatic score against the XML answer (no human rater)", "coverage": coverage, "n_predictions_well_scoped": int(len(frame)), "taxonomy_macro": json.loads(macro.to_json(orient="records")), "rq4": rows, "h4": [me.h4_reading(rows, model) for model in sorted(frame["model"].unique())], "primary_model": PRIMARY, } m34.atomic_write(dest / "report.json", json.dumps(report, indent=1).encode()) for h in report["h4"]: print(h) print(macro[["model", "mode", "modality", "correct", "wrong_wellbore", "no_citation", "evidence_missed", "wrong_with_evidence", "abstained", "answered_unanswerable"]].round(3).to_string(index=False)) if __name__ == "__main__": main()