Skip to content

Commit 7c39e21

Browse files
feat(BaseGraph): deprecation warning for substation_nodes argument in get_components (#23)
* feat: depraction of substation_nodes in get_components Signed-off-by: Vincent Koppen <[email protected]> * Update VERSION Signed-off-by: Thijs Baaijen <[email protected]> --------- Signed-off-by: Vincent Koppen <[email protected]> Signed-off-by: Thijs Baaijen <[email protected]> Co-authored-by: Thijs Baaijen <[email protected]>
1 parent e68031b commit 7c39e21

File tree

6 files changed

+40
-15
lines changed

6 files changed

+40
-15
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.2
1+
1.3

src/power_grid_model_ds/_core/model/graphs/models/base.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
#
33
# SPDX-License-Identifier: MPL-2.0
44

5+
import warnings
56
from abc import ABC, abstractmethod
67
from contextlib import contextmanager
78
from typing import TYPE_CHECKING, Generator
89

9-
import numpy as np
1010
from numpy._typing import NDArray
1111

1212
from power_grid_model_ds._core.model.arrays.pgm_arrays import Branch3Array, BranchArray, NodeArray
@@ -250,9 +250,23 @@ def get_all_paths(self, ext_start_node_id: int, ext_end_node_id: int) -> list[li
250250

251251
return [self._internals_to_externals(path) for path in internal_paths]
252252

253-
def get_components(self, substation_nodes: NDArray[np.int32]) -> list[list[int]]:
253+
def get_components(self, substation_nodes: list[int] | None = None) -> list[list[int]]:
254254
"""Returns all separate components when the substation_nodes are removed of the graph as lists"""
255-
internal_components = self._get_components(substation_nodes=self._externals_to_internals(substation_nodes))
255+
if substation_nodes:
256+
warnings.warn(
257+
message="""
258+
get_components: substation_nodes argument is deprecated and will be removed in a future release.
259+
The functionality is still available with the use of the `tmp_remove_nodes` context manager.
260+
261+
Example:
262+
>>> with graph.tmp_remove_nodes(substation_nodes):
263+
>>> components = graph.get_components()
264+
""",
265+
category=DeprecationWarning,
266+
stacklevel=2,
267+
)
268+
with self.tmp_remove_nodes(substation_nodes or []):
269+
internal_components = self._get_components()
256270
return [self._internals_to_externals(component) for component in internal_components]
257271

258272
def get_connected(
@@ -391,7 +405,7 @@ def _get_shortest_path(self, source, target): ...
391405
def _get_all_paths(self, source, target) -> list[list[int]]: ...
392406

393407
@abstractmethod
394-
def _get_components(self, substation_nodes: list[int]) -> list[list[int]]: ...
408+
def _get_components(self) -> list[list[int]]: ...
395409

396410
@abstractmethod
397411
def _find_fundamental_cycles(self) -> list[list[int]]: ...

src/power_grid_model_ds/_core/model/graphs/models/rustworkx.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,8 @@ def _get_shortest_path(self, source: int, target: int) -> tuple[list[int], int]:
8484
def _get_all_paths(self, source: int, target: int) -> list[list[int]]:
8585
return list(rx.all_simple_paths(self._graph, source, target))
8686

87-
def _get_components(self, substation_nodes: list[int]) -> list[list[int]]:
88-
no_os_graph = self._graph.copy()
89-
for os_node in substation_nodes:
90-
no_os_graph.remove_node(os_node)
91-
components = rx.connected_components(no_os_graph)
87+
def _get_components(self) -> list[list[int]]:
88+
components = rx.connected_components(self._graph)
9289
return [list(component) for component in components]
9390

9491
def _get_connected(self, node_id: int, nodes_to_ignore: list[int], inclusive: bool = False) -> list[int]:

src/power_grid_model_ds/_core/model/grids/helpers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@ def set_feeder_ids(grid: "Grid"):
4545
601 | 101 | 204
4646
"""
4747
_reset_feeder_ids(grid)
48-
feeder_node_ids = grid.node.filter(node_type=NodeType.SUBSTATION_NODE).id
49-
components = grid.graphs.active_graph.get_components(feeder_node_ids)
48+
feeder_node_ids = grid.node.filter(node_type=NodeType.SUBSTATION_NODE)["id"]
49+
with grid.graphs.active_graph.tmp_remove_nodes(feeder_node_ids):
50+
components = grid.graphs.active_graph.get_components()
5051
for component_node_ids in components:
5152
component_branches = _get_active_component_branches(grid, component_node_ids)
5253

tests/unit/model/graphs/test_graph_model.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,19 @@ def test_tmp_remove_nodes(graph_with_2_routes: BaseGraphModel) -> None:
166166

167167

168168
def test_get_components(graph_with_2_routes: BaseGraphModel):
169+
graph = graph_with_2_routes
170+
graph.add_node(99)
171+
graph.add_node(100)
172+
173+
components = graph.get_components()
174+
175+
assert len(components) == 3
176+
assert set(components[0]) == {1, 2, 3, 4, 5}
177+
assert set(components[1]) == {99}
178+
assert set(components[2]) == {100}
179+
180+
181+
def test_get_components_with_substation_nodes(graph_with_2_routes):
169182
graph = graph_with_2_routes
170183
graph.add_node(99)
171184
graph.add_branch(1, 99)

tests/unit/model/grids/test_grid_search.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ def test_get_branches_in_path_empty_path(self, basic_grid):
5757

5858

5959
def test_component_three_winding_transformer(grid_with_3wt):
60-
component_list = grid_with_3wt.graphs.active_graph.get_components(
61-
grid_with_3wt.node.filter(node_type=NodeType.SUBSTATION_NODE.value).id
62-
)
60+
substation_nodes = grid_with_3wt.node.filter(node_type=NodeType.SUBSTATION_NODE.value).id
61+
with grid_with_3wt.graphs.active_graph.tmp_remove_nodes(substation_nodes):
62+
component_list = grid_with_3wt.graphs.active_graph.get_components()
6363

6464
# check the components are as expected
6565
# use sets to make sure the order of the components is not important

0 commit comments

Comments
 (0)