Skip to content

Commit c1c355b

Browse files
committed
Add from_pgm_data
Signed-off-by: Thijs Baaijen <[email protected]>
1 parent e8852b8 commit c1c355b

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,25 @@ def set_feeder_ids(self):
440440
set_is_feeder(grid=self)
441441
set_feeder_ids(grid=self)
442442

443+
@classmethod
444+
def from_pgm_data(cls, pgm_data: dict[str, npt.NDArray], load_graphs: bool = True) -> "Grid":
445+
"""Initialize the grid from PowerGridModel input data.
446+
447+
Args:
448+
pgm_data (dict[str, npt.NDArray]): The PowerGridModel input data.
449+
"""
450+
new_grid = cls.empty()
451+
for array_name, array_data in pgm_data.items():
452+
pgm_array_class = getattr(new_grid, array_name).__class__
453+
pgm_array = pgm_array_class(array_data)
454+
setattr(new_grid, array_name, pgm_array)
455+
456+
new_grid._id_counter = new_grid.max_id
457+
458+
if load_graphs:
459+
new_grid.graphs = GraphContainer.from_arrays(new_grid)
460+
return new_grid
461+
443462

444463
def _add_branch_array(branch: BranchArray | Branch3Array, grid: Grid):
445464
"""Add a branch array to the grid"""

tests/unit/model/grids/test_grid_base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,10 @@ def test_from_txt_file(self, tmp_path: Path):
348348
assert 1 == grid.branches.filter(to_status=0).size
349349
assert 1 == grid.transformer.size
350350
np.testing.assert_array_equal([14, 10, 11, 12, 13, 15, 16, 17], grid.branches.id)
351+
352+
353+
def test_from_pgm_input_data(input_data_pgm):
354+
grid = Grid.from_pgm_data(input_data_pgm)
355+
assert grid.node.size == 3
356+
assert grid.line.size == 2
357+
grid.check_ids()

0 commit comments

Comments
 (0)