import importlib.util from pathlib import Path import numpy as np import pytest spec = importlib.util.spec_from_file_location( "context_background", Path(__file__).parents[1] / "simulation/semantic-learning/context_background.py", ) m = importlib.util.module_from_spec(spec) spec.loader.exec_module(m) def fixtures(): rr = [ dict(name="a", split="train", group="g", target=np.array([[1, 255]], np.uint8)), dict(name="b", split="train", group="g", target=np.array([[0, 255]], np.uint8)), dict(name="c", split="train", group="h", target=np.array([[0, 1]], np.uint8)), ] w = { "a": np.array([[0.25, 0]], np.float32), "b": np.array([[0.3, 0]], np.float32), "c": np.array([[0.2, 0.25]], np.float32), } a = {r["name"]: np.zeros((1, 2), bool) for r in rr} return rr, w, a def test_positive_only_window_receives_shared_negative_mass(): rr, w, a = fixtures() a["a"][0, 1] = True out, ww = m.prepare_context(rr, w, a) assert out[0]["target"].tolist() == [[1, 0]] np.testing.assert_array_equal(ww["a"], np.array([[0.25, 0.15]], np.float32)) np.testing.assert_array_equal(ww["b"], np.array([[0.15, 0]], np.float32)) np.testing.assert_array_equal(ww["c"], w["c"]) assert rr[0]["target"][0, 1] == 255 and w["a"][0, 1] == 0 def test_empty_additions_are_bit_exact(): rr, w, a = fixtures() out, ww = m.prepare_context(rr, w, a) for b, n in zip(rr, out, strict=True): np.testing.assert_array_equal(n["target"], b["target"]) np.testing.assert_array_equal(ww[b["name"]], w[b["name"]]) def test_redistribute_known_background_without_new_window(): rr, w, a = fixtures() a["b"][0, 1] = True out, ww = m.prepare_context(rr, w, a) np.testing.assert_array_equal(ww["b"], np.array([[0.15, 0.15]], np.float32)) np.testing.assert_array_equal(ww["a"], w["a"]) assert out[0]["target"][0, 1] == 255 @pytest.mark.parametrize( "failure", [ "paint", "existing_bg", "test", "negative", "unknown_weight", "nan", "missing", "zero_street_mass", ], ) def test_reject_invalid_admission(failure): rr, w, a = fixtures() if failure == "paint": a["a"][0, 0] = True if failure == "existing_bg": a["b"][0, 0] = True if failure == "test": rr[0]["split"] = "test" if failure == "negative": w["b"][0, 0] = -1 if failure == "unknown_weight": w["a"][0, 1] = 0.1 if failure == "nan": w["a"][0, 0] = np.nan if failure == "missing": a.pop("b") if failure == "zero_street_mass": w["b"][0, 0] = 0 a["a"][0, 1] = True with pytest.raises(ValueError): m.prepare_context(rr, w, a) def test_gate_rejects_paint_regression_despite_better_background(): context_gate = m.context_gate old = dict( context_fp=100, bce=1.0, original_fp=3, prior_added_fp=2, weak_fn=0, clear_fn=0, background_fp=105, ) new = dict(old, context_fp=20, bce=0.2, background_fp=25) assert context_gate(old, new, old)["passed"] assert not context_gate(old, dict(new, weak_fn=1), old)["passed"] assert not context_gate(old, dict(new, original_fp=4), old)["passed"] assert not context_gate(old, dict(new, context_fp=51), old)["passed"] assert not context_gate(old, dict(new, bce=1.0), old)["passed"]