import importlib.util from pathlib import Path import numpy as np import pytest p = Path(__file__).resolve().parents[1]/"simulation/semantic-learning/paint_comparison.py" s = importlib.util.spec_from_file_location("paint_comparison", p) m = importlib.util.module_from_spec(s) s.loader.exec_module(m) def fixture(): t = np.zeros((12, 16), np.uint8) t[:, :2] = 255 objs = [] for i, x in enumerate([3, 10]): t[2:10, x:x+3] = 1 objs.append(dict(id=str(i), outline_native_pixels=[[x,2],[x+2,2],[x+2,9],[x,9]], final_paint_pixels=24, kind="observed_crosswalk_bar", semantic_group="one", visibility="complete_in_view")) return t, objs def test_unknown_does_not_change_errors_or_create_instance_split(): t, objs = fixture() t[5, 3:6] = 255 objs[0]["final_paint_pixels"] -= 3 a = (t == 1).astype(float) before = m.score(a, t, objs) a[t == 255] = 1 after = m.score(a, t, objs) for key in ["tp", "fn", "fp", "tn", "observations", "gaps"]: assert before[key] == after[key] assert after["fn"] == after["fp"] == 0 assert "instance_merge_split" in after["not_assessed"] def test_gap_is_known_background_only_and_not_merge_claim(): t, objs = fixture() t[5, 6:10] = 255 p = np.ones_like(t, float) got = m.score(p, t, objs) assert got["gaps"][0]["background_pixels"] == 28 assert got["gaps"][0]["fp"] == 28 assert "instance_merge_split" in got["not_assessed"] def test_partial_line_unknown_gap_not_assessed(): t, objs = fixture() for o in objs: o["kind"] = "observed_line_fragment" assert not m.score((t == 1).astype(float), t, objs)["gaps"] def test_corrupt_core_or_native_prediction_rejected(): t, objs = fixture() objs[0]["final_paint_pixels"] += 1 with pytest.raises(ValueError): m.score(np.zeros_like(t, float), t, objs) with pytest.raises(ValueError): m.score(np.zeros((2, 2)), t, []) with pytest.raises(ValueError): m.score(np.full_like(t, np.nan, dtype=float), t, []) def test_paired_errors_exclude_unknown_and_reconcile(): t, objs = fixture() b = (t == 1).astype(float) a = 1-b a[t == 255] = 1 got=m.paired_changes(a,b,t) assert got==dict(new_fn=48,recovered_fn=0,new_fp=120,removed_fp=0)