-
Notifications
You must be signed in to change notification settings - Fork 7
feat: support merge of two grids #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+189
−4
Merged
Changes from 15 commits
Commits
Show all changes
51 commits
Select commit
Hold shift + click to select a range
e0fe4f2
test commit
wwadman 8e121f9
fixing too long line
wwadman ae4d72b
after visionary talk with Thijs
wwadman 8b5e693
iets doet t :)
wwadman 402f835
grids seem ok when eyeballing in Graph Editor
wwadman 7e79786
Second test grid ready to be tested
wwadman 78c4788
Another test
wwadman 4b2cf47
Testing removal of duplicate line ids
wwadman 2ddf464
SOme formatting
wwadman 60068b4
poe format
wwadman e7fbf65
Cleaner code
wwadman 7c5a798
Poe all and questions removed
wwadman 7df3173
DCO Remediation Commit for wander.wadman <[email protected]>
wwadman db61e42
add mode argument
wwadman 16e837a
Counter empty columns
wwadman e9d7c36
Merge remote-tracking branch 'origin/main' into feat/merge-grids
wwadman de0241d
Merge branch 'main' into feat/merge-grids
Thijss ffb59ef
Resolving test failing
wwadman d9f5c75
Resolving mypy src tests
wwadman 2e03fe5
Resolving last mypy src test
wwadman 88d86eb
Removing comment
wwadman 1add36c
Moved tests to test_helpers
wwadman b7cfa78
poe all formatting
wwadman ee598d6
License added
wwadman 5af0b51
feat: add check ids
jaapschoutenalliander cab74ed
chore: format
jaapschoutenalliander c86efff
Only deepcopy arrays of the second grid instead of the entire second …
wwadman f2babcb
Make mode a Literal
wwadman 1ccc534
Update src/power_grid_model_ds/_core/model/grids/base.py
wwadman 0995388
Make mode a Literal in merge_grids
wwadman 1804fa1
switch other_grid type to Self
wwadman 6a1c7f0
Update src/power_grid_model_ds/_core/model/grids/_helpers.py
wwadman 4975265
Update tests/unit/model/grids/test_helpers.py
wwadman 8e59c9a
Hardcode nodes in first test
wwadman a0db913
Hardcode second test
wwadman 43aee11
add test with extendedgrid
wwadman 3f74247
add test of incorrect mode
wwadman f8b7ca5
poe cleanup
wwadman fbee2bd
Update tests/unit/model/grids/test_helpers.py
wwadman 36c84fd
Improve branches test
wwadman 83365b8
Hardcode node ids check
wwadman b81c2f0
Forgot remove another assertion around a check_ids() call
wwadman 7d38c6f
add fixtures
wwadman 1a13adb
Renaming extended grid variable name
wwadman e3768c3
poe cleanup
wwadman b4df652
Fix fixtures
wwadman 88231ca
Cleanup
wwadman 987a328
switch from testing array types to grid types
wwadman 78fd69d
switch from testing array types to grid types
wwadman eef7c41
update doc string
wwadman b54506a
poe cleanup
wwadman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,24 @@ | ||
| # SPDX-FileCopyrightText: Contributors to the Power Grid Model project <[email protected]> | ||
| # | ||
| # SPDX-License-Identifier: MPL-2.0 | ||
| import copy | ||
| import dataclasses | ||
| import logging | ||
| from typing import TYPE_CHECKING, Type, TypeVar | ||
|
|
||
| from power_grid_model_ds._core.model.arrays import ( | ||
| AsymVoltageSensorArray, | ||
| Branch3Array, | ||
| BranchArray, | ||
| IdArray, | ||
| NodeArray, | ||
| SourceArray, | ||
| SymGenArray, | ||
| SymLoadArray, | ||
| SymPowerSensorArray, | ||
| SymVoltageSensorArray, | ||
| TransformerTapRegulatorArray, | ||
| ) | ||
| from power_grid_model_ds._core.model.arrays.base.array import FancyArray | ||
| from power_grid_model_ds._core.model.graphs.container import GraphContainer | ||
|
|
||
|
|
@@ -14,8 +28,8 @@ | |
| if TYPE_CHECKING: | ||
| from .base import Grid | ||
|
|
||
| G = TypeVar("G", bound="Grid") | ||
|
|
||
| G = TypeVar("G", bound="Grid") | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
@@ -42,3 +56,54 @@ def create_empty_grid(grid_class: Type[G], graph_model: type[BaseGraphModel] = R | |
| empty_fields = grid_class._get_empty_fields() # noqa # pylint: disable=protected-access | ||
| empty_fields["graphs"] = GraphContainer.empty(graph_model=graph_model) | ||
| return grid_class(**empty_fields) | ||
|
|
||
|
|
||
| def merge_grids(grid: G, other_grid: G, mode: str) -> G: | ||
| """See Grid.merge()""" | ||
|
|
||
| match mode: | ||
| case "recalculate_ids": | ||
| other_grid = copy.deepcopy(other_grid) | ||
| offset = grid.id_counter # Possible improvement: grid.id_counter - other_grid.min_id() + 1 | ||
| _increment_grid_ids_by_offset(other_grid, offset) | ||
| case "keep_ids": | ||
| pass | ||
| case _: | ||
| raise NotImplementedError(f"Merge mode {mode} is not implemented") | ||
|
|
||
| # Append all arrays from the first grid to the second | ||
| for array in other_grid.all_arrays(): | ||
| grid.append(array, check_max_id=False) | ||
|
|
||
| return grid | ||
|
|
||
|
|
||
| def _increment_grid_ids_by_offset(grid: G, offset: int) -> None: | ||
| for array in grid.all_arrays(): | ||
| if isinstance(array, IdArray): | ||
| _update_id_column(array, "id", offset) | ||
|
|
||
| columns = [] | ||
| match array: | ||
| case SymPowerSensorArray() | SymVoltageSensorArray() | AsymVoltageSensorArray(): | ||
| columns = [] | ||
| case NodeArray(): | ||
| columns = ["feeder_node_id", "feeder_branch_id"] | ||
| case TransformerTapRegulatorArray(): | ||
| columns = ["regulated_object"] | ||
| case BranchArray(): | ||
| columns = ["from_node", "to_node", "feeder_node_id", "feeder_branch_id"] | ||
| case Branch3Array(): | ||
| columns = ["node_1", "node_2", "node_3"] | ||
| case SymGenArray() | SymLoadArray() | SourceArray(): | ||
| columns = ["node"] | ||
| case _: | ||
| raise NotImplementedError(f"The array of type {type(array)} is not implemented for appending") | ||
wwadman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| for column in columns: | ||
| _update_id_column(array, column, offset) | ||
|
|
||
|
|
||
| def _update_id_column(array: IdArray, column: str, offset: int) -> None: | ||
| mask = array.is_empty(column) | ||
| array[column][~mask] += offset | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.