def test_back_project_edge(self): rag = TestRag.get_rag() rag_edge_weights = np.asarray((5, 7, 1, 3, 2)) edge_weights = hg.rag_back_project_edge_weights(rag, rag_edge_weights) iv = 0 expected_edge_weights = np.asarray( (iv, iv, 5, iv, iv, iv, iv, iv, iv, 5, iv, iv, 7, 7, iv, iv, 1, iv, iv, 3, 3, iv, 2, iv)) self.assertTrue(np.allclose(edge_weights, expected_edge_weights))
def __graph_cut(self, tree, leaf_graph, handle_rag=True): """ Graph cut corresponding to the tree cut. The edge (i, j) has a non zero value if the closest ancestors of i and j in the cut are different. :param tree: input tree (Concept :class:`~higra.CptHierarchy`) :param leaf_graph: graph on the tree leaves (deduced from :class:`~higra.CptHierarchy`) :param handle_rag: if `True` and if `leaf_graph` is a region adjacency graph then the cut is given for the original graph (the pre-graph of the region adjacency graph). :return: a graph cut """ sm = self._graph_cut(tree, leaf_graph) if hg.CptRegionAdjacencyGraph.validate(leaf_graph) and handle_rag: sm = hg.rag_back_project_edge_weights(leaf_graph, sm) return sm
def saliency(tree, altitudes, leaf_graph, handle_rag=True): """ The saliency map of the input hierarchy :math:`(tree, altitudes)` for the leaf graph :math:`g` is an array of edge weights :math:`sm` for :math:`g` such that for each pair of adjacent vertices :math:`(i,j)` in :math:`g`, :math:`sm(i,j)` is equal to the ultra-metric distance between :math:`i` and :math:`j` corresponding to the hierarchy. Formally, this is computed using the following property: :math:`sm(i,j) = altitudes(lowest\_common\_ancestor_{tree}(i,j))`. Complexity: :math:`\mathcal{O}(n\log(n) + m)` with :math:`n` the number of vertices in the tree and :math:`m` the number of edges in the graph. :param tree: input tree (Concept :class:`~higra.CptHierarchy`) :param altitudes: altitudes of the vertices of the tree :param leaf_graph: graph whose vertex set is equal to the leaves of the input tree (deduced from :class:`~higra.CptHierarchy`) :param handle_rag: if tree has been constructed on a rag, then saliency values will be propagated to the original graph, hence leading to a saliency on the original graph and not on the rag :return: 1d array of edge weights """ lca_map = hg.attribute_lca_map(tree, leaf_graph=leaf_graph) sm = altitudes[lca_map] if hg.CptRegionAdjacencyGraph.validate(leaf_graph) and handle_rag: sm = hg.rag_back_project_edge_weights(leaf_graph, sm) return sm