Skip to content

Commit d52e93c

Browse files
Feature: add tmp_remove_nodes method to graph (#17)
* Add .all_branches property to BaseGraphModel Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com> * Add tmp-remove-nodes method Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com> * Add .all_branches property to BaseGraphModel Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com> * Add test Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Vincent Koppen <vincent.koppen@alliander.com> Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com> * yield nothing Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com> * fix merge issues Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com> * make .in_edges a public method Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com> * rename method to .in_branches Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com> * remove unnecessary docstring Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com> * fix constants for graph performance tests Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com> --------- Signed-off-by: Thijs Baaijen <13253091+Thijss@users.noreply.github.com> Co-authored-by: Vincent Koppen <vincent.koppen@alliander.com>
1 parent 96fddbe commit d52e93c

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# SPDX-License-Identifier: MPL-2.0
44

55
from abc import ABC, abstractmethod
6+
from contextlib import contextmanager
67
from typing import Generator
78

89
import numpy as np
@@ -72,6 +73,14 @@ def has_node(self, node_id: int) -> bool:
7273

7374
return self._has_node(node_id=internal_node_id)
7475

76+
def in_branches(self, node_id: int) -> Generator[tuple[int, int], None, None]:
77+
"""Return all branches that have the node as an endpoint."""
78+
int_node_id = self.external_to_internal(node_id)
79+
internal_edges = self._in_branches(int_node_id=int_node_id)
80+
return (
81+
(self.internal_to_external(source), self.internal_to_external(target)) for source, target in internal_edges
82+
)
83+
7584
def add_node(self, ext_node_id: int, raise_on_fail: bool = True) -> None:
7685
"""Add a node to the graph."""
7786
if self.has_node(ext_node_id):
@@ -173,6 +182,28 @@ def delete_branch3_array(self, branch3_array: Branch3Array, raise_on_fail: bool
173182
branches = _get_branch3_branches(branch3)
174183
self.delete_branch_array(branches, raise_on_fail=raise_on_fail)
175184

185+
@contextmanager
186+
def tmp_remove_nodes(self, nodes: list[int]) -> Generator:
187+
"""Context manager that temporarily removes nodes and their branches from the graph.
188+
Example:
189+
>>> with graph.tmp_remove_nodes([1, 2, 3]):
190+
>>> assert not graph.has_node(1)
191+
>>> assert graph.has_node(1)
192+
In practice, this is useful when you want to e.g. calculate the shortest path between two nodes without
193+
considering certain nodes.
194+
"""
195+
edge_list = []
196+
for node in nodes:
197+
edge_list += list(self.in_branches(node))
198+
self.delete_node(node)
199+
200+
yield
201+
202+
for node in nodes:
203+
self.add_node(node)
204+
for source, target in edge_list:
205+
self.add_branch(source, target)
206+
176207
def get_shortest_path(self, ext_start_node_id: int, ext_end_node_id: int) -> tuple[list[int], int]:
177208
"""Calculate the shortest path between two nodes
178209
@@ -279,6 +310,9 @@ def _branch_is_relevant(self, branch: BranchArray) -> bool:
279310
return branch.is_active.item()
280311
return True
281312

313+
@abstractmethod
314+
def _in_branches(self, int_node_id: int) -> Generator[tuple[int, int], None, None]: ...
315+
282316
@abstractmethod
283317
def _get_connected(self, node_id: int, nodes_to_ignore: list[int], inclusive: bool = False) -> list[int]: ...
284318

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ def _get_connected(self, node_id: int, nodes_to_ignore: list[int], inclusive: bo
100100

101101
return connected_nodes
102102

103+
def _in_branches(self, int_node_id: int) -> Generator[tuple[int, int], None, None]:
104+
return ((source, target) for source, target, _ in self._graph.in_edges(int_node_id))
105+
103106
def _find_fundamental_cycles(self) -> list[list[int]]:
104107
"""Find all fundamental cycles in the graph using Rustworkx.
105108

tests/unit/model/graphs/test_graph_model.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
"""Grid tests"""
66

7+
from collections import Counter
8+
79
import numpy as np
810
import pytest
911
from numpy.testing import assert_array_equal
@@ -55,6 +57,17 @@ def test_graph_all_branches_parallel(graph):
5557
assert [(1, 2), (1, 2), (2, 1)] == list(graph.all_branches)
5658

5759

60+
def test_graph_in_branches(graph):
61+
graph.add_node(1)
62+
graph.add_node(2)
63+
graph.add_branch(1, 2)
64+
graph.add_branch(1, 2)
65+
graph.add_branch(2, 1)
66+
67+
assert [(2, 1), (2, 1), (2, 1)] == list(graph.in_branches(1))
68+
assert [(1, 2), (1, 2), (1, 2)] == list(graph.in_branches(2))
69+
70+
5871
def test_graph_delete_branch(graph):
5972
"""Test whether a branch is deleted correctly"""
6073
graph.add_node(1)
@@ -338,3 +351,30 @@ def test_get_connected_ignore_multiple_nodes(self, graph_with_2_routes):
338351
connected_nodes = graph.get_connected(node_id=1, nodes_to_ignore=[2, 4])
339352

340353
assert {5} == set(connected_nodes)
354+
355+
356+
def test_tmp_remove_nodes(graph_with_2_routes) -> None:
357+
graph = graph_with_2_routes
358+
359+
assert graph.nr_branches == 4
360+
361+
# add parallel branches to test whether they are restored correctly
362+
graph.add_branch(1, 5)
363+
graph.add_branch(5, 1)
364+
365+
assert graph.nr_nodes == 5
366+
assert graph.nr_branches == 6
367+
368+
before_sets = [frozenset(branch) for branch in graph.all_branches]
369+
counter_before = Counter(before_sets)
370+
371+
with graph.tmp_remove_nodes([1, 2]):
372+
assert graph.nr_nodes == 3
373+
assert list(graph.all_branches) == [(5, 4)]
374+
375+
assert graph.nr_nodes == 5
376+
assert graph.nr_branches == 6
377+
378+
after_sets = [frozenset(branch) for branch in graph.all_branches]
379+
counter_after = Counter(after_sets)
380+
assert counter_before == counter_after

0 commit comments

Comments
 (0)