import importlib.util from pathlib import Path import numpy as np import pytest spec = importlib.util.spec_from_file_location( "annotation_training_view", Path(__file__).parents[1] / "simulation/semantic-learning/annotation_training_view.py", ) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) training_view = module.training_view def example(): t = np.array([[1, 1, 0, 255], [1, 0, 1, 255]], dtype="uint8") b = t.copy() b[1, 2] = 255 added = (b == 255) & (t == 1) weak = added.copy() weak[0, 1] = True return dict( split="train", target=t, base_target=b, added=added, weak=weak, clear=(t == 1) & ~weak ) def test_withdrawal_updates_both_targets_and_conditions_without_mutation(): r = example() t = r["target"].copy() t[0, 0] = t[0, 2] = 255 n = training_view(r, t) assert n["target"][0, 0] == n["base_target"][0, 0] == 255 assert n["target"][0, 2] == n["base_target"][0, 2] == 255 assert not n["clear"][0, 0] assert r["clear"][0, 0] and r["target"][0, 0] == 1 assert np.array_equal(n["weak"], r["weak"]) n["target"][1, 0] = 255 assert t[1, 0] == r["target"][1, 0] == 1 @pytest.mark.parametrize("split", ["regression", "test", "validation"]) def test_forbid_nontraining(split): r = example() r["split"] = split with pytest.raises(ValueError): training_view(r, r["target"]) @pytest.mark.parametrize( "where,value", [((0, 0), 0), ((0, 2), 1), ((0, 3), 0), ((0, 3), 1), ((0, 1), 255), ((1, 2), 255)], ) def test_forbid_class_flips_new_labels_or_loss_of_weak_support(where, value): r = example() t = r["target"].copy() t[where] = value with pytest.raises(ValueError): training_view(r, t) def test_invalid_parent_and_condition(): r = example() r["base_target"][0, 0] = 0 with pytest.raises(ValueError): training_view(r, r["target"]) r = example() r["clear"][0, 2] = True with pytest.raises(ValueError): training_view(r, r["target"]) def test_identity_and_wrong_shape(): r = example() n = training_view(r, r["target"]) for key in ["target", "base_target", "weak", "clear", "added"]: assert np.array_equal(n[key], r[key]) with pytest.raises(ValueError): training_view(r, r["target"][:, :2])