Skip to content

Commit 33ead65

Browse files
authored
map_over_subtree -> map_over_datasets (#9622)
1 parent 017279c commit 33ead65

9 files changed

+54
-54
lines changed

doc/api.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ For manipulating, traversing, navigating, or mapping over the tree structure.
687687
DataTree.relative_to
688688
DataTree.iter_lineage
689689
DataTree.find_common_ancestor
690-
DataTree.map_over_subtree
690+
DataTree.map_over_datasets
691691
DataTree.pipe
692692
DataTree.match
693693
DataTree.filter
@@ -954,7 +954,7 @@ DataTree methods
954954

955955
open_datatree
956956
open_groups
957-
map_over_subtree
957+
map_over_datasets
958958
DataTree.to_dict
959959
DataTree.to_netcdf
960960
DataTree.to_zarr

doc/user-guide/hierarchical-data.rst

+4-4
Original file line numberDiff line numberDiff line change
@@ -549,13 +549,13 @@ See that the same change (fast-forwarding by adding 10 years to the age of each
549549
Mapping Custom Functions Over Trees
550550
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
551551

552-
You can map custom computation over each node in a tree using :py:meth:`xarray.DataTree.map_over_subtree`.
552+
You can map custom computation over each node in a tree using :py:meth:`xarray.DataTree.map_over_datasets`.
553553
You can map any function, so long as it takes :py:class:`xarray.Dataset` objects as one (or more) of the input arguments,
554554
and returns one (or more) xarray datasets.
555555

556556
.. note::
557557

558-
Functions passed to :py:func:`~xarray.DataTree.map_over_subtree` cannot alter nodes in-place.
558+
Functions passed to :py:func:`~xarray.DataTree.map_over_datasets` cannot alter nodes in-place.
559559
Instead they must return new :py:class:`xarray.Dataset` objects.
560560

561561
For example, we can define a function to calculate the Root Mean Square of a timeseries
@@ -569,11 +569,11 @@ Then calculate the RMS value of these signals:
569569

570570
.. ipython:: python
571571
572-
voltages.map_over_subtree(rms)
572+
voltages.map_over_datasets(rms)
573573
574574
.. _multiple trees:
575575

576-
We can also use the :py:meth:`~xarray.map_over_subtree` decorator to promote a function which accepts datasets into one which
576+
We can also use the :py:meth:`~xarray.map_over_datasets` decorator to promote a function which accepts datasets into one which
577577
accepts datatrees.
578578

579579
Operating on Multiple Trees

doc/whats-new.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ New Features
2323
~~~~~~~~~~~~
2424
- ``DataTree`` related functionality is now exposed in the main ``xarray`` public
2525
API. This includes: ``xarray.DataTree``, ``xarray.open_datatree``, ``xarray.open_groups``,
26-
``xarray.map_over_subtree``, ``xarray.register_datatree_accessor`` and
26+
``xarray.map_over_datasets``, ``xarray.register_datatree_accessor`` and
2727
``xarray.testing.assert_isomorphic``.
2828
By `Owen Littlejohns <https://github.com/owenlittlejohns>`_,
2929
`Eni Awowale <https://github.com/eni-awowale>`_,

xarray/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from xarray.core.dataarray import DataArray
3535
from xarray.core.dataset import Dataset
3636
from xarray.core.datatree import DataTree
37-
from xarray.core.datatree_mapping import TreeIsomorphismError, map_over_subtree
37+
from xarray.core.datatree_mapping import TreeIsomorphismError, map_over_datasets
3838
from xarray.core.extensions import (
3939
register_dataarray_accessor,
4040
register_dataset_accessor,
@@ -86,7 +86,7 @@
8686
"load_dataarray",
8787
"load_dataset",
8888
"map_blocks",
89-
"map_over_subtree",
89+
"map_over_datasets",
9090
"merge",
9191
"ones_like",
9292
"open_dataarray",

xarray/core/datatree.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from xarray.core.datatree_mapping import (
2424
TreeIsomorphismError,
2525
check_isomorphic,
26-
map_over_subtree,
26+
map_over_datasets,
2727
)
2828
from xarray.core.formatting import datatree_repr, dims_and_coords_repr
2929
from xarray.core.formatting_html import (
@@ -254,14 +254,14 @@ def _constructor(
254254
def __setitem__(self, key, val) -> None:
255255
raise AttributeError(
256256
"Mutation of the DatasetView is not allowed, please use `.__setitem__` on the wrapping DataTree node, "
257-
"or use `dt.to_dataset()` if you want a mutable dataset. If calling this from within `map_over_subtree`,"
257+
"or use `dt.to_dataset()` if you want a mutable dataset. If calling this from within `map_over_datasets`,"
258258
"use `.copy()` first to get a mutable version of the input dataset."
259259
)
260260

261261
def update(self, other) -> NoReturn:
262262
raise AttributeError(
263263
"Mutation of the DatasetView is not allowed, please use `.update` on the wrapping DataTree node, "
264-
"or use `dt.to_dataset()` if you want a mutable dataset. If calling this from within `map_over_subtree`,"
264+
"or use `dt.to_dataset()` if you want a mutable dataset. If calling this from within `map_over_datasets`,"
265265
"use `.copy()` first to get a mutable version of the input dataset."
266266
)
267267

@@ -1330,7 +1330,7 @@ def filter(self: DataTree, filterfunc: Callable[[DataTree], bool]) -> DataTree:
13301330
--------
13311331
match
13321332
pipe
1333-
map_over_subtree
1333+
map_over_datasets
13341334
"""
13351335
filtered_nodes = {
13361336
node.path: node.dataset for node in self.subtree if filterfunc(node)
@@ -1356,7 +1356,7 @@ def match(self, pattern: str) -> DataTree:
13561356
--------
13571357
filter
13581358
pipe
1359-
map_over_subtree
1359+
map_over_datasets
13601360
13611361
Examples
13621362
--------
@@ -1383,7 +1383,7 @@ def match(self, pattern: str) -> DataTree:
13831383
}
13841384
return DataTree.from_dict(matching_nodes, name=self.root.name)
13851385

1386-
def map_over_subtree(
1386+
def map_over_datasets(
13871387
self,
13881388
func: Callable,
13891389
*args: Iterable[Any],
@@ -1417,7 +1417,7 @@ def map_over_subtree(
14171417
# TODO this signature means that func has no way to know which node it is being called upon - change?
14181418

14191419
# TODO fix this typing error
1420-
return map_over_subtree(func)(self, *args, **kwargs)
1420+
return map_over_datasets(func)(self, *args, **kwargs)
14211421

14221422
def pipe(
14231423
self, func: Callable | tuple[Callable, str], *args: Any, **kwargs: Any

xarray/core/datatree_mapping.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def check_isomorphic(
7272
raise TreeIsomorphismError("DataTree objects are not isomorphic:\n" + diff)
7373

7474

75-
def map_over_subtree(func: Callable) -> Callable:
75+
def map_over_datasets(func: Callable) -> Callable:
7676
"""
7777
Decorator which turns a function which acts on (and returns) Datasets into one which acts on and returns DataTrees.
7878
@@ -112,8 +112,8 @@ def map_over_subtree(func: Callable) -> Callable:
112112
113113
See also
114114
--------
115-
DataTree.map_over_subtree
116-
DataTree.map_over_subtree_inplace
115+
DataTree.map_over_datasets
116+
DataTree.map_over_datasets_inplace
117117
DataTree.subtree
118118
"""
119119

@@ -122,7 +122,7 @@ def map_over_subtree(func: Callable) -> Callable:
122122
# TODO inspect function to work out immediately if the wrong number of arguments were passed for it?
123123

124124
@functools.wraps(func)
125-
def _map_over_subtree(*args, **kwargs) -> DataTree | tuple[DataTree, ...]:
125+
def _map_over_datasets(*args, **kwargs) -> DataTree | tuple[DataTree, ...]:
126126
"""Internal function which maps func over every node in tree, returning a tree of the results."""
127127
from xarray.core.datatree import DataTree
128128

@@ -227,7 +227,7 @@ def _map_over_subtree(*args, **kwargs) -> DataTree | tuple[DataTree, ...]:
227227
else:
228228
return tuple(result_trees)
229229

230-
return _map_over_subtree
230+
return _map_over_datasets
231231

232232

233233
def _handle_errors_with_path_context(path: str):

xarray/core/datatree_ops.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import textwrap
55

66
from xarray.core.dataset import Dataset
7-
from xarray.core.datatree_mapping import map_over_subtree
7+
from xarray.core.datatree_mapping import map_over_datasets
88

99
"""
1010
Module which specifies the subset of xarray.Dataset's API which we wish to copy onto DataTree.
@@ -17,7 +17,7 @@
1717
_MAPPED_DOCSTRING_ADDENDUM = (
1818
"This method was copied from :py:class:`xarray.Dataset`, but has been altered to "
1919
"call the method on the Datasets stored in every node of the subtree. "
20-
"See the `map_over_subtree` function for more details."
20+
"See the `map_over_datasets` function for more details."
2121
)
2222

2323
# TODO equals, broadcast_equals etc.
@@ -174,7 +174,7 @@ def _wrap_then_attach_to_cls(
174174
target_cls_dict, source_cls, methods_to_set, wrap_func=None
175175
):
176176
"""
177-
Attach given methods on a class, and optionally wrap each method first. (i.e. with map_over_subtree).
177+
Attach given methods on a class, and optionally wrap each method first. (i.e. with map_over_datasets).
178178
179179
Result is like having written this in the classes' definition:
180180
```
@@ -206,7 +206,7 @@ def method_name(self, *args, **kwargs):
206206
)
207207
target_cls_dict[method_name] = wrapped_method
208208

209-
if wrap_func is map_over_subtree:
209+
if wrap_func is map_over_datasets:
210210
# Add a paragraph to the method's docstring explaining how it's been mapped
211211
orig_method_docstring = orig_method.__doc__
212212

@@ -277,7 +277,7 @@ class MappedDatasetMethodsMixin:
277277
target_cls_dict=vars(),
278278
source_cls=Dataset,
279279
methods_to_set=_ALL_DATASET_METHODS_TO_MAP,
280-
wrap_func=map_over_subtree,
280+
wrap_func=map_over_datasets,
281281
)
282282

283283

@@ -291,7 +291,7 @@ class MappedDataWithCoords:
291291
target_cls_dict=vars(),
292292
source_cls=Dataset,
293293
methods_to_set=_DATA_WITH_COORDS_METHODS_TO_MAP,
294-
wrap_func=map_over_subtree,
294+
wrap_func=map_over_datasets,
295295
)
296296

297297

@@ -305,5 +305,5 @@ class DataTreeArithmeticMixin:
305305
target_cls_dict=vars(),
306306
source_cls=Dataset,
307307
methods_to_set=_ARITHMETIC_METHODS_TO_MAP,
308-
wrap_func=map_over_subtree,
308+
wrap_func=map_over_datasets,
309309
)

xarray/tests/test_datatree.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1805,7 +1805,7 @@ def test_tree(self, create_test_datatree):
18051805

18061806

18071807
class TestDocInsertion:
1808-
"""Tests map_over_subtree docstring injection."""
1808+
"""Tests map_over_datasets docstring injection."""
18091809

18101810
def test_standard_doc(self):
18111811

@@ -1839,7 +1839,7 @@ def test_standard_doc(self):
18391839
.. note::
18401840
This method was copied from :py:class:`xarray.Dataset`, but has
18411841
been altered to call the method on the Datasets stored in every
1842-
node of the subtree. See the `map_over_subtree` function for more
1842+
node of the subtree. See the `map_over_datasets` function for more
18431843
details.
18441844
18451845
Normally, it should not be necessary to call this method in user code,
@@ -1870,7 +1870,7 @@ def test_one_liner(self):
18701870
18711871
This method was copied from :py:class:`xarray.Dataset`, but has been altered to
18721872
call the method on the Datasets stored in every node of the subtree. See
1873-
the `map_over_subtree` function for more details."""
1873+
the `map_over_datasets` function for more details."""
18741874
)
18751875

18761876
actual_doc = insert_doc_addendum(mixin_doc, _MAPPED_DOCSTRING_ADDENDUM)

0 commit comments

Comments
 (0)