Skip to content
This repository was archived by the owner on Oct 24, 2024. It is now read-only.

Is it possible to move a node drectly? #210

Closed
gluincth opened this issue Jan 25, 2023 · 17 comments
Closed

Is it possible to move a node drectly? #210

gluincth opened this issue Jan 25, 2023 · 17 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@gluincth
Copy link

Hi

first thanks for this package!
Question: Is it possible to move a node to another position in the tree directly (possibly in place)?

At the moment I do e.g.

dt['/other_node/mv_this_node/'] = datatree.DataTree(dt['/mv_this_node/'].to_dataset())
newdt = dt.drop_nodes('mv_this_node')

@TomNicholas
Copy link
Member

Hi!

At the moment I have no shortcut for that. We could make one, we would just have to decide what the API for such an operation should look like. e.g.

dt.move('/other_node/mv_this_node/', to='/')

@TomNicholas TomNicholas added the enhancement New feature or request label Jan 27, 2023
@gluincth
Copy link
Author

Hi

thanks for your answer. I would think such a shortcut would be a nice addition. Although, at the moment there probably other things with higher priority. As for the API: I do not know what the most pythonic way to do this. My intuition would say just

dt.move("origin", "target")

i.e without keyword like the console command in a shell, but this is not a strong preference.

@TomNicholas TomNicholas added the good first issue Good for newcomers label Jan 28, 2023
@TomNicholas
Copy link
Member

Actually you can also move a node just by reassigning the parent, e.g.

dt['/other_node/mv_this_node/'].parent = dt['/']

i.e without keyword like the console command in a shell

If we made the node to move a positional arg, and to a keyword arg then both syntaxes would be valid. But without 'to' would also be fine!

@gluincth would you be interested in submitting a PR to add this feature?

@gluincth
Copy link
Author

@TomNicholas

I'd like to help out. However, my time is very limited and I am not sure if my python-fu is up to it. I will put it on my to-do list but cannot make any promises to be able to do this soon. I have not yet looked at the code base, so where would be a good starting point to add such a short cut?

@Emeka-Onwuepe
Copy link

@TomNicholas I will love to work on this as an Outreachy intern.

@TomNicholas
Copy link
Member

Hi @Emeka-Onwuepe ! Thank you for your interest. You would need to add an additional method (called move) to the DataTree class within the datatree/datatree.py file.

@nashira26
Copy link

I am an Outreachy intern and I would love to contribute to this.

@KennethSeruuma
Copy link

Hello members, i am an Outreachy participant. In response to @gluincth's question, I think a direct way to change a node's position in a tree would be to edit its edges, and make it have new neighbors that correspond to its intended new position. Otherwise, i find that @TomNicholas' idea of creating a "move" method in the DataTree class is most appropriate.

@TomNicholas
Copy link
Member

Hi @nashira26 and @KennethSeruuma, thanks also for your interest. Pull requests are welcome!

a direct way to change a node's position in a tree would be to edit its edges

@KennethSeruuma a node's edges are simply what is stored in its .parent and .children properties, so I think your suggestion is equivalent to my original suggestion to just re-assign the .parent property.

@utkarsh-chaurasia
Copy link

utkarsh-chaurasia commented Mar 12, 2023

Hello, I am an Outreachy participant and I would like to contribute to this @gluincth's question @TomNicholas .
Yes, it is possible to move a node to another position in the tree directly, without having to create a new node and deleting the old one.

One way to achieve this is by using the move_node() method provided by the data tree library you are using. This method allows you to move a node to a new parent node and also to a new position among its siblings.

For example, if you are using the datatree library, you can move a node mv_this_node to a new position /other_node/mv_this_node/ in the tree directly as follows:

Code: dt.move_node('/mv_this_node/', '/other_node/mv_this_node/')

This will move the node mv_this_node to a new position /other_node/mv_this_node/ in the tree directly, without creating a new node and deleting the old one.

Note that the move_node() method may behave differently depending on the library you are using, so it is important to check the documentation of your specific library for more details on how to move nodes in the tree.

@GlenMue
Copy link

GlenMue commented Mar 13, 2023

heyy!! everyone im going to solve this problem

@KennethSeruuma
Copy link

Thats great @utkarsh-chaurasia and @TomNicholas . Hey guys, i would like to request anyone with access to good learning material for datatrees to help us access this material. It would aid these contributions alot.

@KennethSeruuma
Copy link

Actually, i have read some nice info on datatree in the README.md file. Follow the links in the README.md file to reach even more detailed information about datatree.

@KennethSeruuma
Copy link

@TomNicholas i would like to inquire whether there are any community specific questions for us (interns) to fill in our application forms.

@TomNicholas
Copy link
Member

Hi @KennethSeruuma - no community-specific questions at the moment!

@KennethSeruuma
Copy link

Alright. Thanks

@TomNicholas
Copy link
Member

Closing this here, with a solution potentially implemented upstream in pydata/xarray#9442

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants