-
Notifications
You must be signed in to change notification settings - Fork 2
/
mainQT5.py
477 lines (422 loc) · 31 KB
/
mainQT5.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#!/usr/bin/python3
import matplotlib
matplotlib.use("Qt5Agg", force = True)
from matplotlib.pyplot import *
from skimage.exposure import adjust_gamma
from skimage.color import rgb2gray
from skimage.util import dtype
import PIL.ImageOps
from shapely.geometry import Point
from shapely.geometry.polygon import Polygon
style.use('ggplot')
from sys import argv
import pickle
import glob
from PyQt5 import QtCore, QtGui, uic, QtWidgets
from skimage.transform import rescale, resize, downscale_local_mean
#from PyQt4.QtCore import *
#from PyQt4.QtGui import *
from ast import literal_eval as make_tuple
import re
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import (
FigureCanvasQTAgg as FigureCanvas,
NavigationToolbar2QT as NavigationToolbar)
from skimage.feature import blob_dog, blob_log
from skimage import io
from math import sqrt
from collections import Counter
from skimage import exposure
from skimage import transform
from numpy import array, transpose, vstack, hstack
from numpy import sum, angle, nan_to_num
from pandas import DataFrame, read_csv, concat
from skimage.feature import corner_harris, corner_subpix, corner_peaks
from skimage.transform import warp, AffineTransform
from matplotlib.lines import Line2D
import matplotlib
import imageio
from skimage.filters import median
#matplotlib.style.use('ggplot')
#mpl.rcParams[''] = 2
#form_class = uic.loadUiType("C:\Users\thiag\Documents\GitHub\RapID-cell-counter\bycells2.ui")[0]
form_class = uic.loadUiType("bycells2v2.ui")[0]
#form_class2 = uic.loadUiType("bycells_classwindow.ui")[0]
class MainWindow(QtWidgets.QMainWindow, form_class):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent)
self.setupUi(self)
self.cellpoints = np.array([])
self.FindCells.clicked.connect(self.Id_cells)
self.AddClassified.clicked.connect(self.create_csv)
self.imageviewbutton.clicked.connect(self.openMainFig)
self.numLayers.valueChanged.connect(self.redrawLayers)
#self.maxSigSpin.valueChanged.connect(self.Id_cells)
#self.minSigSpin.valueChanged.connect(self.Id_cells)
#self.log_overlap.valueChanged.connect(self.Id_cells)
#self.thresholdSpin.valueChanged.connect(self.Id_cells)
self.cropsize = 25
self.fig = Figure()
self.THEimage = np.array([])
self.BLUEimage = 0
self.BLUEblobs = np.array([])
self.REDimage = 0
self.GREENimage = 0
self.THEblobs = np.array([])
self.table.setColumnCount(5)
self.layout.addWidget(self.table, 1, 0)
self.table.setHorizontalHeaderLabels(['Layer', 'Fluorescent cell count', 'Area', 'Nuclei count', 'Fluorescent fraction' ])# , 'Density' at pos 3
for num, layer in enumerate( [str(x+1) for x in range(int(self.numLayers.text()))] + ['Total selected reg', 'Total image']):
self.table.insertRow(num)
self.table.setItem(num , 0, QtWidgets.QTableWidgetItem(layer))
self.table.setItem(num , 1, QtWidgets.QTableWidgetItem("0"))
self.table.setItem(num , 2, QtWidgets.QTableWidgetItem("0"))
self.table.setItem(num , 3, QtWidgets.QTableWidgetItem("0"))
self.table.setItem(num , 4, QtWidgets.QTableWidgetItem("0"))
#self.table.setItem(num , 5, QtWidgets.QTableWidgetItem("0"))
self.directory = 'singleCells/'
self.guidePoints = {'TR': 0, 'TL' : 0, 'BL' : 0, 'BR': 0}
self.innergridRight = [(self.guidePoints['TR']*i+ self.guidePoints['BR']*(int(self.numLayers.text())-i))/int(self.numLayers.text()) for i in range(1,int(self.numLayers.text()) +1)]
self.innergridLeft = [(self.guidePoints['TL']*i+ self.guidePoints['BL']*(int(self.numLayers.text())-i))/int(self.numLayers.text()) for i in range(1,int(self.numLayers.text()) +1)]
self.polygonList = []
self.bigpoligon = 0
self.figname = 0
self.imgPolygon =0
self.orange_rgb_representation = np.array([93.6, 37.8, 0])/255
#self.saveDir.setText('singleCells/')
def openDIRwindow(self):
dirwindow = allDirectoriesWindow(self)
dirwindow.exec_()
def removeCell(self, cellnumber):
self.THEblobs[cellnumber:-1] = self.THEblobs[cellnumber+1:]
self.THEblobs = self.THEblobs[:-1]
self.ImgAddPatches()
def chooseDirectory(self):
directory = QtWidgets.QFileDialog.getExistingDirectory(self)
self.saveDir.setText(str(directory) + '/')
self.DatabaseSize.setText(str( len(glob.glob(str(self.saveDir.text())+ '*.png') ) ) )
def extract_specific_color_from_image(self,figure2transform, color_rep_rgb):
rgb_temp = dtype.img_as_float(figure2transform, force_copy=True) + 2
reshaped_rgb = np.reshape(-np.log(rgb_temp), (-1, 3))
single_stain = -np.dot(reshaped_rgb, color_rep_rgb)
single_stain = 255*(single_stain - single_stain.min())/ (single_stain.max() - single_stain.min())
return np.reshape(single_stain, (rgb_temp.shape[0], rgb_temp.shape[1]))
def openMainFig(self):
if self.THEimage.any() == True:
self.rmmpl()
self.THEimage = np.array([])
self.BLUEimage = 0
while self.table.rowCount() < int(self.numLayers.text())+2: self.table.insertRow(0)
while self.table.rowCount() > int(self.numLayers.text())+2: self.table.removeRow(0)
for num, layer in enumerate([str(x+1) for x in range(int(self.numLayers.text()))] + ['Total selected reg', 'Total image']):
self.table.setItem(num , 0, QtWidgets.QTableWidgetItem(layer))
self.table.setItem(num , 1, QtWidgets.QTableWidgetItem("0"))
self.table.setItem(num , 2, QtWidgets.QTableWidgetItem("0"))
self.table.setItem(num , 3, QtWidgets.QTableWidgetItem("0"))
self.table.setItem(num , 4, QtWidgets.QTableWidgetItem("0"))
#self.table.setItem(num , 5, QtWidgets.QTableWidgetItem("0"))
self.directory = 'singleCells/'
self.guidePoints = {'TR': 0, 'TL' : 0, 'BL' : 0, 'BR': 0}
self.innergridRight = [(self.guidePoints['TR']*i+ self.guidePoints['BR']*(int(self.numLayers.text())-i))/int(self.numLayers.text()) for i in range(1,int(self.numLayers.text()) +1)]
self.innergridLeft = [(self.guidePoints['TL']*i+ self.guidePoints['BL']*(int(self.numLayers.text())-i))/int(self.numLayers.text()) for i in range(1,int(self.numLayers.text()) +1)]
self.polygonList = []
self.bigpoligon = 0
self.nMarkedCells.setText(str(0))
self.THEblobs = np.array([])
name = QtWidgets.QFileDialog.getOpenFileName(self, 'Single File', '~/Desktop/', "Image files (*.jpg *.png *.tif *.tiff)")
self.figname = str(name[0])
image = imageio.imread(self.figname)
### if image is too large we will resize it
if image.shape[0] > 2000 or image.shape[1] > 2000:
if image.shape[0] > image.shape[1]: resize_factor = 2000/image.shape[0]
else:resize_factor = 2000/image.shape[1]
image = resize(image, (int(image.shape[0] * resize_factor), int(image.shape[1] * resize_factor)),anti_aliasing=True)
#self.saveNames.setText(str(name).split("/")[-1][:-4] + 'i')
#image = median(image)
self.THEimage = image
self.imgPolygon = Polygon([[0,0],[0,image.shape[1]],[image.shape[0],image.shape[1]],[image.shape[0],0]] )
self.BLUEimage = image[:,:,2]
#self.BLUEblobs = blob_log(self.BLUEimage[self.cropsize:-self.cropsize,self.cropsize:-self.cropsize], max_sigma=int(self.maxSigSpin.text()), num_sigma=10, min_sigma = int(self.minSigSpin.text()),overlap = float(self.log_overlap.text()) ,threshold=float(self.thresholdSpin.text()))
self.REDimage = image[:,:,0]
self.GREENimage = image[:,:,1]
self.ORANGEimage = self.extract_specific_color_from_image(image, self.orange_rgb_representation)
baseimage = self.fig.add_subplot(111)
#baseimage.axis('off', frameon=False)
#baseimage.grid(False)
#baseimage.imshow(image)
#axis('off')
#subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)
self.fig, ax = subplots(1, 1)
ax.imshow(self.THEimage)
ax.axis('off')
subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)
self.canvas = FigureCanvas(self.fig)
self.mplvl.addWidget(self.canvas)
self.canvas.draw()
self.toolbar = NavigationToolbar(self.canvas, self.widget, coordinates=True)
self.mplvl.addWidget(self.toolbar)
cid = self.fig.canvas.mpl_connect('button_press_event', self.onclick)
def onclick(self, event):
print('button=%d, x=%d, y=%d, xdata=%f, ydata=%f' %(event.button, event.x, event.y, event.xdata, event.ydata))
if event.button == 3:
if str(self.rClicktype.currentText()) == 'Add cell':
squaresize = self.cropsize
#print(len(self.THEblobs))
self.THEblobs =np.array(self.THEblobs.tolist() + [[int(event.ydata), int(event.xdata), -99]])
#print(len(self.THEblobs))
#self.table.setHorizontalHeaderLabels(['index', 'auto class', 'manual class'])
#rowPosition = self.table.rowCount()
#self.table.insertRow(rowPosition)
self.nMarkedCells.setText(str(int(self.nMarkedCells.text()) + 1))
self.ImgAddPatches()
if str(self.rClicktype.currentText()) == 'Add Top Right box corner':
self.guidePoints['TR'] = [int(event.ydata),int(event.xdata)]
#self.rClicktype.setCurrentText('Add Top Left box corner')
if 0 not in self.guidePoints.values():
self.polygonList = []
self.innergridRight = [(array(self.guidePoints['TR'])*i+ array(self.guidePoints['BR'])*(int(self.numLayers.text())-i))/int(self.numLayers.text()) for i in range(0,int(self.numLayers.text()) +1)]
self.innergridLeft = [(array(self.guidePoints['TL'])*i+ array(self.guidePoints['BL'])*(int(self.numLayers.text())-i))/int(self.numLayers.text()) for i in range(0,int(self.numLayers.text()) +1)]
#print(self.innergridLeft, self.innergridRight)
self.bigpoligon = Polygon([self.guidePoints['TR'], self.guidePoints['TL'],self.guidePoints['BL'],self.guidePoints['BR']])
for i in list(range(len(self.innergridLeft)-1))[::-1]:
self.polygonList += [Polygon([self.innergridRight[i], self.innergridLeft[i],self.innergridLeft[i+1], self.innergridRight[i+1]])]
self.ImgAddPatches()
if str(self.rClicktype.currentText()) == 'Add Top Left box corner':
self.guidePoints['TL'] = [int(event.ydata),int(event.xdata)]
#self.rClicktype.setCurrentText('Add Bottom Left box corner')
if 0 not in self.guidePoints.values():
self.polygonList = []
self.innergridRight = [(array(self.guidePoints['TR'])*i+ array(self.guidePoints['BR'])*(int(self.numLayers.text())-i))/int(self.numLayers.text()) for i in range(0,int(self.numLayers.text()) +1)]
self.innergridLeft = [(array(self.guidePoints['TL'])*i+ array(self.guidePoints['BL'])*(int(self.numLayers.text())-i))/int(self.numLayers.text()) for i in range(0,int(self.numLayers.text()) +1)]
#print(self.innergridLeft, self.innergridRight)
self.bigpoligon = Polygon([self.guidePoints['TR'], self.guidePoints['TL'],self.guidePoints['BL'],self.guidePoints['BR']])
for i in list(range(len(self.innergridLeft)-1))[::-1]:
self.polygonList += [Polygon([self.innergridRight[i], self.innergridLeft[i],self.innergridLeft[i+1], self.innergridRight[i+1]])]
self.ImgAddPatches()
if str(self.rClicktype.currentText()) == 'Add Bottom Left box corner':
self.guidePoints['BL'] = [int(event.ydata),int(event.xdata)]
#self.rClicktype.setCurrentText('Add Bottom Right corner')
if 0 not in self.guidePoints.values():
self.polygonList = []
self.innergridRight = [(array(self.guidePoints['TR'])*i+ array(self.guidePoints['BR'])*(int(self.numLayers.text())-i))/int(self.numLayers.text()) for i in range(0,int(self.numLayers.text()) +1)]
self.innergridLeft = [(array(self.guidePoints['TL'])*i+ array(self.guidePoints['BL'])*(int(self.numLayers.text())-i))/int(self.numLayers.text()) for i in range(0,int(self.numLayers.text()) +1)]
#print(self.innergridLeft, self.innergridRight)
self.bigpoligon = Polygon([self.guidePoints['TR'], self.guidePoints['TL'],self.guidePoints['BL'],self.guidePoints['BR']])
for i in list(range(len(self.innergridLeft)-1))[::-1]:
self.polygonList += [Polygon([self.innergridRight[i], self.innergridLeft[i],self.innergridLeft[i+1], self.innergridRight[i+1]])]
self.ImgAddPatches()
if str(self.rClicktype.currentText()) == 'Add Bottom Right box corner':
self.guidePoints['BR'] = [int(event.ydata),int(event.xdata)]
#self.rClicktype.setCurrentText('Add Top Right box corner')
if 0 not in self.guidePoints.values():
self.polygonList = []
self.innergridRight = [(array(self.guidePoints['TR'])*i+ array(self.guidePoints['BR'])*(int(self.numLayers.text())-i))/int(self.numLayers.text()) for i in range(0,int(self.numLayers.text()) +1)]
self.innergridLeft = [(array(self.guidePoints['TL'])*i+ array(self.guidePoints['BL'])*(int(self.numLayers.text())-i))/int(self.numLayers.text()) for i in range(0,int(self.numLayers.text()) +1)]
#print(self.innergridLeft, self.innergridRight)
self.bigpoligon = Polygon([self.guidePoints['TR'], self.guidePoints['TL'],self.guidePoints['BL'],self.guidePoints['BR']])
#print(self.bigpoligon)
for i in list(range(len(self.innergridLeft)-1))[::-1]:
self.polygonList += [Polygon([self.innergridRight[i], self.innergridLeft[i],self.innergridLeft[i+1], self.innergridRight[i+1]])]
self.ImgAddPatches()
if str(self.rClicktype.currentText()) == 'Remove cell':
dist = np.sum((self.THEblobs[:,0:2]-[event.ydata,event.xdata])**2,1)
if min(dist) < 800:
line = dist.tolist().index(min(dist))
#print(line)
self.removeCell(line)
self.nMarkedCells.setText(str(int(self.nMarkedCells.text()) - 1))
#self.ImgAddPatches()
elif event.button == 2:
#print(self.THEblobs[:,0:2])
dist = np.sum((self.THEblobs[:,0:2]-[event.ydata,event.xdata])**2,1)
if min(dist) < 800:
line = dist.tolist().index(min(dist))
#print(line)
self.removeCell(line)
self.nMarkedCells.setText(str(int(self.nMarkedCells.text()) - 1))
#self.ImgAddPatches()
def redrawLayers(self):
if 0 not in self.guidePoints.values():
self.polygonList = []
self.innergridRight = [(array(self.guidePoints['TR'])*i+ array(self.guidePoints['BR'])*(int(self.numLayers.text())-i))/int(self.numLayers.text()) for i in range(0,int(self.numLayers.text()) +1)]
self.innergridLeft = [(array(self.guidePoints['TL'])*i+ array(self.guidePoints['BL'])*(int(self.numLayers.text())-i))/int(self.numLayers.text()) for i in range(0,int(self.numLayers.text()) +1)]
#print(self.innergridLeft, self.innergridRight)
self.bigpoligon = Polygon([self.guidePoints['TR'], self.guidePoints['TL'],self.guidePoints['BL'],self.guidePoints['BR']])
#print(self.bigpoligon)
for i in list(range(len(self.innergridLeft)-1)):
self.polygonList += [Polygon([self.innergridRight[i], self.innergridLeft[i],self.innergridLeft[i+1], self.innergridRight[i+1]])]
self.ImgAddPatches()
def changeFIGURE(self, newFIG):
self.rmmpl()
self.canvas = FigureCanvas(newFIG)
self.mplvl.addWidget(self.canvas)
self.canvas.draw()
self.toolbar = NavigationToolbar(self.canvas, self.widget, coordinates=True)
self.mplvl.addWidget(self.toolbar)
cid = self.fig.canvas.mpl_connect('button_press_event', self.onclick)
def rmmpl(self,):
self.mplvl.removeWidget(self.canvas)
self.canvas.close()
self.mplvl.removeWidget(self.toolbar)
self.toolbar.close()
def Id_cells(self):
if type(self.BLUEimage) == type(0): return
while self.table.rowCount() < int(self.numLayers.text())+2: self.table.insertRow(0)
while self.table.rowCount() > int(self.numLayers.text())+2: self.table.removeRow(0)
for num, layer in enumerate( [str(x+1) for x in range(int(self.numLayers.text()))] + ['Total selected reg', 'Total image']):
self.table.setItem(num , 0, QtWidgets.QTableWidgetItem(layer))
self.table.setItem(num , 1, QtWidgets.QTableWidgetItem("0"))
self.table.setItem(num , 2, QtWidgets.QTableWidgetItem("0"))
self.table.setItem(num , 3, QtWidgets.QTableWidgetItem("0"))
self.table.setItem(num , 4, QtWidgets.QTableWidgetItem("0"))
#self.table.setItem(num , 5, QtWidgets.QTableWidgetItem("0"))
squaresize = self.cropsize
image_gray = self.BLUEimage
self.BLUEblobs = blob_log(self.BLUEimage, max_sigma=int(self.maxSigSpin.text()), num_sigma=10, min_sigma = int(self.minSigSpin.text()),overlap = float(self.log_overlap.text().replace(',', '.')) ,threshold=float(self.thresholdSpin.text().replace(',', '.')), exclude_border = squaresize)
self.table.setItem(int(self.numLayers.text()) +1 , 3, QtWidgets.QTableWidgetItem(str(len(self.BLUEblobs))))
if str(self.fMarker.currentText()) == 'RFP':
blobs = blob_log(self.REDimage, max_sigma=int(self.maxSigSpin.text()), num_sigma=10, min_sigma = int(self.minSigSpin.text()),overlap = float(self.log_overlap.text().replace(',', '.')) ,threshold=float(self.thresholdSpin.text().replace(',', '.')), exclude_border = squaresize)
self.table.setItem(int(self.numLayers.text()) +1 , 3, QtWidgets.QTableWidgetItem(str(len(self.BLUEblobs))))
if str(self.fMarker.currentText()) == 'GFP':
blobs = blob_log(self.GREENimage, max_sigma=int(self.maxSigSpin.text()), num_sigma=10, min_sigma = int(self.minSigSpin.text()),overlap = float(self.log_overlap.text().replace(',', '.')) ,threshold=float(self.thresholdSpin.text().replace(',', '.')), exclude_border = squaresize)
self.table.setItem(int(self.numLayers.text()) +1 , 3, QtWidgets.QTableWidgetItem(str(len(self.BLUEblobs))))
if str(self.fMarker.currentText()) == 'GFP or RFP':
intermediary_fig = self.REDimage + self.GREENimage.astype(float)
intermediary_fig = 255*(intermediary_fig - intermediary_fig.min())/ (intermediary_fig.max() - intermediary_fig.min())
blobs = blob_log(intermediary_fig.astype('uint8'), max_sigma=int(self.maxSigSpin.text()), num_sigma=10, min_sigma = int(self.minSigSpin.text()),overlap = float(self.log_overlap.text().replace(',', '.')) ,threshold=float(self.thresholdSpin.text().replace(',', '.')), exclude_border = squaresize)
self.table.setItem(int(self.numLayers.text()) +1 , 3, QtWidgets.QTableWidgetItem(str(len(self.BLUEblobs))))
if str(self.fMarker.currentText()) == 'GFP and RFP':
intermediary_fig = self.REDimage * self.GREENimage.astype(float)
intermediary_fig = 255*(intermediary_fig - intermediary_fig.min())/ (intermediary_fig.max() - intermediary_fig.min())
blobs = blob_log(intermediary_fig.astype('uint8'), max_sigma=int(self.maxSigSpin.text()), num_sigma=10, min_sigma = int(self.minSigSpin.text()),overlap = float(self.log_overlap.text().replace(',', '.')) ,threshold=float(self.thresholdSpin.text().replace(',', '.')), exclude_border = squaresize)
self.table.setItem(int(self.numLayers.text()) + 1 , 3, QtWidgets.QTableWidgetItem(str(len(self.BLUEblobs))))
if str(self.fMarker.currentText()) == 'Orange':
blobs = blob_log(self.ORANGEimage.astype('uint8'), max_sigma=int(self.maxSigSpin.text()), num_sigma=10, min_sigma = int(self.minSigSpin.text()),overlap = float(self.log_overlap.text().replace(',', '.')) ,threshold=float(self.thresholdSpin.text().replace(',','.')), exclude_border = squaresize)
self.table.setItem(int(self.numLayers.text()) + 1 , 3, QtWidgets.QTableWidgetItem(str(len(self.BLUEblobs))))
#blobsDAPI = blob_log(self.BLUEimage[squaresize:-squaresize,squaresize:-squaresize], max_sigma=10, num_sigma=10, min_sigma = 3, threshold=.1)
self.THEblobs = blobs
print(blobs.shape)
self.nMarkedCells.setText(str(len(blobs)))
self.table.setItem(int(self.numLayers.text()) +1 , 1, QtWidgets.QTableWidgetItem(str(len(blobs))))
#self.table.setItem(9 , 2, QtWidgets.QTableWidgetItem(str(len(blobsDAPI))))
#if float(self.table.item(int(self.numLayers.text()) +1,2).text()) != 0:
# self.table.setItem(int(self.numLayers.text()) +1 , 3, QtWidgets.QTableWidgetItem(str(float(self.table.item(int(self.numLayers.text()) +1,1).text())/float(self.table.item(int(self.numLayers.text()) +1,2).text()))))
self.ImgAddPatches()
def ImgAddPatches(self):
colors = ['w', 'r', 'g', 'y', 'w', 'r', 'g', 'y', 'orange', 'w', 'r'] *100
squaresize = self.cropsize
close(self.fig)
self.fig, ax = subplots(1, 1)
ax.imshow(self.THEimage)
ax.axis('off')
subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)
while self.table.rowCount() < int(self.numLayers.text())+2: self.table.insertRow(0)
while self.table.rowCount() > int(self.numLayers.text())+2: self.table.removeRow(0)
for num, layer in enumerate( [str(x+1) for x in range(int(self.numLayers.text()))] + ['Total selected reg', 'Total image']):
self.table.setItem(num , 0, QtWidgets.QTableWidgetItem(layer))
self.table.setItem(num , 1, QtWidgets.QTableWidgetItem("0"))
self.table.setItem(num , 2, QtWidgets.QTableWidgetItem("0"))
self.table.setItem(num , 3, QtWidgets.QTableWidgetItem("0"))
self.table.setItem(num , 4, QtWidgets.QTableWidgetItem("0"))
#self.table.setItem(num , 5, QtWidgets.QTableWidgetItem("0"))
self.table.setItem(int(self.numLayers.text())+1 , 1, QtWidgets.QTableWidgetItem(str(len(self.THEblobs))))
self.table.setItem(int(self.numLayers.text()) +1 , 3, QtWidgets.QTableWidgetItem(str(len(self.BLUEblobs))))
if float(self.table.item(int(self.numLayers.text()) +1,3).text()) > 0:
self.table.setItem(int(self.numLayers.text()) +1 , 4, QtWidgets.QTableWidgetItem(str(float(self.table.item(int(self.numLayers.text()) +1,1).text())/float(self.table.item(int(self.numLayers.text()) +1,3).text()))[:10]))
if 0 not in self.guidePoints.values():
ctr = 0
polygonListCount = array([0 for i in self.polygonList])
#print('pollistcount before:'+str(polygonListCount))
for number, blob in enumerate(self.THEblobs):
y, x, r = blob
blobPoint = Point(y,x)
if self.bigpoligon.contains(blobPoint):
ctr+= 1
whichpolygon = [1 if x.contains(blobPoint) else 0 for x in self.polygonList]
polygonListCount += array(whichpolygon)
#print('pollistcount:'+str(polygonListCount))
#c = Rectangle((x + int(squaresize/2), y + int(squaresize/2)),squaresize,squaresize, color=colors[whichpolygon.index(1)], linewidth=.5, alpha = 0.3)
if r + int(self.cellMarkerSize.text()) > 0:
c = Circle((x,y), r+ int(self.cellMarkerSize.text()) if r+ int(self.cellMarkerSize.text())<squaresize else squaresize, color=colors[whichpolygon.index(1)], linewidth=2.5, alpha = 0.95)
elif r == -99:
c = Circle((x,y), 6, linewidth=2.5, ec = 'red', alpha = 0.95, color=colors[whichpolygon.index(1)])
else:
c = Circle((x,y), 1, linewidth=1, alpha = 0.95, color=colors[whichpolygon.index(1)])
ax.add_patch(c)
if self.cellIndexradio.isChecked(): ax.text(x,y, polygonListCount[whichpolygon.index(1)], color = 'white', fontsize = 10)
self.nMarkedCells.setText(str(ctr) )
self.table.setItem(int(self.numLayers.text()) +1 , 2, QtWidgets.QTableWidgetItem(str(self.imgPolygon.area/self.bigpoligon.area)[:4]))
self.table.setItem( int(self.numLayers.text()), 2, QtWidgets.QTableWidgetItem(str(int(self.bigpoligon.area/self.bigpoligon.area))))
self.table.setItem(int(self.numLayers.text()) , 1, QtWidgets.QTableWidgetItem(str(ctr)))
#self.table.setItem(int(self.numLayers.text()) , 3, QtWidgets.QTableWidgetItem(str(ctr)))
#self.table.setItem(int(self.numLayers.text()) +1 , 3, QtWidgets.QTableWidgetItem(str(float(self.table.item(int(self.numLayers.text()) +1,1).text())/float(self.table.item(int(self.numLayers.text()) +1,2).text()))[:6]))
for n, pol in enumerate(self.polygonList):
self.table.setItem(n, 2, QtWidgets.QTableWidgetItem(str(pol.area/self.bigpoligon.area)[:4]))
#self.table.setItem(n, 3, QtWidgets.QTableWidgetItem(str(polygonListCount[n]/(pol.area/self.bigpoligon.area))[:6]))
self.table.setItem(n, 1, QtWidgets.QTableWidgetItem(str(polygonListCount[n])))
#### add blue cells to dapi count
ctrDAPI = 0
polygonListCountDAPI = array([0 for i in self.polygonList])
for number, blob in enumerate(self.BLUEblobs):
y, x, r = blob
blobPoint = Point(y,x)
if self.bigpoligon.contains(blobPoint):
ctrDAPI+= 1
whichpolygonDAPI = [1 if x.contains(blobPoint) else 0 for x in self.polygonList]
polygonListCountDAPI += array(whichpolygonDAPI)
self.table.setItem(int(self.numLayers.text()) , 3, QtWidgets.QTableWidgetItem(str(ctrDAPI)))
if float(self.table.item(int(self.numLayers.text()),3).text())>0: self.table.setItem(int(self.numLayers.text()) , 4, QtWidgets.QTableWidgetItem(str(float(self.table.item(int(self.numLayers.text()),1).text())/ctrDAPI)))
for n, pol in enumerate(self.polygonList):
if polygonListCountDAPI[n] >0 :
self.table.setItem(n, 4, QtWidgets.QTableWidgetItem(str(polygonListCount[n]/polygonListCountDAPI[n])[:6]))
else:
self.table.setItem(n, 4, QtWidgets.QTableWidgetItem(str(0)))
self.table.setItem(n, 3, QtWidgets.QTableWidgetItem(str(polygonListCountDAPI[n])))
if 0 in self.guidePoints.values():
for number, blob in enumerate(self.THEblobs):
y, x, r = blob
#c = Rectangle((x + int(squaresize/2), y + int(squaresize/2)),squaresize,squaresize, color='gray', linewidth=.5, alpha = 0.3)
if r + int(self.cellMarkerSize.text()) > 0:
c = Circle((x,y), r+ int(self.cellMarkerSize.text()) if r+ int(self.cellMarkerSize.text())<squaresize else squaresize, color='gray',ec= 'white' ,linewidth=1, alpha = 0.5)
elif r == -99:
c = Circle((x,y),5, alpha = 0.5, ec= 'red', linewidth=2)
else:
c = Circle((x,y),1, alpha = 0.5)
ax.add_patch(c)
#c = Circle((x,y), r+10 if r+10<squaresize else squaresize, color='gray', linewidth=.5, alpha = 0.3)
if self.cellIndexradio.isChecked(): ax.text(x,y, str(number), color = 'white', fontsize = 6)
for number, key in enumerate(self.guidePoints):
if self.guidePoints[key] != 0:
ax.add_patch(Circle(self.guidePoints[key][::-1], int(self.numLayers.text()), color='w', linewidth=2, fill=True))
ax.text(self.guidePoints[key][1]-7,self.guidePoints[key][0]+5, key, color = 'firebrick', fontsize = 10) #add here number of guide point
if self.guidePoints['TR'] != 0 and self.guidePoints['TL'] != 0: ax.plot([self.guidePoints['TR'][1],self.guidePoints['TL'][1]]
, [self.guidePoints['TR'][0],self.guidePoints['TL'][0]], '-', color='w', linewidth=2)
if self.guidePoints['TL'] != 0 and self.guidePoints['BL'] != 0: ax.plot([self.guidePoints['TL'][1],self.guidePoints['BL'][1]]
, [self.guidePoints['TL'][0],self.guidePoints['BL'][0]], '-', color='w', linewidth=2)
if self.guidePoints['BR'] != 0 and self.guidePoints['BL'] != 0: ax.plot([self.guidePoints['BR'][1],self.guidePoints['BL'][1]]
, [self.guidePoints['BR'][0],self.guidePoints['BL'][0]], '-', color='w', linewidth=2)
if self.guidePoints['TR'] != 0 and self.guidePoints['BR'] != 0: ax.plot([self.guidePoints['TR'][1],self.guidePoints['BR'][1]]
, [self.guidePoints['TR'][0],self.guidePoints['BR'][0]], '-', color='w', linewidth=2)
if 0 not in self.guidePoints.values():
for i in range(len(self.innergridLeft)):
ax.plot([self.innergridRight[i][1], self.innergridLeft[i][1]], [self.innergridRight[i][0], self.innergridLeft[i][0]], '-', color = 'w', linewidth = 1)
ax.axis('off')
subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0, hspace=0)
for item in [self.fig, ax]:
item.patch.set_visible(False)
self.changeFIGURE(self.fig)
def create_csv(self):
layer = np.array([str(self.table.item(i,0).text()) for i in range(self.table.rowCount() ) ])
fcells = np.array([str(self.table.item(i,1).text()) for i in range(self.table.rowCount() ) ])
area = np.array([str(self.table.item(i,2).text()) for i in range(self.table.rowCount() ) ])
#density = np.array([str(self.table.item(i,3).text()) for i in range(self.table.rowCount() ) ])
nucleiCount = np.array([str(self.table.item(i,3).text()) for i in range(self.table.rowCount() ) ])
transfectedRatio = np.array([str(self.table.item(i,4).text()) for i in range(self.table.rowCount() ) ])
classtable = DataFrame( np.transpose(np.vstack((layer, fcells,area, nucleiCount, transfectedRatio))))#, index=dates, columns=[nome , classe])
print(classtable)
saveclassification = classtable.to_csv(self.figname +'_count.csv',index=False,header=['layers','Fluorescent cells'
, 'Area', 'DAPI cell count', 'Fluorescent to DAPI ration' ])
app = QtWidgets.QApplication(sys.argv)
myWindow = MainWindow()
myWindow.show()
app.exec_()