-
Notifications
You must be signed in to change notification settings - Fork 42
Make DatasetView in map_over_subtree aware of its path? #266
Comments
This is an interesting idea, and would actually be fairly easy to do - I would just have to change the type passed to the user function to be a |
One reason not to implement this feature is that it might encourage people to write dataset-processing functions that only work when mapped over a tree, and not over normal |
To be clear, an alternative to your suggestion @observingClouds would be requiring the user to pass a function with a different signature to def my_func(path, ds):
... rather than def my_func(ds):
path = ds.path
... |
Thanks for your additional thoughts @TomNicholas! Would it maybe be best to integrate the path into the |
Yes perhaps... It would help prevent anti-patterns to have |
Well, how would you implement: def my_func(ds, path):
... One way to do it could be to add a keyword argument to the Not sure if _handle_errors_with_path_context is the best place to ingest the path information but that could be a possibility: def _handle_errors_with_path_context(path, include_path=False):
"""Wraps given function so that if it fails it also raises path to node on which it failed."""
def decorator(func):
def wrapper(*args, **kwargs):
+ if include_path: kwargs['path'] = path
try:
return func(*args, **kwargs)
except Exception as e: There are probably nicer places to do this though. |
I was going to say that I would simply change But actually there is a better reason not to do it like that, which is that dt1 = DataTree.from_dict({'model1/highres': xr.Dataset({'temp': ...})})
dt2 = DataTree.from_dict({'model2/highres': xr.Dataset({'temp': ...})})
dt1 * dt2 # will happily multiply `model1/temp` by `model2/temp`, despite different paths We don't want a signature like def my_func(path1, ds1, path2, ds2, ...):
... The function you point to above ( |
On reflection it's not overkill - imagine the function I was mapping was |
Closing in favour of pydata/xarray#9346 |
Raised by @observingClouds in #254 (comment)
Currently, I use the @map_over_subtree decorator, which also has some limitations as the function does not know its tree origin (as noted in the code) and it needs to be inferred from the dataset itself, which is sometimes possible (here the length of the dataset) but does not need to be always the case.
I do not know how the tree information could be passed through the decorator, but maybe it is okay if the
DatasetView
class has an additional property (e.g._path
) that could be filled withdt.path
during the call of DatasetView._from_node()?. This would lead toand would allow for tree-aware manipulation of the datasets.
The text was updated successfully, but these errors were encountered: