datatree.map_over_subtree#

datatree.map_over_subtree(func: Callable) Callable[source][source]#

Decorator which turns a function which acts on (and returns) Datasets into one which acts on and returns DataTrees.

Applies a function to every dataset in one or more subtrees, returning new trees which store the results.

The function will be applied to any data-containing dataset stored in any of the nodes in the trees. The returned trees will have the same structure as the supplied trees.

func needs to return one Datasets, DataArrays, or None in order to be able to rebuild the subtrees after mapping, as each result will be assigned to its respective node of a new tree via DataTree.__setitem__. Any returned value that is one of these types will be stacked into a separate tree before returning all of them.

The trees passed to the resulting function must all be isomorphic to one another. Their nodes need not be named similarly, but all the output trees will have nodes named in the same way as the first tree passed.

Parameters
  • func (callable()) – Function to apply to datasets with signature:

    func(*args, **kwargs) -> Union[Dataset, Iterable[Dataset]].

    (i.e. func must accept at least one Dataset and return at least one Dataset.) Function will not be applied to any nodes without datasets.

  • *args (tuple, optional) – Positional arguments passed on to func. If DataTrees any data-containing nodes will be converted to Datasets via .ds .

  • **kwargs (Any) – Keyword arguments passed on to func. If DataTrees any data-containing nodes will be converted to Datasets via .ds .

Returns

mapped (callable()) – Wrapped function which returns one or more tree(s) created from results of applying func to the dataset at each node.

See also

DataTree.map_over_subtree, DataTree.map_over_subtree_inplace, DataTree.subtree