Skip to content
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

setting node name breaks tree linkage #309

Open
marcel-goldschen-ohm opened this issue Feb 7, 2024 · 7 comments · May be fixed by #310
Open

setting node name breaks tree linkage #309

marcel-goldschen-ohm opened this issue Feb 7, 2024 · 7 comments · May be fixed by #310
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@marcel-goldschen-ohm
Copy link

# a simple tree
root = DataTree(name='root')
child = DataTree(name='child', parent=root)
grandchild = DataTree(name='grandchild', parent=child)

# changing the name of a child node does not correctly update the dict key in it's parent's children
child.name = 'childish'
print(root)  # this appears to be fine
print(list(root.children))  # however, the keys in root.children have not been updated
print(root['childish'])  # so this fails

Simple fix seems to be wherever the name property is being set it needs to also ensure that the keys in self.parent.children are updated as needed. Not sure if there is anywhere else that is storing these keys that also needs updating.

@TomNicholas
Copy link
Collaborator

Thank you for reporting this! The offending setter is here

@name.setter

This should update the key it is stored under in it's parent.

This should be a pretty simple fix if you (or perhaps @etienneschalk ?) are interested in going in? (If not then no worries)

@TomNicholas TomNicholas added bug Something isn't working good first issue Good for newcomers labels Feb 8, 2024
@etienneschalk
Copy link
Contributor

etienneschalk commented Feb 8, 2024

Hello @TomNicholas

In the context of merging datatree into xarray, should new developments continue to be made on this repo, or in the xarray repo? Or is there a code freeze until datatree can be worked with from inside the xarray repo? Or simply, new developments happening here will be integrated into xarray with some git wizardry?

Edit: the answer is in the README: https://github.com/xarray-contrib/datatree?tab=readme-ov-file#deprecation-notice

@TomNicholas
Copy link
Collaborator

In the context of merging datatree into xarray, should new developments continue to be made on this repo, or in the xarray repo? Or is there a code freeze until datatree can be worked with from inside the xarray repo? Or simply, new developments happening here will be integrated into xarray with some git wizardry?

I think we accept bug fixes here, but not new features. And whilst those bugfixes will be moved to xarray, you won't necessarily get full attribution for them (i.e. I'll probably do it the dumb copy-paste way instead of the git wizardry way).

@TomNicholas
Copy link
Collaborator

But we should fix the bug here! Because people will still be using this repository for a while yet (as this is what is uploaded to pypi/conda as xarray-datatree)

@etienneschalk etienneschalk linked a pull request Feb 8, 2024 that will close this issue
5 tasks
@marcel-goldschen-ohm
Copy link
Author

I'm happy to tackle the fix, but will be traveling for a conference that runs through most of next week, so probably wouldn't get to it until after that. If someone else wants to fix it before then, by all means ;)

@etienneschalk
Copy link
Contributor

What should be the expected behaviour when renaming a child node to None?

I had a look at how xarray behaves when renaming a DataArray inside of a Dataset. It seems that the renaming is just ignored when trying to change the name property of the DataArray directly:

import xarray as xr

https://docs.xarray.dev/en/stable/generated/xarray.DataArray.name.html

xds = xr.Dataset({"a": xr.DataArray([1])})
print(xds)
<xarray.Dataset>
Dimensions:  (dim_0: 1)
Dimensions without coordinates: dim_0
Data variables:
    a        (dim_0) int64 1
print(xds["a"])
<xarray.DataArray 'a' (dim_0: 1)>
array([1])
Dimensions without coordinates: dim_0
xds["a"].name = "toto"
print(xds["a"])
<xarray.DataArray 'a' (dim_0: 1)>
array([1])
Dimensions without coordinates: dim_0
xda = xds["a"]
xda.name = "toto"
print(xda)
<xarray.DataArray 'toto' (dim_0: 1)>
array([1])
Dimensions without coordinates: dim_0
print(xds)
<xarray.Dataset>
Dimensions:  (dim_0: 1)
Dimensions without coordinates: dim_0
Data variables:
    a        (dim_0) int64 1

@marcel-goldschen-ohm
Copy link
Author

@etienneschalk, I find that to be very counterintuitive behavior. My naive expectation would be that the variable should be renamed as desired and the dataset updated to reflect that, and if there was any issue (like renaming to None or to the name of another variable) an exception would be raised. Of course, this is an xarray issue.

marcel-goldschen-ohm added a commit to marcel-goldschen-ohm/datatree that referenced this issue Mar 1, 2024
Fixed a bug (xarray-contrib#309) whereby setting the DataTree.name property broke the tree linkage because it did not update the nodes key in it's parent's children dict.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants