Skip to content

Commit 97aa730

Browse files
committed
another file
Signed-off-by: Thijs Baaijen <[email protected]>
1 parent 6deabb1 commit 97aa730

File tree

1 file changed

+46
-39
lines changed
  • src/power_grid_model_ds/_core/model/grids

1 file changed

+46
-39
lines changed

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

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -95,27 +95,34 @@ def __str__(self) -> str:
9595
for transformer3 in self.three_winding_transformer:
9696
nodes = [transformer3.node_1.item(), transformer3.node_2.item(), transformer3.node_3.item()]
9797
for combo in itertools.combinations(nodes, 2):
98-
grid_str += f"S{combo[0]} S{combo[1]} {transformer3.id.item()},3-transformer\n"
98+
grid_str += f"S{combo[0]} S{combo[1]} {transformer3['id'].item()},3-transformer\n"
9999

100100
for branch in self.branches:
101-
from_node = self.node.get(id=branch.from_node).record
102-
to_node = self.node.get(id=branch.to_node).record
103-
104-
from_node_str = f"S{from_node.id}" if from_node.node_type == NodeType.SUBSTATION_NODE else str(from_node.id)
105-
to_node_str = f"S{to_node.id}" if to_node.node_type == NodeType.SUBSTATION_NODE else str(to_node.id)
106-
107-
suffix_str = str(branch.id.item())
108-
if branch.from_status.item() == 0 or branch.to_status.item() == 0:
101+
from_node = self.node.get(id=branch["from_node"])
102+
to_node = self.node.get(id=branch["to_node"])
103+
104+
# pylint: disable=no-member # pylint false positive on from_node["id"].item()
105+
from_node_str = (
106+
f"S{from_node['id']}"
107+
if from_node["node_type"] == NodeType.SUBSTATION_NODE
108+
else str(from_node["id"].item())
109+
)
110+
to_node_str = (
111+
f"S{to_node['id']}" if to_node["node_type"] == NodeType.SUBSTATION_NODE else str(to_node["id"].item())
112+
)
113+
114+
suffix_str = str(branch["id"].item())
115+
if branch["from_status"].item() == 0 or branch["to_status"].item() == 0:
109116
suffix_str = f"{suffix_str},open"
110117

111-
if branch.id in self.transformer.id:
118+
if branch["id"] in self.transformer["id"]:
112119
suffix_str = f"{suffix_str},transformer"
113-
elif branch.id in self.link.id:
120+
elif branch["id"] in self.link["id"]:
114121
suffix_str = f"{suffix_str},link"
115-
elif branch.id in self.line.id:
122+
elif branch["id"] in self.line["id"]:
116123
pass # no suffix needed
117124
else:
118-
raise ValueError(f"Branch {branch.id} is not a transformer, link or line")
125+
raise ValueError(f"Branch {branch['id']} is not a transformer, link or line")
119126

120127
grid_str += f"{from_node_str} {to_node_str} {suffix_str}\n"
121128
return grid_str
@@ -152,9 +159,9 @@ def get_typed_branches(self, branch_ids: list[int] | npt.NDArray[np.int32]) -> B
152159
raise ValueError("No branch_ids provided.")
153160
for branch_array in self.branch_arrays:
154161
array = branch_array.filter(branch_ids)
155-
if 0 < array.size != len(branch_ids):
162+
if 0 < len(array) != len(branch_ids):
156163
raise ValueError("Branches are not of the same type.")
157-
if array.size:
164+
if len(array):
158165
return array
159166
raise RecordDoesNotExist(f"Branches {branch_ids} not found in grid.")
160167

@@ -164,19 +171,19 @@ def reverse_branches(self, branches: BranchArray):
164171
return
165172
if not isinstance(branches, (LineArray, LinkArray, TransformerArray)):
166173
try:
167-
branches = self.get_typed_branches(branches.id)
174+
branches = self.get_typed_branches(branches["id"])
168175
except ValueError:
169176
# If the branches are not of the same type, reverse them per type (though this is slower)
170177
for array in self.branch_arrays:
171-
self.reverse_branches(array.filter(branches.id))
178+
self.reverse_branches(array.filter(branches["id"]))
172179
return
173180

174-
from_nodes = branches.from_node
175-
to_nodes = branches.to_node
181+
from_nodes = branches["from_node"]
182+
to_nodes = branches["to_node"]
176183

177184
array_field = self.find_array_field(branches.__class__)
178185
array = getattr(self, array_field.name)
179-
array.update_by_id(branches.id, from_node=to_nodes, to_node=from_nodes)
186+
array.update_by_id(branches["id"], from_node=to_nodes, to_node=from_nodes)
180187

181188
@classmethod
182189
def empty(cls: Type[Self], graph_model: type[BaseGraphModel] = RustworkxGraphModel) -> Self:
@@ -213,7 +220,7 @@ def add_branch(self, branch: BranchArray) -> None:
213220
self._append(array=branch)
214221
self.graphs.add_branch_array(branch_array=branch)
215222

216-
logging.debug(f"added branch {branch.id} from {branch.from_node} to {branch.to_node}")
223+
logging.debug(f"added branch {branch['id']} from {branch['from_node']} to {branch['to_node']}")
217224

218225
def delete_branch(self, branch: BranchArray) -> None:
219226
"""Remove a branch from the grid
@@ -224,7 +231,7 @@ def delete_branch(self, branch: BranchArray) -> None:
224231
_delete_branch_array(branch=branch, grid=self)
225232
self.graphs.delete_branch(branch=branch)
226233
logging.debug(
227-
f"""deleted branch {branch.id.item()} from {branch.from_node.item()} to {branch.to_node.item()}"""
234+
f"""deleted branch {branch["id"].item()} from {branch["from_node"].item()} to {branch["to_node"].item()}"""
228235
)
229236

230237
def delete_branch3(self, branch: Branch3Array) -> None:
@@ -244,25 +251,25 @@ def add_node(self, node: NodeArray) -> None:
244251
"""
245252
self._append(array=node)
246253
self.graphs.add_node_array(node_array=node)
247-
logging.debug(f"added rail {node.id}")
254+
logging.debug(f"added rail {node['id']}")
248255

249256
def delete_node(self, node: NodeArray) -> None:
250257
"""Remove a node from the grid
251258
252259
Args:
253260
node (NodeArray): The node to remove
254261
"""
255-
self.node = self.node.exclude(id=node.id)
256-
self.sym_load = self.sym_load.exclude(node=node.id)
257-
self.source = self.source.exclude(node=node.id)
262+
self.node = self.node.exclude(id=node["id"])
263+
self.sym_load = self.sym_load.exclude(node=node["id"])
264+
self.source = self.source.exclude(node=node["id"])
258265

259266
for branch_array in self.branch_arrays:
260-
matching_branches = branch_array.filter(from_node=node.id, to_node=node.id, mode_="OR")
267+
matching_branches = branch_array.filter(from_node=node["id"], to_node=node["id"], mode_="OR")
261268
for branch in matching_branches:
262269
self.delete_branch(branch)
263270

264271
self.graphs.delete_node(node=node)
265-
logging.debug(f"deleted rail {node.id}")
272+
logging.debug(f"deleted rail {node['id']}")
266273

267274
def make_active(self, branch: BranchArray) -> None:
268275
"""Make a branch active
@@ -272,13 +279,13 @@ def make_active(self, branch: BranchArray) -> None:
272279
"""
273280
array_field = self.find_array_field(branch.__class__)
274281
array_attr = getattr(self, array_field.name)
275-
branch_mask = array_attr.id == branch.id
276-
array_attr.from_status[branch_mask] = 1
277-
array_attr.to_status[branch_mask] = 1
282+
branch_mask = array_attr["id"] == branch["id"]
283+
array_attr["from_status"][branch_mask] = 1
284+
array_attr["to_status"][branch_mask] = 1
278285
setattr(self, array_field.name, array_attr)
279286

280287
self.graphs.make_active(branch=branch)
281-
logging.debug(f"activated branch {branch.id}")
288+
logging.debug(f"activated branch {branch['id']}")
282289

283290
def make_inactive(self, branch: BranchArray, at_to_side: bool = True) -> None:
284291
"""Make a branch inactive. This is done by setting from or to status to 0.
@@ -290,13 +297,13 @@ def make_inactive(self, branch: BranchArray, at_to_side: bool = True) -> None:
290297
"""
291298
array_field = self.find_array_field(branch.__class__)
292299
array_attr = getattr(self, array_field.name)
293-
branch_mask = array_attr.id == branch.id
300+
branch_mask = array_attr["id"] == branch["id"]
294301
status_side = "to_status" if at_to_side else "from_status"
295302
array_attr[status_side][branch_mask] = 0
296303
setattr(self, array_field.name, array_attr)
297304

298305
self.graphs.make_inactive(branch=branch)
299-
logging.debug(f"deactivated branch {branch.id}")
306+
logging.debug(f"deactivated branch {branch['id']}")
300307

301308
def get_branches_in_path(self, nodes_in_path: list[int]) -> BranchArray:
302309
"""Returns all branches within a path of nodes
@@ -325,7 +332,7 @@ def get_nearest_substation_node(self, node_id: int):
325332
substation_nodes = self.node.filter(node_type=NodeType.SUBSTATION_NODE.value)
326333

327334
for node in connected_nodes:
328-
if node in substation_nodes.id:
335+
if node in substation_nodes["id"]:
329336
return substation_nodes.get(node)
330337
raise RecordDoesNotExist(f"No {NodeType.SUBSTATION_NODE.name} connected to node {node_id}")
331338

@@ -352,11 +359,11 @@ def get_downstream_nodes(self, node_id: int, inclusive: bool = False):
352359
"""
353360
substation_nodes = self.node.filter(node_type=NodeType.SUBSTATION_NODE.value)
354361

355-
if node_id in substation_nodes.id:
362+
if node_id in substation_nodes["id"]:
356363
raise NotImplementedError("get_downstream_nodes is not implemented for substation nodes!")
357364

358365
return self.graphs.active_graph.get_downstream_nodes(
359-
node_id=node_id, start_node_ids=list(substation_nodes.id), inclusive=inclusive
366+
node_id=node_id, start_node_ids=list(substation_nodes["id"]), inclusive=inclusive
360367
)
361368

362369
def cache(self, cache_dir: Path, cache_name: str, compress: bool = True):
@@ -463,6 +470,6 @@ def _delete_branch_array(branch: BranchArray | Branch3Array, grid: Grid):
463470
"""Delete a branch array from the grid"""
464471
array_field = grid.find_array_field(branch.__class__)
465472
array_attr = getattr(grid, array_field.name)
466-
setattr(grid, array_field.name, array_attr.exclude(id=branch.id))
473+
setattr(grid, array_field.name, array_attr.exclude(id=branch["id"]))
467474

468-
grid.transformer_tap_regulator = grid.transformer_tap_regulator.exclude(regulated_object=branch.id)
475+
grid.transformer_tap_regulator = grid.transformer_tap_regulator.exclude(regulated_object=branch["id"])

0 commit comments

Comments
 (0)