-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbackgroundWindow.py
More file actions
547 lines (370 loc) · 20.6 KB
/
backgroundWindow.py
File metadata and controls
547 lines (370 loc) · 20.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
# general imports
import numpy
# import PyQt5 elements
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QFileDialog, QProgressDialog, QPushButton, QMainWindow, QWidget
# import UI
from UIs.backgroundWidgetUi import UiBackgroundWidget
class BackgroundWidget(QWidget):
"""
BackgroundWidget
This widget is used to remove background from a single pixel or the whole map.
"""
# TODO: implement 1D maps
def __init__(self, parent, map_handle):
# call widget init
super().__init__()
# link app
self._app = QApplication.instance()
# link map handle
self._map = map_handle
# load and set up UI
self.ui = UiBackgroundWidget(self)
# set limits to for the interval selection
self.ui.lower_boundary_slider.setMaximum(self._map.get_resolution()-1)
self.ui.upper_boundary_slider.setMaximum(self._map.get_resolution()-1)
self.ui.upper_boundary_slider.setValue(self._map.get_resolution()-1)
# fill combo boxes for pixel selection
self.ui.x_spin_box.setMaximum(self._map.get_size()[0]-1)
if self._map.get_dimension() == 2:
self.ui.y_spin_box.setMaximum(self._map.get_size()[1]-1)
# set parent
self.setParent(parent)
# link callbacks for radio buttons
self.ui.minimum_counts_radio_button.toggled.connect(self.cb_background_settings_buttons)
self.ui.interval_average_radio_button.toggled.connect(self.cb_background_settings_buttons)
self.ui.background_from_pixel_radio_button.toggled.connect(self.cb_background_settings_buttons)
self.ui.background_from_file_radio_button.toggled.connect(self.cb_background_settings_buttons)
# link callbacks for sliders
self.ui.lower_boundary_slider.sliderPressed.connect(self.cb_boundary_slider_pressed)
self.ui.lower_boundary_slider.sliderMoved.connect(self.cb_boundary_slider_moved)
self.ui.lower_boundary_slider.sliderReleased.connect(self.cb_boundary_slider_released)
self.ui.upper_boundary_slider.sliderPressed.connect(self.cb_boundary_slider_pressed)
self.ui.upper_boundary_slider.sliderMoved.connect(self.cb_boundary_slider_moved)
self.ui.upper_boundary_slider.sliderReleased.connect(self.cb_boundary_slider_released)
# link callback for button
self.ui.file_browse_push_button.clicked.connect(self.cb_file_browse_push_button)
self.ui.remove_background_push_button.clicked.connect(self.cb_remove_background_push_button)
def cb_background_settings_buttons(self):
# check which radio button is clicked and enable or disable the respective UI elements
if self.ui.minimum_counts_radio_button.isChecked():
self.ui.lower_boundary_slider.setEnabled(False)
self.ui.upper_boundary_slider.setEnabled(False)
self.ui.x_spin_box.setEnabled(False)
self.ui.y_spin_box.setEnabled(False)
self.ui.file_browse_push_button.setEnabled(False)
self.ui.file_path_line_edit.setEnabled(False)
elif self.ui.interval_average_radio_button.isChecked():
self.ui.lower_boundary_slider.setEnabled(True)
self.ui.upper_boundary_slider.setEnabled(True)
self.ui.x_spin_box.setEnabled(False)
self.ui.y_spin_box.setEnabled(False)
self.ui.file_browse_push_button.setEnabled(False)
self.ui.file_path_line_edit.setEnabled(False)
elif self.ui.background_from_pixel_radio_button.isChecked():
self.ui.lower_boundary_slider.setEnabled(False)
self.ui.upper_boundary_slider.setEnabled(False)
self.ui.x_spin_box.setEnabled(True)
if self._map.get_dimension() == 2:
self.ui.y_spin_box.setEnabled(True)
self.ui.file_browse_push_button.setEnabled(False)
self.ui.file_path_line_edit.setEnabled(False)
elif self.ui.background_from_file_radio_button.isChecked():
self.ui.lower_boundary_slider.setEnabled(False)
self.ui.upper_boundary_slider.setEnabled(False)
self.ui.x_spin_box.setEnabled(False)
self.ui.y_spin_box.setEnabled(False)
self.ui.file_browse_push_button.setEnabled(True)
self.ui.file_path_line_edit.setEnabled(True)
def cb_file_browse_push_button(self):
# get file name
file_name = QFileDialog.getOpenFileName(self._app.windows['backgroundWindow'], 'Select File', '')
file_name = file_name[0]
# check if a file has been selected
if file_name == '':
return
# set file name to line edit
self.ui.file_path_line_edit.setText(file_name)
def cb_remove_background_push_button(self):
# remove background on focused pixel
if self.ui.focused_pixel_radio_button.isChecked():
# load spectrum
spectrum = self._map.get_spectrum()
if self.ui.minimum_counts_radio_button.isChecked():
# remove minimum as a background
spectrum[:, 1] = spectrum[:, 1]-numpy.min(spectrum[:, 1])
elif self.ui.interval_average_radio_button.isChecked():
# remove interval average as a background
spectrum[:, 1] = spectrum[:, 1]-numpy.mean(
spectrum[self.ui.lower_boundary_slider.value():self.ui.upper_boundary_slider.value(), 1])
elif self.ui.background_from_pixel_radio_button.isChecked():
# remove spectrum at specific pixel as a background
spectrum[:, 1] = spectrum[:, 1]-self._map.get_spectrum(
pixel=[self.ui.x_spin_box.value(), self.ui.y_spin_box.value()])[:, 1]
elif self.ui.background_from_file_radio_button.isChecked():
# remove spectrum from file as a background
background = numpy.loadtxt(self.ui.file_path_line_edit.text())
spectrum[:, 1] = spectrum[:, 1]-background[:, 1]
# update spectrum
self._map.set_spectrum(spectrum, emit=True)
# remove background on whole map
else:
# check the dimension of the map
if self._map.get_dimension() == 1:
# get map size
nx = self._map.get_size()[0]
# create progressbar dialog
progress_dialog = QProgressDialog('', '', 0, nx, self._app.windows['backgroundWindow'])
progress_dialog.setWindowTitle('Removing Background')
progress_dialog.setWindowModality(Qt.WindowModal)
progress_dialog_cancel_button = QPushButton('Stop')
progress_dialog.setCancelButton(progress_dialog_cancel_button)
progress_dialog.show()
# start live plotting
self._app.start_live_plotting()
# remove background on each pixel
if self.ui.minimum_counts_radio_button.isChecked():
for ix in range(nx):
# check if process was cancelled
if progress_dialog.wasCanceled():
break
# load spectrum
spectrum = self._map.get_spectrum(pixel=[ix])
# remove minimum as a background
spectrum[:, 1] = spectrum[:, 1]-numpy.min(spectrum[:, 1])
if ix == self._map.get_focus()[0]:
# update spectrum
self._map.set_spectrum(spectrum, pixel=[ix], emit=True)
else:
# update spectrum
self._map.set_spectrum(spectrum, pixel=[ix], emit=False)
# process events
self._app.processEvents()
# update progress bar
progress_dialog.setValue(ix + 1)
elif self.ui.interval_average_radio_button.isChecked():
for ix in range(nx):
# check if process was cancelled
if progress_dialog.wasCanceled():
break
# load spectrum
spectrum = self._map.get_spectrum(pixel=[ix])
# remove interval average as a background
spectrum[:, 1] = spectrum[:, 1]-numpy.mean(
spectrum[self.ui.lower_boundary_slider.value():self.ui.upper_boundary_slider.value(), 1])
if ix == self._map.get_focus()[0]:
# update spectrum
self._map.set_spectrum(spectrum, pixel=[ix], emit=True)
else:
# update spectrum
self._map.set_spectrum(spectrum, pixel=[ix], emit=False)
# process events
self._app.processEvents()
# update progress bar
progress_dialog.setValue(ix + 1)
elif self.ui.background_from_pixel_radio_button.isChecked():
background = numpy.copy(self._map.get_spectrum(pixel=[self.ui.x_spin_box.value()])[:, 1])
for ix in range(nx):
# check if process was cancelled
if progress_dialog.wasCanceled():
break
# load spectrum
spectrum = self._map.get_spectrum(pixel=[ix])
# remove spectrum at specific pixel as a background
spectrum[:, 1] = spectrum[:, 1] - background
if ix == self._map.get_focus()[0]:
# update spectrum
self._map.set_spectrum(spectrum, pixel=[ix], emit=True)
else:
# update spectrum
self._map.set_spectrum(spectrum, pixel=[ix], emit=False)
# process events
self._app.processEvents()
# update progress bar
progress_dialog.setValue(ix + 1)
elif self.ui.background_from_file_radio_button.isChecked():
background = numpy.loadtxt(self.ui.file_path_line_edit.text())
for ix in range(nx):
# check if process was cancelled
if progress_dialog.wasCanceled():
break
# load spectrum
spectrum = self._map.get_spectrum(pixel=[ix])
# remove spectrum from file as a background
spectrum[:, 1] = spectrum[:, 1]-background[:, 1]
if ix == self._map.get_focus()[0]:
# update spectrum
self._map.set_spectrum(spectrum, pixel=[ix], emit=True)
else:
# update spectrum
self._map.set_spectrum(spectrum, pixel=[ix], emit=False)
# process events
self._app.processEvents()
# update progress bar
progress_dialog.setValue(ix + 1)
# stop live plotting
self._app.stop_live_plotting()
elif self._map.get_dimension() == 2:
# get map size
nx, ny = self._map.get_size()
# create progressbar dialog
progress_dialog = QProgressDialog('', '', 0, nx * ny, self._app.windows['backgroundWindow'])
progress_dialog.setWindowTitle('Removing Background')
progress_dialog.setWindowModality(Qt.WindowModal)
progress_dialog_cancel_button = QPushButton('Stop')
progress_dialog.setCancelButton(progress_dialog_cancel_button)
progress_dialog.show()
# start live plotting
self._app.start_live_plotting()
# remove background on each pixel
if self.ui.minimum_counts_radio_button.isChecked():
for ix in range(nx):
for iy in range(ny):
# check if process was cancelled
if progress_dialog.wasCanceled():
break
# load spectrum
spectrum = self._map.get_spectrum(pixel=[ix, iy])
# remove minimum as a background
spectrum[:, 1] = spectrum[:, 1]-numpy.min(spectrum[:, 1])
if ix == self._map.get_focus()[0] and iy == self._map.get_focus()[1]:
# update spectrum
self._map.set_spectrum(spectrum, pixel=[ix, iy], emit=True)
else:
# update spectrum
self._map.set_spectrum(spectrum, pixel=[ix, iy], emit=False)
# process events
self._app.processEvents()
# update progress bar
progress_dialog.setValue(ix * ny + iy + 1)
# check if process was cancelled
if progress_dialog.wasCanceled():
break
elif self.ui.interval_average_radio_button.isChecked():
for ix in range(nx):
for iy in range(ny):
# check if process was cancelled
if progress_dialog.wasCanceled():
break
# load spectrum
spectrum = self._map.get_spectrum(pixel=[ix, iy])
# remove interval average as a background
spectrum[:, 1] = spectrum[:, 1]-numpy.mean(
spectrum[self.ui.lower_boundary_slider.value():self.ui.upper_boundary_slider.value(), 1])
if ix == self._map.get_focus()[0] and iy == self._map.get_focus()[1]:
# update spectrum
self._map.set_spectrum(spectrum, pixel=[ix, iy], emit=True)
else:
# update spectrum
self._map.set_spectrum(spectrum, pixel=[ix, iy], emit=False)
# process events
self._app.processEvents()
# update progress bar
progress_dialog.setValue(ix * ny + iy + 1)
# check if process was cancelled
if progress_dialog.wasCanceled():
break
elif self.ui.background_from_pixel_radio_button.isChecked():
background = numpy.copy(self._map.get_spectrum(pixel=[self.ui.x_spin_box.value(),
self.ui.y_spin_box.value()])[:, 1])
for ix in range(nx):
for iy in range(ny):
# check if process was cancelled
if progress_dialog.wasCanceled():
break
# load spectrum
spectrum = self._map.get_spectrum(pixel=[ix, iy])
# remove spectrum at specific pixel as a background
spectrum[:, 1] = spectrum[:, 1] - background
if ix == self._map.get_focus()[0] and iy == self._map.get_focus()[1]:
# update spectrum
self._map.set_spectrum(spectrum, pixel=[ix, iy], emit=True)
else:
# update spectrum
self._map.set_spectrum(spectrum, pixel=[ix, iy], emit=False)
# process events
self._app.processEvents()
# update progress bar
progress_dialog.setValue(ix * ny + iy + 1)
# check if process was cancelled
if progress_dialog.wasCanceled():
break
elif self.ui.background_from_file_radio_button.isChecked():
background = numpy.loadtxt(self.ui.file_path_line_edit.text())
for ix in range(nx):
for iy in range(ny):
# check if process was cancelled
if progress_dialog.wasCanceled():
break
# load spectrum
spectrum = self._map.get_spectrum(pixel=[ix, iy])
# remove spectrum from file as a background
spectrum[:, 1] = spectrum[:, 1]-background[:, 1]
if ix == self._map.get_focus()[0] and iy == self._map.get_focus()[1]:
# update spectrum
self._map.set_spectrum(spectrum, pixel=[ix, iy], emit=True)
else:
# update spectrum
self._map.set_spectrum(spectrum, pixel=[ix, iy], emit=False)
# process events
self._app.processEvents()
# update progress bar
progress_dialog.setValue(ix * ny + iy + 1)
# check if process was cancelled
if progress_dialog.wasCanceled():
break
# stop live plotting
self._app.stop_live_plotting()
def cb_boundary_slider_moved(self):
# update cursors on spectru mwindow
self._app.windows['spectrumWindow'].get_current_widget().update_cursors(self.ui.lower_boundary_slider.value(),
self.ui.upper_boundary_slider.value())
def cb_boundary_slider_pressed(self):
# create cursors on spectru mwindow
self._app.windows['spectrumWindow'].get_current_widget().create_cursors(self.ui.lower_boundary_slider.value(),
self.ui.upper_boundary_slider.value())
def cb_boundary_slider_released(self):
# destroy cursors on spectru mwindow
self._app.windows['spectrumWindow'].get_current_widget().destroy_cursors()
# bring window to front
self.show()
self.setWindowState(self.windowState() & ~Qt.WindowMinimized | Qt.WindowActive)
self.activateWindow()
class BackgroundWindow(QMainWindow):
"""
BackgroundWindow
Hosts a background widget for each loaded map.
"""
def __init__(self, parent=None):
# call widget init
QWidget.__init__(self, parent)
# link app
self._app = QApplication.instance()
# dictionary for remove background widgets
self._background_widgets = {}
# set fixed size
self.setFixedWidth(320)
self.setFixedHeight(321)
# set window title
self.setWindowTitle("Remove Background")
def add_widget(self, map_id):
# get map handle
map_handle = self._app.maps.get_map(map_id)
# add a widget to the widgets list
self._background_widgets[map_handle.get_id()] = BackgroundWidget(self, map_handle)
def change_widget(self, map_id):
# get map handle
map_handle = self._app.maps.get_map(map_id)
# hide all widgets
for key in self._background_widgets.keys():
self._background_widgets[key].setVisible(False)
# make the widget for the select map visible
self._background_widgets[map_handle.get_id()].setVisible(True)
def remove_widget(self, map_id):
# remove the widget of the deleted map
self._background_widgets[map_id].setParent(None)
self._background_widgets[map_id].deleteLater()
del self._background_widgets[map_id]
# close the window if there are no more maps
if len(self._background_widgets) == 0:
self.close()