-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBioImage.py
executable file
·326 lines (284 loc) · 11.1 KB
/
BioImage.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
from PIL import Image
import skimage
from skimage import data, color, img_as_ubyte
from matplotlib import pyplot as plt
import pickle
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from random import randint
import gaussfitter
from scipy import ndimage
from scipy import ndimage as ndi
from skimage.morphology import extrema
from skimage import exposure
from skimage.measure import label
from skimage.morphology import reconstruction, binary_closing
from scipy.ndimage.morphology import binary_fill_holes
from skimage.filters import threshold_isodata
from skimage.measure import perimeter
import os
#folders = ['Y_2_converted', 'Y_3_converted', 'Y_4_converted','O_1_converted', 'O_2_converted']
folders = ['old', 'young']
channel = {1:'B/W', 2:'CD63', 3: 'PanEV', 4: 'alt KL', 6: 'SSC', 7:'cd81', 9: 'B/W', 11:'WT KL'}
class BioImage:
def __init__(self, folder, num, root = '../Amrita_29feb'):
base_folder = root
pickle_folder = base_folder + folder + '_pickle/'
header_pickle= pickle_folder + folder + '__' + str(num) + '.p'
if not os.path.exists(pickle_folder):
os.mkdir(pickle_folder)
self.name = folder + ' : ' + str(num)
self.mask = None
try:
self.image = pickle.load(open(header_pickle, 'rb'))
self.ch1 = self.image['Ch1 - B/W']
self.ch2 = self.image['Ch2 - CD63']
self.ch3 = self.image['Ch3 - PanEV']
try:
self.ch4 = self.image['Ch4 - ']
except:
pass
self.ch6 = self.image['Ch6 - Side Scatter']
self.ch7 = self.image['Ch7 - CD81']
self.ch9 = self.image['Ch9 - B/W']
self.ch11 = self.image['Ch11 - CD9']
except:
header = base_folder + folder + '_np/' + folder + '__' + str(num) + '_'
self.ch1 = Image.open(header + 'Ch1.ome.tif')
self.ch2 = Image.open(header + 'Ch2.ome.tif')
self.ch3 = Image.open(header + 'Ch3.ome.tif')
try:
self.ch4 = Image.open(header + 'Ch4.ome.tif')
except:
pass
self.ch6 = Image.open(header + 'Ch6.ome.tif')
self.ch7 = Image.open(header + 'Ch7.ome.tif')
self.ch9 = Image.open(header + 'Ch9.ome.tif')
self.ch11 = Image.open(header +'Ch11.ome.tif')
# Convert to nparray and normalize to 4000:
self.ch1 = np.array(self.ch1)/4000.0
self.ch2 = np.array(self.ch2)/4000.0
self.ch3 = np.array(self.ch3)/4000.0
try:
self.ch4 = np.array(self.ch4)/4000.0
except:
pass
self.ch6 = np.array(self.ch6)/4000.0
self.ch7 = np.array(self.ch7)/4000.0
self.ch9 = np.array(self.ch9)/4000.0
self.ch11 = np.array(self.ch11)/4000.0
try:
self.image = {'Ch' + str(1 )+ channel[1]:self.ch1,
'Ch' + str(9 )+ channel[9 ]:self.ch9,
'Ch' + str(6 )+ channel[6 ]:self.ch6,
'Ch' + str(2 )+ channel[2 ]:self.ch2,
'Ch' + str(3 )+ channel[3 ]:self.ch3,
'Ch' + str(4 )+ channel[4 ]:self.ch4,
'Ch' + str(7 )+ channel[7 ]:self.ch7,
'Ch' + str(11)+ channel[11]:self.ch11}
except:
self.image = {'Ch' + str(1 )+ channel[1]:self.ch1,
'Ch' + str(9 )+ channel[9 ]:self.ch9,
'Ch' + str(6 )+ channel[6 ]:self.ch6,
'Ch' + str(2 )+ channel[2 ]:self.ch2,
'Ch' + str(3 )+ channel[3 ]:self.ch3,
'Ch' + str(7 )+ channel[7 ]:self.ch7,
'Ch' + str(11)+ channel[11]:self.ch11}
pickle.dump(self.image, open(header_pickle,'wb'))
def showImage(self):
plt.figure()
plt.suptitle(self.name)
i = 1
for ch_name, image in self.image.items():
plt.subplot(2,4,i)
i = i + 1
plt.imshow(image, cmap='magma')
plt.title(ch_name)
plt.axis('off')
plt.show()
def showPipeline(self):
self.showImage();
self.show3D(self.ch1)
self.show3D(self.ch6)
self.showMaxima(self.ch1)
self.showMaxima(self.ch6)
return
def show3D(self, im):
fig = plt.figure()
ax = fig.gca(projection='3d')
xx, yy = np.mgrid[0:im.shape[0], 0:im.shape[1]]
ax.plot_surface(xx, yy, im, rstride=1, cstride=1, cmap='magma', linewidth=0)
plt.axis('on')
plt.tight_layout()
plt.show()
return
def showMaxima(self, im):
fig = plt.figure()
plt.imshow(im)
im = self.runHistogramAbs(im)
im = ndimage.gaussian_filter(im, sigma=1)
im = exposure.rescale_intensity(im)
h = 0.2
h_maxima = extrema.h_maxima(im, h)
label_h_maxima = label(h_maxima)
img = skimage.exposure.rescale_intensity(im)
overlay_h = color.label2rgb(label_h_maxima, img, alpha=0.7,
bg_label=0,
bg_color=None)
plt.imshow(overlay_h)
plt.show()
return
def getExtrema(self):
Extrema = np.array([0,0,0])
img = self.ch1
image_gray = img
image_rgb = img
im = self.runHistogramAbs(img)
# im = ndimage.gaussian_filter(im, sigma=0.7)
# im = exposure.rescale_intensity(im)
im = ndimage.gaussian_filter(im, sigma=1)
im = exposure.rescale_intensity(im)
h = 0.2
h_maxima = extrema.h_maxima(im, h)
label_h_maxima = label(h_maxima)
Extrema[0] = np.count_nonzero(label_h_maxima)
#check if the extrema is right next to the edge
if Extrema[0]==1:
indices = np.nonzero(h_maxima)
distance = im.shape[1] - indices[1]
Extrema[2] = distance[0]
img = self.ch6
image_gray = img
image_rgb = img
im = self.runHistogramAbs(img)
# im = ndimage.gaussian_filter(im, sigma=0.7)
im = ndimage.gaussian_filter(im, sigma=1)
im = exposure.rescale_intensity(im)
h = 0.2
h_maxima = extrema.h_maxima(im, h)
label_h_maxima = label(h_maxima)
Extrema[1] = np.count_nonzero(label_h_maxima)
params = self.getParams()
Extrema = np.append(Extrema, params)
return Extrema
def getFeatures(self):
features = []
# Preprocess the ch1 image -- let's just try an abs
img = self.ch1
hist,bins = np.histogram(img.ravel(),256,[0,np.max(img)])
#plt.hist(img.ravel(),256,[0,np.max(img)]); plt.show()
zero = np.argmax(hist)
img = img - bins[zero]
ch1 = np.abs(img)
ch1 = ndimage.gaussian_filter(ch1, sigma=2)
ch6 = self.ch6
ch6 = ndimage.gaussian_filter(ch6, sigma=2)
features = np.concatenate([features, gaussfitter.gaussfit(ch1)])
features = np.concatenate([features, gaussfitter.gaussfit(ch6)])
return features
def runHistogramAbs(self, img):
hist,bins = np.histogram(img.ravel(),256,[0,np.max(img)])
zero = np.argmax(hist)
img = img - bins[zero]
im = np.abs(img)
# im[im<0] = 0
# im = im/np.max(im)
return im
def runHistogram(self, img):
hist,bins = np.histogram(img.ravel(),256,[0,np.max(img)])
zero = np.argmax(hist)
img = img - bins[zero]
# im = np.abs(img)
img[img<0] = 0
# im = img/np.max(img)
return img
def getThresholded(self, img, abs=True):
if abs:
im = self.runHistogramAbs(img)
else:
im =self.runHistogram(img)
try:
threshold = threshold_isodata(im)
except:
return img
im = im > threshold
filled = binary_fill_holes(im).astype(int)
mask = binary_closing(filled)
return mask
def getCh2Activation(self):
ch1 = self.ch1
ch1 = self.getThresholded(ch1)
ch2 = self.ch2
ch2 = self.getThresholded(ch2,False)
img = np.multiply(ch1,ch2)
return np.count_nonzero(img)>0
def getCh4Activation(self):
ch1 = self.ch1
ch1 = self.getThresholded(ch1)
ch4 = self.ch4
h = 0.08
h_maxima = extrema.h_maxima(ch4, h)
label_h_maxima = np.multiply(ch1, label(h_maxima))
return np.count_nonzero(label_h_maxima)>0
def getCh7Activation(self):
ch1 = self.ch1
ch1 = self.getThresholded(ch1)
ch2 = self.ch7
ch2 = self.runHistogram(ch2)
ch2 = ndi.gaussian_filter(ch2, sigma=3)
h = 0.08
h_maxima = extrema.h_maxima(ch2, h)
label_h_maxima = np.multiply(ch1, label(h_maxima))
return np.count_nonzero(label_h_maxima)>0
def getCh11Activation(self):
ch1 = self.ch1
ch1 = self.getThresholded(ch1)
ch2 = self.ch11
ch2 = self.runHistogram(ch2)
ch2 = ndi.gaussian_filter(ch2, sigma=3)
h = 0.08
h_maxima = extrema.h_maxima(ch2, h)
label_h_maxima = np.multiply(ch1, label(h_maxima))
return np.count_nonzero(label_h_maxima)>0
def getActivations(self):
activations = np.array([False,False,False])
activations[0] = self.getCh2Activation()
activations[1] = self.getCh7Activation()
activations[2] = self.getCh11Activation()
return activations
def getActivationSize(self, ch):
ch = self.getThresholded(ch, False)
return np.count_nonzero(ch)
def getActivationSizes(self):
return np.array([self.getActivationSize(self.ch2),
self.getActivationSize(self.ch7),
self.getActivationSize(self.ch11)])
def getParams(self):
img = self.ch1
hist,bins = np.histogram(img.ravel(),256,[0,np.max(img)])
#plt.hist(img.ravel(),256,[0,np.max(img)]); plt.show()
zero = np.argmax(hist)
img = img - bins[zero]
im = np.abs(img)
threshold = threshold_isodata(im)
im = im > threshold
filled = binary_fill_holes(im).astype(int)
filled = binary_closing(filled)
A1 = np.count_nonzero(filled)
P1 = perimeter(filled, neighbourhood=16)
C1 = 4*3.14*A1/(P1*P1)
img = self.ch6
hist,bins = np.histogram(img.ravel(),256,[0,np.max(img)])
#plt.hist(img.ravel(),256,[0,np.max(img)]); plt.show()
zero = np.argmax(hist)
img = img - bins[zero]
im = np.abs(img)
threshold = threshold_isodata(im)
im = im > threshold
filled = binary_fill_holes(im).astype(int)
filled = binary_closing(filled)
A2 = np.count_nonzero(filled)
P2 = perimeter(filled, neighbourhood=16)
C2 = 4*3.14*A2/(P2*P2)
params = np.array([A1,P1,C1,A2,P2,C2])
return params