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

207 lines
6.8 KiB
Python

"""Checkable partial-support paths; no implicit topology through unknown/exterior."""
import importlib.util
import json
from pathlib import Path
import numpy as np
import pytest
spec = importlib.util.spec_from_file_location(
"topology_test", Path(__file__).parents[1] / "simulation/semantic-learning/observed_topology.py"
)
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
def pair():
t = np.zeros((5, 7), np.uint8)
left, right, gap = (np.zeros(t.shape, bool) for _ in range(3))
left[2, 1] = right[2, 5] = True
gap[2, 2:5] = True
t[left | right] = 1
p = np.zeros(t.shape, np.float32)
p[2, 1:6] = 1
return p, t, dict(left=left, right=right), [dict(left="left", right="right", gap=gap)]
def run(args, **kw):
return m.audit_topology(
*args, roi=kw.pop("roi", [0, 0, 7, 5]), connectivity=kw.pop("connectivity", 4), **kw
)
def test_confirmed_bridge_with_unknown_bypass_is_still_known_witness():
args = pair()
p, t, _, _ = args
t[1, 1:6] = 255
p[1, 1:6] = 1
before = [x.copy() for x in (p, t)]
result = run(args)
row = result["relations"][0]
assert row["status"] == "confirmed_known_support_bridge"
assert row["witness"] == [[x, 2] for x in range(1, 6)]
assert row["witness_unknown_pixels"] == 0
assert not row["full_object_merge_assessed"]
assert np.array_equal(p, before[0]) and np.array_equal(t, before[1])
json.dumps(result) # Public result must be plain JSON data.
def test_unknown_connection_requires_predicted_paint_on_unknown():
args = pair()
p, t, _, rel = args
t[2, 3] = 255
rel[0]["gap"][2, 3] = False
row = run(args)["relations"][0]
assert row["status"] == "indeterminate_unknown_connection"
assert row["witness_unknown_pixels"] == 1
p[2, 3] = 0
assert run(args)["relations"][0]["status"] == "no_connection_within_reviewed_corridor"
def test_missing_endpoint_never_proves_separation():
args = pair()
args[0][2, 1] = 0
result = run(args)
assert result["relations"][0]["status"] == "unassessed_missing_predicted_endpoint"
assert result["cores"][0]["fully_missed_original_components"] == 1
def test_exterior_bypass_is_not_an_inside_roi_connection():
args = pair()
p, t, _, rel = args
p[2, 3] = 0
p[1, 1:6] = 1
t[1, 1:6] = 255
row = run(args, roi=[0, 2, 7, 3])["relations"][0]
assert row["status"] == "no_connection_within_reviewed_corridor"
assert row["exterior_connection_assessed"] is False
def test_diagonal_is_explicitly_four_vs_eight_connectivity():
p, t, cores, relations = pair()
t[:] = 0
p[:] = 0
for mask in cores.values():
mask[:] = False
cores["left"][1, 1] = cores["right"][3, 3] = True
t[cores["left"] | cores["right"]] = 1
relations[0]["gap"][:] = False
relations[0]["gap"][2, 2] = True
p[1, 1] = p[2, 2] = p[3, 3] = 1
assert run((p, t, cores, relations), connectivity=4)["relations"][0]["witness"] is None
assert (
run((p, t, cores, relations), connectivity=8)["relations"][0]["status"]
== "confirmed_known_support_bridge"
)
def test_core_adjacency_without_background_is_not_false_bridge():
p, t, cores, relations = pair()
cores["right"][:] = False
cores["right"][2, 2] = True
t[2, 5] = 0
t[2, 2] = 1
relations[0]["gap"][2, 2] = False
assert (
run((p, t, cores, relations))["relations"][0]["status"]
== "unassessed_core_adjacency_without_background"
)
def test_fragmentation_is_per_original_known_component():
t = np.zeros((5, 7), np.uint8)
t[2, 1:6] = 1
p = (t == 1).astype(np.float32)
p[2, 3] = 0
core = t == 1
result = run((p, t, {"line": core}, []))["cores"][0]
assert result["fragmented_original_components"] == 1
assert result["known_core_fragmentation_witness"] == [[x, 2] for x in range(2, 5)]
assert result["full_object_split_assessed"] is False
t[2, 3] = 255
core[2, 3] = False
row = run((p, t, {"line": core}, []))["cores"][0]
assert row["original_components"] == 2 and row["recovered_components"] == 2
assert row["fragmented_original_components"] == 0
p[:] = 0
row = run((p, t, {"line": core}, []))["cores"][0]
assert row["fully_missed_original_components"] == 2
assert row["known_core_fragmentation_witness"] is None
@pytest.mark.parametrize(
"error",
[
"nan",
"range",
"shape",
"target",
"core_unknown",
"gap_paint",
"overlap",
"outside",
"duplicate",
],
)
def test_invalid_support_rejected(error):
p, t, cores, relations = pair()
kwargs = {}
if error == "nan":
p[0, 0] = np.nan
elif error == "range":
p[0, 0] = 1.1
elif error == "shape":
p = p[:-1]
elif error == "target":
t = t.astype(np.int64)
elif error == "core_unknown":
t[2, 1] = 255
elif error == "gap_paint":
relations[0]["gap"][2, 1] = True
elif error == "overlap":
cores["right"][2, 1] = True
elif error == "outside":
kwargs["roi"] = [0, 0, 4, 5]
elif error == "duplicate":
relations *= 2
with pytest.raises(ValueError):
run((p, t, cores, relations), **kwargs)
@pytest.mark.parametrize(
"kwargs", [dict(max_nodes=1), dict(max_path=2), dict(connectivity=6), dict(roi=[0, 0, 8, 5])]
)
def test_bounds_refuse_without_truncated_confirmed_witness(kwargs):
with pytest.raises(ValueError):
run(pair(), **kwargs)
def test_many_original_components_do_not_overflow_int32_pair_ids():
y, x = np.indices((384, 384))
core = (x + y) % 2 == 0
target = core.astype(np.uint8)
result = m.audit_topology(
target.astype(np.float32), target, {"many": core}, [], roi=[0, 0, 384, 384], connectivity=4
)
row = result["cores"][0]
assert row["original_components"] == row["recovered_components"] == 73728
assert row["fragmented_original_components"] == row["fully_missed_original_components"] == 0
def test_outside_core_bypass_does_not_erase_local_fragmentation():
target = np.zeros((5, 7), np.uint8)
target[2, 1:6] = 1
core = target == 1
probability = core.astype(np.float32)
probability[2, 3] = 0 # A genuine FN cuts the recovered known-positive core.
probability[1, 1:6] = 1 # Predicted paint reconnects it through known BG.
bypass = [(1, 2), (1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (5, 2)]
assert all(probability[y, x] == 1 for x, y in bypass)
row = run((probability, target, {"line": core}, []))["cores"][0]
assert row["original_components"] == 1 and row["recovered_components"] == 2
assert row["fragmented_original_components"] == 1
assert row["full_object_split_assessed"] is False
witness = row["known_core_fragmentation_witness"]
assert [3, 2] in witness and all(core[y, x] for x, y in witness)