-
Notifications
You must be signed in to change notification settings - Fork 1
/
GUI_App.py
280 lines (222 loc) · 11.2 KB
/
GUI_App.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
import os
import sys
from pathlib import Path
import numpy as np
import skimage.io as skio
import tifffile as tf
from PyQt5.QtCore import Qt, QThread, pyqtSignal
from PyQt5.QtWidgets import (QApplication, QMainWindow, QVBoxLayout, QHBoxLayout, QFrame,
QLabel, QLineEdit, QGroupBox, QComboBox, QPushButton, QFileDialog,
QProgressDialog)
from .util import inputoutput as io
from .psf.psf import PSF
from .util.deconvolution import Deconvolution
class Worker(QThread):
"""
This class executes the main work and is called when the "Start the Deconvolution" Button is pressed
"""
progress = pyqtSignal(int)
message = pyqtSignal(str)
def __init__(self, psf_parameters, decon_parameters, io_parameters, parent=None):
super().__init__(parent)
self.psf_data = psf_parameters
self.decon_data = decon_parameters
self.io_data = io_parameters
def run(self):
# ------------- load image -----------
self.progress.emit(10)
self.message.emit("Loading image") # send message to progress window
img = skio.imread(self.io_data['read_path'])
# correct shape if image not quadratic
if img.ndim == 2:
if img.shape[0] != img.shape[1]:
minshape = np.min(img.shape)
img = img[:minshape, :minshape]
xdims = img.shape
else:
if img.shape[1] != img.shape[2]:
minshape = np.min(img.shape)
img = img[:, minshape, :minshape]
xdims = (img.shape[1], img.shape[2])
# ------------- create PSF ----------------
self.psf_data['xysize'] = xdims[0]
self.message.emit("Creating PSF")
self.progress.emit(20)
pf = PSF(xdims, **self.psf_data)
# ------------- start deconvolution -------
Dec = Deconvolution(pf.data, img, self.decon_data['lambda'],
self.decon_data['lambda_t'],
self.decon_data['epsilon'],
self.decon_data['max_iterations'], verbose=False)
self.message.emit("Deconvolution in Progress")
self.progress.emit(30)
result = Dec.deconvolve_shortcut()
# ------------- save results -------
io.set_baseline(img, result)
os.makedirs(self.io_data['save_path'], exist_ok=True)
self.message.emit("Saving Results")
tf.imwrite(self.io_data['save_path']+ '/'+self.io_data['save_name'], result, photometric='minisblack')
class ParameterInputWindow(QMainWindow):
"""
This class creates the main GUI input window where parameters are given
"""
def __init__(self):
super().__init__()
self.setWindowTitle("Parameter Input")
self.setGeometry(100, 100, 400, 300)
main_widget = QFrame(self)
self.setCentralWidget(main_widget)
layout = QVBoxLayout(main_widget)
self.create_psf_section()
self.create_decon_section()
self.create_fileinput_section()
layout.addWidget(self.psf_section)
layout.addWidget(self.decon_section)
layout.addWidget(self.fileinput_section)
# Button to start the deconvolution
self.show_progress_button = QPushButton("Start Deconvolution", main_widget)
layout.addWidget(self.show_progress_button)
self.show_progress_button.clicked.connect(self.show_progress_dialog)
# -------------------- PSF parameters ----------------------------
def create_psf_section(self):
self.psf_section = QGroupBox("PSF Parameters", self)
psf_section_layout = QVBoxLayout(self.psf_section)
self.type_label = QLabel("Microscope type", self.psf_section)
psf_section_layout.addWidget(self.type_label)
self.type_combo = QComboBox(self.psf_section)
self.type_combo.addItems(["confocal", "widefield"])
psf_section_layout.addWidget(self.type_combo)
self.ex_label = QLabel("Excitation wavelength (nm)", self.psf_section)
psf_section_layout.addWidget(self.ex_label)
self.ex_entry = QLineEdit(self.psf_section)
self.ex_entry.setText("488")
psf_section_layout.addWidget(self.ex_entry)
self.em_label = QLabel("Emission wavelength (nm)", self.psf_section)
psf_section_layout.addWidget(self.em_label)
self.em_entry = QLineEdit(self.psf_section)
self.em_entry.setText("600")
psf_section_layout.addWidget(self.em_entry)
self.na_label = QLabel("Numerical aperture of the objective", self.psf_section)
psf_section_layout.addWidget(self.na_label)
self.na_entry = QLineEdit(self.psf_section)
self.na_entry.setText("1.4")
psf_section_layout.addWidget(self.na_entry)
self.mo_label = QLabel("Objective total magnification", self.psf_section)
psf_section_layout.addWidget(self.mo_label)
self.mo_entry = QLineEdit(self.psf_section)
self.mo_entry.setText("100")
psf_section_layout.addWidget(self.mo_entry)
self.rind_obj_label = QLabel("Refractive index of the objective immersion medium", self.psf_section)
psf_section_layout.addWidget(self.rind_obj_label)
self.rind_obj_entry = QLineEdit(self.psf_section)
self.rind_obj_entry.setText("1.518")
psf_section_layout.addWidget(self.rind_obj_entry)
self.rind_sp_label = QLabel("Refractive index of the specimen medium", self.psf_section)
psf_section_layout.addWidget(self.rind_sp_label)
self.rind_sp_entry = QLineEdit(self.psf_section)
self.rind_sp_entry.setText("1.518")
psf_section_layout.addWidget(self.rind_sp_entry)
self.ccd_label = QLabel("Pixel dimension of the CCD (in the plane of the camera)", self.psf_section)
psf_section_layout.addWidget(self.ccd_label)
self.ccd_entry = QLineEdit(self.psf_section)
self.ccd_entry.setText("6450")
psf_section_layout.addWidget(self.ccd_entry)
# -------------------- Deconvolution parameters ----------------------------
def create_decon_section(self):
self.decon_section = QGroupBox("Decon Parameters", self)
decon_section_layout = QVBoxLayout(self.decon_section)
self.lam_label = QLabel("𝝺 (weight for spatial regularization)", self.decon_section)
decon_section_layout.addWidget(self.lam_label)
self.lam_entry = QLineEdit(self.decon_section)
self.lam_entry.setText("0.5")
decon_section_layout.addWidget(self.lam_entry)
self.lam_t_label = QLabel("𝝺_t (weight for temporal regularization)", self.decon_section)
decon_section_layout.addWidget(self.lam_t_label)
self.lam_t_entry = QLineEdit(self.decon_section)
self.lam_t_entry.setText("0.5")
decon_section_layout.addWidget(self.lam_t_entry)
# -------------------- input output parameters ----------------------------
def create_fileinput_section(self):
self.fileinput_section = QGroupBox("Input/ Output", self)
fileinput_section_layout = QVBoxLayout(self.fileinput_section)
self.input_label = QLabel("Select a file to process:", self.fileinput_section)
fileinput_section_layout.addWidget(self.input_label)
file_selector_layout = QHBoxLayout()
self.file_path_label = QLabel("", self.fileinput_section)
file_selector_layout.addWidget(self.file_path_label)
select_file_btn = QPushButton("Browse", self.fileinput_section)
select_file_btn.clicked.connect(self.select_file)
file_selector_layout.addWidget(select_file_btn)
fileinput_section_layout.addLayout(file_selector_layout)
self.output_label = QLabel("Select a folder to save results in:", self.fileinput_section)
fileinput_section_layout.addWidget(self.output_label)
folder_selector_layout = QHBoxLayout()
self.folder_path_label = QLabel("", self.fileinput_section)
folder_selector_layout.addWidget(self.folder_path_label)
select_folder_btn = QPushButton("Browse", self.fileinput_section)
select_folder_btn.clicked.connect(self.select_folder)
folder_selector_layout.addWidget(select_folder_btn)
fileinput_section_layout.addLayout(folder_selector_layout)
def select_file(self):
file_path, _ = QFileDialog.getOpenFileName()
self.file_path_label.setText(file_path)
def select_folder(self):
folder_path = QFileDialog.getExistingDirectory()
self.folder_path_label.setText(folder_path)
def set_parameter_dict(self):
"""
save the GUI parameters into dictionaries
"""
self.parameters = dict()
self.psf_data = dict()
self.psf_data['type'] = self.type_combo.currentText()
self.psf_data['lambdaEx'] = float(self.ex_entry.text())
self.psf_data['lambdaEm'] = float(self.em_entry.text())
self.psf_data['numAper'] = float(self.na_entry.text())
self.psf_data['magObj'] = float(self.mo_entry.text())
self.psf_data['rindexObj'] = float(self.rind_obj_entry.text())
self.psf_data['rindexSp'] = float(self.rind_sp_entry.text())
self.psf_data['ccdSize'] = float(self.ccd_entry.text())
self.psf_data['nor'] = 0
self.psf_data['depth'] = 0
self.psf_data['nslices'] = 1
self.psf_data['dz'] = 0
self.decon_data = dict()
self.decon_data['lambda'] = float(self.lam_entry.text())
self.decon_data['lambda_t'] = float(self.lam_t_entry.text())
self.decon_data['epsilon'] = 0.001
self.decon_data['max_iterations'] = 1
self.decon_data['delta'] = 1
self.io_data = dict()
self.io_data['read_path'] = self.file_path_label.text()
self.io_data['save_path'] = self.folder_path_label.text()
fname = Path(self.io_data['read_path']).stem
self.io_data['save_name'] = fname+ '_deconvolved.tif'
def show_progress_dialog(self):
"""
Show a small window with a progress bar and call the worker to execute the deconvolution
"""
self.set_parameter_dict()
self.worker = Worker(self.psf_data, self.decon_data, self.io_data)
# set an indeterminate progress bar, for a "loading" bar, change the second "0" to "100"
self.progress_dialog = QProgressDialog("Working...", "Cancel", 0, 0, self)
self.progress_dialog.setWindowTitle("Deconvolution in Progress")
self.progress_dialog.setWindowModality(Qt.WindowModal)
self.progress_dialog.setAutoClose(True)
self.progress_dialog.setAutoReset(True)
self.progress_dialog.setMinimumDuration(10)
# receive signals: progress status (if not indeterminate) and status message
self.worker.progress.connect(self.progress_dialog.setValue)
self.worker.message.connect(self.update_progress_label)
self.worker.finished.connect(self.on_worker_finished)
self.progress_dialog.canceled.connect(self.worker.terminate)
self.worker.start()
def on_worker_finished(self):
self.close()
def update_progress_label(self, message):
self.progress_dialog.setLabelText(message)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = ParameterInputWindow()
window.show()
sys.exit(app.exec_())