143 lines
6.5 KiB
Python
143 lines
6.5 KiB
Python
"""CPU-only synthetic unit tests for WIN-TEST-SELECT-v1.1 + §7 rules."""
|
|
from itertools import permutations
|
|
from pathlib import Path
|
|
import sys
|
|
import unittest
|
|
import numpy as np
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parent))
|
|
import win_test_select_rules as sel
|
|
|
|
|
|
def box_mesh(nx=4, ny=4, nz=4, sx=2.0, sy=1.0, sz=0.5):
|
|
axes = [np.linspace(0, sx, nx), np.linspace(0, sy, ny), np.linspace(0, sz, nz)]
|
|
x = np.stack(np.meshgrid(*axes, indexing='ij'), axis=-1).reshape(-1, 3)
|
|
ids = np.arange(len(x)).reshape(nx, ny, nz)
|
|
t = []
|
|
for i in range(nx - 1):
|
|
for j in range(ny - 1):
|
|
for k in range(nz - 1):
|
|
for perm in permutations(range(3)):
|
|
v = np.array([i, j, k])
|
|
row = [ids[tuple(v)]]
|
|
for a in perm:
|
|
v = v.copy()
|
|
v[a] += 1
|
|
row.append(ids[tuple(v)])
|
|
t.append(row)
|
|
return x, np.asarray(t, dtype=np.int32)
|
|
|
|
|
|
def holed_ring_mesh():
|
|
"""3x3x1 voxel grid with center cell removed -> watertight genus-1 tet mesh."""
|
|
nx, ny, nz = 4, 4, 2
|
|
axes = [np.linspace(0, 3, nx), np.linspace(0, 3, ny), np.linspace(0, 1, nz)]
|
|
x = np.stack(np.meshgrid(*axes, indexing='ij'), axis=-1).reshape(-1, 3)
|
|
ids = np.arange(len(x)).reshape(nx, ny, nz)
|
|
t = []
|
|
for i in range(nx - 1):
|
|
for j in range(ny - 1):
|
|
if i == 1 and j == 1:
|
|
continue
|
|
for perm in permutations(range(3)):
|
|
v = np.array([i, j, 0])
|
|
row = [ids[tuple(v)]]
|
|
for a in perm:
|
|
v = v.copy()
|
|
v[a] += 1
|
|
row.append(ids[tuple(v)])
|
|
t.append(row)
|
|
return x, np.asarray(t, dtype=np.int32)
|
|
|
|
|
|
class TestWinTestSelectRules(unittest.TestCase):
|
|
def test_pool_permutation_and_uniform_gate_draw_deterministic(self):
|
|
pool = [f'{i:08d}' for i in range(50)]
|
|
q1, sha1 = sel.permute_pool(pool)
|
|
q2, sha2 = sel.permute_pool(list(reversed(pool)))
|
|
self.assertEqual(sha1, sha2)
|
|
self.assertEqual(q1, q2)
|
|
self.assertNotEqual(q1, pool)
|
|
|
|
x, t = box_mesh(3, 3, 3)
|
|
faces = sel.exterior_faces(t)
|
|
raw = np.arange(1000, 1000 + len(x))
|
|
l1, r1 = sel.draw_gate_node(x, raw, faces, 12345)
|
|
l2, r2 = sel.draw_gate_node(x, raw, faces, 12345)
|
|
self.assertEqual((l1, r1), (l2, r2))
|
|
self.assertIn(l1, set(np.unique(faces)))
|
|
|
|
def test_euler_genus_box_and_holed_ring(self):
|
|
_, t_box = box_mesh(3, 3, 3)
|
|
self.assertEqual(sel.surface_euler_genus(sel.exterior_faces(t_box)), 0)
|
|
_, t_ring = holed_ring_mesh()
|
|
self.assertEqual(sel.surface_euler_genus(sel.exterior_faces(t_ring)), 1)
|
|
|
|
def test_stratum_precedence_and_per_stratum_backups(self):
|
|
def make_rec(cid, c1=False, c2=False, c3=False, c4=False, g=0):
|
|
cnt = int(c1) + int(c2) + int(c3) + int(c4)
|
|
return dict(abc_id=cid, metrics=dict(C1=c1, C2=c2, C3=c3, C4=c4, genus=g,
|
|
criteria_count=cnt, eligible=(cnt >= 2)))
|
|
|
|
# Build queue containing overlapping parts matching multiple strata + rejections.
|
|
recs = [dict(abc_id='bad0', reject_reason='NON_MANIFOLD')]
|
|
# 6 parts matching S4, S2, S3 (g=1, C1+C2+C3+C4) -> first 3 fill S4, next 3 spill to S2!
|
|
for i in range(6):
|
|
recs.append(make_rec(f'multi_{i}', c1=True, c2=True, c3=True, c4=True, g=1))
|
|
# 5 parts matching S3 & S1 (g=0, C1+C2+C3 -> also S4, but S4 already full -> S3 gets 3, S1 gets 2)
|
|
for i in range(5):
|
|
recs.append(make_rec(f'g0_{i}', c1=True, c2=True, c3=True, c4=False, g=0))
|
|
# 2 parts matching only S1 (C1+C2, g=0) -> 1 completes S1 primary (12/12!), 1 goes to S1 backup
|
|
for i in range(2):
|
|
recs.append(make_rec(f's1_{i}', c1=True, c2=True, c3=False, c4=False, g=0))
|
|
# Now backups for all 4 strata (2 each):
|
|
for i in range(10):
|
|
recs.append(make_rec(f'bk_all_{i}', c1=True, c2=True, c3=True, c4=True, g=1))
|
|
for i in range(3):
|
|
recs.append(make_rec(f'bk_s1_{i}', c1=True, c2=True, c3=False, c4=False, g=0))
|
|
|
|
res = sel.assign_queue(recs)
|
|
self.assertTrue(res['complete'])
|
|
for s in (1, 2, 3, 4):
|
|
self.assertEqual(len(res['primary'][s]), 3)
|
|
self.assertEqual(len(res['backup'][s]), 2)
|
|
# First 3 multi_* went to S4 (highest priority), next 3 spilled to S2 (second priority).
|
|
self.assertEqual([r['abc_id'] for r in res['primary'][4]], ['multi_0', 'multi_1', 'multi_2'])
|
|
self.assertEqual([r['abc_id'] for r in res['primary'][2]], ['multi_3', 'multi_4', 'multi_5'])
|
|
self.assertEqual([r['abc_id'] for r in res['primary'][3]], ['g0_0', 'g0_1', 'g0_2'])
|
|
self.assertEqual([r['abc_id'] for r in res['primary'][1]], ['g0_3', 'g0_4', 's1_0'])
|
|
|
|
def test_near_duplicate_rigid_scale_invariance_and_separation(self):
|
|
x, t = box_mesh(4, 3, 3, sx=3.0, sy=1.2, sz=0.5)
|
|
f = sel.exterior_faces(t)
|
|
d_orig = sel.stage1_descriptor(x, t, f, n_samples=1024)
|
|
# Rigid rotation + uniform scaling + translation -> must be caught as near-duplicate!
|
|
theta = 0.73
|
|
rot = np.array([[np.cos(theta), -np.sin(theta), 0.0],
|
|
[np.sin(theta), np.cos(theta), 0.0],
|
|
[0.0, 0.0, 1.0]])
|
|
x_rot = (x * 3.7) @ rot.T + np.array([12.0, -5.0, 4.0])
|
|
d_rot = sel.stage1_descriptor(x_rot, t, f, n_samples=1024)
|
|
self.assertTrue(sel.stage1_flagged(d_orig, d_rot))
|
|
ov_dup = sel.stage2_overlap(d_orig['cloud'], d_rot['cloud'], max_pts=256)
|
|
self.assertEqual(ov_dup['decision'], 'REJECT_NEAR_DUPLICATE')
|
|
|
|
# Distinct genus-1 ring -> rejected at Stage 1 (genus differs), and high overlap distance if Stage 2 run.
|
|
xr, tr = holed_ring_mesh()
|
|
d_ring = sel.stage1_descriptor(xr, tr, sel.exterior_faces(tr), n_samples=1024)
|
|
self.assertFalse(sel.stage1_flagged(d_orig, d_ring))
|
|
ov_diff = sel.stage2_overlap(d_orig['cloud'], d_ring['cloud'], max_pts=256)
|
|
self.assertEqual(ov_diff['decision'], 'PASS')
|
|
|
|
def test_power_law_fallback_fit(self):
|
|
vols = np.array([10.0, 25.0, 50.0, 120.0, 250.0])
|
|
times = 0.015 * (vols ** 0.42)
|
|
fit = sel.fit_power_law_fill_time(vols, times)
|
|
self.assertAlmostEqual(fit['a'], 0.015, places=10)
|
|
self.assertAlmostEqual(fit['b'], 0.42, places=10)
|
|
self.assertEqual(len(fit['sha256']), 64)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main(verbosity=2)
|