"""Scientific isolation and nonregression gates for grouped threshold selection.""" import importlib import sys from pathlib import Path import pytest for dependency in ["numpy", "cv2", "scipy", "shapely"]: pytest.importorskip(dependency) ROOT = Path(__file__).resolve().parents[1] for directory in [ "simulation/semantic-learning", "simulation/urban-plan-research/systematic", "simulation/urban-plan-research/marking_pass", ]: sys.path.insert(0, str(ROOT / directory)) study = importlib.import_module("grouped_selection_study") def test_control_street_never_enters_development(): records = [dict(street="a", split="heldout"), dict(street="control", split="train")] assert study.development(records, ["a"]) == [records[0]] def test_threshold_rejects_known_false_positives_and_uses_interior_plateau(): rows = [ dict(threshold=t, unsupported=0, worst_street=0.8, macro_family=0.9, found=10) for t in [0.2, 0.25, 0.3, 0.35, 0.4] ] rows.append(dict(threshold=0.1, unsupported=1, worst_street=1, macro_family=1, found=20)) assert study.select(rows)["threshold"] == 0.3 assert study.select([rows[-1]]) is None def cohort(found, fp=0): return dict( rows=[dict(id="window", objects=[dict(id=i, matched=True) for i in found])], streets={"a": dict(unsupported=fp)}, summary=dict(found=len(found), unsupported=fp), ) def test_equal_count_cannot_hide_loss_of_a_previously_found_object(): verdict = study.transfer_gate(cohort(["x", "y"]), cohort(["x", "z", "w"])) assert verdict["lost_objects"] == ["y"] assert not verdict["passed"] def test_improvement_requires_no_loss_and_no_extra_false_positive(): assert study.transfer_gate(cohort(["x"], 1), cohort(["x"], 0))["passed"] assert not study.transfer_gate(cohort(["x"]), cohort(["x", "y"], 1))["passed"] assert not study.transfer_gate(cohort(["x"]), cohort(["x"]))["passed"] def test_unmatchable_objects_do_not_distort_threshold_objective(): objects = [ dict(id="a", family="arrow", matched=True), dict(id="b", family="bar", matched=False), ] rows = [dict(id="w", street="s", objects=objects)] result = study.objective(rows, {"w": dict(objects=objects)}) assert result["worst_street"] == 1 assert result["family_matchable"] == {"arrow": [1, 1]}