Skip to content

Commit

Permalink
Extend feature tests and python versions
Browse files Browse the repository at this point in the history
  • Loading branch information
constantinpape committed Mar 29, 2022
1 parent 20d7c7f commit 8025aa0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 36 additions & 10 deletions test/segmentation/test_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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()
Expand All @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()

0 comments on commit 8025aa0

Please sign in to comment.