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]))
def test_assess_optimal_partitions_BCE_optimal_cut(self): t = hg.Tree((8, 8, 9, 9, 10, 10, 11, 13, 12, 12, 11, 13, 14, 14, 14)) 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) optimal_partitions = [ np.asarray((0, 0, 0, 0, 0, 0, 0, 0), dtype=np.int32), np.asarray((0, 0, 0, 0, 1, 1, 1, 1), dtype=np.int32), np.asarray((0, 0, 1, 1, 2, 2, 2, 2), dtype=np.int32), np.asarray((0, 0, 1, 1, 2, 2, 2, 3), dtype=np.int32), np.asarray((0, 0, 1, 1, 2, 2, 3, 4), dtype=np.int32), np.asarray((0, 0, 1, 1, 2, 3, 4, 5), dtype=np.int32), np.asarray((0, 0, 1, 2, 3, 4, 5, 6), dtype=np.int32), np.asarray((0, 1, 2, 3, 4, 5, 6, 7), dtype=np.int32) ] self.assertTrue( hg.is_in_bijection(optimal_partitions[2], assesser.optimal_partition())) for i in range(len(optimal_partitions)): self.assertTrue( hg.is_in_bijection(optimal_partitions[i], assesser.optimal_partition(i + 1)))
def test_labelisation_horizontal_cut(self): tree = hg.Tree(np.asarray((5, 5, 6, 6, 6, 7, 7, 7))) altitudes = np.asarray((0, 0, 0, 0, 0, 0.5, 0, 0.7), dtype=np.double) ref_t0 = np.asarray((1, 2, 3, 3, 3), dtype=np.int32) ref_t1 = np.asarray((1, 1, 2, 2, 2), dtype=np.int32) ref_t2 = np.asarray((1, 1, 1, 1, 1), dtype=np.int32) output_t0 = hg.labelisation_horizontal_cut_from_threshold(tree, altitudes, 0) output_t1 = hg.labelisation_horizontal_cut_from_threshold(tree, altitudes, 0.5) output_t2 = hg.labelisation_horizontal_cut_from_threshold(tree, altitudes, 0.7) self.assertTrue(hg.is_in_bijection(ref_t0, output_t0)) self.assertTrue(hg.is_in_bijection(ref_t1, output_t1)) self.assertTrue(hg.is_in_bijection(ref_t2, output_t2))
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)
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))
def test_labelisation_optimal_cut_from_energy(self): t = hg.Tree((8, 8, 9, 7, 7, 11, 11, 9, 10, 10, 12, 12, 12)) energy_attribute = np.asarray( (2, 1, 3, 2, 1, 1, 1, 2, 2, 4, 10, 5, 20)) res = hg.labelisation_optimal_cut_from_energy(t, energy_attribute) ref = np.asarray((0, 0, 1, 1, 1, 2, 3)) self.assertTrue(hg.is_in_bijection(res, ref))
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))
def test_labelisation_hierarchy_supervertices(self): tree = hg.Tree(np.asarray((5, 5, 6, 6, 6, 7, 7, 7))) altitudes = np.asarray((0, 0, 0, 0, 0, 0.5, 0, 0.7), dtype=np.double) ref = np.asarray((0, 1, 2, 2, 2), dtype=np.int32) output = hg.labelisation_hierarchy_supervertices(tree, altitudes) self.assertTrue(hg.is_in_bijection(ref, output)) self.assertTrue(np.amax(output) == 2) self.assertTrue(np.amin(output) == 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, 3, 3), (1, 1, 3, 3), (2, 2, 3, 3), (2, 2, 3, 3))) self.assertTrue(hg.is_in_bijection(labels, expected))
def test_horizontal_cut_nodes_delinearization(self): g = hg.get_4_adjacency_graph((2, 2)) tree = hg.Tree((4, 4, 5, 6, 5, 6, 6)) hg.CptHierarchy.link(tree, g) altitudes = np.asarray((0, 0, 0, 0, 1, 2, 3)) hch = hg.HorizontalCutExplorer(tree, altitudes) c = hch.horizontal_cut_from_num_regions(3) lbls = c.labelisation_leaves(tree) self.assertTrue(np.all(lbls.shape == (2, 2))) ref_lbls = np.array((0, 0, 1, 2)) self.assertTrue(hg.is_in_bijection(lbls[:], ref_lbls)) vweights = c.reconstruct_leaf_data(tree, altitudes) ref_vweights = np.array(((1, 1), (0, 0))) self.assertTrue(np.all(vweights == ref_vweights))
def test_fit_contour_2d(self): shape = (4, 5) g = hg.get_4_adjacency_graph(shape) data = np.asarray((0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), np.int32) ref = np.asarray( ((0, 0, 0, 0, 0, 0, 0, 0, 0), (1, 0, 0, 0, 5, 0, 0, 0, 7), (0, 2, 0, 4, 0, 6, 0, 8, 0), (0, 0, 3, 0, 0, 0, 7, 0, 0), (0, 0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 0, 0)), np.int32) contours = hg.fit_contour_2d(g, shape, data) contours.subdivide(0.000001, False, 0) contours_khalimsky = TestContour2d.contour_2_khalimsky( g, shape, contours) self.assertTrue(hg.is_in_bijection(ref, contours_khalimsky))