-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathmm3_GUI_helpers.py
More file actions
executable file
·731 lines (581 loc) · 34.6 KB
/
mm3_GUI_helpers.py
File metadata and controls
executable file
·731 lines (581 loc) · 34.6 KB
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
#! /usr/bin/env python3
from __future__ import print_function, division
from PyQt5.QtWidgets import QApplication, QMainWindow, QMenuBar, QRadioButton, QMenu, QAction, QButtonGroup, QFileDialog, QVBoxLayout, QHBoxLayout, QWidget, QLabel, QGridLayout, QAction, QDockWidget, QPushButton, QInputDialog
from PyQt5.QtGui import QIcon, QImage, QPainter, QPen, QPixmap, qGray, QColor
from PyQt5.QtCore import Qt, QPoint, QRectF
from skimage import io, img_as_ubyte, color, draw, measure
import numpy as np
import sys
import re
import os
import yaml
import multiprocessing
def init_params(param_file_path):
# load all the parameters into a global dictionary
global params
with open(param_file_path, 'r') as param_file:
params = yaml.safe_load(param_file)
# set up how to manage cores for multiprocessing
params['num_analyzers'] = multiprocessing.cpu_count()
# useful folder shorthands for opening files
params['TIFF_dir'] = os.path.join(params['experiment_directory'], params['image_directory'])
params['ana_dir'] = os.path.join(params['experiment_directory'], params['analysis_directory'])
params['hdf5_dir'] = os.path.join(params['ana_dir'], 'hdf5')
params['chnl_dir'] = os.path.join(params['ana_dir'], 'channels')
params['empty_dir'] = os.path.join(params['ana_dir'], 'empties')
params['sub_dir'] = os.path.join(params['ana_dir'], 'subtracted')
params['seg_dir'] = os.path.join(params['ana_dir'], 'segmented')
params['cell_dir'] = os.path.join(params['ana_dir'], 'cell_data')
params['track_dir'] = os.path.join(params['ana_dir'], 'tracking')
# use jd time in image metadata to make time table. Set to false if no jd time
if params['TIFF_source'] == 'elements' or params['TIFF_source'] == 'nd2ToTIFF':
params['use_jd'] = True
else:
params['use_jd'] = False
class Window(QMainWindow):
def __init__(self, parent=None, imgPaths=None, fov_id_list=None, training_dir=None):
super(Window, self).__init__(parent)
top = 400
left = 400
width = 400
height = 1200
self.setWindowTitle("You got this!")
self.setGeometry(top,left,width,height)
self.ImgsWidget = OverlayImgsWidget(self, imgPaths=imgPaths, fov_id_list=fov_id_list, training_dir=training_dir)
self.setCentralWidget(self.ImgsWidget)
brushSizeGroup = QButtonGroup()
onePxButton = QRadioButton("1px")
onePxButton.setShortcut("Ctrl+1")
onePxButton.clicked.connect(self.ImgsWidget.mask_widget.onePx)
brushSizeGroup.addButton(onePxButton)
threePxButton = QRadioButton("3px")
threePxButton.setShortcut("Ctrl+3")
threePxButton.clicked.connect(self.ImgsWidget.mask_widget.threePx)
brushSizeGroup.addButton(threePxButton)
fivePxButton = QRadioButton("5px")
fivePxButton.setShortcut("Ctrl+5")
fivePxButton.clicked.connect(self.ImgsWidget.mask_widget.fivePx)
brushSizeGroup.addButton(fivePxButton)
sevenPxButton = QRadioButton("7px")
sevenPxButton.setShortcut("Ctrl+7")
sevenPxButton.clicked.connect(self.ImgsWidget.mask_widget.sevenPx)
brushSizeGroup.addButton(sevenPxButton)
ninePxButton = QRadioButton("9px")
ninePxButton.setShortcut("Ctrl+9")
ninePxButton.clicked.connect(self.ImgsWidget.mask_widget.ninePx)
brushSizeGroup.addButton(ninePxButton)
brushSizeLayout = QVBoxLayout()
brushSizeLayout.addWidget(onePxButton)
brushSizeLayout.addWidget(threePxButton)
brushSizeLayout.addWidget(fivePxButton)
brushSizeLayout.addWidget(sevenPxButton)
brushSizeLayout.addWidget(ninePxButton)
brushSizeGroupWidget = QWidget()
brushSizeGroupWidget.setLayout(brushSizeLayout)
brushSizeDockWidget = QDockWidget()
brushSizeDockWidget.setWidget(brushSizeGroupWidget)
self.addDockWidget(Qt.LeftDockWidgetArea, brushSizeDockWidget)
brushColorGroup = QButtonGroup()
# whiteButton = QRadioButton("White")
# whiteButton.setShortcut("Ctrl+W")
# whiteButton.clicked.connect(self.ImgsWidget.mask_widget.whiteColor)
# brushColorGroup.addButton(whiteButton)
cellButton = QRadioButton("Cell")
cellButton.setShortcut("Ctrl+C")
cellButton.clicked.connect(self.ImgsWidget.mask_widget.redColor)
brushColorGroup.addButton(cellButton)
notCellButton = QRadioButton("Not cell")
notCellButton.setShortcut("Ctrl+N")
notCellButton.clicked.connect(self.ImgsWidget.mask_widget.blackColor)
brushColorGroup.addButton(notCellButton)
resetButton = QPushButton("Reset mask")
resetButton.setShortcut("Ctrl+R")
resetButton.clicked.connect(self.ImgsWidget.mask_widget.reset)
clearButton = QPushButton("Clear mask")
clearButton.clicked.connect(self.ImgsWidget.mask_widget.clear)
brushColorLayout = QVBoxLayout()
# brushColorLayout.addWidget(whiteButton)
brushColorLayout.addWidget(cellButton)
brushColorLayout.addWidget(notCellButton)
brushColorLayout.addWidget(resetButton)
brushColorLayout.addWidget(clearButton)
brushColorGroupWidget = QWidget()
brushColorGroupWidget.setLayout(brushColorLayout)
brushColorDockWidget = QDockWidget()
brushColorDockWidget.setWidget(brushColorGroupWidget)
self.addDockWidget(Qt.LeftDockWidgetArea, brushColorDockWidget)
advanceFrameButton = QPushButton("Next frame")
advanceFrameButton.setShortcut("Ctrl+F")
advanceFrameButton.clicked.connect(self.ImgsWidget.mask_widget.next_frame)
advanceFrameButton.clicked.connect(self.ImgsWidget.img_widget.next_frame)
priorFrameButton = QPushButton("Prior frame")
priorFrameButton.clicked.connect(self.ImgsWidget.mask_widget.prior_frame)
priorFrameButton.clicked.connect(self.ImgsWidget.img_widget.prior_frame)
advancePeakButton = QPushButton("Next peak")
advancePeakButton.setShortcut("Ctrl+P")
advancePeakButton.clicked.connect(self.ImgsWidget.mask_widget.next_peak)
advancePeakButton.clicked.connect(self.ImgsWidget.img_widget.next_peak)
priorPeakButton = QPushButton("Prior peak")
priorPeakButton.clicked.connect(self.ImgsWidget.mask_widget.prior_peak)
priorPeakButton.clicked.connect(self.ImgsWidget.img_widget.prior_peak)
advanceFOVButton = QPushButton("Next FOV")
advanceFOVButton.clicked.connect(self.ImgsWidget.mask_widget.next_fov)
advanceFOVButton.clicked.connect(self.ImgsWidget.img_widget.next_fov)
priorFOVButton = QPushButton("Prior FOV")
priorFOVButton.clicked.connect(self.ImgsWidget.mask_widget.prior_fov)
priorFOVButton.clicked.connect(self.ImgsWidget.img_widget.prior_fov)
saveAndNextButton = QPushButton("Save and next frame")
saveAndNextButton.setShortcut("Ctrl+S")
saveAndNextButton.clicked.connect(self.ImgsWidget.mask_widget.buttonSave)
saveAndNextButton.clicked.connect(self.ImgsWidget.img_widget.buttonSave)
saveAndNextButton.clicked.connect(self.ImgsWidget.mask_widget.next_frame)
saveAndNextButton.clicked.connect(self.ImgsWidget.img_widget.next_frame)
fileAdvanceLayout = QVBoxLayout()
fileAdvanceLayout.addWidget(advanceFrameButton)
fileAdvanceLayout.addWidget(priorFrameButton)
fileAdvanceLayout.addWidget(advancePeakButton)
fileAdvanceLayout.addWidget(priorPeakButton)
fileAdvanceLayout.addWidget(advanceFOVButton)
fileAdvanceLayout.addWidget(priorFOVButton)
fileAdvanceLayout.addWidget(saveAndNextButton)
fileAdvanceGroupWidget = QWidget()
fileAdvanceGroupWidget.setLayout(fileAdvanceLayout)
fileAdvanceDockWidget = QDockWidget()
fileAdvanceDockWidget.setWidget(fileAdvanceGroupWidget)
self.addDockWidget(Qt.RightDockWidgetArea, fileAdvanceDockWidget)
# setFrameInputWidget = FrameSetter()
# setPeakInputWidget = PeakSetter()
# setFOVInputWidget = FOVSetter()
#
# setFOVPeakFrameInputLayout = QVBoxLayout()
# setFOVPeakFrameInputLayout.addWidget(setFrameInputWidget)
# setFOVPeakFrameInputLayout.addWidget(setPeakInputWidget)
# setFOVPeakFrameInputLayout.addWidget(setFOVInputWidget)
#
# setFOVPeakFrameWidget = QWidget()
# setFOVPeakFrameWidget.setLayout(setFOVPeakFrameInputLayout)
#
# setFOVPeakFrameDockWidget = QDockWidget()
# setFOVPeakFrameDockWidget.setWidget(setFrameInputWidget)
# self.addDockWidget(Qt.RightDockWidgetArea, setFOVPeakFrameDockWidget)
#
# def FrameSetter(self):
# i, okPressed = QInputDialog.getInt(self, "Jump to frame", "Frame index (0-indexed):", 0, 0)
# if okPressed:
# self.
class OverlayImgsWidget(QWidget):
def __init__(self,parent,imgPaths,fov_id_list,training_dir):
super(OverlayImgsWidget, self).__init__(parent)
self.imgLayout = QGridLayout()
mask_dir = os.path.join(training_dir, 'masks/cells')
image_dir = os.path.join(training_dir, 'images/cells')
# self.maskImgPaths = [paths[1] for paths in imgPaths]
# self.phaseImgPaths = [paths[0] for paths in imgPaths]
names = ['image','mask'] # image has to be after mask to place image on top
positions = [(0,0),(0,0)]
for name,position in zip(names,positions):
if name == '':
continue
if name == 'mask':
test_key = [k for k in imgPaths.keys()][0]
if not imgPaths[test_key][0][1] is None:
self.mask_widget = MaskTransparencyWidget(self, imgPaths=imgPaths, fov_id_list=fov_id_list, mask_dir=mask_dir)
self.imgLayout.addWidget(self.mask_widget, *position)
else:
# Write some code to make a blank MaskTransparencyWidget
#######################################################################################################################
# NOTE: write the blank widget code!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#######################################################################################################################
self.mask_widget = BlankMaskTransparencyWidget(self, fov_id_list=fov_id_list, mask_dir=mask_dir)
elif name == 'image':
self.img_widget = PhaseWidget(self, imgPaths=imgPaths, fov_id_list=fov_id_list, image_dir=image_dir)
self.imgLayout.addWidget(self.img_widget, *position)
self.setLayout(self.imgLayout)
class MaskTransparencyWidget(QWidget):
def __init__(self,parent,imgPaths,fov_id_list,mask_dir):
super(MaskTransparencyWidget, self).__init__(parent)
self.mask_dir = mask_dir
self.frameIndex = 0
self.imgPaths = imgPaths
self.fov_id_list = fov_id_list
self.fovIndex = 0
self.fov_id = self.fov_id_list[self.fovIndex]
self.imgIndex = 0
self.maskImgPath = self.imgPaths[self.fov_id][self.imgIndex][1]
# TO DO: check if re-annotated mask exists in training_dir, and present that instead of original mask
# make indicator appear if we're re-editing the mask again.
experiment_name = params['experiment_name']
original_file_name = self.maskImgPath
pat = re.compile(r'.+(xy\d{3,4})_(p\d{3,4})_.+') # supports 3- or 4-digit naming
mat = pat.match(original_file_name)
fovID = mat.groups()[0]
peakID = mat.groups()[1]
fileBaseName = '{}_{}_{}_t{:0=4}.tif'.format(experiment_name, fovID, peakID, self.frameIndex+1)
savePath = os.path.join(self.mask_dir,fileBaseName)
self.maskStack = io.imread(self.maskImgPath)
if os.path.isfile(savePath):
print('Re-annotated mask exists in training directory. Loading it.')
# add widget to express whether this mask is one you already re-annotated
# print(self.frameIndex)
self.maskStack[self.frameIndex,:,:] = io.imread(savePath)
overwriteSegFile = True
else:
overwriteSegFile = False
img = self.maskStack[self.frameIndex,:,:]
img[img>0] = 255
self.RGBImg = color.gray2rgb(img).astype('uint8')
self.RGBImg[:,:,1:] = 0 # set GB channels to 0 to make the transarency mask red
alphaFloat = 0.15
alphaArray = np.zeros(img.shape, dtype='uint8')
alphaArray = np.expand_dims(alphaArray, -1)
self.alpha = int(255*alphaFloat)
alphaArray[...] = self.alpha
self.RGBAImg = np.append(self.RGBImg, alphaArray, axis=-1)
self.originalHeight, self.originalWidth, self.originalChannelNumber = self.RGBAImg.shape
self.maskQimage = QImage(self.RGBAImg, self.originalWidth, self.originalHeight, self.RGBAImg.strides[0], QImage.Format_RGBA8888).scaled(1024, 1024, aspectRatioMode=Qt.KeepAspectRatio)
self.maskQpixmap = QPixmap(self.maskQimage)
self.label = QLabel(self)
self.label.setPixmap(self.maskQpixmap)
self.drawing = False
self.brushSize = 2
self.brushColor = QColor(0,0,0,self.alpha)
self.lastPoint = QPoint()
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.drawing = True
self.lastPoint = event.pos()
def mouseMoveEvent(self, event):
if (event.buttons() & Qt.LeftButton) & self.drawing:
# make the mouse position the center of a circle whose radius is defined as self.brushSize
rr,cc = draw.circle(event.y(), event.x(), self.brushSize)
for pix in zip(rr,cc):
rowIndex = pix[0]
colIndex = pix[1]
self.maskQimage.setPixel(colIndex, rowIndex, self.brushColor.rgba())
self.maskQpixmap = QPixmap(self.maskQimage)
self.label.setPixmap(self.maskQpixmap)
self.lastPoint = event.pos()
self.update()
def mouseReleaseEvent(self, event):
if event.button == Qt.LeftButton:
self.drawing = False
def save(self):
filePath, _ = QFileDialog.getSaveFileName(self, "Save Image", "", "PNG(*.png);;JPEG(*.jpg *.jpeg);;TIFF(*.tif *.tiff);;ALL FILES(*.*)")
if filePath == "":
return
saveImg = self.maskQimage.convertToFormat(QImage.Format_Grayscale8).scaled(self.originalWidth,self.originalHeight)
qimgHeight = saveImg.height()
qimgWidth = saveImg.width()
for rowIndex in range(qimgHeight):
for colIndex in range(qimgWidth):
pixVal = qGray(saveImg.pixel(colIndex,rowIndex))
if pixVal > 0:
saveImg.setPixelColor(colIndex,rowIndex,QColor(1,1,1))
pixVal = qGray(saveImg.pixel(colIndex, rowIndex))
saveImg.save(filePath)
def buttonSave(self):
experiment_name = params['experiment_name']
original_file_name = self.maskImgPath
pat = re.compile(r'.+(xy\d{3,4})_(p\d{3,4})_.+') # supports 3- or 4-digit naming
mat = pat.match(original_file_name)
fovID = mat.groups()[0]
peakID = mat.groups()[1]
fileBaseName = '{}_{}_{}_t{:0=4}.tif'.format(experiment_name, fovID, peakID, self.frameIndex+1)
savePath = os.path.join(self.mask_dir,fileBaseName)
# labelSavePath = os.path.join(params['seg_dir'],fileBaseName)
print("Saved binary mask image as: ", savePath)
if not os.path.isdir(self.mask_dir):
os.makedirs(self.mask_dir)
# saveImg = self.maskQimage.convertToFormat(QImage.Format_Grayscale8)
# This was bugging out and making the image not the same size as it started
# saveImg = self.maskQimage.convertToFormat(QImage.Format_Grayscale8).scaled(self.originalWidth,self.originalHeight,aspectRatioMode=Qt.KeepAspectRatio)
saveImg = self.maskQimage.convertToFormat(QImage.Format_Grayscale8).scaled(self.originalWidth,self.originalHeight)
qimgHeight = saveImg.height()
qimgWidth = saveImg.width()
print(self.originalHeight, self.originalWidth, qimgHeight, qimgWidth)
saveArr = np.zeros((qimgHeight,qimgWidth),dtype='uint8')
for rowIndex in range(qimgHeight):
for colIndex in range(qimgWidth):
pixVal = qGray(saveImg.pixel(colIndex,rowIndex))
if pixVal > 0:
saveArr[rowIndex,colIndex] = 1
io.imsave(savePath, saveArr)
# labelArr = measure.label(saveArr, connectivity=1)
# labelArr = labelArr.astype('uint8')
# print(labelSavePath)
# io.imsave(labelSavePath,labelArr)
def reset(self):
self.maskQimage = QImage(self.RGBAImg, self.originalWidth, self.originalHeight, self.RGBAImg.strides[0], QImage.Format_RGBA8888).scaled(1024, 1024, aspectRatioMode=Qt.KeepAspectRatio)
self.maskQpixmap = QPixmap(self.maskQimage)
self.label.setPixmap(self.maskQpixmap)
self.update()
def clear(self):
self.imgFill = QColor(0, 0, 0, self.alpha)
self.maskQimage = QImage(self.RGBAImg, self.originalWidth, self.originalHeight, self.RGBAImg.strides[0], QImage.Format_RGBA8888).scaled(1024, 1024, aspectRatioMode=Qt.KeepAspectRatio)
self.maskQimage.fill(self.imgFill)
self.maskQpixmap = QPixmap(self.maskQimage)
self.label.setPixmap(self.maskQpixmap)
self.update()
def onePx(self):
self.brushSize = 1
def threePx(self):
self.brushSize = 3
def fivePx(self):
self.brushSize = 5
def sevenPx(self):
self.brushSize = 7
def ninePx(self):
self.brushSize = 9
def blackColor(self):
self.brushColor = QColor(0, 0, 0, self.alpha)
def redColor(self):
self.brushColor = QColor(255, 0, 0, self.alpha)
def whiteColor(self):
self.brushColor = QColor(255, 255, 255, self.alpha)
# def setFOVPeakFrameIndex(self,frame_index,peak_index,fov_id):
# self.frameIndex = frame_index
# self.fov_id = fov_id
# self.imgIndex = peak_id
def setImg(self, img):
img[img>0] = 255
self.RGBImg = color.gray2rgb(img).astype('uint8')
self.RGBImg[:,:,1:] = 0 # set green and blue channels to 0 to make the transarency mask red
alphaFloat = 0.25
alphaArray = np.zeros(img.shape, dtype='uint8')
alphaArray = np.expand_dims(alphaArray, -1)
self.alpha = int(255*alphaFloat)
alphaArray[...] = self.alpha
self.RGBAImg = np.append(self.RGBImg, alphaArray, axis=-1)
self.originalHeight, self.originalWidth, self.originalChannelNumber = self.RGBAImg.shape
self.maskQimage = QImage(self.RGBAImg, self.originalWidth, self.originalHeight, self.RGBAImg.strides[0], QImage.Format_RGBA8888).scaled(1024, 1024, aspectRatioMode=Qt.KeepAspectRatio)
self.maskQpixmap = QPixmap(self.maskQimage)
self.label.setPixmap(self.maskQpixmap)
def next_frame(self):
self.frameIndex += 1
try:
experiment_name = params['experiment_name']
original_file_name = self.maskImgPath
pat = re.compile(r'.+(xy\d{3,4})_(p\d{3,4})_.+') # supports 3- or 4-digit naming
mat = pat.match(original_file_name)
fovID = mat.groups()[0]
peakID = mat.groups()[1]
fileBaseName = '{}_{}_{}_t{:0=4}.tif'.format(experiment_name, fovID, peakID, self.frameIndex+1)
savePath = os.path.join(self.mask_dir,fileBaseName)
if os.path.isfile(savePath):
print('Re-annotated mask exists in training directory. Loading it.')
# add widget to express whether this mask is one you already re-annotated
self.maskStack[self.frameIndex,:,:] = io.imread(savePath)
img = self.maskStack[self.frameIndex,:,:]
except IndexError:
sys.exit("You've already edited the last frame's mask. Write in functionality to increment to next peak_id now!")
self.setImg(img)
def prior_frame(self):
self.frameIndex -= 1
try:
experiment_name = params['experiment_name']
original_file_name = self.maskImgPath
pat = re.compile(r'.+(xy\d{3,4})_(p\d{3,4})_.+') # supports 3- or 4-digit naming
mat = pat.match(original_file_name)
fovID = mat.groups()[0]
peakID = mat.groups()[1]
fileBaseName = '{}_{}_{}_t{:0=4}.tif'.format(experiment_name, fovID, peakID, self.frameIndex+1)
savePath = os.path.join(self.mask_dir,fileBaseName)
if os.path.isfile(savePath):
print('Re-annotated mask exists in training directory. Loading it.')
# add widget to express whether this mask is one you already re-annotated
self.maskStack[self.frameIndex,:,:] = io.imread(savePath)
img = self.maskStack[self.frameIndex,:,:]
except IndexError:
sys.exit("You've already edited the last frame's mask. Write in functionality to increment to next peak_id now!")
self.setImg(img)
def next_peak(self):
self.imgIndex += 1
self.maskImgPath = self.imgPaths[self.fov_id][self.imgIndex][1]
self.maskStack = io.imread(self.maskImgPath)
self.frameIndex = 0
experiment_name = params['experiment_name']
original_file_name = self.maskImgPath
pat = re.compile(r'.+(xy\d{3,4})_(p\d{3,4})_.+') # supports 3- or 4-digit naming
mat = pat.match(original_file_name)
fovID = mat.groups()[0]
peakID = mat.groups()[1]
fileBaseName = '{}_{}_{}_t{:0=4}.tif'.format(experiment_name, fovID, peakID, self.frameIndex+1)
savePath = os.path.join(self.mask_dir,fileBaseName)
if os.path.isfile(savePath):
print('Re-annotated mask exists in training directory. Loading it.')
# add widget to express whether this mask is one you already re-annotated
self.maskStack[self.frameIndex,:,:] = io.imread(savePath)
img = self.maskStack[self.frameIndex,:,:]
self.setImg(img)
def prior_peak(self):
self.imgIndex -= 1
self.maskImgPath = self.imgPaths[self.fov_id][self.imgIndex][1]
self.maskStack = io.imread(self.maskImgPath)
self.frameIndex = 0
experiment_name = params['experiment_name']
original_file_name = self.maskImgPath
pat = re.compile(r'.+(xy\d{3,4})_(p\d{3,4})_.+') # supports 3- or 4-digit naming
mat = pat.match(original_file_name)
fovID = mat.groups()[0]
peakID = mat.groups()[1]
fileBaseName = '{}_{}_{}_t{:0=4}.tif'.format(experiment_name, fovID, peakID, self.frameIndex+1)
savePath = os.path.join(self.mask_dir,fileBaseName)
if os.path.isfile(savePath):
print('Re-annotated mask exists in training directory. Loading it.')
# add widget to express whether this mask is one you already re-annotated
self.maskStack[self.frameIndex,:,:] = io.imread(savePath)
img = self.maskStack[self.frameIndex,:,:]
self.setImg(img)
def next_fov(self):
self.fovIndex += 1
self.fov_id = self.fov_id_list[self.fovIndex]
self.imgIndex = 0
self.maskImgPath = self.imgPaths[self.fov_id][self.imgIndex][1]
self.maskStack = io.imread(self.maskImgPath)
self.frameIndex = 0
experiment_name = params['experiment_name']
original_file_name = self.maskImgPath
pat = re.compile(r'.+(xy\d{3,4})_(p\d{3,4})_.+') # supports 3- or 4-digit naming
mat = pat.match(original_file_name)
fovID = mat.groups()[0]
peakID = mat.groups()[1]
fileBaseName = '{}_{}_{}_t{:0=4}.tif'.format(experiment_name, fovID, peakID, self.frameIndex+1)
savePath = os.path.join(self.mask_dir,fileBaseName)
if os.path.isfile(savePath):
print('Re-annotated mask exists in training directory. Loading it.')
# add widget to express whether this mask is one you already re-annotated
self.maskStack[self.frameIndex,:,:] = io.imread(savePath)
img = self.maskStack[self.frameIndex,:,:]
self.setImg(img)
def prior_fov(self):
self.fovIndex -= 1
self.fov_id = self.fov_id_list[self.fovIndex]
self.imgIndex = 0
self.maskImgPath = self.imgPaths[self.fov_id][self.imgIndex][1]
self.maskStack = io.imread(self.maskImgPath)
self.frameIndex = 0
experiment_name = params['experiment_name']
original_file_name = self.maskImgPath
pat = re.compile(r'.+(xy\d{3,4})_(p\d{3,4})_.+') # supports 3- or 4-digit naming
mat = pat.match(original_file_name)
fovID = mat.groups()[0]
peakID = mat.groups()[1]
fileBaseName = '{}_{}_{}_t{:0=4}.tif'.format(experiment_name, fovID, peakID, self.frameIndex+1)
savePath = os.path.join(self.mask_dir,fileBaseName)
if os.path.isfile(savePath):
print('Re-annotated mask exists in training directory. Loading it.')
# add widget to express whether this mask is one you already re-annotated
self.maskStack[self.frameIndex,:,:] = io.imread(savePath)
img = self.maskStack[self.frameIndex,:,:]
self.setImg(img)
class PhaseWidget(QWidget):
def __init__(self, parent,imgPaths,fov_id_list,image_dir):#,frame_index,peak_id,fov_id):
super(PhaseWidget, self).__init__(parent)
self.image_dir = image_dir
self.imgPaths = imgPaths
self.fov_id_list = fov_id_list
self.fovIndex = 0
self.fov_id = self.fov_id_list[self.fovIndex]
self.imgIndex = 0
self.phaseImgPath = self.imgPaths[self.fov_id][self.imgIndex][0]
self.phaseStack = io.imread(self.phaseImgPath)
self.frameIndex = 0
self.img = self.phaseStack[self.frameIndex,:,:]
# self.originalImgMax = np.max(self.img)
self.originalImgMax = np.max(self.phaseStack)
originalRGBImg = color.gray2rgb(self.img/2**16*2**8).astype('uint8')
self.originalPhaseQImage = QImage(originalRGBImg, originalRGBImg.shape[1], originalRGBImg.shape[0], originalRGBImg.strides[0], QImage.Format_RGB888)
rescaledImg = self.img/self.originalImgMax*255
RGBImg = color.gray2rgb(rescaledImg).astype('uint8')
self.originalHeight, self.originalWidth, self.originalChannelNumber = RGBImg.shape
self.phaseQimage = QImage(RGBImg, RGBImg.shape[1], RGBImg.shape[0], RGBImg.strides[0], QImage.Format_RGB888).scaled(1024, 1024, aspectRatioMode=Qt.KeepAspectRatio)
self.phaseQpixmap = QPixmap(self.phaseQimage)
self.label = QLabel(self)
self.label.setPixmap(self.phaseQpixmap)
def setImg(self):
# self.originalImgMax = np.max(self.img)
originalRGBImg = color.gray2rgb(self.img/2**16*2**8).astype('uint8')
self.originalPhaseQImage = QImage(originalRGBImg, originalRGBImg.shape[1], originalRGBImg.shape[0], originalRGBImg.strides[0], QImage.Format_RGB888)
# rescaledImg = self.img/np.max(self.img)*255
rescaledImg = self.img/self.originalImgMax*255
RGBImg = color.gray2rgb(rescaledImg).astype('uint8')
self.phaseQimage = QImage(RGBImg, RGBImg.shape[1], RGBImg.shape[0], RGBImg.strides[0], QImage.Format_RGB888).scaled(1024, 1024, aspectRatioMode=Qt.KeepAspectRatio)
self.phaseQpixmap = QPixmap(self.phaseQimage)
self.label.setPixmap(self.phaseQpixmap)
def next_frame(self):
self.frameIndex += 1
try:
self.img = self.phaseStack[self.frameIndex,:,:]
except IndexError:
sys.exit("You've already edited the last frame's mask. Write in functionality to increment to next peak_id now!")
self.setImg()
def prior_frame(self):
self.frameIndex -= 1
try:
self.img = self.phaseStack[self.frameIndex,:,:]
except IndexError:
sys.exit("You've already edited the last frame's mask. Write in functionality to increment to next peak_id now!")
self.setImg()
def next_peak(self):
self.imgIndex += 1
self.phaseImgPath = self.imgPaths[self.fov_id][self.imgIndex][0]
self.phaseStack = io.imread(self.phaseImgPath)
self.frameIndex = 0
self.img = self.phaseStack[self.frameIndex,:,:]
self.setImg()
def prior_peak(self):
self.imgIndex -= 1
self.phaseImgPath = self.imgPaths[self.fov_id][self.imgIndex][0]
self.phaseStack = io.imread(self.phaseImgPath)
self.frameIndex = 0
self.img = self.phaseStack[self.frameIndex,:,:]
self.setImg()
def next_fov(self):
self.fovIndex += 1
self.fov_id = self.fov_id_list[self.fovIndex]
self.imgIndex = 0
self.phaseImgPath = self.imgPaths[self.fov_id][self.imgIndex][0]
self.phaseStack = io.imread(self.phaseImgPath)
self.frameIndex = 0
self.img = self.phaseStack[self.frameIndex,:,:]
self.setImg()
def prior_fov(self):
self.fovIndex -= 1
self.fov_id = self.fov_id_list[self.fovIndex]
self.imgIndex = 0
self.phaseImgPath = self.imgPaths[self.fov_id][self.imgIndex][0]
self.phaseStack = io.imread(self.phaseImgPath)
self.frameIndex = 0
self.img = self.phaseStack[self.frameIndex,:,:]
self.setImg()
def buttonSave(self):
experiment_name = params['experiment_name']
original_file_name = self.phaseImgPath
pat = re.compile(r'.+(xy\d{3,4})_(p\d{3,4})_.+')
mat = pat.match(original_file_name)
fovID = mat.groups()[0]
peakID = mat.groups()[1]
fileBaseName = '{}_{}_{}_t{:0=4}.tif'.format(experiment_name, fovID, peakID, self.frameIndex+1)
savePath = os.path.join(self.image_dir,fileBaseName)
print("Saved phase image as: ", savePath)
if not os.path.isdir(self.image_dir):
os.makedirs(self.image_dir)
# saveImg = self.originalPhaseQImage.convertToFormat(QImage.Format_Grayscale8)
# saveImg.save(savePath)
io.imsave(savePath, self.img)
if __name__ == "__main__":
imgPaths = {1:[('/home/wanglab/Users_local/Jeremy/Imaging/20190214/analysis/channels/20190214_JDW3418_xy001_p0028_c1.tif',
'/home/wanglab/Users_local/Jeremy/Imaging/20190214/analysis/segmented/20190214_JDW3418_xy001_p0028_seg_unet.tif'),
('/home/wanglab/Users_local/Jeremy/Imaging/20190214/analysis/channels/20190214_JDW3418_xy001_p0039_c1.tif',
'/home/wanglab/Users_local/Jeremy/Imaging/20190214/analysis/segmented/20190214_JDW3418_xy001_p0039_seg_unet.tif')],
2:[('/home/wanglab/Users_local/Jeremy/Imaging/20190214/analysis/channels/20190214_JDW3418_xy001_p0127_c1.tif',
'/home/wanglab/Users_local/Jeremy/Imaging/20190214/analysis/segmented/20190214_JDW3418_xy001_p0127_seg_unet.tif'),
('/home/wanglab/Users_local/Jeremy/Imaging/20190214/analysis/channels/20190214_JDW3418_xy001_p0104_c1.tif',
'/home/wanglab/Users_local/Jeremy/Imaging/20190214/analysis/segmented/20190214_JDW3418_xy001_p0104_seg_unet.tif')]}
fov_id_list = [1,2]
training_dir = '/home/wanglab/sandbox/pyqtpainter/commonDir'
init_params('/home/wanglab/Users_local/Jeremy/Imaging/20190214/20190214_params_Unet.yaml')
app = QApplication(sys.argv)
window = Window(imgPaths=imgPaths, fov_id_list=fov_id_list, training_dir=training_dir)
window.show()
app.exec_()