feat(semantics): add portable annotation, learning and review contracts
Add portable corpus, annotation provenance, admission, review and bounded agent-cycle contracts. Preserve dataset ownership and explicit research acceptance limits.
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
"""Numerical and fitting safeguards for the Worker-only ranking probe."""
|
||||
|
||||
import importlib
|
||||
import importlib.util
|
||||
import sys
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
import numpy as np
|
||||
|
||||
if importlib.util.find_spec("cv2") is None:
|
||||
raise unittest.SkipTest("Scientific probe checks run in the pinned Worker renderer image")
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "simulation/semantic-learning"))
|
||||
sys.path.insert(0, str(ROOT / "simulation/urban-plan-research/pipeline/base"))
|
||||
|
||||
PhysicalFields = importlib.import_module("boundary_anchors").PhysicalFields
|
||||
probe = importlib.import_module("boundary_rank_probe")
|
||||
components, describe, fit, PRIOR = probe.components, probe.describe, probe.fit, probe.PRIOR
|
||||
|
||||
|
||||
class ProbeTests(unittest.TestCase):
|
||||
def fields(self):
|
||||
rgb = np.zeros((128, 128, 3), np.uint8)
|
||||
rgb[:, 64:] = 160
|
||||
z = np.zeros((128, 128), np.float32)
|
||||
z[:, 64:] = 0.12
|
||||
one = np.ones(z.shape, np.float32)
|
||||
zero = np.zeros_like(one)
|
||||
return PhysicalFields(rgb, z, one, zero, zero, zero, 0.05)
|
||||
|
||||
def test_reference_score_preserved_exactly(self):
|
||||
f = self.fields()
|
||||
uv = np.array([[63.0, 40.0], [64.0, 60.0], [67.0, 90.0]])
|
||||
before = [a.copy() for a in (f.gray, f.z, f.alpha)]
|
||||
expected, mask = f.measure(uv, [1, 0])
|
||||
terms, got = components(f, uv, [1, 0])
|
||||
np.testing.assert_array_equal(got, expected * mask)
|
||||
np.testing.assert_allclose(terms @ PRIOR, got, atol=1e-7, rtol=1e-6)
|
||||
for a, b in zip(before, (f.gray, f.z, f.alpha), strict=True):
|
||||
np.testing.assert_array_equal(a, b)
|
||||
|
||||
def test_missing_evidence_cannot_gain_score(self):
|
||||
f = self.fields()
|
||||
f.alpha[:] = 0
|
||||
terms, score = components(f, np.array([[64.0, 64.0]]), [1, 0])
|
||||
self.assertFalse(terms.any())
|
||||
self.assertFalse(score.any())
|
||||
|
||||
def test_reverse_geometry_same_measurement(self):
|
||||
f = self.fields()
|
||||
path = np.array([[64.0, 25.0], [64.0, 95.0]])
|
||||
a, sa = describe(f, path, [10, 10, 110, 110], [1, 0])
|
||||
b, sb = describe(f, path[::-1], [10, 10, 110, 110], [1, 0])
|
||||
np.testing.assert_allclose(a, b, atol=1e-7)
|
||||
self.assertAlmostEqual(sa, sb, places=7)
|
||||
|
||||
def test_pair_fit_is_bounded_and_deterministic(self):
|
||||
d = np.array([[0.3, 0, -0.1, 0], [0.2, 0.1, 0, 0], [0.1, 0, 0, 0.1]])
|
||||
a = fit(d)
|
||||
b = fit(d)
|
||||
np.testing.assert_array_equal(a, b)
|
||||
self.assertTrue(((a >= 0) & (a <= 2)).all())
|
||||
self.assertGreater(a[0], PRIOR[0])
|
||||
|
||||
def test_invalid_pairs_fail_closed(self):
|
||||
for d in [[], [[1, 2, 3, 4]], [[float("nan")] * 4] * 2]:
|
||||
with self.assertRaises(ValueError):
|
||||
fit(d)
|
||||
|
||||
def test_outside_review_has_no_target(self):
|
||||
with self.assertRaises(ValueError):
|
||||
describe(
|
||||
self.fields(),
|
||||
np.array([[1.0, 1.0], [5.0, 5.0]]),
|
||||
[30, 30, 90, 90],
|
||||
[1, 0],
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user