Skip to content

Commit d26d848

Browse files
Fix issue in table computation and add tests
1 parent 3f6c95a commit d26d848

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

flamingo_tools/segmentation/postprocessing.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import numpy as np
77
import nifty.tools as nt
88
import pandas as pd
9-
import vigra
109

1110
from elf.io import open_file
1211
from scipy.spatial import distance
@@ -113,15 +112,14 @@ def neighbors_in_radius(table: pd.DataFrame, radius: float = 15) -> np.ndarray:
113112
# Filter the segmentation based on a spatial statistics from above.
114113
#
115114

116-
# FIXME: functions causes ValueError by using arrays of different lengths
117-
118115

116+
# FIXME: this computes the distance in pixels, but the MoBIE table contains it in physical units (=nm)
117+
# This is inconsistent.
119118
def _compute_table(segmentation):
120-
segmentation, n_ids, _ = vigra.analysis.relabelConsecutive(segmentation[:], start_label=1, keep_zeros=True)
121119
props = measure.regionprops(segmentation)
122-
coordinates = np.array([prop.centroid for prop in props])[1:]
123-
label_ids = np.unique(segmentation)[1:]
124-
sizes = np.array([prop.area for prop in props])[1:]
120+
label_ids = np.array([prop.label for prop in props])
121+
coordinates = np.array([prop.centroid for prop in props])
122+
sizes = np.array([prop.area for prop in props])
125123
table = pd.DataFrame({
126124
"label_id": label_ids,
127125
"n_pixels": sizes,
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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()

test/test_segmentation/test_unet_prediction.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
import sys
32
import tempfile
43
import unittest
54

@@ -9,8 +8,6 @@
98
import z5py
109
from torch_em.model import UNet3d
1110

12-
sys.path.append("../..")
13-
1411

1512
class TestUnetPrediction(unittest.TestCase):
1613
shape = (64, 128, 128)

0 commit comments

Comments
 (0)