Skip to content

Commit ca399fc

Browse files
edenoclaude
andcommitted
improve(core): add strict validation for edge_map source keys
Add proper validation to reject invalid source edge_ids in edge_map to prevent user confusion and provide clear error messages. Changes: - Validate that all edge_map keys exist in the graph - Raise ValueError with helpful message listing valid edge_ids - Update tests to expect validation errors instead of silent ignoring - Maintain support for arbitrary target values (strings, new IDs, etc.) This improves user experience by catching configuration errors early rather than silently ignoring invalid mappings. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent bc34e7f commit ca399fc

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

src/track_linearization/core.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,8 +803,13 @@ def get_linearized_position(
803803
# Convert segment indices to edge labels
804804
edge_ids = edge_id_by_index[seg_idx]
805805

806-
# Apply edge_map to labels
806+
# Apply edge_map to labels and validate
807807
if edge_map is not None:
808+
invalid_keys = [k for k in edge_map.keys() if k not in index_by_edge_id]
809+
if invalid_keys:
810+
raise ValueError(f"edge_map contains invalid source edge_ids: {invalid_keys}. "
811+
f"Valid edge_ids are: {list(index_by_edge_id.keys())}")
812+
808813
# Apply mapping, keeping original dtype flexible for strings/mixed types
809814
mapped_edge_ids = np.array([edge_map.get(eid, eid) for eid in edge_ids])
810815
# Keep using original seg_idx for internal operations - only use mapped_edge_ids for output

src/track_linearization/tests/test_core.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -670,17 +670,15 @@ def test_edge_map_validation_invalid_keys(self, simple_rectangular_track):
670670
"""Test edge_map validation with invalid keys."""
671671
position = np.array([[15, 0]])
672672

673-
# Map non-existent edge ID - should be ignored
673+
# Map non-existent edge ID - should raise ValueError
674674
invalid_edge_map = {999: 1} # 999 doesn't exist
675675

676-
pos_df = get_linearized_position(
677-
position=position,
678-
track_graph=simple_rectangular_track,
679-
edge_map=invalid_edge_map
680-
)
681-
682-
# Should still work (invalid keys ignored)
683-
assert hasattr(pos_df, 'linear_position'), "Invalid edge_map keys should be ignored"
676+
with pytest.raises(ValueError, match="edge_map contains invalid source edge_ids"):
677+
get_linearized_position(
678+
position=position,
679+
track_graph=simple_rectangular_track,
680+
edge_map=invalid_edge_map
681+
)
684682

685683
def test_edge_map_none_vs_no_parameter(self, simple_rectangular_track):
686684
"""Test that edge_map=None is equivalent to not providing edge_map."""

src/track_linearization/tests/test_edge_map.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ def test_edge_map_merge_two_edges_to_one_label():
3030
df = core.get_linearized_position(pts, g, edge_map={10:99, 20:99}, use_HMM=False)
3131
assert set(df["track_segment_id"].unique()) == {99}
3232

33-
def test_edge_map_invalid_source_ignored():
33+
def test_edge_map_invalid_source_raises():
3434
g = _mk_line_graph()
3535
pts = np.array([[0.2,0.0]])
36-
# 999 is not a real edge_id in the graph; should be ignored
37-
df = core.get_linearized_position(pts, g, edge_map={999:42, 10:50}, use_HMM=False)
38-
# Should work and use the valid mapping (10->50) while ignoring invalid key (999)
39-
assert df.track_segment_id.iloc[0] == 50
36+
with pytest.raises(ValueError, match="edge_map contains invalid source edge_ids"):
37+
# 999 is not a real edge_id in the graph; should raise ValueError
38+
core.get_linearized_position(pts, g, edge_map={999:42, 10:50}, use_HMM=False)

0 commit comments

Comments
 (0)