From 5e85cf10186507427c78e296f4ba68c3b085e765 Mon Sep 17 00:00:00 2001 From: serversdown Date: Thu, 29 Oct 2026 19:30:00 -0400 Subject: [PATCH] tooling(scratch): decode-check summary and grouped detail report summary.md for Brian: per field, events per verdict, how many fields fully match, non-matches by event year, carve-out counts. details.json for Claude: each non-matching (field, verdict) with the attribute values its failures concentrate in (>= 10% of the failures; concentrated at >= 95% of compared events) and example events, each with a one-event re-run command. On a 2,000-event sample the event-type mismatches group as 'type = Full Histogram, 1815/1815' -- #41, named by the report. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01ALX2R8pghDaZuti9U7mEE9 --- scratch/check_s3_decode.py | 4 + scratch/decode_check_report.py | 143 +++++++++++++++++++++++++++++++++ tests/test_check_s3_decode.py | 42 ++++++++++ 3 files changed, 189 insertions(+) create mode 100644 scratch/decode_check_report.py diff --git a/scratch/check_s3_decode.py b/scratch/check_s3_decode.py index 5580ad0..244a40a 100644 --- a/scratch/check_s3_decode.py +++ b/scratch/check_s3_decode.py @@ -43,6 +43,7 @@ sys.path.insert(0, str(_REPO)) from minimateplus.bw_xml_report import XmlReport, parse_xml_report_file # noqa: E402 from minimateplus.decode_check import compare, sfm_side # noqa: E402 from minimateplus.decode_check_carveouts import CARVEOUTS # noqa: E402 +from scratch import decode_check_report as report # noqa: E402 RUNS_DIR = Path.home() / "dl2-archive" / "checker-runs" _BIN = re.compile(r"^[A-Z]\d{3}[0-9A-Z]{4}\.[0-9A-Z]{2}0[WH]$", re.I) @@ -183,6 +184,9 @@ def main(argv: Optional[list[str]] = None) -> int: "commit": _git_head(), "events": len(work), "seconds": round(time.monotonic() - started, 1)} (out_dir / "run.json").write_text(json.dumps(run, indent=2)) + agg = report.aggregate(out_dir / "results.jsonl.gz") + report.write_summary(agg, out_dir / "summary.md") + report.write_details(agg, out_dir / "details.json") print(out_dir) return 0 diff --git a/scratch/decode_check_report.py b/scratch/decode_check_report.py new file mode 100644 index 0000000..80ad8b0 --- /dev/null +++ b/scratch/decode_check_report.py @@ -0,0 +1,143 @@ +""" +Reports for the Series 3 decode checker (scratch/check_s3_decode.py). + +From a run's results.jsonl.gz: + + summary.md for Brian: per field, how many events got each verdict; how many + fields fully match; non-matches by event year; carve-out counts + details.json for Claude: each non-matching (field, verdict) with the event + attributes its failures concentrate in, and example events, each + with a command that re-runs just that event + +Streams the results file and holds only counters and a capped example list. +""" +from __future__ import annotations + +import gzip +import json +from collections import Counter, defaultdict +from pathlib import Path +from typing import Optional + +VERDICTS = ("match", "mismatch", "sfm_blank", "accepted", "no_reference", "error") +_NOT_DONE = ("mismatch", "sfm_blank", "error") +_GROUP_SHARE = 0.10 # an attribute value is shown when it holds >= 10% of a failure +_CONCENTRATED = 0.95 # ... and flagged when nearly every compared event with it fails +_EXAMPLES = 8 +_KEEP = 60 # candidate examples kept per failure while streaming + + +def _run_dirs(results_path: Path) -> tuple[str, str]: + run = results_path.parent / "run.json" + try: + args = json.loads(run.read_text())["args"] + return args["bin_dir"], args["xml_dir"] + except (OSError, KeyError, ValueError): + return "", "" + + +def aggregate(results_path: Path) -> dict: + fields: dict[str, Counter] = defaultdict(Counter) + by_year: dict[str, Counter] = defaultdict(Counter) + compared: dict[str, dict[str, Counter]] = defaultdict(lambda: defaultdict(Counter)) + failing: dict[str, dict[str, Counter]] = defaultdict(lambda: defaultdict(Counter)) + examples: dict[str, list] = defaultdict(list) + carveouts: Counter = Counter() + events = 0 + with gzip.open(results_path, "rt", encoding="utf-8") as fh: + for line in fh: + ev = json.loads(line) + events += 1 + attrs = ev.get("attrs") or {} + covered = set() + for field, verdict, bw, sfm, detail, carveout in ev["results"]: + fields[field][verdict] += 1 + if carveout: + covered.add(carveout) + if verdict != "no_reference": + for a, v in attrs.items(): + compared[field][a][str(v)] += 1 + if verdict in ("match", "accepted", "no_reference"): + continue + key = f"{field}|{verdict}" + by_year[field][attrs.get("year", "?")] += 1 + for a, v in attrs.items(): + failing[key][a][str(v)] += 1 + if len(examples[key]) < _KEEP: + examples[key].append({"file": ev["file"], "attrs": attrs, "bw": bw, + "sfm": sfm, "detail": detail}) + carveouts.update(covered) + bin_dir, xml_dir = _run_dirs(Path(results_path)) + return {"events": events, "fields": fields, "by_year": by_year, "compared": compared, + "failing": failing, "examples": examples, "carveouts": carveouts, + "bin_dir": bin_dir, "xml_dir": xml_dir, "regressions": None} + + +def _fully_matching(counts: Counter) -> bool: + return not any(counts[v] for v in _NOT_DONE) + + +def write_summary(agg: dict, path: Path, title: Optional[str] = None) -> None: + fields = agg["fields"] + order = sorted(fields, key=lambda f: (-sum(fields[f][v] for v in _NOT_DONE), f)) + done = sum(_fully_matching(fields[f]) for f in fields) + lines = [f"# Decode check — {title or Path(path).parent.name}", "", + f"{agg['events']} events · {done} of {len(fields)} fields fully matching", ""] + if agg.get("regressions") is not None: + n = agg["regressions"] + lines += [f"**{n} regressions** since the previous run" + (" — see changes.md" if n else ""), ""] + lines += ["| field | " + " | ".join(VERDICTS) + " |", "|---|" + "---:|" * len(VERDICTS)] + lines += [f"| {f} | " + " | ".join(str(fields[f][v]) for v in VERDICTS) + " |" for f in order] + + years = sorted({y for c in agg["by_year"].values() for y in c}) + bad = [f for f in order if not _fully_matching(fields[f])] + if bad: + lines += ["", "## Not matching, by event year", "", + "| field | " + " | ".join(years) + " |", "|---|" + "---:|" * len(years)] + lines += [f"| {f} | " + " | ".join(str(agg["by_year"][f][y] or "") for y in years) + " |" + for f in bad] + if agg["carveouts"]: + lines += ["", "## Carve-outs (events covered)", "", "| carve-out | events |", "|---|---:|"] + lines += [f"| {c} | {n} |" for c, n in agg["carveouts"].most_common()] + Path(path).write_text("\n".join(lines) + "\n") + + +def write_details(agg: dict, path: Path) -> None: + def total_of(key: str) -> int: + field, verdict = key.split("|", 1) + return agg["fields"][field][verdict] + + out = {} + for key in sorted(agg["examples"], key=lambda k: (-total_of(k), k)): + field = key.split("|", 1)[0] + total = total_of(key) + groups = {} + for attr, values in agg["failing"].get(key, {}).items(): + rows = [] + for value, n in values.most_common(): + if n < _GROUP_SHARE * total: + break + seen = agg["compared"][field][attr][value] + rate = round(n / seen, 3) if seen else 1.0 + rows.append({"value": value, "failing": n, "compared": seen, "rate": rate, + "concentrated": rate >= _CONCENTRATED}) + if rows: + groups[attr] = rows + out[key] = {"total": total, "groups": groups, + "examples": _pick_examples(agg, key, groups)} + Path(path).write_text(json.dumps(out, indent=1)) + + +def _pick_examples(agg: dict, key: str, groups: dict) -> list[dict]: + """One example per top group value first, then the rest in run order.""" + pool, picked = agg["examples"][key], [] + for attr, rows in groups.items(): + for row in rows: + ex = next((e for e in pool if str(e["attrs"].get(attr)) == row["value"] + and e not in picked), None) + if ex is not None and len(picked) < _EXAMPLES: + picked.append(ex) + picked += [e for e in pool if e not in picked][: _EXAMPLES - len(picked)] + rerun = f"python scratch/check_s3_decode.py {agg['bin_dir']} {agg['xml_dir']} --file " + return [{"file": e["file"], "bw": e["bw"], "sfm": e["sfm"], "detail": e["detail"], + "rerun": rerun + e["file"]} for e in picked] diff --git a/tests/test_check_s3_decode.py b/tests/test_check_s3_decode.py index b39ebfb..b45c4bf 100644 --- a/tests/test_check_s3_decode.py +++ b/tests/test_check_s3_decode.py @@ -55,3 +55,45 @@ def test_field_filter_keeps_only_matching_rows(tmp_path): "--field", "samples."]) == 0 for line in gzip.open(tmp_path / "run/results.jsonl.gz", "rt"): assert all(r[0].startswith("samples.") for r in json.loads(line)["results"]) + + +# ── Task 6: summary and detailed report ───────────────────────────────────── + +report = importlib.import_module("scratch.decode_check_report") + + +def _write_results(path, events): + with gzip.open(path, "wt") as fh: + for ev in events: + fh.write(json.dumps(ev) + "\n") + + +def _ev(name, sr, verdict): + return {"file": name, "attrs": {"sample_rate": sr, "year": "2024"}, + "results": [["SampleRate", verdict, sr + " sps", "1024", "", None]]} + + +def test_details_group_failures_by_what_they_share(tmp_path): + res = tmp_path / "results.jsonl.gz" + _write_results(res, [_ev("A.W", "2048", "mismatch"), _ev("B.W", "2048", "mismatch"), + _ev("C.W", "1024", "match")]) + agg = report.aggregate(res) + report.write_details(agg, tmp_path / "details.json") + d = json.loads((tmp_path / "details.json").read_text())["SampleRate|mismatch"] + assert d["total"] == 2 + assert d["groups"]["sample_rate"] == [ + {"value": "2048", "failing": 2, "compared": 2, "rate": 1.0, "concentrated": True}] + assert d["examples"] and all("--file" in e["rerun"] for e in d["examples"]) + + +def test_summary_counts_fields_fully_matching(tmp_path): + res = tmp_path / "results.jsonl.gz" + _write_results(res, [_ev("A.W", "2048", "mismatch"), _ev("C.W", "1024", "match")]) + report.write_summary(report.aggregate(res), tmp_path / "summary.md") + text = (tmp_path / "summary.md").read_text() + assert "0 of 1 fields fully matching" in text and "| SampleRate |" in text + + +def test_a_run_writes_both_reports(tmp_path): + assert run.main([str(FIX), str(FIX), "--out", str(tmp_path / "run"), "--jobs", "1"]) == 0 + assert (tmp_path / "run/summary.md").exists() and (tmp_path / "run/details.json").exists()