Skip to content

Commit 8a99715

Browse files
Copilotjpalm3r
andauthored
Add tie-break coverage for tolerance-based tuple alias matching
Agent-Logs-Url: https://github.com/DHI/modelskill/sessions/1128e01b-f2da-4282-97c2-453c5bff0482 Co-authored-by: jpalm3r <28826351+jpalm3r@users.noreply.github.com>
1 parent f16a751 commit 8a99715

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

src/modelskill/model/network.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def __init__(
151151
def __repr__(self) -> str:
152152
return f"<{self.__class__.__name__}>: {self.name}"
153153

154-
_CHAINAGE_TOLERANCE = 1e-3 # Tolerance in the same units as source-network edge/breakpoint distances.
154+
_CHAINAGE_TOLERANCE = 1e-3 # Tolerance in source-network distance units (e.g., meters if chainage is in meters).
155155

156156
@property
157157
def time(self) -> pd.DatetimeIndex:
@@ -239,7 +239,8 @@ def _resolve_alias(self, alias: str | tuple[str, float]) -> int | None:
239239
Breakpoint tuple aliases are matched first by exact key lookup and then
240240
by edge ID and distance within ``_CHAINAGE_TOLERANCE``. If multiple
241241
candidates are within tolerance, the closest distance is selected; ties
242-
are broken by choosing the smallest node ID.
242+
are broken by choosing the smallest node ID. Distance units are the
243+
same as the network chainage units.
243244
"""
244245
if alias in self._alias_map:
245246
return self._alias_map[alias]

tests/test_network.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,23 @@ def test_extract_with_tuple_breakpoint_uses_closest_within_tolerance(
710710
extracted = nmr.extract(obs)
711711
assert extracted.node == node_b
712712

713+
def test_extract_with_tuple_breakpoint_tie_uses_smallest_node_id(
714+
self, sample_network, sample_node_data
715+
):
716+
nmr = NetworkModelResult(sample_network)
717+
base_distance = 10.0
718+
tol = NetworkModelResult._CHAINAGE_TOLERANCE
719+
node_a = int(sample_network.find(node="123"))
720+
node_b = int(sample_network.find(node="456"))
721+
nmr._alias_map[("reach_test", base_distance + 4e-4)] = node_a
722+
nmr._alias_map[("reach_test", base_distance + 8e-4)] = node_b
723+
724+
obs = NodeObservation(
725+
sample_node_data, at=("reach_test", base_distance + tol * 0.6)
726+
)
727+
extracted = nmr.extract(obs)
728+
assert extracted.node == min(node_a, node_b)
729+
713730
def test_extract_tuple_alias_wrong_key_raises(self, sample_network, sample_node_data):
714731
nmr = NetworkModelResult(sample_network)
715732
obs = NodeObservation(sample_node_data, at=("nonexistent_edge", 0.0))

0 commit comments

Comments
 (0)