Exemple #1
0
def attribute_contour_strength(tree, edge_weights, vertex_perimeter=None, edge_length=None, leaf_graph=None):
    """
    Strength of the contour of each node of the given tree. The strength of the contour of a node is defined as the
    mean edge weights on the contour.

    :param tree: input tree (Concept :class:`~higra.CptHierarchy`)
    :param edge_weights: edge_weights of the leaf graph
    :param vertex_perimeter: perimeter of each vertex of the leaf graph (provided by :func:`~higra.attribute_vertex_perimeter` on `leaf_graph`)
    :param edge_length: length of each edge of the leaf graph (provided by :func:`~higra.attribute_edge_length` on `leaf_graph`)
    :param leaf_graph: (deduced from :class:`~higra.CptHierarchy`)
    :return: a 1d array
    """

    if vertex_perimeter is None:
        vertex_perimeter = hg.attribute_vertex_perimeter(leaf_graph)

    if edge_length is None:
        edge_length = hg.attribute_edge_length(leaf_graph)

    perimeter = attribute_contour_length(tree, vertex_perimeter, edge_length, leaf_graph)

    # perimeter of the root may be null
    if np.isclose(perimeter[-1], 0):
        perimeter[-1] = 1

    if hg.CptRegionAdjacencyGraph.validate(leaf_graph):
        edge_weights = hg.rag_accumulate_on_edges(leaf_graph, hg.Accumulators.sum, edge_weights)

    vertex_weights_sum = hg.accumulate_graph_edges(leaf_graph, edge_weights, hg.Accumulators.sum)
    edge_weights_sum = attribute_contour_length(tree, vertex_weights_sum, edge_weights, leaf_graph)

    return edge_weights_sum / perimeter
Exemple #2
0
def attribute_frontier_strength(tree, edge_weights, leaf_graph):
    """
    Mean edge weight along the frontier represented by each node the given partition tree.

    In a partition tree, each node represent the merging of 2 or more regions.
    The frontier of a node is then defined as the common contour between the merged regions.
    This function compute the strength of a common contour as the sum of the weights of edges going from one of the
    merged region to the other one divided by the length of the contour.

    The result has the same dtype as the edge_weights array.

    :param tree: input tree
    :param edge_weights: weight of the edges of the leaf graph (if leaf_graph is a region adjacency graph, edge_weights might be weights on the edges of the pre-graph of the rag).
    :param leaf_graph: graph on the leaves of the input tree (deduced from :class:`~higra.CptHierarchy`)
    :return: a 1d array
    """
    # this is a rag like graph
    if hg.CptRegionAdjacencyGraph.validate(
            leaf_graph) and edge_weights.shape[0] != leaf_graph.num_edges():
        edge_weights = hg.rag_accumulate_on_edges(leaf_graph,
                                                  hg.Accumulators.sum,
                                                  edge_weights=edge_weights)

    frontier_length = hg.attribute_frontier_length(tree, leaf_graph=leaf_graph)
    frontier_strength = hg.attribute_frontier_length(tree, edge_weights,
                                                     leaf_graph)
    frontier_strength[tree.num_leaves():] = frontier_strength[tree.num_leaves(
    ):] / frontier_length[tree.num_leaves():]
    return frontier_strength
Exemple #3
0
    def test_accumulate_edge(self):
        rag = TestRag.get_rag()
        edge_weights = np.ones((24, ))

        rag_edge_weights = hg.rag_accumulate_on_edges(rag, hg.Accumulators.sum,
                                                      edge_weights)

        expected_rag_edge_weights = (2, 2, 1, 2, 1)

        self.assertTrue(
            np.allclose(rag_edge_weights, expected_rag_edge_weights))
Exemple #4
0
def attribute_edge_length(graph):
    """
    Edge length of the given graph.

    In general the length of an edge if simply equal to 1. But, if the graph is a region adjacency graph then the
    length of an edge is equal to the sum of length of the corresponding edges in the original graph (obtained with a
    recursive call to ``attribute_edge_length`` on the original graph).

    :param graph: input graph
    :return: a nd array
    """
    if hg.CptRegionAdjacencyGraph.validate(graph):  # this is a rag like graph
        pre_graph = hg.CptRegionAdjacencyGraph.get_pre_graph(graph)
        pre_graph_edge_length = attribute_edge_length(pre_graph)
        return hg.rag_accumulate_on_edges(graph,
                                          hg.Accumulators.sum,
                                          edge_weights=pre_graph_edge_length)
    res = np.ones((graph.num_edges(), ), dtype=np.float64)
    return res