FanQY commited on
Commit
e62e849
·
verified ·
1 Parent(s): c2fc1f0

Upload tools/audit_split_vs_pool.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. tools/audit_split_vs_pool.py +127 -0
tools/audit_split_vs_pool.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Is each test split drawn from the same distribution as the pool it accompanies?
3
+
4
+ Index-level and program-text overlap checks answer whether a split leaks. They do
5
+ not answer whether it is representative, and a split can be leakage-free while
6
+ being drawn from a different distribution than the pool it accompanies. That is
7
+ what went wrong with the loft and revolve test splits of this release, and this
8
+ script is the check that would have caught it.
9
+
10
+ Two axes, both computed over the full population rather than a sample:
11
+
12
+ source text the fraction of programs containing each CadQuery construct
13
+ solids the median and p90 of the stored triangle count (num_faces)
14
+
15
+ Validation is the control. It is carved from the pool rather than generated
16
+ independently, so it should track the pool on every operation; where test does
17
+ not and validation does, the divergence is a property of how the test directory
18
+ was produced.
19
+
20
+ python tools/audit_split_vs_pool.py
21
+ python tools/audit_split_vs_pool.py --data-root data --op loft
22
+ """
23
+
24
+ from __future__ import annotations
25
+
26
+ import argparse
27
+ import glob
28
+ import os
29
+ import statistics as st
30
+
31
+ import pyarrow.parquet as pq
32
+
33
+ OPS = ["extrude", "revolve", "sweep", "loft"]
34
+ CONSTRUCTS = [
35
+ "union", "cut", "box", "cylinder", "moveTo", "lineTo", "radiusArc",
36
+ "threePointArc", "close", "placeSketch", "workplane", "sketch",
37
+ "circle", "polyline", "extrude", "revolve", "sweep", "loft",
38
+ ]
39
+ # Flag a construct whose test rate differs from the pool by more than this many
40
+ # percentage points, or a triangle-count median that differs by more than this
41
+ # fraction. These are the gate the release process applies.
42
+ TOL_POINTS = 5.0
43
+ TOL_MEDIAN = 0.25
44
+
45
+
46
+ def collect(files):
47
+ by_split = {}
48
+ for f in files:
49
+ t = pq.read_table(f, columns=["split", "program", "num_faces"])
50
+ for sp, pr, nf in zip(t.column("split").to_pylist(),
51
+ t.column("program").to_pylist(),
52
+ t.column("num_faces").to_pylist()):
53
+ d = by_split.setdefault(sp, {"n": 0, "c": {k: 0 for k in CONSTRUCTS},
54
+ "faces": []})
55
+ d["n"] += 1
56
+ if pr:
57
+ for k in CONSTRUCTS:
58
+ if k in pr:
59
+ d["c"][k] += 1
60
+ if nf is not None:
61
+ d["faces"].append(int(nf))
62
+ return by_split
63
+
64
+
65
+ def pct(d, k):
66
+ return 100.0 * d["c"][k] / max(1, d["n"])
67
+
68
+
69
+ def p90(v):
70
+ """Linear-interpolated 90th percentile, the definition used in the paper."""
71
+ if not v:
72
+ return 0.0
73
+ v = sorted(v)
74
+ k = (len(v) - 1) * 0.90
75
+ f = int(k)
76
+ c = min(f + 1, len(v) - 1)
77
+ return v[f] + (v[c] - v[f]) * (k - f)
78
+
79
+
80
+ def main() -> None:
81
+ ap = argparse.ArgumentParser()
82
+ ap.add_argument("--data-root", default="data")
83
+ ap.add_argument("--op", default=None, choices=OPS)
84
+ args = ap.parse_args()
85
+
86
+ failed = False
87
+ for op in ([args.op] if args.op else OPS):
88
+ files = sorted(glob.glob(os.path.join(args.data_root, op, "*.parquet")))
89
+ if not files:
90
+ print(f"{op}: no shards under {args.data_root}/{op}")
91
+ continue
92
+ d = collect(files)
93
+ pool, val, test = d.get("train"), d.get("validation"), d.get("test")
94
+ if not (pool and test):
95
+ print(f"{op}: missing train or test rows")
96
+ continue
97
+
98
+ print(f"\n=== {op} pool n={pool['n']:,} validation n={val['n'] if val else 0:,}"
99
+ f" test n={test['n']:,}")
100
+ print(f"{'construct':16s} {'pool %':>8s} {'val %':>8s} {'test %':>8s} {'test-pool':>10s}")
101
+ for k in CONSTRUCTS:
102
+ a, b = pct(pool, k), pct(test, k)
103
+ if a < 0.5 and b < 0.5:
104
+ continue
105
+ v = pct(val, k) if val else float("nan")
106
+ flag = " <-- exceeds tolerance" if abs(b - a) > TOL_POINTS else ""
107
+ if flag:
108
+ failed = True
109
+ print(f"{k:16s} {a:8.1f} {v:8.1f} {b:8.1f} {b - a:+10.1f}{flag}")
110
+
111
+ mp, mt = st.median(pool["faces"]), st.median(test["faces"])
112
+ mv = st.median(val["faces"]) if val and val["faces"] else float("nan")
113
+ rel = abs(mt - mp) / max(1.0, mp)
114
+ flag = " <-- exceeds tolerance" if rel > TOL_MEDIAN else ""
115
+ if flag:
116
+ failed = True
117
+ print(f"{'triangles med':16s} {mp:8.0f} {mv:8.0f} {mt:8.0f} "
118
+ f"{mt / max(1.0, mp):9.2f}x{flag}")
119
+ print(f"{'triangles p90':16s} {p90(pool['faces']):8.0f} "
120
+ f"{p90(val['faces']) if val else 0:8.0f} {p90(test['faces']):8.0f}")
121
+
122
+ print("\nGATE: FAIL, at least one split is unrepresentative of its pool"
123
+ if failed else "\nGATE: PASS")
124
+
125
+
126
+ if __name__ == "__main__":
127
+ main()