#!/usr/bin/env python3 """Is each test split drawn from the same distribution as the pool it accompanies? Index-level and program-text overlap checks answer whether a split leaks. They do not answer whether it is representative, and a split can be leakage-free while being drawn from a different distribution than the pool it accompanies. That is what went wrong with the loft and revolve test splits of this release, and this script is the check that would have caught it. Two axes, both computed over the full population rather than a sample: source text the fraction of programs containing each CadQuery construct solids the median and p90 of the stored triangle count (num_faces) Validation is the control. It is carved from the pool rather than generated independently, so it should track the pool on every operation; where test does not and validation does, the divergence is a property of how the test directory was produced. python tools/audit_split_vs_pool.py python tools/audit_split_vs_pool.py --data-root data --op loft """ from __future__ import annotations import argparse import glob import os import statistics as st import pyarrow.parquet as pq OPS = ["extrude", "revolve", "sweep", "loft"] CONSTRUCTS = [ "union", "cut", "box", "cylinder", "moveTo", "lineTo", "radiusArc", "threePointArc", "close", "placeSketch", "workplane", "sketch", "circle", "polyline", "extrude", "revolve", "sweep", "loft", ] # Flag a construct whose test rate differs from the pool by more than this many # percentage points, or a triangle-count median that differs by more than this # fraction. These are the gate the release process applies. TOL_POINTS = 5.0 TOL_MEDIAN = 0.25 def collect(files): by_split = {} for f in files: t = pq.read_table(f, columns=["split", "program", "num_faces"]) for sp, pr, nf in zip(t.column("split").to_pylist(), t.column("program").to_pylist(), t.column("num_faces").to_pylist()): d = by_split.setdefault(sp, {"n": 0, "c": {k: 0 for k in CONSTRUCTS}, "faces": []}) d["n"] += 1 if pr: for k in CONSTRUCTS: if k in pr: d["c"][k] += 1 if nf is not None: d["faces"].append(int(nf)) return by_split def pct(d, k): return 100.0 * d["c"][k] / max(1, d["n"]) def p90(v): """Linear-interpolated 90th percentile, the definition used in the paper.""" if not v: return 0.0 v = sorted(v) k = (len(v) - 1) * 0.90 f = int(k) c = min(f + 1, len(v) - 1) return v[f] + (v[c] - v[f]) * (k - f) def main() -> None: ap = argparse.ArgumentParser() ap.add_argument("--data-root", default="data") ap.add_argument("--op", default=None, choices=OPS) args = ap.parse_args() failed = False for op in ([args.op] if args.op else OPS): files = sorted(glob.glob(os.path.join(args.data_root, op, "*.parquet"))) if not files: print(f"{op}: no shards under {args.data_root}/{op}") continue d = collect(files) pool, val, test = d.get("train"), d.get("validation"), d.get("test") if not (pool and test): print(f"{op}: missing train or test rows") continue print(f"\n=== {op} pool n={pool['n']:,} validation n={val['n'] if val else 0:,}" f" test n={test['n']:,}") print(f"{'construct':16s} {'pool %':>8s} {'val %':>8s} {'test %':>8s} {'test-pool':>10s}") for k in CONSTRUCTS: a, b = pct(pool, k), pct(test, k) if a < 0.5 and b < 0.5: continue v = pct(val, k) if val else float("nan") flag = " <-- exceeds tolerance" if abs(b - a) > TOL_POINTS else "" if flag: failed = True print(f"{k:16s} {a:8.1f} {v:8.1f} {b:8.1f} {b - a:+10.1f}{flag}") mp, mt = st.median(pool["faces"]), st.median(test["faces"]) mv = st.median(val["faces"]) if val and val["faces"] else float("nan") rel = abs(mt - mp) / max(1.0, mp) flag = " <-- exceeds tolerance" if rel > TOL_MEDIAN else "" if flag: failed = True print(f"{'triangles med':16s} {mp:8.0f} {mv:8.0f} {mt:8.0f} " f"{mt / max(1.0, mp):9.2f}x{flag}") print(f"{'triangles p90':16s} {p90(pool['faces']):8.0f} " f"{p90(val['faces']) if val else 0:8.0f} {p90(test['faces']):8.0f}") print("\nGATE: FAIL, at least one split is unrepresentative of its pool" if failed else "\nGATE: PASS") if __name__ == "__main__": main()