datatree/datatree.py
def map_over_subtree(func):
    """
    Decorator which turns a function which acts on (and returns) single Datasets into one which acts on DataTrees.

    Applies a function to every dataset in this subtree, returning a new tree which stores the results.

    The function will be applied to any dataset stored in this node, as well as any dataset stored in any of the
    descendant nodes. The returned tree will have the same structure as the original subtree.

    func needs to return a Dataset in order to rebuild the subtree.

    Parameters
    ----------
    func : callable
        Function to apply to datasets with signature:
        `func(node.ds, *args, **kwargs) -> Dataset`.

        Function will not be applied to any nodes without datasets.
    *args : tuple, optional
        Positional arguments passed on to `func`.
    **kwargs : Any
        Keyword arguments passed on to `func`.

    Returns
    -------
    mapped : callable
        Wrapped function which returns tree created from results of applying ``func`` to the dataset at each node.

    See also
    --------
    DataTree.map_over_subtree
    DataTree.map_over_subtree_inplace
    """

    @functools.wraps(func)
    def _map_over_subtree(tree, *args, **kwargs):
        """Internal function which maps func over every node in tree, returning a tree of the results."""

        # Recreate and act on root node
        out_tree = DataNode(name=tree.name, data=tree.ds)
        if out_tree.has_data:
            out_tree.ds = func(out_tree.ds, *args, **kwargs)

        # Act on every other node in the tree, and rebuild from results
        for node in tree.descendants:
            # TODO make a proper relative_path method
            relative_path = node.pathstr.replace(tree.pathstr, '')
            result = func(node.ds, *args, **kwargs) if node.has_data else None
            out_tree[relative_path] = result

        return out_tree
    return _map_over_subtree

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

    Applies a function to every dataset in this subtree, returning a new tree which stores the results.

    The function will be applied to any dataset stored in this node, as well as any dataset stored in any of the
    descendant nodes. The returned tree will have the same structure as the original subtree.

    func needs to return a Dataset in order to rebuild the subtree.

    Parameters
    ----------
    func : callable
        Function to apply to datasets with signature:
        `func(node.ds, *args, **kwargs) -> Dataset`.

        Function will not be applied to any nodes without datasets.
    *args : tuple, optional
        Positional arguments passed on to `func`.
    **kwargs : Any
        Keyword arguments passed on to `func`.

    Returns
    -------
    mapped : callable
        Wrapped function which returns tree created from results of applying ``func`` to the dataset at each node.

    See also
    --------
    DataTree.map_over_subtree
    DataTree.map_over_subtree_inplace
    
Decorator which turns a function which acts on (and returns) single Datasets into one which acts on DataTrees.
Applies a function to every dataset in this subtree, returning a new tree which stores the results.

The function will be applied to any dataset stored in this node, as well as any dataset stored in any of the
descendant nodes. The returned tree will have the same structure as the original subtree.

func needs to return a Dataset in order to rebuild the subtree.

Parameters

func : callable
Function to apply to datasets with signature:
`func(node.ds, *args, **kwargs) -> Dataset`.

Function will not be applied to any nodes without datasets.
args : tuple, optional
Positional arguments passed on to `func`.
kwargs : Any
Keyword arguments passed on to `func`.

Returns

mapped : callable
Wrapped function which returns tree created from results of applying ``func`` to the dataset at each node.

See also


Decorator which turns a function which acts on ( and returns ) single Datasets into one which acts on DataTrees . Applies a function to every dataset in this subtree returning a new tree which stores the results . The function will be applied to any dataset stored in this node as well as any dataset stored in any of the descendant nodes . The returned tree will have the same structure as the original subtree . func needs to return a Dataset in order to rebuild the subtree . Parameters func : callable Function to apply to datasets with signature : ` func ( node . ds * args ** kwargs ) - > Dataset ` . Function will not be applied to any nodes without datasets . args : tuple optional Positional arguments passed on to ` func ` . kwargs : Any Keyword arguments passed on to ` func ` . Returns mapped : callable Wrapped function which returns tree created from results of applying ` ` func ` ` to the dataset at each node . See also
def map_over_subtree(func):
    @functools.wraps(func)
    def _map_over_subtree(tree, *args, **kwargs):
        out_tree = DataNode(name=tree.name, data=tree.ds)
        if out_tree.has_data:
            out_tree.ds = func(out_tree.ds, *args, **kwargs)
        for node in tree.descendants:
            relative_path = node.pathstr.replace(tree.pathstr, '')
            result = func(node.ds, *args, **kwargs) if node.has_data else None
            out_tree[relative_path] = result
        return out_tree
    return _map_over_subtree
def map_over_subtree ( func ) : @ functools . wraps ( func ) def _map_over_subtree ( tree , * args , ** kwargs ) : """Internal function which maps func over every node in tree, returning a tree of the results.""" out_tree = DataNode ( name = tree . name , data = tree . ds ) if out_tree . has_data : out_tree . ds = func ( out_tree . ds , * args , ** kwargs ) for node in tree . descendants : relative_path = node . pathstr . replace ( tree . pathstr , '' ) result = func ( node . ds , * args , ** kwargs ) if node . has_data else None out_tree [ relative_path ] = result return out_tree return _map_over_subtree
Decorator which turns a function which acts on (and returns) single Datasets into one which acts on DataTrees.
Decorator which turns a function which acts on ( and returns ) single Datasets into one which acts on DataTrees .
"""
    Decorator which turns a function which acts on (and returns) single Datasets into one which acts on DataTrees.

    Applies a function to every dataset in this subtree, returning a new tree which stores the results.

    The function will be applied to any dataset stored in this node, as well as any dataset stored in any of the
    descendant nodes. The returned tree will have the same structure as the original subtree.

    func needs to return a Dataset in order to rebuild the subtree.

    Parameters
    ----------
    func : callable
        Function to apply to datasets with signature:
        `func(node.ds, *args, **kwargs) -> Dataset`.

        Function will not be applied to any nodes without datasets.
    *args : tuple, optional
        Positional arguments passed on to `func`.
    **kwargs : Any
        Keyword arguments passed on to `func`.

    Returns
    -------
    mapped : callable
        Wrapped function which returns tree created from results of applying ``func`` to the dataset at each node.

    See also
    --------
    DataTree.map_over_subtree
    DataTree.map_over_subtree_inplace
    """ """Internal function which maps func over every node in tree, returning a tree of the results.""" # Recreate and act on root node # Act on every other node in the tree, and rebuild from results # TODO make a proper relative_path method
func