feat(semantics): version scene edits and refine paint geometry
This commit is contained in:
@@ -0,0 +1,187 @@
|
||||
import copy
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from shapely.geometry import mapping, shape
|
||||
from shapely.ops import unary_union
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "simulation/semantic-learning"))
|
||||
import paint_objects as po
|
||||
from paint_context import contextual_additions as admit
|
||||
|
||||
|
||||
def contextual_additions(*args, contrast_fn=None):
|
||||
return admit(
|
||||
*args,
|
||||
experimental_continuations=True,
|
||||
contrast_fn=contrast_fn or (lambda *_: {"contrast": 0.3, "source": "synthetic"}),
|
||||
)
|
||||
|
||||
|
||||
def strip(name, start, end, width=0.2, **extra):
|
||||
g = po.strip_geometry([start, end], width)
|
||||
return dict(
|
||||
id=name,
|
||||
kind="linear_marking",
|
||||
representation="strip",
|
||||
placement_status="placed",
|
||||
parameters=dict(centerline=[start, end], width_m=width),
|
||||
geometry=mapping(g),
|
||||
observation_geometry=mapping(g),
|
||||
observation_ids=[name],
|
||||
fit=dict(score=0.9),
|
||||
sources=["synthetic"],
|
||||
**extra,
|
||||
)
|
||||
|
||||
|
||||
def test_same_observed_line_extends_without_changing_seed():
|
||||
seed = strip("seed", (0, 0), (5, 0))
|
||||
candidate = strip("candidate", (4, 0), (9, 0))
|
||||
before = copy.deepcopy(seed)
|
||||
added, log = contextual_additions([seed], [candidate])
|
||||
assert seed == before and len(added) == 1 and log[0]["accepted"]
|
||||
g = shape(added[0]["geometry"])
|
||||
assert g.bounds[0] > 5 and g.bounds[2] == 9
|
||||
assert added[0]["context_admission"]["method"] == "observed-continuation"
|
||||
|
||||
|
||||
def test_parallel_offset_and_real_gap_cannot_supply_continuation():
|
||||
seed = strip("seed", (0, 0), (5, 0))
|
||||
for candidate in [strip("offset", (4, 0.4), (9, 0.4)), strip("gap", (5.2, 0), (9, 0))]:
|
||||
added, _ = contextual_additions([seed], [candidate])
|
||||
assert not added
|
||||
|
||||
|
||||
def test_added_candidates_never_seed_a_chain_and_order_is_stable():
|
||||
seed = strip("seed", (0, 0), (5, 0))
|
||||
a = strip("a", (4, 0), (9, 0))
|
||||
b = strip("b", (8, 0), (13, 0))
|
||||
first, _ = contextual_additions([seed], [a, b])
|
||||
second, _ = contextual_additions([seed], [b, a])
|
||||
assert first == second and len(first) == 1
|
||||
assert shape(first[0]["geometry"]).bounds[2] == 9
|
||||
|
||||
|
||||
def test_actual_missing_crosswalk_bar_requires_independent_strict_bars():
|
||||
seeds = [strip(str(i), (i, 0), (i, 4), 0.45, group_id="strict-zebra") for i in [0, 1, 3, 4]]
|
||||
candidate = strip("missing", (2, 0), (2, 4), 0.45)
|
||||
added, _ = contextual_additions(seeds, [candidate])
|
||||
assert len(added) == 1
|
||||
assert added[0]["kind"] == "crosswalk_bar"
|
||||
assert not contextual_additions(seeds, [strip("spot", (2, 1), (2, 1.5), 0.45)])[0]
|
||||
duplicates = [strip(str(i), (0, 0), (0, 4), 0.45) for i in range(4)]
|
||||
assert not contextual_additions(duplicates, [candidate])[0]
|
||||
|
||||
|
||||
def test_connected_junction_needs_a_direct_frozen_anchor():
|
||||
seed = strip("seed", (0, 0), (5, 0))
|
||||
a = strip("a", (4, 0), (9, 0), junction_components=["component"])
|
||||
b = strip("b", (8, 0), (8, 3), junction_components=["component"])
|
||||
added, _ = contextual_additions([seed], [a, b])
|
||||
assert len(added) == 2
|
||||
assert {o["context_admission"]["method"] for o in added} == {
|
||||
"observed-continuation",
|
||||
"anchored-junction",
|
||||
}
|
||||
assert not contextual_additions([], [a, b])[0]
|
||||
detached = strip("detached", (12, 0), (12, 3), junction_components=["component"])
|
||||
assert len(contextual_additions([seed], [a, detached])[0]) == 1
|
||||
|
||||
|
||||
def test_catalog_overlap_cannot_be_relabelled_a_strip():
|
||||
seed = strip("seed", (0, 0), (5, 0))
|
||||
symbol = strip("symbol", (6, 0), (8, 0))
|
||||
symbol["representation"] = "catalog"
|
||||
added, log = contextual_additions([seed, symbol], [strip("candidate", (4, 0), (9, 0))])
|
||||
assert not added and log[0]["reason"] == "catalog-conflict"
|
||||
|
||||
|
||||
def test_no_new_geometry_outside_observed_candidate():
|
||||
seed = strip("seed", (0, 0), (5, 0))
|
||||
candidate = strip("candidate", (4, 0), (9, 0))
|
||||
added, _ = contextual_additions([seed], [candidate])
|
||||
assert (
|
||||
unary_union([shape(o["geometry"]) for o in added])
|
||||
.difference(shape(candidate["geometry"]))
|
||||
.area
|
||||
< 1e-8
|
||||
)
|
||||
|
||||
|
||||
def test_old_well_supported_part_cannot_pay_for_bad_tail():
|
||||
from shapely.geometry import box
|
||||
|
||||
seed = strip("seed", (0, 0), (8, 0))
|
||||
candidate = strip("tail", (4, 0), (9, 0))
|
||||
support = unary_union([box(4, -0.1, 7, 0.1), box(7, -0.03, 8, 0.03), box(8, -0.02, 9, 0.02)])
|
||||
candidate["observation_geometry"] = mapping(support)
|
||||
assert not contextual_additions([seed], [candidate])[0]
|
||||
|
||||
|
||||
def test_novel_part_requires_own_rgb_and_clears_old_group_metadata():
|
||||
seed = strip("seed", (0, 0), (5, 0))
|
||||
candidate = strip(
|
||||
"candidate",
|
||||
(4, 0),
|
||||
(9, 0),
|
||||
group_id="obsolete",
|
||||
observed_bar_count=99,
|
||||
crosswalk_parameters={"step_m": 17},
|
||||
)
|
||||
assert not contextual_additions([seed], [candidate], contrast_fn=lambda *_: {"contrast": 0})[0]
|
||||
result = contextual_additions([seed], [candidate])[0][0]
|
||||
assert all(k not in result for k in ["group_id", "observed_bar_count", "crosswalk_parameters"])
|
||||
|
||||
|
||||
def test_crosswalk_addition_uses_strict_group_not_proposal_group():
|
||||
seeds = [strip(str(i), (i, 0), (i, 4), 0.45, group_id="strict") for i in [0, 1, 3, 4]]
|
||||
candidate = strip("missing", (2, 0), (2, 4), 0.45, group_id="obsolete", observed_bar_count=99)
|
||||
result = contextual_additions(seeds, [candidate])[0][0]
|
||||
assert result["group_id"] == "strict" and "observed_bar_count" not in result
|
||||
seeds[0]["group_id"] = "ambiguous"
|
||||
assert not contextual_additions(seeds, [candidate])[0]
|
||||
|
||||
|
||||
def test_bilateral_rgb_rejects_bright_region_edge_and_accepts_paint():
|
||||
import numpy as np
|
||||
from paint_context import strip_contrast
|
||||
|
||||
params = dict(centerline=[[1, 2], [4, 2]], width_m=0.2)
|
||||
matrix = [0.01, 0, 0, 0.01, 0, 0]
|
||||
rgb = np.full((400, 600, 3), 60, dtype=np.uint8)
|
||||
rgb[190:210, 100:400] = 220
|
||||
evidence = strip_contrast(params, rgb, matrix)
|
||||
assert evidence["contrast"] > 0.5 and min(evidence["sample_counts"]) >= 4
|
||||
rgb[210:] = 250
|
||||
assert strip_contrast(params, rgb, matrix)["contrast"] < 0
|
||||
params["centerline"] = [[1, 0.15], [4, 0.15]]
|
||||
assert strip_contrast(params, rgb, matrix) is None
|
||||
|
||||
|
||||
def test_bilateral_rgb_is_rotation_invariant():
|
||||
import numpy as np
|
||||
from paint_context import strip_contrast
|
||||
|
||||
rgb = np.full((600, 600, 3), 60, dtype=np.uint8)
|
||||
yy, xx = np.mgrid[:600, :600]
|
||||
angle = 0.7
|
||||
u = np.array([np.cos(angle), np.sin(angle)])
|
||||
n = np.array([-u[1], u[0]])
|
||||
q = np.array([1, 1])
|
||||
end = q + 3 * u
|
||||
x = (xx + 0.5) * 0.01 - q[0]
|
||||
y = (yy + 0.5) * 0.01 - q[1]
|
||||
along = x * u[0] + y * u[1]
|
||||
across = x * n[0] + y * n[1]
|
||||
rgb[(along > 0) & (along < 3) & (np.abs(across) < 0.1)] = 220
|
||||
params = dict(centerline=[q.tolist(), end.tolist()], width_m=0.2)
|
||||
assert strip_contrast(params, rgb, [0.01, 0, 0, 0.01, 0, 0])["contrast"] > 0.5
|
||||
|
||||
|
||||
def test_shipping_default_never_extends_even_a_bright_supported_seed():
|
||||
seed = strip("seed", (0, 0), (5, 0))
|
||||
candidate = strip("candidate", (4, 0), (9, 0))
|
||||
additions, decisions = admit([seed], [candidate], contrast_fn=lambda *_: {"contrast": 0.9})
|
||||
assert not additions
|
||||
assert decisions[0]["reason"] == "unqualified-continuation-disabled"
|
||||
Reference in New Issue
Block a user