forked from CellProfiler/CellProfiler-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshollanalysis.py
325 lines (215 loc) · 7.79 KB
/
shollanalysis.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
# coding=utf-8
"""
ShollAnalysis
====================
|
============ ============ ===============
Supports 2D? Supports 3D? Respects masks?
============ ============ ===============
YES YES YES
============ ============ ===============
Measurements made by this module
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- *Branches*: Total number of pixels with more than two neighbors.
- *Endpoints*: Total number of pixels with only one neighbor.
"""
import numpy
import scipy.ndimage
import skimage.draw
import skimage.segmentation
import skimage.util
import cellprofiler.measurement
import cellprofiler.module
import cellprofiler.setting
def sholl(image, radius, step):
r_radius = radius
c_radius = radius
r, c = image.shape
centroid_r, centroid_c = r // 2, c // 2
shells = numpy.minimum(
centroid_r // (step + 1),
centroid_c // (step + 1)
)
masks = numpy.zeros((shells, r, c))
for index in range(shells):
if index == 0:
next_step = 0
previous_shell_rr, previous_shell_cc = skimage.draw.ellipse(
centroid_r,
centroid_c,
r_radius + next_step,
c_radius + next_step
)
next_step += step
shell_rr, shell_cc = skimage.draw.ellipse(
centroid_r,
centroid_c,
r_radius + next_step,
c_radius + next_step
)
masks[index, shell_rr, shell_cc] = 1
masks[index, previous_shell_rr, previous_shell_cc] = 0
neighborhoods = numpy.zeros((shells, r, c))
for index in range(shells):
neighborhoods[index] = image * masks[index]
return neighborhoods
def _neighbors(image):
"""
Counts the neighbor pixels for each pixel of an image:
x = [
[0, 1, 0],
[1, 1, 1],
[0, 1, 0]
]
_neighbors(x)
[
[0, 3, 0],
[3, 4, 3],
[0, 3, 0]
]
:type image: numpy.ndarray
:param image: A two-or-three dimensional image
:return: neighbor pixels for each pixel of an image
"""
padding = skimage.util.pad(image, 1, "constant")
mask = padding > 0
padding = padding.astype(numpy.float)
if image.ndim == 2:
response = 3 ** 2 * scipy.ndimage.uniform_filter(padding) - 1
labels = (response * mask)[1:-1, 1:-1]
return labels.astype(numpy.uint16)
elif image.ndim == 3:
response = 3 ** 3 * scipy.ndimage.uniform_filter(padding) - 1
labels = (response * mask)[1:-1, 1:-1, 1:-1]
return labels.astype(numpy.uint16)
def branches(image):
return _neighbors(image) > 2
def endpoints(image):
return _neighbors(image) == 1
class ShollAnalysis(cellprofiler.module.Module):
category = "Measurement"
module_name = "ShollAnalysis"
variable_revision_number = 1
def create_settings(self):
self.skeleton_name = cellprofiler.setting.ImageNameSubscriber(
"Select an image to measure"
)
self.radius = cellprofiler.setting.Integer(
"Radius"
)
self.step = cellprofiler.setting.Integer(
"Step"
)
def settings(self):
return [
self.skeleton_name,
self.radius,
self.step
]
def run(self, workspace):
names = []
input_image_name = self.skeleton_name.value
image_set = workspace.image_set
input_image = image_set.get_image(input_image_name, must_be_grayscale=True)
dimensions = input_image.dimensions
r_radius = self.radius.value
c_radius = self.radius.value
r, c = input_image.pixel_data.shape
centroid_r, centroid_c = r // 2, c // 2
shells = numpy.minimum(
centroid_r // (self.step.value + 1),
centroid_c // (self.step.value + 1)
)
masks = numpy.zeros((shells, r, c))
for index in range(shells):
if index == 0:
next_step = 0
previous_shell_rr, previous_shell_cc = skimage.draw.ellipse(
centroid_r,
centroid_c,
r_radius + next_step,
c_radius + next_step
)
next_step += self.step.value
shell_rr, shell_cc = skimage.draw.ellipse(
centroid_r,
centroid_c,
r_radius + next_step,
c_radius + next_step
)
masks[index, shell_rr, shell_cc] = 1
masks[index, previous_shell_rr, previous_shell_cc] = 0
neighborhoods = numpy.zeros((shells, r, c))
for index in range(shells):
neighborhoods[index] = input_image.pixel_data * masks[index]
names.append("Branches_{}".format(index))
names.append("Endpoints_{}".format(index))
self.neighborhoods = neighborhoods
statistics = self.measure(input_image, workspace)
if self.show_window:
workspace.display_data.dimensions = dimensions
workspace.display_data.names = names
workspace.display_data.statistics = statistics
def display(self, workspace, figure=None):
layout = (1, 1)
figure.set_subplots(
dimensions=workspace.display_data.dimensions,
subplots=layout
)
figure.subplot_table(
col_labels=workspace.display_data.names,
statistics=workspace.display_data.statistics,
title="Measurement",
x=0,
y=0
)
def get_categories(self, pipeline, object_name):
if object_name == cellprofiler.measurement.IMAGE:
return [
"Skeleton"
]
return []
def get_feature_name(self, name):
image = self.skeleton_name.value
return "ShollAnalysis_{}_{}".format(image, name)
def get_measurements(self, pipeline, object_name, category):
name = self.skeleton_name.value
if object_name == cellprofiler.measurement.IMAGE and category == "Skeleton":
return [
"ShollAnalysis_Branches_{}".format(name),
"ShollAnalysis_Endpoints_{}".format(name)
]
return []
def get_measurement_columns(self, pipeline):
image = cellprofiler.measurement.IMAGE
features = [
self.get_measurement_name("Branches"),
self.get_measurement_name("Endpoints")
]
column_type = cellprofiler.measurement.COLTYPE_INTEGER
return [(image, feature, column_type) for feature in features]
def get_measurement_images(self, pipeline, object_name, category, measurement):
if measurement in self.get_measurements(pipeline, object_name, category):
return [self.skeleton_name.value]
return []
def get_measurement_name(self, name):
feature = self.get_feature_name(name)
return feature
def measure(self, image, workspace):
image = image.pixel_data
data = sholl(image, self.radius.value, self.step.value)
measurements = workspace.measurements
measurement_name = self.skeleton_name.value
statistics = []
for index in range(self.neighborhoods.shape[0]):
name = "ShollAnalysis_Branches_{}_{}".format(measurement_name, index)
value = numpy.count_nonzero(branches(data))
statistics.append(value)
measurements.add_image_measurement(name, value)
name = "ShollAnalysis_Endpoints_{}_{}".format(measurement_name, index)
value = numpy.count_nonzero(endpoints(data))
statistics.append(value)
measurements.add_image_measurement(name, value)
return [statistics]
def volumetric(self):
return True