Files
NODEDC_MISSION_CORE/tests/test_feature_refinement.py
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

79 lines
2.8 KiB
Python

import importlib.util
from pathlib import Path
import pytest
spec = importlib.util.spec_from_file_location(
"feature_refinement",
Path(__file__).parents[1] / "simulation/semantic-learning/feature_refinement.py",
)
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
def tensor():
return pytest.importorskip("torch")
def test_identity_and_ablation():
t = tensor()
n = m.network(12, channels=4, width=8)
f, c = t.randn(31, 4), t.randn(31, 1)
assert t.equal(n(f, c, "scene_features"), c)
with t.no_grad():
n.output.weight.fill_(0.02)
assert t.equal(n(f, c, "coarse_only"), n(f * 100, c, "coarse_only"))
assert not t.equal(n(f, c, "scene_features"), n(f * 100, c, "scene_features"))
@pytest.mark.parametrize("shape", [(8, 9), (13, 17), (1, 1)])
def test_native_alignment(shape):
t = tensor()
f = t.arange(2 * 3 * 5).float().reshape(1, 2, 3, 5)
ids = t.arange(shape[0] * shape[1])
got = m.sample_native(f, shape, ids)
expected = t.nn.functional.interpolate(f, size=shape, mode="bilinear", align_corners=False)
t.testing.assert_close(got, expected[0].flatten(1).T, atol=1e-5, rtol=1e-6)
def test_tiled_full_and_c4():
t = tensor()
n = m.network(7, channels=4, width=8)
with t.no_grad():
n.output.weight.fill_(0.02)
f, c = t.randn(1, 4, 7, 7), t.randn(21, 21)
a = m.predict(n, f, c, "scene_features", chunk=441)
b = m.predict(n, f, c, "scene_features", chunk=37)
t.testing.assert_close(a, b, atol=2e-5, rtol=1e-6)
for k in range(4):
z = m.predict(n, t.rot90(f, k, [-2, -1]), t.rot90(c, k), "scene_features")
t.testing.assert_close(a, t.rot90(z, -k), atol=2e-5, rtol=1e-6)
def test_seed_and_no_fine_gradient():
t = tensor()
a, b = m.network(8, 4, 8), m.network(8, 4, 8)
assert all(t.equal(x, y) for x, y in zip(a.parameters(), b.parameters(), strict=True))
with t.no_grad():
a.output.weight.fill_(0.02)
a(t.randn(7, 4), t.ones(7, 1), "coarse_only").sum().backward()
assert t.count_nonzero(a.layers[0].weight.grad[:, :4]) == 0
def test_outside_points_rejected():
t = tensor()
with pytest.raises(ValueError):
m.sample_native(t.ones(1, 4, 2, 2), (4, 4), t.tensor([16]))
def test_capacity_gate():
b = dict(bce=1.0, weak_fn=2, clear_fn=4, background_fp=100)
a = dict(bce=0.4, weak_fn=1, clear_fn=3, background_fp=40)
c = dict(bce=0.8, weak_fn=2, clear_fn=4, background_fp=80)
assert m.capacity_screen(b, a, c)["passed"]
for key, bad in [("bce", 0.6), ("weak_fn", 3), ("clear_fn", 5), ("background_fp", 51)]:
assert not m.capacity_screen(b, dict(a, **{key: bad}), c)["passed"]
assert not m.capacity_screen(b, a, dict(c, weak_fn=0))["passed"]
with pytest.raises(ValueError):
m.capacity_screen(b, dict(a, bce=float("nan")), c)