forked from CellProfiler/CellProfiler-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_mergeobjects.py
413 lines (297 loc) · 12.2 KB
/
test_mergeobjects.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
import numpy
import numpy.testing
import skimage.morphology
import skimage.segmentation
import centrosome.cpmorphology
import pytest
import mergeobjects
instance = mergeobjects.MergeObjects
@pytest.fixture(scope="module")
def image_labels():
labels = numpy.zeros((20, 20), dtype=numpy.uint8)
labels[2:8, 2:8] = 1
labels[0:8, 12:18] = 2
labels[12:18, 0:8] = 3
labels[12:20, 12:20] = 4
return labels
@pytest.fixture(scope="module")
def volume_labels():
labels = numpy.zeros((9, 20, 20), dtype=numpy.uint8)
labels[0:9, 2:8, 2:8] = 1
labels[0:5, 0:8, 12:18] = 2
labels[4:9, 12:18, 0:8] = 3
labels[1:8, 12:20, 12:20] = 4
return labels
@pytest.fixture(
scope="module",
params=[False, True],
ids=["keep_lonely", "remove_lonely"])
def remove_below(request):
return request.param
def make_params():
# Format:
# [use, method, vals[]]
params = []
methods = [mergeobjects.A_ABSOLUTE, mergeobjects.A_RELATIVE]
abs_vals = [0, 10, 20, 50]
rel_vals = [0.001, 0.01, 0.1, 1.0]
# Add non used
params.append([False, None, []])
for method in methods:
if method == mergeobjects.A_ABSOLUTE:
params += [[True, method, val] for val in abs_vals]
else:
params += [[True, method, val] for val in rel_vals]
return params
@pytest.fixture(
scope="module",
params=make_params()
)
def contact_area_params(request):
return request.param
def test_run(object_set_with_data, module, workspace_with_data, remove_below, contact_area_params):
use_contact_area, contact_method, relabs_neighbor_size = contact_area_params
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
module.size.value = 6.
module.remove_below_threshold.value = remove_below
module.use_contact_area.value = use_contact_area
if use_contact_area:
module.contact_area_method.value = contact_method
if contact_method == mergeobjects.A_ABSOLUTE:
module.abs_neighbor_size.value = relabs_neighbor_size
else:
module.rel_neighbor_size.value = relabs_neighbor_size
module.run(workspace_with_data)
actual = workspace_with_data.object_set.get_objects("OutputObjects").segmented
if actual.ndim == 2:
factor = 3 ** 2
else:
factor = (4.0 / 3.0) * (3 ** 3)
size = numpy.pi * factor
expected = object_set_with_data.get_objects("InputObjects").segmented
merged = numpy.copy(expected)
sizes = numpy.bincount(expected.ravel())
mask_sizes = (sizes < size) & (sizes != 0)
if use_contact_area and contact_method == mergeobjects.A_RELATIVE:
border_mask = skimage.segmentation.find_boundaries(expected, mode='inner')
surface_areas = numpy.bincount(expected[border_mask].ravel())
for n in numpy.nonzero(mask_sizes)[0]:
mask = expected == n
bound = skimage.segmentation.find_boundaries(mask, mode='outer')
neighbors = numpy.bincount(expected[bound].ravel())
if len(neighbors) == 1:
if remove_below:
max_neighbor = 0
else:
continue
else:
neighbors[0] = 0
max_neighbor = numpy.argmax(neighbors)
if not use_contact_area:
merged[merged == n] = max_neighbor
else:
if contact_method == mergeobjects.A_ABSOLUTE:
neighbor_size = relabs_neighbor_size
conditional = neighbors[max_neighbor] > relabs_neighbor_size
else:
neighbor_size = relabs_neighbor_size
if remove_below and max_neighbor == 0:
conditional = True
else:
conditional = (float(neighbors[max_neighbor]) / surface_areas[n]) > relabs_neighbor_size
if neighbor_size == 0 or conditional:
merged[merged == n] = max_neighbor
expected = centrosome.cpmorphology.relabel(merged)[0]
numpy.testing.assert_array_equal(actual, expected)
def test_2d_regular(image_labels, module, object_set_empty, objects_empty, workspace_empty):
labels = image_labels.copy()
labels[5, 5] = 5
labels[2, 15] = 6
labels[15, 2] = 7
labels[15, 15] = 8
objects_empty.segmented = labels
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
module.size.value = 2.
module.run(workspace_empty)
actual = object_set_empty.get_objects("OutputObjects").segmented
expected = image_labels
numpy.testing.assert_array_equal(actual, expected)
def test_3d_regular(volume_labels, module, object_set_empty, objects_empty, workspace_empty):
labels = volume_labels.copy()
labels[5, 5, 5] = 5
labels[2, 2, 15] = 6
labels[5, 15, 2] = 7
labels[5, 15, 15] = 8
objects_empty.segmented = labels
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
module.size.value = 2.
module.run(workspace_empty)
actual = object_set_empty.get_objects("OutputObjects").segmented
expected = volume_labels
numpy.testing.assert_array_equal(actual, expected)
def test_unchanged_3d_merge_large_object(volume_labels, module, object_set_empty, objects_empty, workspace_empty):
labels = volume_labels.copy()
# Create a 'large' object
labels[5:10, 4:6, 4:6] = 5
objects_empty.segmented = labels
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
# Set size below minimum
module.size.value = 3.
module.run(workspace_empty)
actual = object_set_empty.get_objects("OutputObjects").segmented
expected = labels
# Since the 3D size is above the minimum size threshold, no object should be merged
numpy.testing.assert_array_equal(actual, expected)
def test_changed_3d_merge_large_object(volume_labels, module, object_set_empty, objects_empty, workspace_empty):
labels = volume_labels.copy()
# Create a 'large' object
labels[5:10, 4:6, 4:6] = 5
objects_empty.segmented = labels
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
# Set size below minimum
module.size.value = 3.
# Set to planewise so the 'large' object is merged at each plane
module.plane_wise.value = True
module.run(workspace_empty)
actual = object_set_empty.get_objects("OutputObjects").segmented
expected = volume_labels
# We're filling plane-wise here, so each 2D plane should have the object merged
numpy.testing.assert_array_equal(actual, expected)
def test_2d_keep_nonneighbored_objects(image_labels, module, object_set_empty, objects_empty, workspace_empty):
labels = image_labels.copy()
# Create "small" object
labels[8:12, 9:11] = 5
objects_empty.segmented = labels
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
module.size.value = 4.
# Modify threshold removal procedure
module.remove_below_threshold.value = False
module.run(workspace_empty)
actual = object_set_empty.get_objects("OutputObjects").segmented
# Object with no neighbors should not be removed
expected = labels
numpy.testing.assert_array_equal(actual, expected)
def test_3d_keep_nonneighbored_object(volume_labels, module, object_set_empty, objects_empty, workspace_empty):
labels = volume_labels.copy()
labels[8:12, 9:11, 4:6] = 5
objects_empty.segmented = labels
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
module.size.value = 4.
# Modify threshold removal procedure
module.remove_below_threshold.value = False
module.run(workspace_empty)
actual = object_set_empty.get_objects("OutputObjects").segmented
# Object with no neighbors should not be removed
expected = labels
numpy.testing.assert_array_equal(actual, expected)
def test_2d_abs_neighbor_size_some(image_labels, module, object_set_empty, objects_empty, workspace_empty):
labels = image_labels.copy()
# Create an object which doesn't meet contact criteria
labels[12:15, 0:1] = 7
# Create one which does
labels[2:8, 2:4] = 8
# Create one which meets the criteria for one object but not another
labels[10:12, 12:17] = 9
labels[8:10, 14:16] = 9
objects_empty.segmented = labels
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
module.size.value = 4.
module.remove_below_threshold.value = False
# Set the minimum contact area
module.use_contact_area.value = True
module.contact_area_method.value = mergeobjects.A_ABSOLUTE
module.abs_neighbor_size.value = 5
module.run(workspace_empty)
actual = object_set_empty.get_objects("OutputObjects").segmented
expected = labels.copy()
# Objects with less than 6 contacting pixels stay
expected[2:8, 2:4] = 1
# Some objects are relabeled
expected[expected == 7] = 5
expected[expected == 9] = 6
numpy.testing.assert_array_equal(actual, expected)
def test_2d_abs_neighbor_size_all(image_labels, module, object_set_empty, objects_empty, workspace_empty):
labels = image_labels.copy()
# Create an object which doesn't meet contact criteria
labels[12:15, 0:1] = 7
# Create one which does
labels[2:8, 2:4] = 8
# Create one which meets the criteria for one object but not another
labels[10:12, 12:17] = 9
labels[8:10, 14:16] = 9
objects_empty.segmented = labels
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
module.size.value = 5.
module.remove_below_threshold.value = False
# Set the minimum contact area low so all objects get merged
module.use_contact_area.value = True
module.contact_area_method.value = mergeobjects.A_ABSOLUTE
module.abs_neighbor_size.value = 3
module.run(workspace_empty)
actual = object_set_empty.get_objects("OutputObjects").segmented
expected = image_labels.copy()
# Have to set the weird one
expected[10:12, 12:17] = 4
expected[8:10, 14:16] = 4
numpy.testing.assert_array_equal(actual, expected)
def test_2d_rel_neighbor_size_some(image_labels, module, object_set_empty, objects_empty, workspace_empty):
labels = image_labels.copy()
# Create an object which doesn't meet contact criteria
labels[12:15, 0:1] = 7
# Create one which does
labels[2:8, 2:4] = 8
# Create one which meets the criteria for one object but not another
labels[10:12, 12:17] = 9
labels[8:10, 14:16] = 9
objects_empty.segmented = labels
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
module.size.value = 4.
module.remove_below_threshold.value = False
# Set the minimum contact area
module.use_contact_area.value = True
module.contact_area_method.value = mergeobjects.A_RELATIVE
module.rel_neighbor_size.value = 0.5
module.run(workspace_empty)
actual = object_set_empty.get_objects("OutputObjects").segmented
expected = labels.copy()
# Objects with more than 50% contacting will be removed
expected[12:15, 0:1] = 3
# Some objects get relabeled
expected[expected == 8] = 5
expected[expected == 9] = 6
numpy.testing.assert_array_equal(actual, expected)
def test_2d_rel_neighbor_size_all(image_labels, module, object_set_empty, objects_empty, workspace_empty):
labels = image_labels.copy()
# Create an object which doesn't meet contact criteria
labels[12:15, 0:1] = 7
# Create one which does
labels[2:8, 2:4] = 8
# Create one which meets the criteria for one object but not another
labels[10:12, 12:17] = 9
labels[8:10, 14:16] = 9
objects_empty.segmented = labels
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
module.size.value = 5.
module.remove_below_threshold.value = False
# Set the minimum contact area low so all objects get merged
module.use_contact_area.value = True
module.contact_area_method.value = mergeobjects.A_ABSOLUTE
module.rel_neighbor_size.value = 0.1
module.run(workspace_empty)
actual = object_set_empty.get_objects("OutputObjects").segmented
expected = image_labels.copy()
# Have to set the weird one
expected[10:12, 12:17] = 4
expected[8:10, 14:16] = 4
numpy.testing.assert_array_equal(actual, expected)