Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
Signed-off-by: Thijs Baaijen <[email protected]>
  • Loading branch information
Thijss committed Feb 5, 2025
2 parents c93cd2f + d61f85d commit f7d7d18
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
10 changes: 0 additions & 10 deletions src/power_grid_model_ds/_core/model/graphs/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import dataclasses
from dataclasses import dataclass
from pathlib import PosixPath
from typing import Generator

import numpy as np
Expand Down Expand Up @@ -119,15 +118,6 @@ def make_inactive(self, branch: BranchArray) -> None:
graph.delete_branch(from_ext_node_id=from_node, to_ext_node_id=to_node)
setattr(self, field.name, graph)

def cache(self, cache_dir: PosixPath, compress: bool) -> PosixPath:
"""Cache the container into a folder with .pkl and graph files"""
cache_dir.mkdir(parents=True, exist_ok=True)

for field in self.graph_attributes:
graph = getattr(self, field.name)
graph.cache(cache_dir=cache_dir, graph_name=field.name, compress=compress)
return cache_dir

@classmethod
def from_arrays(cls, arrays: MinimalGridArrays) -> "GraphContainer":
"""Build from arrays"""
Expand Down
4 changes: 2 additions & 2 deletions src/power_grid_model_ds/_core/model/graphs/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ def delete_branch_array(self, branch_array: BranchArray, raise_on_fail: bool = T
if self._branch_is_relevant(branch):
self.delete_branch(branch.from_node.item(), branch.to_node.item(), raise_on_fail=raise_on_fail)

def delete_branch3_array(self, branch_array: Branch3Array, raise_on_fail: bool = True) -> None:
def delete_branch3_array(self, branch3_array: Branch3Array, raise_on_fail: bool = True) -> None:
"""Delete all branch3s in the branch3 array from the graph."""
for branch3 in branch_array:
for branch3 in branch3_array:
branches = _get_branch3_branches(branch3)
self.delete_branch_array(branches, raise_on_fail=raise_on_fail)

Expand Down
51 changes: 51 additions & 0 deletions tests/unit/model/graphs/test_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,57 @@ def test_from_arrays(basic_grid):
assert set(basic_grid.node.id) == set(graphs.complete_graph.external_ids)


@pytest.fixture
def graph_container_with_5_nodes():
graph_container = GraphContainer.empty()
for node_id in range(1, 6):
node = NodeArray.empty(1)
node.id = node_id
graph_container.add_node(node)
return graph_container


@pytest.fixture
def three_winding_transformers():
three_winding_transformers = ThreeWindingTransformerArray.empty(2)
three_winding_transformers.id = [301, 302]
three_winding_transformers.node_1 = [1, 1]
three_winding_transformers.node_2 = [2, 4]
three_winding_transformers.node_3 = [3, 5]
three_winding_transformers.status_1 = [1, 1]
three_winding_transformers.status_2 = [1, 1]
three_winding_transformers.status_3 = [0, 1]

return three_winding_transformers


def test_add_branch3(graph_container_with_5_nodes, three_winding_transformers):
graph_container_with_5_nodes.add_branch3(three_winding_transformers)
for from_node, to_node in [(1, 2), (1, 4), (1, 5), (4, 5)]:
assert graph_container_with_5_nodes.active_graph.has_branch(from_node, to_node)
assert graph_container_with_5_nodes.complete_graph.has_branch(from_node, to_node)

for from_node, to_node in [(1, 3), (2, 3)]:
assert not graph_container_with_5_nodes.active_graph.has_branch(from_node, to_node)
assert graph_container_with_5_nodes.complete_graph.has_branch(from_node, to_node)


def test_delete_branch3(graph_container_with_5_nodes, three_winding_transformers):
graph_container_with_5_nodes.add_branch3(three_winding_transformers)
graph_container_with_5_nodes.delete_branch3(three_winding_transformers[0])

assert not graph_container_with_5_nodes.active_graph.has_branch(1, 2)
assert not graph_container_with_5_nodes.complete_graph.has_branch(1, 2)
for from_node, to_node in [(1, 4), (1, 5), (4, 5)]:
assert graph_container_with_5_nodes.active_graph.has_branch(from_node, to_node)
assert graph_container_with_5_nodes.complete_graph.has_branch(from_node, to_node)
graph_container_with_5_nodes.delete_branch3(three_winding_transformers[1])

for from_node, to_node in [(1, 2), (1, 4), (1, 5), (4, 5)]:
assert not graph_container_with_5_nodes.active_graph.has_branch(from_node, to_node)
assert not graph_container_with_5_nodes.complete_graph.has_branch(from_node, to_node)


def test_from_arrays_active_three_winding(basic_grid):
nodes = NodeArray.zeros(3)
nodes.id = [1000, 1001, 1002]
Expand Down

0 comments on commit f7d7d18

Please sign in to comment.