-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Sketch of datatree.inherit method #9065
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
Closed
TomNicholas
wants to merge
2
commits into
pydata:main
from
TomNicholas:datatree-variable-inheritance
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,7 @@ | |
datatree_repr as datatree_repr_html, | ||
) | ||
from xarray.core.indexes import Index, Indexes | ||
from xarray.core.merge import dataset_update_method | ||
from xarray.core.merge import dataset_update_method, merge_core | ||
from xarray.core.options import OPTIONS as XR_OPTS | ||
from xarray.core.treenode import NamedNode, NodePath, Tree | ||
from xarray.core.utils import ( | ||
|
@@ -967,6 +967,50 @@ def update( | |
inplace=True, children=merged_children, **vars_merge_result._asdict() | ||
) | ||
|
||
@property | ||
def inherit(self) -> DataTree: | ||
""" | ||
Returns a copy of this node additionally containing all coordinates that can be inherited from parent nodes. | ||
|
||
Inspired by the CF conventions' "search by proximity" [1]_. | ||
|
||
References | ||
---------- | ||
.. [1] https://cfconventions.org/Data/cf-conventions/cf-conventions-1.9/cf-conventions.html#_search_by_proximity | ||
""" | ||
|
||
def find_compatible_variables( | ||
candidate_variables: Mapping[str, Variable], | ||
existing_variables: Mapping[str, Variable], | ||
) -> Mapping[str, Variable]: | ||
"""Check variables for compatibility as inherited variables.""" | ||
|
||
# To be compatible, candidate variables must be both new and alignable. | ||
# This should drop candidate variables which are duplicated or not mergeable. | ||
return merge_core( | ||
[existing_variables, candidate_variables], | ||
priority_arg=1, | ||
explicit_coords=list(candidate_variables.keys()) | ||
combine_attrs="override", | ||
) | ||
|
||
# TODO this will call merge for every parent up to the root. Is there an alternative design which only calls merge once? | ||
|
||
# TODO use _MergeResult instead? | ||
|
||
local_variables = self._variables | ||
all_inherited_variables: Mapping[str, Variable] = {} | ||
for parent in self.parents: | ||
# Update inherited_variables mapping at each node when new variables are encountered | ||
inheritable_variables = find_compatible_variables( | ||
parent.coords, local_variables | all_inherited_variables | ||
) | ||
|
||
# TODO should we be keeping track of which variables were coordinates? | ||
all_inherited_variables.update(**inheritable_variables) | ||
|
||
return DataTree.copy().update(all_inherited_variables) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is one possibility - this provides the user with a way to access inherited variables without enforcing that constraint on all trees. |
||
|
||
def assign( | ||
self, items: Mapping[Any, Any] | None = None, **items_kwargs: Any | ||
) -> DataTree: | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should just be
align
like in #9056