forked from CellProfiler/CellProfiler-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompensatecolors.py
288 lines (228 loc) · 10.2 KB
/
compensatecolors.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
# coding=utf-8
#################################
#
# Imports from useful Python libraries
#
#################################
import numpy
#################################
#
# Imports from CellProfiler
#
##################################
import cellprofiler.image
import cellprofiler.module
import cellprofiler.setting
__doc__ = """\
CompensateColors
================
**CompensateColors** is a module to deconvolve spectral overlap between two sets
of images; optionally, this can be done within an object set.
|
============ ============ ===============
Supports 2D? Supports 3D? Respects masks?
============ ============ ===============
YES YES YES
============ ============ ===============
See also
^^^^^^^^
Is there another **Module** that is related to this one? If so, refer
to that **Module** in this section. Otherwise, this section can be omitted.
What do I need as input?
^^^^^^^^^^^^^^^^^^^^^^^^
Two sets of images you want to remove spectral overlap from.
What do I get as output?
^^^^^^^^^^^^^^^^^^^^^^^^
An equal number of images which have been treated with color compensation.
As this module can end up running many dozens of images, the output images
will be named by the input name + a user designated suffix, rather than
manual assignment of each name.
Technical notes
^^^^^^^^^^^^^^^
Include implementation details or notes here. Additionally provide any
other background information about this module, including definitions
or adopted conventions. Information which may be too specific to fit into
the general description should be provided here.
Omit this section if there is no technical information to mention.
References
^^^^^^^^^^
Provide citations here, if appropriate. Citations are formatted as a list and,
wherever possible, include a link to the original work. For example,
- Meyer F, Beucher S (1990) “Morphological segmentation.” *J Visual
Communication and Image Representation* 1, 21-46.
(`link <http://dx.doi.org/10.1016/1047-3203(90)90014-M>`__)
"""
COMPENSATE_SUFFIX = "Compensated"
CC_IMAGES = "Across entire image"
CC_OBJECTS = "Within objects"
class CompensateColors(cellprofiler.module.ImageProcessing):
module_name = "CompensateColors"
variable_revision_number = 1
def create_settings(self):
self.image_groups = []
self.add_image(can_delete=False)
self.spacer_1 = cellprofiler.setting.Divider()
self.add_image(can_delete=False)
self.spacer_2 = cellprofiler.setting.Divider()
self.add_image_button = cellprofiler.setting.DoSomething("", 'Add another image', self.add_image)
self.images_or_objects = cellprofiler.setting.Choice(
'Select where to perform color compensation',
[
CC_IMAGES,
CC_OBJECTS
],
doc="""\
You can measure the correlation in several ways:
- *%(CC_OBJECTS)s:* Measure correlation only in those pixels previously
identified as within an object. You will be asked to choose which object
type to measure within.
- *%(CC_IMAGES)s:* Measure the correlation across all pixels in the
images.
All methods measure correlation on a pixel by pixel basis.
""" % globals()
)
self.object_groups = []
self.add_object(can_delete=False)
self.object_count = cellprofiler.setting.HiddenCount(self.object_groups)
self.image_count = cellprofiler.setting.HiddenCount(self.image_groups)
def add_image(self, can_delete=True):
"""Add an image to the image_groups collection
can_delete - set this to False to keep from showing the "remove"
button for images that must be present.
"""
group = cellprofiler.setting.SettingsGroup()
if can_delete:
group.append("divider", cellprofiler.setting.Divider(line=False))
group.append(
"image_name",
cellprofiler.setting.ImageNameSubscriber(
'Select an image to measure',
cellprofiler.setting.NONE,
doc='Select an image to measure the correlation/colocalization in.'
)
)
group.append(
"class_num",
cellprofiler.setting.Integer(
'What compensation class does this image belong to?',
1
)
)
group.append(
"output_name",
cellprofiler.setting.ImageNameProvider(
'Select an output image name',
"None"
)
)
if len(self.image_groups) == 0: # Insert space between 1st two images for aesthetics
group.append("extra_divider", cellprofiler.setting.Divider(line=False))
if can_delete:
group.append("remover", cellprofiler.setting.RemoveSettingButton("", "Remove this image", self.image_groups, group))
self.image_groups.append(group)
def add_object(self, can_delete=True):
"""Add an object to the object_groups collection"""
group = cellprofiler.setting.SettingsGroup()
if can_delete:
group.append("divider", cellprofiler.setting.Divider(line=False))
group.append(
"object_name",
cellprofiler.setting.ObjectNameSubscriber(
'Select an object to perform compensation within',
cellprofiler.setting.NONE,
doc="""\
Select the objects to perform compensation within."""
)
)
if can_delete:
group.append("remover", cellprofiler.setting.RemoveSettingButton('', 'Remove this object', self.object_groups, group))
self.object_groups.append(group)
def settings(self):
"""Return the settings to be saved in the pipeline"""
result = [self.image_count, self.object_count]
for image_group in self.image_groups:
result += [image_group.image_name, image_group.class_num, image_group.output_name]
result += [self.images_or_objects]
result += [object_group.object_name for object_group in self.object_groups]
return result
def prepare_settings(self, setting_values):
"""Make sure there are the right number of image and object slots for the incoming settings"""
image_count = int(setting_values[0])
object_count = int(setting_values[1])
del self.image_groups[image_count:]
while len(self.image_groups) < image_count:
self.add_image()
del self.object_groups[object_count:]
while len(self.object_groups) < object_count:
self.add_object()
def visible_settings(self):
result = []
for image_group in self.image_groups:
result += image_group.visible_settings()
#result += [image_group.image_name, image_group.class_num, image_group.output_name]
#if image_group.can_delete:
#result += [image_group.remover]
result += [self.add_image_button, self.spacer_2, self.images_or_objects]
if self.images_or_objects == CC_OBJECTS:
for object_group in self.object_groups:
result += object_group.visible_settings()
return result
def run(self, workspace):
#so far this seems to work best with first masking to objects, then doing 2x2 (A and C, G and T)
imdict={}
sample_image = workspace.image_set.get_image(self.image_groups[0].image_name.value)
sample_pixels = sample_image.pixel_data
sample_shape = sample_pixels.shape
if self.images_or_objects.value == CC_OBJECTS:
object_name = self.object_groups[0]
objects = workspace.object_set.get_objects(object_name.object_name.value)
object_labels = objects.segmented
object_mask = numpy.where(object_labels > 0, 1, 0)
else:
object_mask = numpy.ones_like(sample_pixels)
for eachgroup in self.image_groups:
eachimage = workspace.image_set.get_image(eachgroup.image_name.value).pixel_data
eachimage = eachimage * object_mask
eachimage = numpy.where(eachimage == 0, (1.0/65535), eachimage)
eachimage = eachimage * 65535
if eachgroup.class_num.value not in imdict.keys():
imdict[eachgroup.class_num.value] = [[eachgroup.image_name.value],eachimage.reshape(-1),[eachgroup.output_name.value]]
else:
imdict[eachgroup.class_num.value][0].append(eachgroup.image_name.value)
imdict[eachgroup.class_num.value][1]=numpy.concatenate((imdict[eachgroup.class_num.value][1],eachimage.reshape(-1)))
imdict[eachgroup.class_num.value][2].append(eachgroup.output_name.value)
keys=imdict.keys()
keys.sort()
imlist=[]
for eachkey in keys:
imlist.append(imdict[eachkey][1])
X = numpy.array(imlist)
X = X.T
M = self.get_medians(X).T
M = M / M.sum(axis=0)
W = numpy.linalg.inv(M)
Y = W.dot(X.T).astype(int)
for eachdim in range(Y.shape[0]):
key=keys[eachdim]
im_out=Y[eachdim].reshape(len(imdict[key][0]),sample_shape[0],sample_shape[1])
im_out = im_out / 65535.
for each_im in range(len(imdict[key][0])):
im_out[each_im] = numpy.where(im_out[each_im] < 0, 0, im_out[each_im])
im_out[each_im] = numpy.where(im_out[each_im] > 1, 1, im_out[each_im])
output_image = cellprofiler.image.Image(im_out[each_im],
parent_image=workspace.image_set.get_image(imdict[key][0][each_im]))
workspace.image_set.add(imdict[key][2][each_im], output_image)
#
# "volumetric" indicates whether or not this module supports 3D images.
# The "gradient_image" function is inherently 2D, and we've noted this
# in the documentation for the module. Explicitly return False here
# to indicate that 3D images are not supported.
#
def volumetric(self):
return False
def get_medians(self, X):
arr = []
for i in range(X.shape[1]):
arr += [numpy.median(X[X.argmax(axis=1) == i], axis=0)]
M = numpy.array(arr)
return M