forked from CellProfiler/CellProfiler-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_constrainobjects.py
244 lines (167 loc) · 6.93 KB
/
test_constrainobjects.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
import numpy
import numpy.random
import numpy.testing
import pytest
import cellprofiler.object
import constrainobjects
instance = constrainobjects.ConstrainObjects
@pytest.fixture(scope="function")
def reference_empty():
obj = cellprofiler.object.Objects()
return obj
@pytest.fixture(scope="function")
def object_set_empty(objects_empty, reference_empty):
objects_set = cellprofiler.object.ObjectSet()
objects_set.add_objects(objects_empty, "InputObjects")
objects_set.add_objects(reference_empty, "ReferenceObjects")
return objects_set
@pytest.fixture(scope="function")
def image_labels():
labels = numpy.zeros((20, 20), dtype=numpy.uint16)
# Make some child objects
labels[2:7, 2:7] = 1
labels[2:7, 14:19] = 2
labels[14:19, 2:7] = 3
labels[14:20, 14:20] = 4
# One protruding object
labels[9:13, 9:13] = 5
return labels
@pytest.fixture(scope="function")
def image_reference():
labels = numpy.zeros((20, 20), dtype=numpy.uint16)
# Make some parent objects
labels[1:8, 1:8] = 1
labels[1:8, 13:20] = 2
labels[13:20, 1:8] = 3
labels[10:18, 10:18] = 4
return labels
def test_2d_ignore(image_labels, image_reference, module, object_set_empty, objects_empty,
reference_empty, workspace_empty):
labels = image_labels.copy()
reference = image_reference.copy()
objects_empty.segmented = labels
reference_empty.segmented = reference
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
module.reference_name.value = "ReferenceObjects"
module.run(workspace_empty)
actual = object_set_empty.get_objects("OutputObjects").segmented
expected = labels.copy()
# The piece of object 4 outside of the parent should be removed
# and object 5 should be ignored
expected[14:20, 18:20] = 0
expected[18:20, 14:20] = 0
numpy.testing.assert_array_equal(actual, expected)
def test_2d_remove_protrude(image_labels, image_reference, module, object_set_empty, objects_empty,
reference_empty, workspace_empty):
labels = image_labels.copy()
reference = image_reference.copy()
objects_empty.segmented = labels
reference_empty.segmented = reference
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
module.reference_name.value = "ReferenceObjects"
module.coersion_method.value = constrainobjects.METHOD_REMOVE
module.run(workspace_empty)
actual = object_set_empty.get_objects("OutputObjects").segmented
expected = labels.copy()
# The piece of object 4 outside of the parent should be removed
# and the part of object 5 inside object 4 should be removed
expected[14:20, 18:20] = 0
expected[18:20, 14:20] = 0
expected[10:13, 10:13] = 0
numpy.testing.assert_array_equal(actual, expected)
def test_2d_remove_orphans(image_labels, image_reference, module, object_set_empty, objects_empty,
reference_empty, workspace_empty):
labels = image_labels.copy()
reference = image_reference.copy()
objects_empty.segmented = labels
reference_empty.segmented = reference
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
module.reference_name.value = "ReferenceObjects"
module.remove_orphans.value = True
module.run(workspace_empty)
actual = object_set_empty.get_objects("OutputObjects").segmented
expected = labels.copy()
# The piece of object 4 outside of the parent should be removed
expected[14:20, 18:20] = 0
expected[18:20, 14:20] = 0
# Object 5 should be removed altogether
expected[expected == 5] = 0
numpy.testing.assert_array_equal(actual, expected)
@pytest.fixture(scope="function")
def volume_labels():
labels = numpy.zeros((10, 20, 20), dtype=numpy.uint16)
# Make some child objects
labels[2:7, 2:7, 2:7] = 1
labels[2:5, 2:7, 14:19] = 2
labels[5:9, 14:19, 2:7] = 3
labels[3:8, 14:20, 14:20] = 4
# One protruding object
labels[0:9, 9:13, 9:13] = 5
return labels
@pytest.fixture(scope="function")
def volume_reference():
labels = numpy.zeros((10, 20, 20), dtype=numpy.uint16)
# Make some parent objects
labels[2:7, 1:8, 1:8] = 1
labels[2:5, 1:8, 13:20] = 2
labels[5:9, 13:20, 1:8] = 3
labels[3:8, 10:18, 10:18] = 4
return labels
def test_3d_ignore(volume_labels, volume_reference, module, object_set_empty, objects_empty,
reference_empty, workspace_empty):
labels = volume_labels.copy()
reference = volume_reference.copy()
objects_empty.segmented = labels
reference_empty.segmented = reference
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
module.reference_name.value = "ReferenceObjects"
module.run(workspace_empty)
actual = object_set_empty.get_objects("OutputObjects").segmented
expected = labels.copy()
# The piece of object 4 outside of the parent should be removed
# and object 5 should be ignored
expected[3:8, 14:20, 18:20] = 0
expected[3:8, 18:20, 14:20] = 0
numpy.testing.assert_array_equal(actual, expected)
def test_3d_remove_protrude(volume_labels, volume_reference, module, object_set_empty, objects_empty,
reference_empty, workspace_empty):
labels = volume_labels.copy()
reference = volume_reference.copy()
objects_empty.segmented = labels
reference_empty.segmented = reference
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
module.reference_name.value = "ReferenceObjects"
module.coersion_method.value = constrainobjects.METHOD_REMOVE
module.run(workspace_empty)
actual = object_set_empty.get_objects("OutputObjects").segmented
expected = labels.copy()
# The piece of object 4 outside of the parent should be removed
# and the part of object 5 inside object 4 should be removed
expected[3:8, 14:20, 18:20] = 0
expected[3:8, 18:20, 14:20] = 0
expected[3:8, 10:13, 10:13] = 0
numpy.testing.assert_array_equal(actual, expected)
def test_3d_remove_orphans(volume_labels, volume_reference, module, object_set_empty, objects_empty,
reference_empty, workspace_empty):
labels = volume_labels.copy()
reference = volume_reference.copy()
objects_empty.segmented = labels
reference_empty.segmented = reference
module.x_name.value = "InputObjects"
module.y_name.value = "OutputObjects"
module.reference_name.value = "ReferenceObjects"
module.remove_orphans.value = True
module.run(workspace_empty)
actual = object_set_empty.get_objects("OutputObjects").segmented
expected = labels.copy()
# The piece of object 4 outside of the parent should be removed
expected[3:8, 14:20, 18:20] = 0
expected[3:8, 18:20, 14:20] = 0
# Object 5 should be removed altogether
expected[expected == 5] = 0
numpy.testing.assert_array_equal(actual, expected)