-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement test and example for stitching, fix cornercase leading to s…
…egfault
- Loading branch information
1 parent
c232223
commit 50a270d
Showing
3 changed files
with
119 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import napari | ||
|
||
from elf.segmentation.stitching import stitch_segmentation | ||
from skimage.data import binary_blobs | ||
from skimage.measure import label | ||
|
||
|
||
def connected_components(input_, block_id=None): | ||
segmentation = label(input_) | ||
return segmentation.astype("uint32") | ||
|
||
|
||
def create_test_data(size=1024): | ||
data = binary_blobs(size, blob_size_fraction=0.1, volume_fraction=0.2) | ||
return data | ||
|
||
|
||
def main(): | ||
data = create_test_data(size=1024) | ||
|
||
# compute the segmentation using tiling and stitching | ||
tile_shape = (256, 256) | ||
tile_overlap = (32, 32) | ||
seg_stitched, seg_tiles = stitch_segmentation( | ||
data, connected_components, tile_shape, tile_overlap, return_before_stitching=True | ||
) | ||
|
||
# compute the segmentation based on connected components without any stitching | ||
seg_full = connected_components(data) | ||
|
||
# check the results visually | ||
v = napari.Viewer() | ||
v.add_image(data, name="image") | ||
v.add_labels(seg_full, name="segmentation") | ||
v.add_labels(seg_stitched, name="stitched segmentation") | ||
v.add_labels(seg_tiles, name="segmented tiles") | ||
napari.run() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import unittest | ||
|
||
from elf.evaluation import rand_index | ||
from skimage.data import binary_blobs | ||
from skimage.measure import label | ||
|
||
|
||
class TestStitching(unittest.TestCase): | ||
def get_data(self, size=1024, ndim=2): | ||
data = binary_blobs(size, blob_size_fraction=0.1, volume_fraction=0.2, n_dim=ndim) | ||
return data | ||
|
||
def test_stitch_segmentation(self): | ||
from elf.segmentation.stitching import stitch_segmentation | ||
|
||
def _segment(input_, block_id=None): | ||
segmentation = label(input_) | ||
return segmentation.astype("uint32") | ||
|
||
tile_overlap = (32, 32) | ||
tile_shapes = [(128, 128), (256, 256), (128, 256)] | ||
for tile_shape in tile_shapes: | ||
for _ in range(3): # test 3 times with different data | ||
data = self.get_data() | ||
expected_segmentation = _segment(data) | ||
segmentation = stitch_segmentation(data, _segment, tile_shape, tile_overlap, verbose=False) | ||
are, _ = rand_index(segmentation, expected_segmentation) | ||
self.assertTrue(are < 0.05) | ||
|
||
def test_stitch_segmentation_3d(self): | ||
from elf.segmentation.stitching import stitch_segmentation | ||
|
||
def _segment(input_, block_id=None): | ||
segmentation = label(input_) | ||
return segmentation.astype("uint32") | ||
|
||
tile_overlap = (16, 16, 16) | ||
tile_shapes = [(32, 32, 32), (64, 64, 64), (32, 64, 24)] | ||
for tile_shape in tile_shapes: | ||
data = self.get_data(256, ndim=3) | ||
expected_segmentation = _segment(data) | ||
segmentation = stitch_segmentation(data, _segment, tile_shape, tile_overlap, verbose=False) | ||
are, _ = rand_index(segmentation, expected_segmentation) | ||
self.assertTrue(are < 0.05) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |