Closed melonora closed 2 weeks ago
@melonora map_over_subtree
was removed from API in the process of moving datatree into xarray codebase.
Please use map_over_datasets
with one of the workarounds as suggested in #9693 for the time being.
additionally, ds.transpose(("x", "y", "z"))
will not work unless you have a dimension named ("x", "y", "z")
(i.e. the dimension name is a tuple
), since Dataset.transpose
takes the dimension names as *args
).
Given that map_over_subtree
intentionally does not exist anymore, I think this is a duplicate of #9693.
Edit: or rather, where in the documentation did you find DataTree.map_over_subtree
? If that really still exists I'd call that a documentation bug.
@melonora @keewis There is no mention of map_over_subtree
in the latest stable docs. So maybe the used doc was outdated?
@melonora To get you working until #9693 is sorted out, here is a workaround (please also take @keewis comment on transpose arguments into account):
import functools
def skip_nodes(func):
@functools.wraps(func)
def _func(ds, *args, **kwargs):
# check if needed dimensions are available in the Dataset
# otherwise return verbatim
if not all(arg in ds.dims for arg in args):
return ds
return func(ds, *args, **kwargs)
return _func
@skip_nodes
def transpose(ds, *args, **kwargs):
return ds.transpose(*args, **kwargs)
multiscale_image = multiscale_image.map_over_datasets(transpose, 'y', 'x', 'c')
I'll close this as dupe of #9693.
additionally,
ds.transpose(("x", "y", "z"))
will not work unless you have a dimension named("x", "y", "z")
(i.e. the dimension name is atuple
), sinceDataset.transpose
takes the dimension names as*args
).Given that
map_over_subtree
intentionally does not exist anymore, I think this is a duplicate of #9693.Edit: or rather, where in the documentation did you find
DataTree.map_over_subtree
? If that really still exists I'd call that a documentation bug.
ah sorry was looking at the xarray_datatree documentation
@melonora @keewis There is no mention of
map_over_subtree
in the latest stable docs. So maybe the used doc was outdated?@melonora To get you working until #9693 is sorted out, here is a workaround (please also take @keewis comment on transpose arguments into account):
import functools def skip_nodes(func): @functools.wraps(func) def _func(ds, *args, **kwargs): # check if needed dimensions are available in the Dataset # otherwise return verbatim if not all(arg in ds.dims for arg in args): return ds return func(ds, *args, **kwargs) return _func @skip_nodes def transpose(ds, *args, **kwargs): return ds.transpose(*args, **kwargs) multiscale_image = multiscale_image.map_over_datasets(transpose, 'y', 'x', 'c')
Thanks! I had a similar workaround for now
Hello,
I am currently migrating to 2024.10.0
. I encountered some code making use of the former map_over_subtree decorator.
What is the suggested migration process to migrate such code to the map_over_subsets
one? Is the decorator aspect of it definitely gone?
Thanks for your answer
What is the suggested migration process to migrate such code to the map_over_subsets one?
Sorry apparently I forgot to add this to the migration guide (I've added it in #9804).
Is the decorator aspect of it definitely gone?
Yes, we decided that it was better to have it be consistent with xr.apply_ufunc
. If you want decorator-like behaviour you could use functools.partial
or just wrap the .map_over_datasets
call in a new function.
What happened?
Looking for a way to map a function over
Datasets
in aDataTree
I was hit by the issue described in #9693. This because of the node with path.
not containing the dimensions I was trying to transpose.Trying to find a workaround with
map_over_subtree
did not work either as seemingly in the latest xarray (2024.10.0) this does not exist. I get anAttributeError
, while according to the documentation the method does exist.What did you expect to happen?
I expect as output a datatree in which the datasets have their dimensions transposed.
Minimal Complete Verifiable Example
MVCE confirmation
Relevant log output
Anything else we need to know?
No response
Environment