import importlib.util import sys from pathlib import Path import numpy as np import pytest SRC = Path(__file__).resolve().parents[1] / "simulation/semantic-learning" sys.path.insert(0, str(SRC)) spec = importlib.util.spec_from_file_location("density_probe", SRC / "supervision_density_probe.py") probe = importlib.util.module_from_spec(spec) spec.loader.exec_module(probe) sys.path.remove(str(SRC)) def example(split="train"): return dict( sample_id="a", source="r1", physical_group="street-a", split=split, inputs=["rgb-sha"] ) def test_growth_adds_labels_but_cannot_rewrite_them_or_move_controls(): a = np.array([[1, 0, 255]], dtype="uint8") b = np.array([[1, 0, 0]], dtype="uint8") probe.check_pair(example(), example(), a, b) with pytest.raises(ValueError, match="Existing"): probe.check_pair(example(), example(), a, np.array([[0, 0, 0]])) with pytest.raises(ValueError, match="control"): probe.check_pair(example("regression"), example("regression"), a, b) with pytest.raises(ValueError, match="input"): probe.check_pair(example(), dict(example(), inputs=["different-image"]), a, b) def test_street_regression_cannot_hide_behind_gain_on_another_street(): good = dict(sparse=dict(recall=0.8, fpr=0.1), dense=dict(recall=0.9, fpr=0.08)) bad = dict(sparse=dict(recall=None, fpr=0.01), dense=dict(recall=None, fpr=0.02)) audit = dict(sparse=dict(recall=0.9, fpr=0.9), dense=dict(recall=0.9, fpr=0.5)) result = probe.qualify({"paint": good, "paving": bad}, audit, True) assert not result["accepted"] and result["failures"] == ["paving: fpr"] assert probe.qualify({"paint": good}, audit, True)["accepted"] assert not probe.qualify({"paint": good}, audit, False)["accepted"] def test_diagnostic_cannot_be_improved_by_erasing_real_paint(): metrics = dict(a=dict(sparse=dict(recall=0.8, fpr=0.1), dense=dict(recall=0.9, fpr=0.08))) audit = dict(sparse=dict(recall=0.9, fpr=0.9), dense=dict(recall=0.8, fpr=0.1)) assert "diagnostic paint recall" in probe.qualify(metrics, audit, True)["failures"] with pytest.raises(ValueError, match="Only train"): probe.samples(None, None, {"split": "regression"}, {})