forked from CellProfiler/CellProfiler-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_seedobjects.py
430 lines (273 loc) · 11.3 KB
/
test_seedobjects.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import numpy
import numpy.testing
import skimage.morphology
import skimage.segmentation
import pytest
import scipy.ndimage
import skimage.filters
import skimage.feature
import skimage.util
import seedobjects
instance = seedobjects.SeedObjects
@pytest.fixture(scope="module")
def image_labels():
labels = numpy.zeros((20, 20), dtype=numpy.uint8)
# Midpoint - 5, 5
# Size = 7x7 (49)
labels[2:9, 2:9] = 1
# Midpoint - 4, 15
# Size = 9x7 (63)
labels[0:9, 12:19] = 2
# Midpoint - 15, 4
# Size = 7x9 (63)
labels[12:19, 0:9] = 3
# Midpoint - 17, 17
# Size = 7x7 (49)
labels[14:21, 14:21] = 4
return labels
@pytest.fixture(scope="module")
def volume_labels():
labels = numpy.zeros((9, 20, 20), dtype=numpy.uint8)
# Midpoint - 4, 5, 5
# Size = 9x7x7 (441)
labels[0:9, 2:9, 2:9] = 1
# Midpoint - 2, 4, 15
# Size = 5x9x7 (315)
labels[0:5, 0:9, 12:19] = 2
# Midpoint - 6, 15, 4
# Size = 7x9x7 (441)
labels[4:11, 12:19, 0:9] = 3
# Midpoint - 5, 17, 17
# Size = 9x7x7 (441)
labels[1:10, 14:21, 14:21] = 4
return labels
def test_run(object_set_with_data, module, workspace_with_data):
input_objs = object_set_with_data.get_objects("InputObjects").segmented
im_dim = input_objs.ndim
if im_dim == 2:
strel = "disk,2"
else:
strel = "ball,2"
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
module.structuring_element.value = strel
module.run(workspace_with_data)
actual = workspace_with_data.object_set.get_objects("OutputObjects").segmented
padded = skimage.util.pad(input_objs, 1, mode='constant', constant_values=0)
seeds = scipy.ndimage.distance_transform_edt(padded)
seeds = skimage.util.crop(seeds, 1)
seeds = skimage.filters.gaussian(seeds, sigma=1)
seeds = skimage.feature.peak_local_max(seeds,
min_distance=1,
threshold_rel=0,
exclude_border=False,
num_peaks=numpy.inf,
indices=False)
expected = skimage.morphology.binary_dilation(seeds, module.structuring_element.value)
numpy.testing.assert_array_equal(actual, expected)
def test_2d_regular(image_labels, module, object_set_empty, objects_empty, workspace_empty):
labels = numpy.zeros_like(image_labels)
labels[5, 5] = 1
labels[4, 15] = 1
labels[15, 4] = 1
labels[17, 17] = 1
objects_empty.segmented = image_labels
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
module.structuring_element.value = "disk,0"
module.run(workspace_empty)
actual = object_set_empty.get_objects("OutputObjects").segmented
expected = labels
numpy.testing.assert_array_equal(actual, expected)
def test_3d_regular(volume_labels, module, object_set_empty, objects_empty, workspace_empty):
labels = numpy.zeros_like(volume_labels)
labels[4, 5, 5] = 1
labels[2, 4, 15] = 1
labels[6, 15, 4] = 1
labels[5, 17, 17] = 1
objects_empty.segmented = volume_labels
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
module.structuring_element.value = "ball,0"
module.run(workspace_empty)
actual = object_set_empty.get_objects("OutputObjects").segmented
expected = labels
numpy.testing.assert_array_equal(actual, expected)
def test_2d_min_dist(image_labels, module, object_set_empty, objects_empty, workspace_empty):
labels = numpy.zeros_like(image_labels)
labels[4, 15] = 1
labels[15, 4] = 1
objects_empty.segmented = image_labels
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
module.structuring_element.value = "disk,0"
module.min_dist.value = 12
module.run(workspace_empty)
actual = object_set_empty.get_objects("OutputObjects").segmented
expected = labels
numpy.testing.assert_array_equal(actual, expected)
def test_3d_min_dist(volume_labels, module, object_set_empty, objects_empty, workspace_empty):
labels = numpy.zeros_like(volume_labels)
labels[2, 4, 15] = 1
labels[7, 15, 4] = 1
objects_empty.segmented = labels
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
module.structuring_element.value = "ball,0"
module.min_dist.value = 64
module.run(workspace_empty)
actual = object_set_empty.get_objects("OutputObjects").segmented
expected = labels
numpy.testing.assert_array_equal(actual, expected)
def test_2d_min_intensity(image_labels, module, object_set_empty, objects_empty, workspace_empty):
labels = numpy.zeros_like(image_labels)
# Only the largest objects should be seeded
labels[4, 15] = 1
labels[15, 4] = 1
objects_empty.segmented = image_labels
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
module.structuring_element.value = "disk,0"
module.min_intensity.value = 0.95
module.run(workspace_empty)
actual = object_set_empty.get_objects("OutputObjects").segmented
expected = labels
numpy.testing.assert_array_equal(actual, expected)
def test_3d_min_intensity(volume_labels, module, object_set_empty, objects_empty, workspace_empty):
labels = numpy.zeros_like(volume_labels)
labels[4, 5, 5] = 1
labels[5, 17, 17] = 1
objects_empty.segmented = volume_labels
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
module.structuring_element.value = "ball,0"
module.min_intensity.value = 0.77
module.run(workspace_empty)
actual = object_set_empty.get_objects("OutputObjects").segmented
expected = labels
numpy.testing.assert_array_equal(actual, expected)
def test_2d_border_exclude(image_labels, module, object_set_empty, objects_empty, workspace_empty):
labels = numpy.zeros_like(image_labels)
labels[5, 5] = 1
objects_empty.segmented = image_labels
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
module.structuring_element.value = "disk,0"
module.exclude_border.value = 5
module.run(workspace_empty)
actual = object_set_empty.get_objects("OutputObjects").segmented
expected = labels
numpy.testing.assert_array_equal(actual, expected)
def test_3d_border_exclude(volume_labels, module, object_set_empty, objects_empty, workspace_empty):
labels = numpy.zeros_like(volume_labels)
labels[4, 5, 5] = 1
objects_empty.segmented = volume_labels
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
module.structuring_element.value = "ball,0"
module.exclude_border.value = 3
module.run(workspace_empty)
actual = object_set_empty.get_objects("OutputObjects").segmented
expected = labels
numpy.testing.assert_array_equal(actual, expected)
def test_2d_max_seeds(image_labels, module, object_set_empty, objects_empty, workspace_empty):
labels = numpy.zeros_like(image_labels)
labels[4, 15] = 1
labels[15, 4] = 1
objects_empty.segmented = image_labels
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
module.structuring_element.value = "disk,0"
module.max_seeds.value = 2
module.run(workspace_empty)
actual = object_set_empty.get_objects("OutputObjects").segmented
expected = labels
numpy.testing.assert_array_equal(actual, expected)
def test_3d_max_seeds(volume_labels, module, object_set_empty, objects_empty, workspace_empty):
labels = numpy.zeros_like(volume_labels)
labels[4, 5, 5] = 1
labels[5, 17, 17] = 1
objects_empty.segmented = volume_labels
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
module.structuring_element.value = "ball,0"
module.max_seeds.value = 2
module.run(workspace_empty)
actual = object_set_empty.get_objects("OutputObjects").segmented
expected = labels
numpy.testing.assert_array_equal(actual, expected)
def test_2d_strel(image_labels, module, object_set_empty, objects_empty, workspace_empty):
labels = numpy.zeros_like(image_labels)
labels[5, 5] = 1
labels[4, 15] = 1
labels[15, 4] = 1
labels[17, 17] = 1
objects_empty.segmented = image_labels
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
module.structuring_element.value = "disk,2"
module.run(workspace_empty)
actual = object_set_empty.get_objects("OutputObjects").segmented
labels = skimage.morphology.binary_dilation(labels, skimage.morphology.disk(2))
expected = labels
numpy.testing.assert_array_equal(actual, expected)
def test_3d_strel(volume_labels, module, object_set_empty, objects_empty, workspace_empty):
labels = numpy.zeros_like(volume_labels)
labels[4, 5, 5] = 1
labels[2, 4, 15] = 1
labels[6, 15, 4] = 1
labels[5, 17, 17] = 1
objects_empty.segmented = volume_labels
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
module.structuring_element.value = "ball,2"
module.run(workspace_empty)
actual = object_set_empty.get_objects("OutputObjects").segmented
labels = skimage.morphology.binary_dilation(labels, skimage.morphology.ball(2))
expected = labels
numpy.testing.assert_array_equal(actual, expected)
def test_2d_multiple_seeds_per_obj(image_labels, module, object_set_empty, objects_empty, workspace_empty):
# Make an object with more than one internal maximum
image_labels[2:9, 2:11] = 1
image_labels[2:5, 6] = 0
image_labels[7:9, 6] = 0
labels = numpy.zeros_like(image_labels)
# This object should now have two seeds
labels[5, 4] = 1
labels[5, 8] = 1
labels[4, 15] = 1
labels[15, 4] = 1
labels[17, 17] = 1
objects_empty.segmented = image_labels
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
module.structuring_element.value = "disk,2"
module.run(workspace_empty)
actual = object_set_empty.get_objects("OutputObjects").segmented
labels = skimage.morphology.binary_dilation(labels, skimage.morphology.disk(2))
expected = labels
numpy.testing.assert_array_equal(actual, expected)
def test_2d_max_seeds_per_object(image_labels, module, object_set_empty, objects_empty, workspace_empty):
# Make an object with more than one internal maximum
image_labels[2:9, 2:11] = 1
image_labels[2:5, 6] = 0
image_labels[7:9, 6] = 0
labels = numpy.zeros_like(image_labels)
# This object should normally get two seeds, but we're going to
# enforce a maximum of 1
labels[5, 4] = 1
labels[5, 8] = 1
labels[4, 15] = 1
labels[15, 4] = 1
labels[17, 17] = 1
objects_empty.segmented = image_labels
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
module.structuring_element.value = "disk,0"
module.max_seeds_per_obj.value = 1
module.run(workspace_empty)
actual = object_set_empty.get_objects("OutputObjects").segmented
labels = skimage.morphology.binary_dilation(labels, skimage.morphology.disk(0))
expected = labels
unequal_pos = tuple(int(x) for x in numpy.where(actual != expected))
assert unequal_pos == (5, 8) or unequal_pos == (5, 4)