"""Export the Manuscript V pooled audit counts as a flat CSV.""" import csv import json from pathlib import Path DATA_DIR = Path(__file__).resolve().parent SOURCE = DATA_DIR.parent / "reproducibility" / "residual_counts.json" OUTPUT = DATA_DIR / "audit_counts.csv" FIELDS = ( "case", "batch", "pair_index", "action_index", "feature_index", "attempts", "registered_heralds", "retained_heralds", ) def audit_rows(payload): if payload["schema"] != "residual-audit-counts-v1": raise ValueError(f"Unsupported count schema: {payload['schema']}") for case in payload["cases"]: pairs = case["compiled_design"]["pairs"] for batch_number, batch in enumerate(case["audit_batches"], start=1): for pair_index, (design, counts) in enumerate(zip(pairs, batch["pairs"], strict=True)): yield ( case["case"], batch_number, pair_index, design["action"], design["feature"], counts["attempts"], counts["registered_heralds"], counts["retained_heralds"], ) def main(): payload = json.loads(SOURCE.read_text(encoding="utf-8")) rows = list(audit_rows(payload)) with OUTPUT.open("w", encoding="utf-8", newline="") as stream: writer = csv.writer(stream, lineterminator="\n") writer.writerow(FIELDS) writer.writerows(rows) print(f"Wrote {len(rows):,} rows to {OUTPUT.name}") if __name__ == "__main__": main()