From 8025aa09e21410e3f38533b171220d0ce04a5488 Mon Sep 17 00:00:00 2001 From: Constantin Pape Date: Tue, 29 Mar 2022 11:30:23 +0200 Subject: [PATCH] Extend feature tests and python versions --- .github/workflows/build.yml | 2 +- test/segmentation/test_features.py | 46 +++++++++++++++++++++++------- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 67de9c6..5f37236 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -12,7 +12,7 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest, macos-latest, windows-latest] - python-version: [3.7] + python-version: [3.8, 3.9, 3.10] steps: - name: Checkout diff --git a/test/segmentation/test_features.py b/test/segmentation/test_features.py index 5722303..8621afe 100644 --- a/test/segmentation/test_features.py +++ b/test/segmentation/test_features.py @@ -6,7 +6,7 @@ class TestFeatures(unittest.TestCase): def make_seg(self, shape): size = np.prod([sh for sh in shape]) - seg = np.zeros(size, dtype='uint32') + seg = np.zeros(size, dtype="uint32") current_id = 1 change_prob = .99 for i in range(seg.size): @@ -19,7 +19,7 @@ def test_region_features(self): from elf.segmentation.features import compute_rag, compute_region_features shape = (32, 128, 128) - inp = np.random.rand(*shape).astype('float32') + inp = np.random.rand(*shape).astype("float32") seg = self.make_seg(shape) rag = compute_rag(seg) uv_ids = rag.uvIds() @@ -28,11 +28,37 @@ def test_region_features(self): self.assertEqual(len(uv_ids), len(feats)) self.assertFalse(np.allclose(feats, 0)) + def test_compute_affinity_features(self): + from elf.segmentation.features import compute_rag, compute_affinity_features + + shape = (16, 64, 64) + seg = self.make_seg(shape) + rag = compute_rag(seg) + + offsets = [[-1, 0, 0], [0, -1, 0], [0, 0, -1], [-3, 0, 0], [0, -3, 0], [0, 0, -3]] + inp = np.random.rand(*((len(offsets),) + shape)).astype("float32") + + feats = compute_affinity_features(rag, inp, offsets) + self.assertEqual(rag.numberOfEdges, len(feats)) + self.assertFalse(np.allclose(feats, 0)) + + def test_compute_boundary_features(self): + from elf.segmentation.features import compute_rag, compute_boundary_features + + shape = (16, 64, 64) + seg = self.make_seg(shape) + rag = compute_rag(seg) + + inp = np.random.rand(*shape).astype("float32") + feats = compute_boundary_features(rag, inp) + self.assertEqual(rag.numberOfEdges, len(feats)) + self.assertFalse(np.allclose(feats, 0)) + def test_boundary_features_with_filters(self): from elf.segmentation.features import compute_rag, compute_boundary_features_with_filters shape = (64, 128, 128) - inp = np.random.rand(*shape).astype('float32') + inp = np.random.rand(*shape).astype("float32") seg = self.make_seg(shape) rag = compute_rag(seg) @@ -52,7 +78,7 @@ def test_lifted_problem_from_probabilities(self): rag = compute_rag(seg) n_classes = 3 - input_maps = [np.random.rand(*shape).astype('float32') for _ in range(n_classes)] + input_maps = [np.random.rand(*shape).astype("float32") for _ in range(n_classes)] assignment_threshold = .5 graph_depth = 4 @@ -105,7 +131,7 @@ def _check_feats(feats): # test g = compute_grid_graph(shape) aff_shape = (ndim,) + shape - affs = np.random.rand(*aff_shape).astype('float32') + affs = np.random.rand(*aff_shape).astype("float32") # for the case without offsets, the edges returned must correspond # to the edges of the grid graph @@ -117,7 +143,7 @@ def _check_feats(feats): # test - with offsets aff_shape = (len(offsets),) + shape - affs = np.random.rand(*aff_shape).astype('float32') + affs = np.random.rand(*aff_shape).astype("float32") edges, feats = compute_grid_graph_affinity_features(g, affs, offsets=offsets) self.assertEqual(len(edges), len(feats)) self.assertEqual(edges.shape[1], 2) @@ -162,14 +188,14 @@ def _check_dists(feats, dist): # all distances must be greater than 0 self.assertTrue((feats >= 0).all()) # cosine distance is bounded at 1 - if dist == 'cosine': + if dist == "cosine": self.assertTrue((feats <= 1).all()) # test g = compute_grid_graph(shape) im_shape = (6,) + shape - im = np.random.rand(*im_shape).astype('float32') - for dist in ('l1', 'l2', 'cosine'): + im = np.random.rand(*im_shape).astype("float32") + for dist in ("l1", "l2", "cosine"): edges, feats = compute_grid_graph_image_features(g, im, dist) # for the case without offsets, the edges returned must correspond # to the edges of the grid graph @@ -245,5 +271,5 @@ def test_apply_mask_to_grid_graph_edges_and_weights(self): self.assertGreater(n_edges_prev, len(edges)) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main()