Files
NODEDC_MISSION_CORE/tests/test_semantic_pixel_growth.py
T
DCCONSTRUCTIONS 25dc0a1b44 feat(semantics): add portable annotation, learning and review contracts
Add portable corpus, annotation provenance, admission, review and bounded agent-cycle contracts. Preserve dataset ownership and explicit research acceptance limits.
2026-10-29 09:10:00 +03:00

51 lines
1.8 KiB
Python

import importlib.util
from pathlib import Path
import numpy as np
import pytest
spec = importlib.util.spec_from_file_location(
"pixel_growth_probe",
Path(__file__).resolve().parents[1] / "simulation/semantic-learning/pixel_growth_probe.py",
)
probe = importlib.util.module_from_spec(spec)
spec.loader.exec_module(probe)
def unit(group, y, split="train"):
y = np.array(y)
return dict(
group=group, split=split, y=y, x=np.column_stack([y * 2 - 1, np.arange(len(y)) % 2])
)
def test_weights_balance_area_window_class_not_pixel_volume():
units = [unit("a", [0] * 30 + [1] * 10), unit("a", [0, 1]), unit("b", [0] * 100)]
w = probe.balanced_weights(units)
y = np.concatenate([u["y"] for u in units])
assert w[y == 1].sum() == pytest.approx(0.5)
assert w[:42][y[:42] == 0].sum() == pytest.approx(0.25)
assert w[42:].sum() == pytest.approx(0.25)
assert w[:40][y[:40] == 1].sum() == pytest.approx(0.25)
def test_controls_cannot_fit_or_normalize():
with pytest.raises(ValueError, match="Only training"):
probe.fit([unit("a", [0, 1]), unit("b", [0, 1], "regression")])
def test_ignore_pixels_never_enter_counts_and_negative_recall_is_undefined():
c = probe.counts([[True, True, False]], np.array([[255, 0, 0]], dtype="uint8"))
assert c == dict(tp=0, fn=0, fp=1, tn=1, ignored=1)
assert probe.aggregate([c])["recall"] is None
assert probe.aggregate([c])["fpr"] == 0.5
def test_fit_is_invariant_to_renaming_groups_and_uses_both_classes():
a = probe.fit([unit("a", [0, 0, 1, 1]), unit("b", [0, 1])])
b = probe.fit([unit("x", [0, 0, 1, 1]), unit("y", [0, 1])])
assert np.allclose(a["coefficients"], b["coefficients"])
assert a["coefficients"][1] > 0 and a["production_eligible"] is False
with pytest.raises(ValueError, match="Both reviewed"):
probe.fit([unit("a", [0, 0])])