-
Notifications
You must be signed in to change notification settings - Fork 7
Add Grid.__eq__ #123
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
Merged
Add Grid.__eq__ #123
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
4c0d47c
Add is equal methods
Thijss 08c87be
Move logic to parent class
Thijss e54c04d
Add tests and piggyback move some existing tests
Thijss 7d55785
Add reuse compliance
Thijss f407842
Revert changes
Thijss 5fae063
Revert changes
Thijss 64e2690
Fix logger
Thijss 8059ec9
Remove redundant set() calls
Thijss 9d8af65
Reduce cognitive complexity
Thijss cb6715e
cleanup
Thijss cafd113
remove Grid.is_equal
Thijss 1b673a7
Add reuse compliance
Thijss 6ea6821
revert order
Thijss 85a96e3
cleanup
Thijss a140758
Merge remote-tracking branch 'origin/main' into feat/add-is-equal
Thijss 7b5606f
Refactor: Move tests so that code folder structure is matched
Thijss 6d666e4
cleanup
Thijss 6eec020
remove module that is not part of this PR
Thijss 06d4068
Merge branch 'refactor-tests' into feat/add-is-equal
Thijss 9e9ec06
remove duplicate test modules
Thijss 270eb4c
Merge branch 'refactor-tests' into feat/add-is-equal
Thijss ab5cc13
fix import
Thijss 0dfd30b
Merge branch 'refactor-tests' into feat/add-is-equal
Thijss d5f4568
revert to main
Thijss 52f37b5
merge main
Thijss c569c8f
Apply suggestion from @Thijss
Thijss 4ba6ad0
rename parameter
Thijss 166b960
docs: add docs on equals method
jaapschoutenalliander 23ae7ce
Merge branch 'feat/add-is-equal' of https://github.com/PowerGridModel…
jaapschoutenalliander 0d04de6
rename parameter
Thijss 3308643
Merge branch 'feat/add-is-equal' of https://github.com/PowerGridModel…
Thijss 51dc096
provide updated parameter
Thijss 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
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
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
102 changes: 102 additions & 0 deletions
102
src/power_grid_model_ds/_core/model/containers/helpers.py
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 |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| # SPDX-FileCopyrightText: Contributors to the Power Grid Model project <[email protected]> | ||
| # | ||
| # SPDX-License-Identifier: MPL-2.0 | ||
| import logging | ||
| from dataclasses import Field, fields | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from power_grid_model_ds._core.model.arrays.base.array import FancyArray | ||
| from power_grid_model_ds._core.utils.misc import array_equal_with_nan | ||
|
|
||
| if TYPE_CHECKING: | ||
| from power_grid_model_ds._core.model.grids.base import FancyArrayContainer | ||
|
|
||
| logger = logging.getLogger("power_grid_model_ds.fancypy") # public location of the container_equal function | ||
|
|
||
|
|
||
| def container_equal( | ||
| container_a: "FancyArrayContainer", | ||
| container_b: "FancyArrayContainer", | ||
| ignore_extras: bool = False, | ||
| early_exit: bool = True, | ||
| fields_to_ignore: list[str] = None, | ||
| ) -> bool: | ||
| """ | ||
| Compares two containers for equality. | ||
|
|
||
| Args: | ||
| container_a: The first container to compare. | ||
| container_b: The second container to compare. | ||
| ignore_extras: | ||
| If True, | ||
| ignores fields present in one container_a but not in container_b. | ||
| ignores extra columns in arrays in container_b that are not present in container_a. | ||
| early_exit: If True, returns False on the first detected difference. False to log all differences as debug. | ||
| fields_to_ignore: A list of field names to exclude from comparison. | ||
|
|
||
| Returns: | ||
| True if the containers are equal, False otherwise. | ||
| """ | ||
| fields_to_ignore = fields_to_ignore or [] | ||
| is_equal = True | ||
|
|
||
| for field in fields(container_a): | ||
| if field.name in fields_to_ignore or (ignore_extras and not hasattr(container_b, field.name)): | ||
| continue | ||
|
|
||
| if not _fields_are_equal(container_a, container_b, field, ignore_extras): | ||
| is_equal = False | ||
| if early_exit: | ||
| return False | ||
|
|
||
| if not ignore_extras and _check_for_extra_fields(container_a, container_b): | ||
| return False | ||
|
|
||
| return is_equal | ||
|
|
||
|
|
||
| def _fields_are_equal( | ||
| container_a: "FancyArrayContainer", container_b: "FancyArrayContainer", field: "Field", ignore_extras: bool | ||
| ) -> bool: | ||
| """Compares a single field between two containers.""" | ||
| value_a = getattr(container_a, field.name) | ||
| value_b = getattr(container_b, field.name) | ||
| class_name = container_a.__class__.__name__ | ||
|
|
||
| if isinstance(value_a, FancyArray): | ||
| if not _check_array_equal(value_a, value_b, ignore_extras): | ||
| logger.debug(f"Array field '{field.name}' differs between {class_name}s.") | ||
| return False | ||
| elif value_a != value_b: | ||
| logger.debug(f"Field '{field.name}' differs between {class_name}s.") | ||
| return False | ||
|
|
||
| return True | ||
|
|
||
|
|
||
| def _check_for_extra_fields(container_a: "FancyArrayContainer", container_b: "FancyArrayContainer") -> bool: | ||
| """Checks if container_b has extra fields not present in container_a.""" | ||
| fields_a = {f.name for f in fields(container_a)} | ||
| fields_b = {f.name for f in fields(container_b)} | ||
| extra_fields = fields_b - fields_a | ||
|
|
||
| if extra_fields: | ||
| logger.debug(f"Container {container_b.__class__.__name__} has extra fields: {extra_fields}") | ||
| return True | ||
| return False | ||
|
|
||
|
|
||
| def _check_array_equal(array_a: FancyArray, array_b: FancyArray, ignore_extras: bool) -> bool: | ||
| """ | ||
| Compares two FancyArrays, optionally ignoring extra columns in array_b. | ||
| NaN values are treated as equal. | ||
| """ | ||
| data_a = array_a.data | ||
| data_b = array_b.data | ||
|
|
||
| if ignore_extras: | ||
| common_columns = [col for col in array_a.columns if col in array_b.columns] | ||
| data_a = array_a[common_columns] | ||
| data_b = array_b[common_columns] | ||
|
|
||
| return array_equal_with_nan(data_a, data_b) | ||
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
Oops, something went wrong.
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.