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

allow kwargs in map_over_datasets? #10009

Open
mathause opened this issue Jan 30, 2025 · 3 comments
Open

allow kwargs in map_over_datasets? #10009

mathause opened this issue Jan 30, 2025 · 3 comments
Labels
enhancement topic-DataTree Related to the implementation of a DataTree class

Comments

@mathause
Copy link
Collaborator

mathause commented Jan 30, 2025

Is your feature request related to a problem?

datatree.map_over_subtree allowed passing kwargs but xr.map_over_datasets does not. I think it probably should?

import numpy as np
import xarray as xr
import datatree

example_dict = {"set1": xr.Dataset({"var1": xr.DataArray([1, 2, 3], dims = "time")})}
dt = datatree.DataTree.from_dict(example_dict)
datatree.map_over_subtree(np.var)(dt, ddof=0) # works
import numpy as np
import xarray as xr

example_dict = {"set1": xr.Dataset({"var1": xr.DataArray([1, 2, 3], dims = "time")})}
dt = xr.DataTree.from_dict(example_dict)
xr.map_over_datasets(np.std, dt, ddof=1) # fails

Describe the solution you'd like

No response

Describe alternatives you've considered

Of course partial could be used or other such constructs but that's not so convenient...

Additional context

No response

@mathause mathause added enhancement topic-DataTree Related to the implementation of a DataTree class labels Jan 30, 2025
@mathause
Copy link
Collaborator Author

Partially (🤷‍) discussed in #9693 (comment)

@TomNicholas
Copy link
Member

I think adding **kwargs would be fine. As @shoyer said in that linked comment it would follow the pattern of xr.apply_ufunc.

The rabbit hole to avoid IMO is passing different kwargs to different datasets.

Do you want to submit a PR for this?

@shoyer
Copy link
Member

shoyer commented Jan 30, 2025

There is an ambiguity between keyword arguments to xarray.map_over_datasets and the wrapped function.

For this reason, my preference would be to add keyword argument support into an explicit argument, e.g., xr.map_over_datasets(np.std, dt, kwargs=dict(ddof=1)), similar to xarray.apply_ufunc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement topic-DataTree Related to the implementation of a DataTree class
Projects
None yet
Development

No branches or pull requests

3 participants