Skip to content

Commit 554a71a

Browse files
committed
feat: depraction of substation_nodes in get_components
Signed-off-by: Vincent Koppen <[email protected]>
1 parent dbe1714 commit 554a71a

File tree

5 files changed

+39
-14
lines changed

5 files changed

+39
-14
lines changed

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 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
@@ -248,9 +248,23 @@ def get_all_paths(self, ext_start_node_id: int, ext_end_node_id: int) -> list[li
248248

249249
return [self._internals_to_externals(path) for path in internal_paths]
250250

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

256270
def get_connected(
@@ -389,7 +403,7 @@ def _get_shortest_path(self, source, target): ...
389403
def _get_all_paths(self, source, target) -> list[list[int]]: ...
390404

391405
@abstractmethod
392-
def _get_components(self, substation_nodes: list[int]) -> list[list[int]]: ...
406+
def _get_components(self) -> list[list[int]]: ...
393407

394408
@abstractmethod
395409
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
@@ -164,6 +164,19 @@ def test_tmp_remove_nodes(graph_with_2_routes) -> None:
164164

165165

166166
def test_get_components(graph_with_2_routes):
167+
graph = graph_with_2_routes
168+
graph.add_node(99)
169+
graph.add_node(100)
170+
171+
components = graph.get_components()
172+
173+
assert len(components) == 3
174+
assert set(components[0]) == {1, 2, 3, 4, 5}
175+
assert set(components[1]) == {99}
176+
assert set(components[2]) == {100}
177+
178+
179+
def test_get_components_with_substation_nodes(graph_with_2_routes):
167180
graph = graph_with_2_routes
168181
graph.add_node(99)
169182
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)