#!/usr/bin/env python3 """Validate source records, regenerate statistics, and check the release.""" from __future__ import annotations import csv import hashlib import subprocess import sys from collections import Counter, defaultdict from pathlib import Path ROOT = Path(__file__).resolve().parents[1] def rows(relative: str) -> list[dict[str, str]]: with (ROOT / relative).open(newline="", encoding="utf-8") as handle: return list(csv.DictReader(handle)) def require(condition: bool, message: str) -> None: if not condition: raise AssertionError(message) def verify_attempts() -> None: attempts = rows("results/complete-suite/attempts.csv") grouped: dict[str, Counter] = defaultdict(Counter) for row in attempts: grouped[row["cell"]][row["task"]] += 1 require(len(attempts) == 6 * 87 * 2, "complete suite must contain 1,044 attempts") require(len(grouped) == 6, "complete suite must contain six cells") for cell, counts in grouped.items(): require(len(counts) == 87 and set(counts.values()) == {2}, f"{cell}: expected 87 tasks x 2") digests: dict[str, set[str]] = defaultdict(set) for row in attempts: digests[row["task"]].add(row["task_digest"]) require(all(len(values) == 1 for values in digests.values()), "task digest differs across cells") primary = {row["cell"]: row for row in rows("results/complete-suite/primary.csv")} for cell in grouped: cell_rows = [row for row in attempts if row["cell"] == cell] require(int(primary[cell]["passes"]) == sum(int(row["passed"]) for row in cell_rows), f"{cell}: pass count mismatch") terminations = rows("results/complete-suite/terminations.csv") for cell in grouped: total = sum(int(row["attempts"]) for row in terminations if row["cell"] == cell) require(total == 174, f"{cell}: termination count mismatch") def verify_panel() -> None: attempts = rows("results/five-arm-panel/attempt-funnel.csv") grouped = Counter((row["condition"], row["task"]) for row in attempts) require(len(attempts) == 5 * 30 * 2, "five-arm panel must contain 300 selected attempts") require(set(grouped.values()) == {2}, "each panel condition-task pair must contain two attempts") require(all(row["scored"] == "True" for row in attempts), "all selected panel attempts must be scored") def verify_manifest() -> None: manifest = ROOT / "MANIFEST.sha256" require(manifest.exists(), "MANIFEST.sha256 is missing") listed: set[str] = set() for line in manifest.read_text(encoding="utf-8").splitlines(): digest, relative = line.split(" ", 1) listed.add(relative) path = ROOT / relative require(path.is_file(), f"manifest path missing: {relative}") require(hashlib.sha256(path.read_bytes()).hexdigest() == digest, f"manifest digest mismatch: {relative}") present = {str(path.relative_to(ROOT)) for path in ROOT.rglob("*") if path.is_file() and path != manifest} require(listed == present, f"manifest file set mismatch: missing={sorted(present-listed)}, stale={sorted(listed-present)}") if __name__ == "__main__": verify_attempts() verify_panel() subprocess.run([sys.executable, str(ROOT / "analysis/reproduce.py"), "--check"], check=True) verify_manifest() print("Artifact records, derived tables, and manifest are consistent.")