예제 #1
0
    def test_delinearize_vertex_weights(self):
        g = hg.get_4_adjacency_graph((4, 5))

        r = hg.delinearize_vertex_weights(np.ones((20, )), g, (4, 5))
        self.assertTrue(r.shape == (4, 5))

        r = hg.delinearize_vertex_weights(np.ones((4, 5)), g, (4, 5))
        self.assertTrue(r.shape == (4, 5))

        r = hg.delinearize_vertex_weights(np.ones((4, 5, 10, 12)), g, (4, 5))
        self.assertTrue(r.shape == (4, 5, 10, 12))

        r = hg.delinearize_vertex_weights(np.ones((20, 4, 5, 2, 3)), g, (4, 5))
        self.assertTrue(r.shape == (4, 5, 4, 5, 2, 3))

        with self.assertRaises(ValueError):
            hg.delinearize_vertex_weights(np.ones((5, 4)), g, (4, 5))

        with self.assertRaises(ValueError):
            hg.delinearize_vertex_weights(np.ones((25, )), g, (4, 5))

        shape = (1, 4)
        g = hg.get_4_adjacency_graph(shape)
        r = hg.delinearize_vertex_weights(np.ones((4, )), g)
        self.assertTrue(r.shape == shape)

        r = hg.delinearize_vertex_weights(np.ones(shape), g)
        self.assertTrue(r.shape == shape)

        r = hg.delinearize_vertex_weights(np.ones((1, 4, 10, 12)), g)
        self.assertTrue(r.shape == (1, 4, 10, 12))

        r = hg.delinearize_vertex_weights(np.ones((4, 5, 2, 3)), g)
        self.assertTrue(r.shape == (1, 4, 5, 2, 3))

        with self.assertRaises(ValueError):
            hg.delinearize_vertex_weights(np.ones((5, 4)), g)

        with self.assertRaises(ValueError):
            hg.delinearize_vertex_weights(np.ones((8, )), g)

        shape = (4, 1)
        g = hg.get_4_adjacency_graph(shape)
        r = hg.delinearize_vertex_weights(np.ones((4, )), g)
        self.assertTrue(r.shape == shape)

        r = hg.delinearize_vertex_weights(np.ones(shape), g)
        self.assertTrue(r.shape == shape)

        r = hg.delinearize_vertex_weights(np.ones((4, 1, 10, 12)), g)
        self.assertTrue(r.shape == (4, 1, 10, 12))

        r = hg.delinearize_vertex_weights(np.ones((4, 5, 2, 3)), g)
        self.assertTrue(r.shape == (4, 1, 5, 2, 3))

        with self.assertRaises(ValueError):
            hg.delinearize_vertex_weights(np.ones((5, 4)), g)

        with self.assertRaises(ValueError):
            hg.delinearize_vertex_weights(np.ones((8, )), g)
예제 #2
0
    def test_assess_fragmentation_curve_BCE_optimal_cut_on_rag(self):
        vertex_map = np.asarray((0, 0, 1, 1, 2, 2, 3, 4), dtype=np.int64)
        g = hg.get_4_adjacency_graph((1, 8))
        hg.CptRegionAdjacencyGraph.link(g, None, vertex_map, np.ones((7, )))
        t = hg.Tree((6, 6, 5, 5, 7, 7, 8, 8, 8))
        hg.CptHierarchy.link(t, g)
        ground_truth = np.asarray((0, 0, 1, 1, 1, 2, 2, 2), dtype=np.int32)

        assesser = hg.make_assesser_fragmentation_optimal_cut(
            t, ground_truth, hg.OptimalCutMeasure.BCE)

        self.assertTrue(assesser.optimal_number_of_regions() == 3)
        self.assertTrue(
            np.isclose(assesser.optimal_score(),
                       (2 + 4.0 / 3 + 2.5) / t.num_leaves()))

        res = assesser.fragmentation_curve()
        res_k = res.num_regions()
        res_scores = res.scores()

        ref_scores = np.asarray((2.75, 4.5, 2 + 4.0 / 3 + 2.5, 2 + 4.0 / 3 + 2,
                                 2 + 4.0 / 3 + 4.0 / 3))
        ref_k = np.asarray((1, 2, 3, 4, 5), dtype=np.int32)

        self.assertTrue(np.allclose(res_scores, (ref_scores / t.num_leaves())))
        self.assertTrue(np.all(res_k == ref_k))
예제 #3
0
    def test_graph_4_adjacency_2_khalimsky(self):
        g = hg.get_4_adjacency_graph((2, 3))
        data = np.asarray((1, 0, 2, 1, 1, 1, 2))

        ref = np.asarray(((0, 1, 0, 2, 0), (0, 1, 1, 2, 1), (0, 1, 0, 2, 0)))
        r = hg.graph_4_adjacency_2_khalimsky(g, data)
        self.assertTrue(np.allclose(ref, r))
예제 #4
0
    def test_hierarchy_alignement(self):
        g = hg.get_4_adjacency_graph((3, 3))
        fine_labels = np.asarray((0, 1, 2, 3, 4, 2, 3, 4, 2), dtype=np.int32)

        aligner = hg.HierarchyAligner.from_labelisation(g, fine_labels)

        t = hg.Tree(np.asarray((9, 10, 10, 9, 11, 11, 9, 11, 11, 13, 12, 12, 13, 13), dtype=np.int64))
        altitudes = np.asarray((0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2), dtype=np.int32)

        sm = aligner.align_hierarchy(t, altitudes)
        sm_k = hg.graph_4_adjacency_2_khalimsky(g, sm, (3, 3))

        sm_coarse = np.asarray((2, 0, 0, 1, 1, 2, 0, 0, 0, 0, 2, 0), dtype=np.int32)

        sm2 = aligner.align_hierarchy(g, sm_coarse)
        sm2_k = hg.graph_4_adjacency_2_khalimsky(g, sm2, (3, 3))

        sm_k_ref = np.asarray(((0, 2, 0, 1, 0),
                               (0, 2, 1, 1, 0),
                               (0, 2, 0, 0, 0),
                               (0, 2, 0, 0, 0),
                               (0, 2, 0, 0, 0)), dtype=np.int32)

        self.assertTrue(np.all(sm_k == sm_k_ref))
        self.assertTrue(np.all(sm2_k == sm_k_ref))
예제 #5
0
 def test_vertex_perimeter2(self):
     g = hg.get_4_adjacency_graph((2, 3))
     hg.set_attribute(g, "no_border_vertex_out_degree", None)
     ref = np.asarray(((2, 3, 2),
                       (2, 3, 2)))
     res = hg.attribute_vertex_perimeter(g)
     self.assertTrue(np.allclose(ref, res))
예제 #6
0
    def test_weighting_graph(self):
        g = hg.get_4_adjacency_graph((2, 2))
        data = np.asarray((0, 1, 2, 3))

        ref = (0.5, 1, 2, 2.5)
        r = hg.weight_graph(g, data, hg.WeightFunction.mean)
        self.assertTrue(np.allclose(ref, r))

        ref = (0, 0, 1, 2)
        r = hg.weight_graph(g, data, hg.WeightFunction.min)
        self.assertTrue(np.allclose(ref, r))

        ref = (1, 2, 3, 3)
        r = hg.weight_graph(g, data, hg.WeightFunction.max)
        self.assertTrue(np.allclose(ref, r))

        ref = (1, 2, 2, 1)
        r = hg.weight_graph(g, data, hg.WeightFunction.L1)
        self.assertTrue(np.allclose(ref, r))

        ref = (math.sqrt(1), 2, 2, math.sqrt(1))
        r = hg.weight_graph(g, data, hg.WeightFunction.L2)
        self.assertTrue(np.allclose(ref, r))

        ref = (1, 2, 2, 1)
        r = hg.weight_graph(g, data, hg.WeightFunction.L_infinity)
        self.assertTrue(np.allclose(ref, r))

        ref = (1, 4, 4, 1)
        r = hg.weight_graph(g, data, hg.WeightFunction.L2_squared)
        self.assertTrue(np.allclose(ref, r))
예제 #7
0
    def test_watershed_hierarchy_by_minima_ordering(self):
        g = hg.get_4_adjacency_graph((1, 7))
        edge_weights = np.asarray((1, 4, 1, 0, 10, 8))
        # same as dynamics
        minima_ranking = np.asarray((2, 2, 0, 3, 3, 1, 1), dtype=np.uint64)
        minima_altitudes = np.asarray((0, 2, 3, 10), dtype=np.float64)

        t, altitudes = hg.watershed_hierarchy_by_minima_ordering(
            g, edge_weights, minima_ranking, minima_altitudes)

        ref_parents = np.asarray((7, 7, 8, 8, 8, 9, 9, 11, 10, 10, 11, 11))
        ref_tree = hg.Tree(ref_parents)
        ref_altitudes = np.asarray((0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3))

        self.assertTrue(hg.CptHierarchy.validate(t))
        self.assertTrue(hg.test_tree_isomorphism(t, ref_tree))
        self.assertTrue(np.allclose(altitudes, ref_altitudes))

        # binary watershed hierarchy
        t, altitudes = hg.watershed_hierarchy_by_minima_ordering(
            g,
            edge_weights,
            minima_ranking,
            minima_altitudes=None,
            canonize_tree=False)

        ref_parents = np.asarray(
            (8, 8, 9, 7, 7, 10, 10, 9, 12, 11, 11, 12, 12))
        ref_tree = hg.Tree(ref_parents)
        ref_altitudes = np.asarray((0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2))
        self.assertTrue(hg.CptBinaryHierarchy.validate(t))
        self.assertTrue(hg.test_tree_isomorphism(t, ref_tree))
        self.assertTrue(np.allclose(altitudes, ref_altitudes))
예제 #8
0
    def test_binary_partition_tree_exponential_linkage_equiv(self):
        np.random.seed(10)

        g = hg.get_4_adjacency_graph((10, 10))
        edge_weights = np.random.rand(g.num_edges())
        edge_weight_weights = np.random.randint(1, 10, g.num_edges())

        tree, altitudes = hg.binary_partition_tree_exponential_linkage(
            g, edge_weights, 0, edge_weight_weights)
        t_ref, alt_ref = hg.binary_partition_tree_average_linkage(
            g, edge_weights, edge_weight_weights)
        self.assertTrue(np.all(tree.parents() == t_ref.parents()))
        self.assertTrue(np.allclose(altitudes, alt_ref))

        tree, altitudes = hg.binary_partition_tree_exponential_linkage(
            g, edge_weights, float('inf'), edge_weight_weights)
        t_ref, alt_ref = hg.binary_partition_tree_complete_linkage(
            g, edge_weights)
        self.assertTrue(np.all(tree.parents() == t_ref.parents()))
        self.assertTrue(np.allclose(altitudes, alt_ref))

        tree, altitudes = hg.binary_partition_tree_exponential_linkage(
            g, edge_weights, float('-inf'), edge_weight_weights)
        t_ref, alt_ref = hg.binary_partition_tree_single_linkage(
            g, edge_weights)
        self.assertTrue(np.all(tree.parents() == t_ref.parents()))
        self.assertTrue(np.allclose(altitudes, alt_ref))
예제 #9
0
    def test_labelisation_horizontal_cut_num_regions(self):
        g = hg.get_4_adjacency_graph((1, 11))
        tree = hg.Tree((11, 11, 11, 12, 12, 16, 13, 13, 13, 14, 14, 17, 16, 15,
                        15, 18, 17, 18, 18))
        hg.CptHierarchy.link(tree, g)
        altitudes = np.asarray(
            (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 3, 1, 2, 3))

        ref_labels = (np.asarray((1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)),
                      np.asarray((1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3)),
                      np.asarray((0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3)),
                      np.asarray((0, 1, 2, 3, 4, 5, 6, 6, 6, 7, 8)))

        k_cuts = (1, 3, 4, 9)
        for i in range(4):
            labels = hg.labelisation_horizontal_cut_from_num_regions(
                tree, altitudes, k_cuts[i])
            self.assertTrue(hg.is_in_bijection(labels, ref_labels[i]))

        # cuts with at least the given number of regions
        k_cuts = (1, 2, 4, 5)
        for i in range(4):
            labels = hg.labelisation_horizontal_cut_from_num_regions(
                tree, altitudes, k_cuts[i])
            self.assertTrue(hg.is_in_bijection(labels, ref_labels[i]))

        # cuts with at most the given number of regions
        k_cuts = (2, 3, 8, 20)
        for i in range(4):
            labels = hg.labelisation_horizontal_cut_from_num_regions(
                tree, altitudes, k_cuts[i], "at_most")
            self.assertTrue(hg.is_in_bijection(labels, ref_labels[i]))
예제 #10
0
    def test_bpt_canonical_vectorial(self):
        graph = hg.get_4_adjacency_graph((2, 3))
        edge_weights = np.asarray(
            ((1, 0, 2, 1, 1, 1, 2), (0, 2, 0, 0, 1, 1, 0), (0, 5, 5, 1, 0, 1,
                                                            1))).T
        sorted_edge_indices = np.asarray((1, 0, 3, 4, 5, 6, 2))
        ref_parents = np.asarray((6, 7, 9, 6, 8, 9, 7, 8, 10, 10, 10))
        ref_mst_edge_map = np.asarray((1, 0, 3, 4, 6))
        ref_altitudes = np.asarray(((0, 0, 0, 0, 0, 0, 0, 1, 1, 1,
                                     2), (0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0),
                                    (0, 0, 0, 0, 0, 0, 5, 0, 1, 0, 1))).T
        ref_altitudes_no_weights = np.asarray(
            (0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 5))

        tree, altitudes = hg.bpt_canonical(graph, edge_weights)
        self.assertTrue(np.all(tree.parents() == ref_parents))
        self.assertTrue(np.all(altitudes == ref_altitudes))
        self.assertTrue(np.all(tree.mst_edge_map == ref_mst_edge_map))

        tree, altitudes = hg.bpt_canonical(graph, edge_weights,
                                           sorted_edge_indices)
        self.assertTrue(np.all(tree.parents() == ref_parents))
        self.assertTrue(np.all(altitudes == ref_altitudes))
        self.assertTrue(np.all(tree.mst_edge_map == ref_mst_edge_map))

        tree, altitudes = hg.bpt_canonical(
            graph, sorted_edge_indices=sorted_edge_indices)
        self.assertTrue(np.all(tree.parents() == ref_parents))
        self.assertTrue(np.all(altitudes == ref_altitudes_no_weights))
        self.assertTrue(np.all(tree.mst_edge_map == ref_mst_edge_map))
예제 #11
0
    def test_seeded_watershed(self):
        g = hg.get_4_adjacency_graph((4, 4))
        edge_weights = np.asarray((1, 2, 5, 5, 4, 8, 1, 4, 3, 4, 4, 1, 5, 2, 6,
                                   2, 5, 2, 0, 7, 0, 3, 4, 0))

        seeds = np.asarray(
            ((1, 1, 0, 0), (1, 0, 0, 0), (0, 0, 0, 0), (1, 1, 2, 2)))

        labels = hg.labelisation_seeded_watershed(g, edge_weights, seeds)

        expected = np.asarray(
            ((1, 1, 2, 2), (1, 1, 2, 2), (1, 1, 2, 2), (1, 1, 2, 2)))
        self.assertTrue(np.all(labels == expected))

        seeds = np.asarray(
            ((1, 1, 9, 9), (1, 9, 9, 9), (9, 9, 9, 9), (2, 2, 3, 3)))

        labels = hg.labelisation_seeded_watershed(g,
                                                  edge_weights,
                                                  seeds,
                                                  background_label=9)

        expected = np.asarray(
            ((1, 1, 3, 3), (1, 1, 3, 3), (2, 2, 3, 3), (2, 2, 3, 3)))
        self.assertTrue(np.all(labels == expected))
예제 #12
0
    def test_contour_accumulator_partition_tree(self):
        graph = hg.get_4_adjacency_graph((3, 3))
        tree = hg.Tree(
            (9, 9, 10, 11, 11, 13, 12, 12, 13, 10, 14, 14, 15, 14, 15, 15))
        hg.CptHierarchy.link(tree, graph)

        node_saliency = np.asarray(
            (0, 0, 0, 0, 0, 0, 20, 0, 0, 5, 2, 7, 3, 8, 1, 50))

        result = hg.accumulate_on_contours(tree, node_saliency,
                                           hg.Accumulators.max)
        expected = np.asarray((0, 7, 5, 7, 8, 0, 20, 8, 7, 0, 20, 8))
        self.assertTrue(np.allclose(result, expected))

        node_saliency = np.asarray(
            ((0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 20), (7, 0),
             (8, 0), (9, 5), (10, 2), (11, 7), (12, 3), (13, 8), (14, 1),
             (15, 50)))

        result = hg.accumulate_on_contours(tree, node_saliency,
                                           hg.Accumulators.sum)
        expected = np.asarray(
            ((1, 0), (33, 14), (12, 5), (35, 14), (30, 10), (7, 0), (46, 31),
             (33, 15), (48, 11), (13, 0), (13, 20), (54, 12)))
        self.assertTrue(np.allclose(result, expected))
예제 #13
0
    def test_horizontal_cut_nodes(self):
        g = hg.get_4_adjacency_graph((1, 11))
        tree = hg.Tree((11, 11, 11, 12, 12, 16, 13, 13, 13, 14, 14, 17, 16, 15, 15, 18, 17, 18, 18))
        hg.CptHierarchy.link(tree, g)
        altitudes = np.asarray((0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 3, 1, 2, 3))

        hch = hg.HorizontalCutExplorer(tree, altitudes)

        cut_nodes = (
            (18,),
            (17, 13, 14),
            (11, 16, 13, 14),
            (0, 1, 2, 3, 4, 5, 13, 9, 10)
        )

        c = hch.horizontal_cut_from_num_regions(3)

        lbls = c.labelisation_leaves(tree)
        ref_lbls = (17, 17, 17, 17, 17, 17, 13, 13, 13, 14, 14)
        self.assertTrue(np.all(lbls == ref_lbls))

        rec = c.reconstruct_leaf_data(tree, altitudes)
        ref_rec = (2, 2, 2, 2, 2, 2, 0, 0, 0, 1, 1)
        self.assertTrue(np.all(rec == ref_rec))

        cut = c.graph_cut(tree)
        ref_cut = (0, 0, 0, 0, 0, 1, 0, 0, 1, 0)
        self.assertTrue(np.all(cut == ref_cut))
    def test_hierarchy_to_optimal_MumfordShah_energy_cut_hierarchy(self):
        # Test strategy:
        # 1) start from a random hierarchy
        # 2) construct the corresponding optimal Mumford-Shah energy cut hierarchy
        # 3) verify that the horizontal cuts of the new hierarchy corresponds to the
        # optimal energy cuts of the first hierarchy obtained from the explicit MF energy
        # and the function labelisation_optimal_cut_from_energy

        shape = (10, 10)
        g = hg.get_4_adjacency_graph(shape)
        np.random.seed(2)
        vertex_weights = np.random.rand(*shape)
        edge_weights = hg.weight_graph(g, vertex_weights, hg.WeightFunction.L1)
        tree1, altitudes1 = hg.watershed_hierarchy_by_area(g, edge_weights)

        tree, altitudes = hg.hierarchy_to_optimal_MumfordShah_energy_cut_hierarchy(
            tree1,
            vertex_weights,
            approximation_piecewise_linear_function=999999)

        for a in altitudes:
            if a != 0:
                res = False
                cut1 = hg.labelisation_horizontal_cut_from_threshold(
                    tree, altitudes, a)
                # du to numerical issues, and especially as we test critical scale level lambda,
                # we test several very close scale levels
                for margin in [-1e-8, 0, 1e-8]:
                    mfs_energy = hg.attribute_piecewise_constant_Mumford_Shah_energy(
                        tree1, vertex_weights, a + margin)
                    cut2 = hg.labelisation_optimal_cut_from_energy(
                        tree1, mfs_energy)
                    res = res or hg.is_in_bijection(cut1, cut2)
                self.assertTrue(res)
예제 #15
0
    def test_perimeter_length_component_tree(self):
        g = hg.get_4_adjacency_graph((4, 4))

        # for reference, tree is a max tree on the following image
        # 0, 1, 4, 4,
        # 7, 5, 6, 8,
        # 2, 3, 4, 1,
        # 9, 8, 6, 7

        t = hg.Tree((28, 27, 24, 24,
                     20, 23, 22, 18,
                     26, 25, 24, 27,
                     16, 17, 21, 19,
                     17, 21, 22, 21, 23, 24, 23, 24, 25, 26, 27, 28, 28),
                    hg.TreeCategory.ComponentTree)

        res = hg.attribute_perimeter_length(t, leaf_graph=g)

        ref = np.asarray((4, 4, 4, 4,
                          4, 4, 4, 4,
                          4, 4, 4, 4,
                          4, 4, 4, 4,
                          4, 6, 4, 4, 4, 10, 6, 10, 22, 20, 18, 16, 16), dtype=np.float64)

        self.assertTrue(np.all(res == ref))
예제 #16
0
    def test_make_rag_from_graph_cut(self):
        g = hg.get_4_adjacency_graph((4, 4))
        edge_weights = np.asarray((0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0,
                                   0, 1, 0, 0, 1, 1, 0, 1, 0))

        rag = hg.make_region_adjacency_graph_from_graph_cut(g, edge_weights)
        detail = hg.CptRegionAdjacencyGraph.construct(rag)
        vertex_map = detail["vertex_map"]
        edge_map = detail["edge_map"]

        self.assertTrue(rag.num_vertices() == 4)
        self.assertTrue(rag.num_edges() == 5)

        expected_edges = ((0, 1), (1, 2), (0, 2), (2, 3), (0, 3))

        i = 0
        for e in rag.edges():
            self.assertTrue((e[0], e[1]) == expected_edges[i])
            i += 1

        expected_vertex_map = np.asarray(
            (0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 2, 2, 0, 0, 3, 3))
        self.assertTrue(np.allclose(vertex_map, expected_vertex_map))

        iv = -1
        expected_edge_map = np.asarray(
            (iv, iv, 0, iv, iv, iv, iv, iv, iv, 0, iv, iv, 1, 1, iv, iv, 2, iv,
             iv, 3, 3, iv, 4, iv))
        self.assertTrue(np.allclose(edge_map, expected_edge_map))
예제 #17
0
    def get_rag():
        g = hg.get_4_adjacency_graph((4, 4))
        vertex_labels = np.asarray(
            (1, 1, 5, 5, 1, 1, 5, 5, 1, 1, 3, 3, 1, 1, 10, 10))

        return hg.make_region_adjacency_graph_from_labelisation(
            g, vertex_labels)
예제 #18
0
파일: test_tree.py 프로젝트: higra/Higra
    def test_filter_non_relevant_node_from_tree(self):
        g = hg.get_4_adjacency_graph((1, 8))
        edge_weights = np.asarray((0, 2, 0, 0, 1, 0, 0))
        tree, altitudes = hg.bpt_canonical(g, edge_weights)

        def functor(tree, altitudes):
            area = hg.attribute_area(tree)
            area_min_children = hg.accumulate_parallel(tree, area, hg.Accumulators.min)
            return area_min_children < 3

        res_tree, res_altitudes = hg.filter_non_relevant_node_from_tree(tree, altitudes, functor, canonize_tree=False)

        sm = hg.saliency(res_tree, res_altitudes)
        sm_ref = np.asarray((0, 0, 0, 0, 1, 0, 0))
        self.assertTrue(np.all(sm == sm_ref))

        ref_parents = (8, 8, 9, 9, 10, 11, 11, 12, 13, 10, 13, 12, 14, 14, 14)
        ref_altitudes = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)
        self.assertTrue(hg.CptBinaryHierarchy.validate(res_tree))
        self.assertTrue(np.all(ref_parents == res_tree.parents()))
        self.assertTrue(np.all(res_altitudes == ref_altitudes))

        res_tree, res_altitudes = hg.filter_non_relevant_node_from_tree(tree, altitudes, functor, canonize_tree=True)

        sm = hg.saliency(res_tree, res_altitudes)
        sm_ref = np.asarray((0, 0, 0, 0, 1, 0, 0))
        self.assertTrue(np.all(sm == sm_ref))

        ref_parents = (8, 8, 8, 8, 8, 9, 9, 9, 10, 10, 10)
        ref_tree = hg.Tree(ref_parents)
        ref_altitudes = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)
        self.assertFalse(hg.CptBinaryHierarchy.validate(res_tree))
        self.assertTrue(hg.test_tree_isomorphism(res_tree, ref_tree))
        self.assertTrue(np.all(res_altitudes == ref_altitudes))
예제 #19
0
    def test_BPT(self):
        graph = hg.get_4_adjacency_graph((2, 3))

        edge_weights = np.asarray((1, 0, 2, 1, 1, 1, 2))

        tree, altitudes = hg.bpt_canonical(graph, edge_weights)
        mst = hg.CptBinaryHierarchy.construct(tree)["mst"]
        mst_edge_map = hg.get_attribute(mst, "mst_edge_map")

        self.assertTrue(tree.num_vertices() == 11)
        self.assertTrue(tree.num_edges() == 10)
        self.assertTrue(
            np.allclose(tree.parents(), (6, 7, 9, 6, 8, 9, 7, 8, 10, 10, 10)))
        self.assertTrue(
            np.allclose(altitudes, (0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 2)))
        self.assertTrue(mst.num_vertices() == 6)
        self.assertTrue(mst.num_edges() == 5)

        ref = [(0, 3), (0, 1), (1, 4), (2, 5), (1, 2)]
        test = []
        for e in mst.edges():
            test.append((e[0], e[1]))
        self.assertTrue(ref == test)

        self.assertTrue(np.all(mst_edge_map == (1, 0, 3, 4, 2)))
예제 #20
0
    def test_contour_strength_component_tree(self):
        g = hg.get_4_adjacency_graph((4, 4))

        edge_weights = np.arange(g.num_edges(), dtype=np.float64)
        # for reference, tree is a max tree on the following image
        # 0, 1, 4, 4,
        # 7, 5, 6, 8,
        # 2, 3, 4, 1,
        # 9, 8, 6, 7

        t = hg.Tree(
            (28, 27, 24, 24, 20, 23, 22, 18, 26, 25, 24, 27, 16, 17, 21, 19,
             17, 21, 22, 21, 23, 24, 23, 24, 25, 26, 27, 28, 28),
            hg.TreeCategory.ComponentTree)
        hg.CptHierarchy.link(t, g)
        res = hg.attribute_contour_strength(t, edge_weights)

        ref_perimeter = np.asarray(
            (4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 4, 4, 4, 10,
             6, 10, 22, 20, 18, 16, 16),
            dtype=np.float64)

        ref_weights = np.asarray(
            (1, 5, 11, 10, 16, 29, 37, 30, 37, 57, 65, 51, 36, 60, 64, 43, 36,
             54, 30, 43, 16, 71, 45, 58, 123, 94, 57, 1, 0),
            dtype=np.float64)

        self.assertTrue(np.allclose(ref_weights / ref_perimeter, res))
예제 #21
0
    def test_LCAFastV(self):
        g = hg.get_4_adjacency_graph((2, 2))
        t = hg.Tree((4, 4, 5, 5, 6, 6, 6))
        lca = hg.LCAFast(t)

        ref = (4, 6, 6, 5)
        res = lca.lca(g)
        self.assertTrue(np.all(ref == res))
예제 #22
0
    def test_watershed(self):
        g = hg.get_4_adjacency_graph((4, 4))
        edge_weights = np.asarray((1, 2, 5, 5, 5, 8, 1, 4, 3, 4, 4, 1, 5, 2, 6,
                                   3, 5, 4, 0, 7, 0, 3, 4, 0))

        labels = hg.labelisation_watershed(g, edge_weights)
        expected = ((1, 1, 1, 2), (1, 1, 2, 2), (1, 1, 3, 3), (1, 1, 3, 3))
        self.assertTrue(np.allclose(labels, expected))
예제 #23
0
    def test_graph_cut_2_labelisation(self):
        graph = hg.get_4_adjacency_graph((3, 3))
        edge_weights = np.asarray((1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0), dtype=np.int32)

        labels = hg.graph_cut_2_labelisation(graph, edge_weights)

        ref_labels = np.asarray((1, 2, 2, 1, 1, 3, 1, 3, 3), dtype=np.int32)
        self.assertTrue(hg.is_in_bijection(labels, ref_labels))
예제 #24
0
 def test_vertex_perimeter_rag(self):
     g = hg.get_4_adjacency_graph((2, 3))
     vertex_labels = np.asarray(((1, 2, 2), (3, 3, 3)))
     rag = hg.make_region_adjacency_graph_from_labelisation(
         g, vertex_labels)
     ref = np.asarray((2, 3, 3))
     res = hg.attribute_vertex_perimeter(rag)
     self.assertTrue(np.allclose(ref, res))
예제 #25
0
    def rebuild_mean_pb_hierarchy(self, rag, vertex_map, edge_map, tree,
                                  altitudes, shape):

        shape = hg.normalize_shape(shape)
        graph = hg.get_4_adjacency_graph(shape)
        hg.CptRegionAdjacencyGraph.link(rag, graph, vertex_map, edge_map)
        hg.CptHierarchy.link(tree, rag)

        return tree, altitudes
예제 #26
0
    def ultrametric_open(self):
        graph = hg.get_4_adjacency_graph((3, 3))
        edge_weights = np.asarray((2, 3, 9, 5, 10, 1, 5, 8, 2, 2, 4, 3), dtype=np.int32)

        subd_ultrametric = hg.ultrametric_open(graph, edge_weights)

        ref_subd_ultrametric = np.asarray((2, 3, 9, 3, 9, 1, 4, 3, 2, 2, 4, 3), dtype=np.int32)

        self.assertTrue(hg.is_in_bijection(subd_ultrametric, ref_subd_ultrametric))
예제 #27
0
    def test_dasgupta_cost(self):
        g = hg.get_4_adjacency_graph((3, 3))
        edge_weights = np.asarray((1, 7, 3, 7, 1, 1, 6, 5, 6, 4, 1, 2))
        tree, _ = hg.bpt_canonical(g, edge_weights)

        cost = hg.dasgupta_cost(tree, edge_weights)

        ref_cost = 2 / 1 + 4 / 3 + 9 / 7 + 9 / 7 + 2 / 1 + 2 / 1 + 9 / 5 + 9 / 6 + 9 / 6 + 7 / 4 + 2 / 1 + 3 / 2
        self.assertTrue(np.isclose(cost, ref_cost))
예제 #28
0
파일: test_tree.py 프로젝트: sha2006/Higra
    def test_filter_non_relevant_node_from_tree(self):
        g = hg.get_4_adjacency_graph((1, 8))
        edge_weights = np.asarray((0, 2, 0, 0, 1, 0, 0))
        tree, altitudes = hg.quasi_flat_zone_hierarchy(g, edge_weights)

        res_tree, res_altitudes = hg.filter_small_nodes_from_tree(tree, altitudes, 3)

        sm = hg.saliency(res_tree, res_altitudes)
        sm_ref = np.asarray((0, 0, 0, 0, 1, 0, 0))
        self.assertTrue(np.all(sm == sm_ref))
예제 #29
0
파일: test_tree.py 프로젝트: sha2006/Higra
    def test_filter_weak_frontier_nodes_from_tree(self):
        g = hg.get_4_adjacency_graph((1, 8))
        edge_weights = np.asarray((0, 2, 0, 0, 1, 0, 0))
        tree, altitudes = hg.bpt_canonical(g, edge_weights)

        res_tree, res_altitudes = hg.filter_weak_frontier_nodes_from_tree(tree, altitudes, edge_weights, 2)

        sm = hg.saliency(res_tree, res_altitudes)
        sm_ref = np.asarray((0, 2, 0, 0, 0, 0, 0))
        self.assertTrue(np.all(sm == sm_ref))
예제 #30
0
 def test_dendrogram_purity_random(self):
     g = hg.get_4_adjacency_graph((10, 10))
     np.random.seed(42)
     for i in range(5):
         ew = np.random.randint(0, 20, g.num_edges())
         tree, _ = hg.quasi_flat_zone_hierarchy(g, ew)
         labels = np.random.randint(0, 10, (100, ))
         v1 = hg.dendrogram_purity(tree, labels)
         v2 = dendrogram_purity_naif(tree, labels)
         self.assertTrue(np.allclose(v1, v2))