/
test_measureobjectoverlap.py
224 lines (196 loc) · 7.44 KB
/
test_measureobjectoverlap.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
import numpy
import numpy.random
import scipy.ndimage
import cellprofiler.image
import cellprofiler.measurement
import cellprofiler.module
import cellprofiler.modules.measureobjectoverlap
import cellprofiler.object
import cellprofiler.pipeline
import cellprofiler.preferences
import cellprofiler.workspace
cellprofiler.preferences.set_headless()
GROUND_TRUTH_IMAGE_NAME = "groundtruth"
TEST_IMAGE_NAME = "test"
GROUND_TRUTH_OBJ_IMAGE_NAME = "DNA"
ID_OBJ_IMAGE_NAME = "Protein"
GROUND_TRUTH_OBJ = "Nuclei"
ID_OBJ = "Protein"
def make_obj_workspace(ground_truth_obj, id_obj, ground_truth, id):
"""make a workspace to test comparing objects"""
""" ground truth object and ID object are dictionaires w/ the following keys"""
"""i - i component of pixel coordinates
j - j component of pixel coordinates
l - label """
module = cellprofiler.modules.measureobjectoverlap.MeasureObjectOverlap()
module.set_module_num(1)
module.object_name_GT.value = GROUND_TRUTH_OBJ
module.object_name_ID.value = ID_OBJ
module.wants_emd.value = True
pipeline = cellprofiler.pipeline.Pipeline()
def callback(caller, event):
assert not isinstance(event, cellprofiler.pipeline.RunExceptionEvent)
pipeline.add_listener(callback)
pipeline.add_module(module)
image_set_list = cellprofiler.image.ImageSetList()
image_set = image_set_list.get_image_set(0)
for name, d in (
(GROUND_TRUTH_OBJ_IMAGE_NAME, ground_truth),
(ID_OBJ_IMAGE_NAME, id),
):
image = cellprofiler.image.Image(
d["image"], mask=d.get("mask"), crop_mask=d.get("crop_mask")
)
image_set.add(name, image)
object_set = cellprofiler.object.ObjectSet()
for name, d in ((GROUND_TRUTH_OBJ, ground_truth_obj), (ID_OBJ, id_obj)):
object = cellprofiler.object.Objects()
if d.shape[1] == 3:
object.ijv = d
else:
object.segmented = d
object_set.add_objects(object, name)
workspace = cellprofiler.workspace.Workspace(
pipeline,
module,
image_set,
object_set,
cellprofiler.measurement.Measurements(),
image_set_list,
)
return workspace, module
def test_get_measurement_columns():
workspace, module = make_obj_workspace(
numpy.zeros((0, 3), int),
numpy.zeros((0, 3), int),
dict(image=numpy.zeros((20, 10), bool)),
dict(image=numpy.zeros((20, 10), bool)),
)
columns = module.get_measurement_columns(workspace.pipeline)
# All columns should be unique
assert len(columns) == len(set([x[1] for x in columns]))
# All columns should be floats and done on images
x = columns[-1]
assert all([x[0] == cellprofiler.measurement.IMAGE])
assert all([x[2] == cellprofiler.measurement.COLTYPE_FLOAT])
for feature in cellprofiler.modules.measureobjectoverlap.FTR_ALL:
field = "_".join(
(
cellprofiler.modules.measureobjectoverlap.C_IMAGE_OVERLAP,
feature,
GROUND_TRUTH_OBJ,
ID_OBJ,
)
)
assert field in [x[1] for x in columns]
def test_get_measurement_scales():
workspace, module = make_obj_workspace(
numpy.zeros((0, 3), int),
numpy.zeros((0, 3), int),
dict(image=numpy.zeros((20, 10), bool)),
dict(image=numpy.zeros((20, 10), bool)),
)
scales = module.get_measurement_scales(
workspace.pipeline,
cellprofiler.measurement.IMAGE,
cellprofiler.modules.measureobjectoverlap.C_IMAGE_OVERLAP,
cellprofiler.modules.measureobjectoverlap.FTR_RAND_INDEX,
None,
)
assert len(scales) == 1
assert scales[0] == "_".join((GROUND_TRUTH_OBJ, ID_OBJ))
def test_test_measure_overlap_no_objects():
# Regression test of issue #934 - no objects
workspace, module = make_obj_workspace(
numpy.zeros((0, 3), int),
numpy.zeros((0, 3), int),
dict(image=numpy.zeros((20, 10), bool)),
dict(image=numpy.zeros((20, 10), bool)),
)
module.run(workspace)
m = workspace.measurements
for feature in cellprofiler.modules.measureobjectoverlap.FTR_ALL:
mname = module.measurement_name(feature)
value = m[cellprofiler.measurement.IMAGE, mname, 1]
if feature == cellprofiler.modules.measureobjectoverlap.FTR_TRUE_NEG_RATE:
assert value == 1
elif feature == cellprofiler.modules.measureobjectoverlap.FTR_FALSE_POS_RATE:
assert value == 0
else:
assert numpy.isnan(value), "%s was %f. not nan" % (mname, value)
#
# Make sure they don't crash
#
workspace, module = make_obj_workspace(
numpy.zeros((0, 3), int),
numpy.ones((1, 3), int),
dict(image=numpy.zeros((20, 10), bool)),
dict(image=numpy.zeros((20, 10), bool)),
)
module.run(workspace)
workspace, module = make_obj_workspace(
numpy.ones((1, 3), int),
numpy.zeros((0, 3), int),
dict(image=numpy.zeros((20, 10), bool)),
dict(image=numpy.zeros((20, 10), bool)),
)
module.run(workspace)
def test_test_measure_overlap_objects():
r = numpy.random.RandomState()
r.seed(51)
workspace, module = make_obj_workspace(
numpy.column_stack(
[r.randint(0, 20, 150), r.randint(0, 10, 150), r.randint(1, 5, 150)]
),
numpy.column_stack(
[r.randint(0, 20, 175), r.randint(0, 10, 175), r.randint(1, 5, 175)]
),
dict(image=numpy.zeros((20, 10), bool)),
dict(image=numpy.zeros((20, 10), bool)),
)
module.wants_emd.value = False
module.run(workspace)
measurements = workspace.measurements
assert isinstance(measurements, cellprofiler.measurement.Measurements)
def test_test_objects_rand_index():
r = numpy.random.RandomState()
r.seed(52)
base = numpy.zeros((100, 100), bool)
base[r.randint(0, 100, size=10), r.randint(0, 100, size=10)] = True
gt = base.copy()
gt[r.randint(0, 100, size=5), r.randint(0, 100, size=5)] = True
test = base.copy()
test[r.randint(0, 100, size=5), r.randint(0, 100, size=5)] = True
gt = scipy.ndimage.binary_dilation(gt, numpy.ones((5, 5), bool))
test = scipy.ndimage.binary_dilation(test, numpy.ones((5, 5), bool))
gt_labels, _ = scipy.ndimage.label(gt, numpy.ones((3, 3), bool))
test_labels, _ = scipy.ndimage.label(test, numpy.ones((3, 3), bool))
workspace, module = make_obj_workspace(
gt_labels,
test_labels,
dict(image=numpy.ones(gt_labels.shape)),
dict(image=numpy.ones(test_labels.shape)),
)
module.run(workspace)
measurements = workspace.measurements
mname = "_".join(
(
cellprofiler.modules.measureobjectoverlap.C_IMAGE_OVERLAP,
cellprofiler.modules.measureobjectoverlap.FTR_RAND_INDEX,
GROUND_TRUTH_OBJ,
ID_OBJ,
)
)
expected_rand_index = measurements.get_current_image_measurement(mname)
rand_index = measurements.get_current_image_measurement(mname)
assert round(abs(rand_index - expected_rand_index), 7) == 0
mname = "_".join(
(
cellprofiler.modules.measureobjectoverlap.C_IMAGE_OVERLAP,
cellprofiler.modules.measureobjectoverlap.FTR_ADJUSTED_RAND_INDEX,
GROUND_TRUTH_OBJ,
ID_OBJ,
)
)
adjusted_rand_index = measurements.get_current_image_measurement(mname)
# assertAlmostEqual(adjusted_rand_index, expected_adjusted_rand_index)