/
test_definegrid.py
186 lines (170 loc) · 7.5 KB
/
test_definegrid.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
import numpy as np
import pytest
import cellprofiler.grid
import cellprofiler.image
import cellprofiler.measurement
import cellprofiler.modules.definegrid
import cellprofiler.object
import cellprofiler.pipeline
import cellprofiler.workspace
GRID_NAME = "grid"
INPUT_IMAGE_NAME = "inputimage"
OUTPUT_IMAGE_NAME = "outputimage"
OBJECTS_NAME = "objects"
def make_workspace(image, labels):
module = cellprofiler.modules.definegrid.DefineGrid()
module.set_module_num(1)
module.grid_image.value = GRID_NAME
module.manual_image.value = INPUT_IMAGE_NAME
module.display_image_name.value = INPUT_IMAGE_NAME
module.object_name.value = OBJECTS_NAME
module.save_image_name.value = OUTPUT_IMAGE_NAME
image_set_list = cellprofiler.image.ImageSetList()
image_set = image_set_list.get_image_set(0)
image_set.add(INPUT_IMAGE_NAME, cellprofiler.image.Image(image))
object_set = cellprofiler.object.ObjectSet()
objects = cellprofiler.object.Objects()
objects.segmented = labels
object_set.add_objects(objects, OBJECTS_NAME)
pipeline = cellprofiler.pipeline.Pipeline()
def callback(caller, event):
assert not isinstance(event, cellprofiler.pipeline.LoadExceptionEvent)
assert not isinstance(event, cellprofiler.pipeline.RunExceptionEvent)
pipeline.add_listener(callback)
pipeline.add_module(module)
measurements = cellprofiler.measurement.Measurements()
workspace = cellprofiler.workspace.Workspace(
pipeline, module, image_set, object_set, measurements, image_set_list
)
return workspace, module
def test_grid_automatic():
image = np.zeros((50, 100))
labels = np.zeros((50, 100), int)
ii, jj = np.mgrid[0:50, 0:100]
#
# Make two circles at 10,11 and 40, 92
#
first_x, first_y = (11, 10)
second_x, second_y = (92, 40)
rows = 4
columns = 10
spacing_y = 10
spacing_x = 9
for i in range(rows):
for j in range(columns):
center_i = first_y + spacing_y * i
center_j = first_x + spacing_x * j
labels[(ii - center_i) ** 2 + (jj - center_j) ** 2 <= 9] = (
i * columns + j + 1
)
workspace, module = make_workspace(image, labels)
assert isinstance(module, cellprofiler.modules.definegrid.DefineGrid)
assert isinstance(workspace, cellprofiler.workspace.Workspace)
module.grid_rows.value = rows
module.grid_columns.value = columns
module.ordering.value = cellprofiler.modules.definegrid.NUM_BY_COLUMNS
module.auto_or_manual.value = cellprofiler.modules.definegrid.AM_AUTOMATIC
module.wants_image.value = True
module.run(workspace)
gridding = workspace.get_grid(GRID_NAME)
assert isinstance(gridding, cellprofiler.grid.Grid)
assert gridding.rows == rows
assert gridding.columns == columns
assert gridding.x_spacing == spacing_x
assert gridding.y_spacing == spacing_y
assert gridding.x_location_of_lowest_x_spot == first_x
assert gridding.y_location_of_lowest_y_spot == first_y
assert np.all(gridding.x_locations == first_x + np.arange(columns) * spacing_x)
assert np.all(gridding.y_locations == first_y + np.arange(rows) * spacing_y)
spot_table = np.arange(rows * columns) + 1
spot_table.shape = (rows, columns)
assert np.all(gridding.spot_table == spot_table)
m = workspace.measurements
assert isinstance(m, cellprofiler.measurement.Measurements)
for feature, value in (
(cellprofiler.modules.definegrid.F_COLUMNS, columns),
(cellprofiler.modules.definegrid.F_ROWS, rows),
(cellprofiler.modules.definegrid.F_X_LOCATION_OF_LOWEST_X_SPOT, first_x),
(cellprofiler.modules.definegrid.F_Y_LOCATION_OF_LOWEST_Y_SPOT, first_y),
(cellprofiler.modules.definegrid.F_X_SPACING, spacing_x),
(cellprofiler.modules.definegrid.F_Y_SPACING, spacing_y),
):
measurement = "_".join(
(cellprofiler.modules.definegrid.M_CATEGORY, GRID_NAME, feature)
)
assert m.has_feature(cellprofiler.measurement.IMAGE, measurement)
assert m.get_current_image_measurement(measurement) == value
image = workspace.image_set.get_image(OUTPUT_IMAGE_NAME)
assert image is not None
def test_fail():
image = np.zeros((50, 100))
labels = np.zeros((50, 100), int)
labels[20:40, 51:62] = 1
workspace, module = make_workspace(image, labels)
assert isinstance(module, cellprofiler.modules.definegrid.DefineGrid)
assert isinstance(workspace, cellprofiler.workspace.Workspace)
module.ordering.value = cellprofiler.modules.definegrid.NUM_BY_COLUMNS
module.auto_or_manual.value = cellprofiler.modules.definegrid.AM_AUTOMATIC
module.wants_image.value = True
with pytest.raises(RuntimeError):
module.run(workspace)
def test_coordinates_plus_savedimagesize():
image = np.zeros((50, 100))
labels = np.zeros((50, 100), int)
first_x, first_y = (11, 10)
second_x, second_y = (92, 40)
rows = 4
columns = 10
spacing_y = 10
spacing_x = 9
workspace, module = make_workspace(image, labels)
assert isinstance(module, cellprofiler.modules.definegrid.DefineGrid)
assert isinstance(workspace, cellprofiler.workspace.Workspace)
module.grid_rows.value = rows
module.grid_columns.value = columns
module.ordering.value = cellprofiler.modules.definegrid.NUM_BY_COLUMNS
module.auto_or_manual.value = cellprofiler.modules.definegrid.AM_MANUAL
module.manual_choice.value = cellprofiler.modules.definegrid.MAN_COORDINATES
module.first_spot_coordinates.value = "%d,%d" % (first_x, first_y)
module.second_spot_coordinates.value = "%d,%d" % (second_x, second_y)
module.first_spot_col.value = 1
module.first_spot_row.value = 1
module.second_spot_col.value = columns
module.second_spot_row.value = rows
module.grid_rows.value = rows
module.grid_columns.value = columns
module.wants_image.value = True
module.run(workspace)
gridding = workspace.get_grid(GRID_NAME)
assert isinstance(gridding, cellprofiler.grid.Grid)
assert gridding.rows == rows
assert gridding.columns == columns
assert gridding.x_spacing == spacing_x
assert gridding.y_spacing == spacing_y
assert gridding.x_location_of_lowest_x_spot == first_x
assert gridding.y_location_of_lowest_y_spot == first_y
assert np.all(gridding.x_locations == first_x + np.arange(columns) * spacing_x)
assert np.all(gridding.y_locations == first_y + np.arange(rows) * spacing_y)
spot_table = np.arange(rows * columns) + 1
spot_table.shape = (rows, columns)
assert np.all(gridding.spot_table == spot_table)
m = workspace.measurements
assert isinstance(m, cellprofiler.measurement.Measurements)
for feature, value in (
(cellprofiler.modules.definegrid.F_COLUMNS, columns),
(cellprofiler.modules.definegrid.F_ROWS, rows),
(cellprofiler.modules.definegrid.F_X_LOCATION_OF_LOWEST_X_SPOT, first_x),
(cellprofiler.modules.definegrid.F_Y_LOCATION_OF_LOWEST_Y_SPOT, first_y),
(cellprofiler.modules.definegrid.F_X_SPACING, spacing_x),
(cellprofiler.modules.definegrid.F_Y_SPACING, spacing_y),
):
measurement = "_".join(
(cellprofiler.modules.definegrid.M_CATEGORY, GRID_NAME, feature)
)
assert m.has_feature(cellprofiler.measurement.IMAGE, measurement)
assert m.get_current_image_measurement(measurement) == value
image = workspace.image_set.get_image(OUTPUT_IMAGE_NAME)
assert image is not None
shape = image.pixel_data.shape
assert shape[0] == 50
assert shape[1] == 100