Skip to content

Commit 06618b7

Browse files
fix: prevent append with overlapping ids (#92)
* fix: prevent append with overlapping ids Signed-off-by: jaapschoutenalliander <[email protected]> * feat: change start id back to non breaking value Signed-off-by: jaapschoutenalliander <[email protected]> --------- Signed-off-by: jaapschoutenalliander <[email protected]>
1 parent 6e915df commit 06618b7

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

src/power_grid_model_ds/_core/model/containers/base.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,18 @@ def _update_id_counter(self, array, check_max_id: bool = True):
212212
array = self.attach_ids(array)
213213
elif np.any(array.id == EMPTY_ID):
214214
raise ValueError(f"Cannot append: array contains empty [{EMPTY_ID}] and non-empty ids.")
215+
elif check_max_id and self.id_counter > 0:
216+
# Only check for overlaps when array has prescribed (non-empty) IDs
217+
# Check if any incoming ID might overlap with existing IDs
218+
# This prevents overlaps since counter tracks the highest used ID
219+
new_min_id = np.min(array.id)
220+
if new_min_id <= self._id_counter:
221+
raise ValueError(
222+
f"Cannot append: minimum id {new_min_id} is not greater than "
223+
f"the current id counter {self._id_counter}"
224+
)
215225

216226
new_max_id = np.max(array.id)
217-
if check_max_id and new_max_id < self._id_counter:
218-
raise ValueError(f"Cannot append: id {new_max_id} is lower than the id counter")
219-
220227
# Update _id_counter
221228
self._id_counter = max(self._id_counter, new_max_id)
222229

tests/unit/model/test_containers.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,46 @@ def test_id_counter():
155155
assert 42 == container.id_counter
156156

157157

158+
def test_append_with_overlapping_ids():
159+
"""Test that appending arrays with overlapping IDs raises an error."""
160+
grid = Grid.empty()
161+
162+
# Create first array with IDs [1, 2, 3]
163+
nodes_1 = NodeArray.zeros(3)
164+
nodes_1.id = [1, 2, 3]
165+
grid.append(nodes_1)
166+
167+
# Create second array with overlapping IDs [3, 4, 5] (ID 3 overlaps)
168+
nodes_2 = NodeArray.zeros(3)
169+
nodes_2.id = [3, 4, 5]
170+
171+
# This should raise a ValueError due to overlapping ID 3
172+
with pytest.raises(ValueError, match="Cannot append: minimum id 3 is not greater than the current id counter 3"):
173+
grid.append(nodes_2)
174+
175+
176+
def test_append_with_non_overlapping_ids():
177+
"""Test that appending arrays with non-overlapping IDs works correctly."""
178+
grid = Grid.empty()
179+
180+
# Create first array with IDs [1, 2, 3]
181+
nodes_1 = NodeArray.zeros(3)
182+
nodes_1.id = [1, 2, 3]
183+
grid.append(nodes_1)
184+
185+
# Create second array with non-overlapping IDs [4, 5, 6]
186+
nodes_2 = NodeArray.zeros(3)
187+
nodes_2.id = [4, 5, 6]
188+
189+
# This should work without error
190+
grid.append(nodes_2)
191+
192+
# Verify all nodes are in the grid
193+
assert grid.node.size == 6
194+
expected_ids = [1, 2, 3, 4, 5, 6]
195+
assert sorted(grid.node.id.tolist()) == expected_ids
196+
197+
158198
def test_branches(grid: Grid):
159199
nodes = NodeArray.zeros(10)
160200
grid.append(nodes)

0 commit comments

Comments
 (0)