"""Diagnostic boundaries: no invented geometry, rejected context, frozen outputs.""" import importlib import importlib.util import sys import tempfile import unittest from pathlib import Path import numpy as np if importlib.util.find_spec("cv2") is None: raise unittest.SkipTest("Scientific checks run in the pinned Worker image") sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "simulation/semantic-learning")) study = importlib.import_module("paint_hypothesis_study") class PaintHypothesisTests(unittest.TestCase): def test_photometric_counterfactual_preserves_geometry(self): image = np.full((64, 64, 3), 80, np.uint8) image[20:25, 5:58] = 180 darker = study.transformed(image, "gain", 0.55) np.testing.assert_array_equal(image[:, :, 0] > 100, darker[:, :, 0] > 50) np.testing.assert_array_equal(study.transformed(image, "identity", 1), image) def test_context_does_not_fabricate_missing_support(self): with tempfile.TemporaryDirectory() as directory: root = Path(directory) self.assertIsNone(study.context(root, {"id": "absent"}, "semantic")) (root / "data").mkdir() road = np.array([[0.8, 0.1, 0.8]], np.float32) sidewalk = np.array([[0.1, 0.05, 0.9]], np.float32) np.savez(root / "data/a-context.npz", road=road, sidewalk=sidewalk) np.testing.assert_array_equal( study.context(root, {"id": "a"}, "semantic"), [[True, False, False]] ) def test_high_recall_candidate_cannot_bypass_false_positive_limit(self): rows = [ dict(variant="bad", recall=0.99, false_positive_rate=0.5), dict(variant="good", recall=0.8, false_positive_rate=0.005), ] self.assertEqual(study.choose_candidate(rows)["variant"], "good") self.assertEqual(study.choose_candidate(list(reversed(rows)))["variant"], "good") def test_frozen_model_cannot_be_overwritten(self): with tempfile.TemporaryDirectory() as directory: path = Path(directory) / "model.json" study.save_new(path, {"model": 1}) with self.assertRaises(FileExistsError): study.save_new(path, {"model": 2}) self.assertEqual(study.json.loads(path.read_text()), {"model": 1}) def test_deterministic_fit_needs_no_spatial_identity(self): x = np.array([[-2.0, 0.1], [-1.0, 0.2], [1.0, 0.4], [2.0, 0.3]]) y = np.array([0, 0, 1, 1]) a = study.fit_classifier(x, y, 0.01, 200) b = study.fit_classifier(x, y, 0.01, 200) np.testing.assert_array_equal(a["coefficients"], b["coefficients"]) np.testing.assert_array_equal(study.probability(x, a) > 0.5, y == 1) if __name__ == "__main__": unittest.main()