|
| 1 | +import os |
| 2 | +import tempfile |
| 3 | +import unittest |
| 4 | + |
| 5 | +from elf.io import open_file |
| 6 | +from skimage.data import binary_blobs |
| 7 | +from skimage.measure import label |
| 8 | + |
| 9 | + |
| 10 | +class TestPostprocessing(unittest.TestCase): |
| 11 | + def _create_example_seg(self, tmp_dir): |
| 12 | + seg = binary_blobs(256, n_dim=3, volume_fraction=0.2) |
| 13 | + seg = label(seg) |
| 14 | + return seg |
| 15 | + |
| 16 | + def _test_postprocessing(self, spatial_statistics, threshold, **spatial_statistics_kwargs): |
| 17 | + from flamingo_tools.segmentation.postprocessing import filter_segmentation |
| 18 | + |
| 19 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 20 | + example_seg = self._create_example_seg(tmp_dir) |
| 21 | + output_path = os.path.join(tmp_dir, "test-output.zarr") |
| 22 | + output_key = "seg-filtered" |
| 23 | + filter_segmentation( |
| 24 | + example_seg, output_path, spatial_statistics, threshold, |
| 25 | + output_key=output_key, **spatial_statistics_kwargs |
| 26 | + ) |
| 27 | + self.assertTrue(os.path.exists(output_path)) |
| 28 | + with open_file(output_path, "r") as f: |
| 29 | + filtered_seg = f[output_key][:] |
| 30 | + self.assertEqual(filtered_seg.shape, example_seg.shape) |
| 31 | + |
| 32 | + def test_nearest_neighbor_distance(self): |
| 33 | + from flamingo_tools.segmentation.postprocessing import nearest_neighbor_distance |
| 34 | + |
| 35 | + self._test_postprocessing(nearest_neighbor_distance, threshold=5) |
| 36 | + |
| 37 | + def test_local_ripleys_k(self): |
| 38 | + from flamingo_tools.segmentation.postprocessing import local_ripleys_k |
| 39 | + |
| 40 | + self._test_postprocessing(local_ripleys_k, threshold=0.5) |
| 41 | + |
| 42 | + def test_neighbors_in_radius(self): |
| 43 | + from flamingo_tools.segmentation.postprocessing import neighbors_in_radius |
| 44 | + |
| 45 | + self._test_postprocessing(neighbors_in_radius, threshold=5) |
| 46 | + |
| 47 | + |
| 48 | +if __name__ == "__main__": |
| 49 | + unittest.main() |
0 commit comments