Exemplo n.º 1
0
def accumulate_on_contours(tree, node_weights, accumulator, leaf_graph):
    """
    For each edge of the leaf graph, accumulates the weights of the nodes whose contour pass by this edge.

    For any edge :math:`\{x,y\}`, let :math:`R_{\{x,y\}}` be the set of regions of the input tree :math:`T`
    having :math:`\{x,y\}` in its contour:

    .. math::

        R_{\{x,y\}} = \{n \in T \, |\, |\{x,y\} \cap n| = 1  \}

    The output value for the edge :math:`\{x,y\}` is then the accumulated weights of the nodes
    in :math:`R_{\{x,y\}}`.

    :Runtime complexity:

    This algorithm runs in :math:`\mathcal{O}(n*k)` with :math:`n` the number of edges in the leaf graph and
    :math:`k` the maximal depth of the tree (i.e. the number of edges on the longest downward path between
    the root and a leaf).

    :param tree: input tree (Concept :class:`~higra.CptHierarchy`)
    :param node_weights: weights on the nodes of the tree
    :param accumulator: see :class:`~higra.Accumulators`
    :param leaf_graph: graph of the tree leaves (deduced from :class:`~higra.CptHierarchy`)
    :return: returns leaf graph edge weights
    """

    depth = hg.attribute_depth(tree)

    res = hg.cpp._accumulate_on_contours(leaf_graph, tree, node_weights, depth,
                                         accumulator)
    return res
Exemplo n.º 2
0
 def test_random_binary_partition_tree_perfectly_unbalanced(self):
     size = 32
     tree, altitudes = hg.random_binary_partition_tree(size, 1)
     depth = hg.attribute_depth(tree)
     for i in range(32):
         num_nodes = 1 if i == 0 else 2
         self.assertTrue(np.sum(depth == i) == num_nodes)
Exemplo n.º 3
0
def attribute_regular_altitudes(tree, depth=None):
    """
    Regular altitudes is comprised between 0 and 1 and is inversely proportional to the depth of a node

    :param tree: input tree
    :param depth: depth of the tree node (provided by :func:`~higra.attribute_depth`)
    :return: a nd array
    """

    if depth is None:
        depth = hg.attribute_depth(tree)

    altitudes = 1 - depth / np.max(depth)
    altitudes[:tree.num_leaves()] = 0
    return altitudes
Exemplo n.º 4
0
 def test_depth(self):
     t = hg.Tree((6, 6, 7, 8, 8, 8, 7, 9, 9, 9))
     ref = np.asarray((3, 3, 2, 2, 2, 2, 2, 1, 1, 0))
     res = hg.attribute_depth(t)
     self.assertTrue(np.all(ref == res))