| 1 |
|
| 2 |
""" |
| 3 |
Copyright (c) 2018 Rolf Hempel, [email protected] |
| 4 |
|
| 5 |
This file is part of the PlanetarySystemStacker tool (PSS). |
| 6 |
https://github.com/Rolf-Hempel/PlanetarySystemStacker |
| 7 |
|
| 8 |
PSS is free software: you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation, either version 3 of the License, or |
| 11 |
(at your option) any later version. |
| 12 |
|
| 13 |
This program is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with PSS. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
|
| 21 |
Part of this module (in class "AlignmentPointEditor" was copied from |
| 22 |
https://stackoverflow.com/questions/35508711/how-to-enable-pan-and-zoom-in-a-qgraphicsview |
| 23 |
|
| 24 |
""" |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
import sys, os |
| 29 |
sys.path.insert(0, os.path.dirname(__file__)) |
| 30 |
|
| 31 |
import getpass |
| 32 |
import platform |
| 33 |
from os import remove |
| 34 |
from pathlib import Path |
| 35 |
from sys import exit, argv |
| 36 |
|
| 37 |
import cv2 |
| 38 |
import matplotlib |
| 39 |
matplotlib.use('Agg') |
| 40 |
import numpy as np |
| 41 |
import psutil |
| 42 |
from PyQt5 import QtWidgets, QtCore, QtGui |
| 43 |
from numpy import uint8, uint16 |
| 44 |
import scipy |
| 45 |
import astropy |
| 46 |
import skimage |
| 47 |
import pip |
| 48 |
|
| 49 |
from alignment_point_editor import AlignmentPointEditorWidget |
| 50 |
from alignment_points import AlignmentPoints |
| 51 |
from configuration import Configuration |
| 52 |
from configuration_editor import ConfigurationEditor |
| 53 |
from display_quickstart import DisplayQuickstart |
| 54 |
from exceptions import NotSupportedError |
| 55 |
from frame_selector import FrameSelectorWidget |
| 56 |
from frame_viewer import FrameViewerWidget |
| 57 |
from frames import Frames |
| 58 |
from job_editor import JobEditor, FileDialog |
| 59 |
from main_gui import Ui_MainWindow |
| 60 |
from miscellaneous import Miscellaneous |
| 61 |
from postproc_editor import PostprocEditorWidget |
| 62 |
from rectangular_patch_editor import RectangularPatchEditorWidget |
| 63 |
from shift_distribution_viewer import ShiftDistributionViewerWidget |
| 64 |
from workflow import Workflow |
| 65 |
from pss_console import PssConsole |
| 66 |
|
| 67 |
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True) |
| 68 |
os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1" |
| 69 |
|
| 70 |
|
| 71 |
class DisplayImage(QtWidgets.QGraphicsView): |
| 72 |
""" |
| 73 |
This is an auxiliary class for debugging purposes. It opens a separate window where images |
| 74 |
can be displayed and updated during the computational steps. Activities are triggered from the |
| 75 |
workflow thread using signals. |
| 76 |
""" |
| 77 |
|
| 78 |
def __init__(self): |
| 79 |
""" |
| 80 |
Initialize the display window with a standard position and size. The window is resized later |
| 81 |
as required by the images to be displayed. |
| 82 |
""" |
| 83 |
super().__init__() |
| 84 |
self.title = 'PyQt5 image' |
| 85 |
self.setWindowTitle(self.title) |
| 86 |
self.left = 100 |
| 87 |
self.top = 100 |
| 88 |
self.width = 640 |
| 89 |
self.height = 480 |
| 90 |
self.setGeometry(self.left, self.top, self.width, self.height) |
| 91 |
|
| 92 |
|
| 93 |
self._scene = QtWidgets.QGraphicsScene() |
| 94 |
self._photo = QtWidgets.QGraphicsPixmapItem() |
| 95 |
self._scene.addItem(self._photo) |
| 96 |
self.setScene(self._scene) |
| 97 |
self.show() |
| 98 |
|
| 99 |
def convert_image_to_pixmap(self, image): |
| 100 |
""" |
| 101 |
Convert a color or monochrome image (stored as a Numpy array of type uint8 or uint16) into |
| 102 |
a pixmap object. |
| 103 |
|
| 104 |
:param image: Numpy array of type uint8 or uint16, size: (size_y, size_x [, 3]) |
| 105 |
:return: QPixmap object holding the image |
| 106 |
""" |
| 107 |
|
| 108 |
|
| 109 |
if image.dtype == uint16: |
| 110 |
image_uint8 = (image / 256.).astype(uint8) |
| 111 |
elif image.dtype == uint8: |
| 112 |
image_uint8 = image |
| 113 |
else: |
| 114 |
raise NotSupportedError("Attempt to set a photo with type neither" |
| 115 |
" uint8 nor uint16") |
| 116 |
self.shape_y = image_uint8.shape[0] |
| 117 |
self.shape_x = image_uint8.shape[1] |
| 118 |
|
| 119 |
|
| 120 |
if len(image_uint8.shape) == 2: |
| 121 |
qt_image = QtGui.QImage(image_uint8, self.shape_x, self.shape_y, self.shape_x, |
| 122 |
QtGui.QImage.Format_Grayscale8) |
| 123 |
|
| 124 |
else: |
| 125 |
qt_image = QtGui.QImage(image_uint8, self.shape_x, self.shape_y, 3 * self.shape_x, |
| 126 |
QtGui.QImage.Format_RGB888) |
| 127 |
return QtGui.QPixmap(qt_image) |
| 128 |
|
| 129 |
def update_image(self, new_image): |
| 130 |
""" |
| 131 |
Replace the image with a new one. |
| 132 |
|
| 133 |
:param new_image: Image, stored as a Numpy array of type uint8 or uint16, |
| 134 |
size: (size_y, size_x [, 3]) |
| 135 |
:return: - |
| 136 |
""" |
| 137 |
|
| 138 |
pixmap = self.convert_image_to_pixmap(new_image) |
| 139 |
self._photo.setPixmap(pixmap) |
| 140 |
|
| 141 |
|
| 142 |
self.resize(pixmap.width()+10, pixmap.height()+10) |
| 143 |
self.repaint() |
| 144 |
|
| 145 |
|
| 146 |
class PlanetarySystemStacker(QtWidgets.QMainWindow): |
| 147 |
""" |
| 148 |
This is the main class of the "Planetary System Stacker" software. It implements the main GUI |
| 149 |
for the communication with the user. It creates the workflow thread which controls all program |
| 150 |
activities asynchronously. |
| 151 |
""" |
| 152 |
|
| 153 |
signal_reset_masters = QtCore.pyqtSignal() |
| 154 |
signal_load_master_dark = QtCore.pyqtSignal(str) |
| 155 |
signal_load_master_flat = QtCore.pyqtSignal(str) |
| 156 |
signal_frames = QtCore.pyqtSignal(object) |
| 157 |
signal_rank_frames = QtCore.pyqtSignal() |
| 158 |
signal_select_frames = QtCore.pyqtSignal() |
| 159 |
signal_align_frames = QtCore.pyqtSignal(int, int, int, int) |
| 160 |
signal_set_roi = QtCore.pyqtSignal(int, int, int, int) |
| 161 |
signal_set_alignment_points = QtCore.pyqtSignal() |
| 162 |
signal_compute_frame_qualities = QtCore.pyqtSignal() |
| 163 |
signal_stack_frames = QtCore.pyqtSignal() |
| 164 |
signal_save_stacked_image = QtCore.pyqtSignal() |
| 165 |
signal_postprocess_image = QtCore.pyqtSignal() |
| 166 |
signal_save_postprocessed_image = QtCore.pyqtSignal(object) |
| 167 |
signal_set_go_back_activity = QtCore.pyqtSignal(object) |
| 168 |
|
| 169 |
def __init__(self, parent=None): |
| 170 |
""" |
| 171 |
Initialize the Planetary System Stacker environment. |
| 172 |
|
| 173 |
:param parent: None |
| 174 |
""" |
| 175 |
|
| 176 |
|
| 177 |
QtWidgets.QWidget.__init__(self, parent) |
| 178 |
self.ui = Ui_MainWindow() |
| 179 |
self.ui.setupUi(self) |
| 180 |
|
| 181 |
|
| 182 |
self.ui.menubar.setNativeMenuBar(False) |
| 183 |
|
| 184 |
|
| 185 |
size_policy = self.ui.label_current_progress.sizePolicy() |
| 186 |
size_policy.setRetainSizeWhenHidden(True) |
| 187 |
self.ui.label_current_progress.setSizePolicy(size_policy) |
| 188 |
size_policy = self.ui.progressBar_current.sizePolicy() |
| 189 |
size_policy.setRetainSizeWhenHidden(True) |
| 190 |
self.ui.progressBar_current.setSizePolicy(size_policy) |
| 191 |
|
| 192 |
|
| 193 |
self.configuration = Configuration() |
| 194 |
self.configuration.initialize_configuration() |
| 195 |
|
| 196 |
|
| 197 |
self.setWindowIcon(QtGui.QIcon(self.configuration.window_icon)) |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
x0 = self.configuration.hidden_parameters_main_window_x0 |
| 203 |
y0 = self.configuration.hidden_parameters_main_window_y0 |
| 204 |
width = self.configuration.hidden_parameters_main_window_width |
| 205 |
height = self.configuration.hidden_parameters_main_window_height |
| 206 |
self.setGeometry(x0, y0, width, height) |
| 207 |
if self.configuration.hidden_parameters_main_window_maximized: |
| 208 |
self.showMaximized() |
| 209 |
|
| 210 |
|
| 211 |
self.widget_saved = None |
| 212 |
|
| 213 |
|
| 214 |
self.setWindowTitle(self.configuration.global_parameters_version) |
| 215 |
|
| 216 |
|
| 217 |
self.ui.actionQuit.triggered.connect(self.closeEvent) |
| 218 |
self.ui.comboBox_back.currentTextChanged.connect(self.go_back) |
| 219 |
self.ui.pushButton_start.clicked.connect(self.play) |
| 220 |
self.ui.pushButton_pause.clicked.connect(self.pause) |
| 221 |
self.ui.pushButton_next_job.clicked.connect(self.go_next) |
| 222 |
self.ui.pushButton_quit.clicked.connect(self.close) |
| 223 |
self.ui.box_automatic.stateChanged.connect(self.automatic_changed) |
| 224 |
self.ui.actionLoad_video_directory.triggered.connect(self.load_video_directory) |
| 225 |
self.ui.actionSave.triggered.connect(self.save_result) |
| 226 |
self.ui.actionSave_as.triggered.connect(self.save_result_as) |
| 227 |
self.ui.actionEdit_configuration.triggered.connect(self.edit_configuration) |
| 228 |
self.ui.actionLoad_config.triggered.connect(self.load_config_file) |
| 229 |
self.ui.actionSave_config.triggered.connect(self.save_config_file) |
| 230 |
self.ui.actionDe_activate_master_frames.triggered.connect(self.reset_masters) |
| 231 |
self.ui.actionLoad_master_dark_frame.triggered.connect(self.load_master_dark) |
| 232 |
self.ui.actionLoad_master_flat_frame.triggered.connect(self.load_master_flat) |
| 233 |
self.ui.actionCreate_new_master_dark_frame.triggered.connect(self.create_master_dark) |
| 234 |
self.ui.actionCreate_new_master_flat_frame.triggered.connect(self.create_master_flat) |
| 235 |
self.ui.actionAbout.triggered.connect(self.about_pss) |
| 236 |
self.ui.actionShow_Quickstart.setChecked(self.configuration.global_parameters_display_quickstart) |
| 237 |
self.ui.actionShow_Quickstart.triggered.connect(self.show_quickstart) |
| 238 |
|
| 239 |
|
| 240 |
self.thread = QtCore.QThread() |
| 241 |
self.workflow = Workflow(self) |
| 242 |
self.workflow.setParent(None) |
| 243 |
self.workflow.moveToThread(self.thread) |
| 244 |
self.workflow.master_dark_created_signal.connect(self.master_dark_created) |
| 245 |
self.workflow.master_flat_created_signal.connect(self.master_flat_created) |
| 246 |
self.workflow.calibration.report_calibration_error_signal.connect( |
| 247 |
self.report_calibration_error) |
| 248 |
self.workflow.work_next_task_signal.connect(self.work_next_task) |
| 249 |
self.workflow.report_error_signal.connect(self.report_error) |
| 250 |
self.workflow.abort_job_signal.connect(self.next_job_after_error) |
| 251 |
self.workflow.work_current_progress_signal.connect(self.set_current_progress) |
| 252 |
self.workflow.set_main_gui_busy_signal.connect(self.gui_set_busy) |
| 253 |
self.workflow.set_status_bar_signal.connect(self.write_status_bar) |
| 254 |
self.workflow.create_image_window_signal.connect(self.create_image_window) |
| 255 |
self.workflow.update_image_window_signal.connect(self.update_image_window) |
| 256 |
self.workflow.terminate_image_window_signal.connect(self.terminate_image_window) |
| 257 |
self.thread.start() |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
self.signal_reset_masters.connect(self.workflow.calibration.reset_masters) |
| 262 |
self.signal_load_master_dark.connect(self.workflow.calibration.load_master_dark) |
| 263 |
self.signal_load_master_flat.connect(self.workflow.calibration.load_master_flat) |
| 264 |
self.signal_frames.connect(self.workflow.execute_frames) |
| 265 |
self.signal_rank_frames.connect(self.workflow.execute_rank_frames) |
| 266 |
self.signal_select_frames.connect(self.workflow.execute_set_index_translation_table) |
| 267 |
self.signal_align_frames.connect(self.workflow.execute_align_frames) |
| 268 |
self.signal_set_roi.connect(self.workflow.execute_set_roi) |
| 269 |
self.signal_set_alignment_points.connect(self.workflow.execute_set_alignment_points) |
| 270 |
self.signal_compute_frame_qualities.connect(self.workflow.execute_compute_frame_qualities) |
| 271 |
self.signal_stack_frames.connect(self.workflow.execute_stack_frames) |
| 272 |
self.signal_save_stacked_image.connect(self.workflow.execute_save_stacked_image) |
| 273 |
self.signal_postprocess_image.connect(self.workflow.execute_postprocess_image) |
| 274 |
self.signal_save_postprocessed_image.connect(self.workflow.execute_save_postprocessed_image) |
| 275 |
self.signal_set_go_back_activity.connect(self.change_go_back_activity) |
| 276 |
|
| 277 |
|
| 278 |
self.automatic = self.ui.box_automatic.isChecked() |
| 279 |
self.busy = False |
| 280 |
self.pause = False |
| 281 |
self.job_number = 0 |
| 282 |
self.job_index = 0 |
| 283 |
self.jobs = [] |
| 284 |
self.activities = ['Previous job', 'Read frames', 'Rank frames', 'Align frames', |
| 285 |
'Select stack size', 'Set ROI', 'Set alignment points', |
| 286 |
'Compute frame qualities', 'Stack frames', 'Save stacked image', |
| 287 |
'Postprocessing', 'Save postprocessed image', 'Next job'] |
| 288 |
self.activity = 'Read frames' |
| 289 |
self.error_occurred = False |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
self.set_previous_actions_button() |
| 294 |
|
| 295 |
|
| 296 |
self.activate_gui_elements( |
| 297 |
[self.ui.box_automatic, self.ui.comboBox_back, self.ui.pushButton_start, |
| 298 |
self.ui.pushButton_pause, |
| 299 |
self.ui.pushButton_next_job, self.ui.actionSave, self.ui.actionSave_as, |
| 300 |
self.ui.actionLoad_postproc_config, self.ui.actionSave_postproc_config, |
| 301 |
self.ui.actionEdit_postproc_config], False) |
| 302 |
self.show_current_progress_widgets(False) |
| 303 |
self.show_batch_progress_widgets(False) |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
if not self.configuration.config_file_exists: |
| 309 |
|
| 310 |
self.write_status_bar( |
| 311 |
'Adapt configuration to your needs and / or confirm by pressing "OK".', 'red') |
| 312 |
self.edit_configuration() |
| 313 |
|
| 314 |
else: |
| 315 |
|
| 316 |
self.write_status_bar( |
| 317 |
"Specify video(s) or dir(s) with image files to be stacked, or single image " |
| 318 |
"files for postprocessing (menu: File / Open).", 'red') |
| 319 |
|
| 320 |
|
| 321 |
if self.configuration.global_parameters_display_quickstart: |
| 322 |
self.display_widget(DisplayQuickstart(self, self.configuration)) |
| 323 |
|
| 324 |
|
| 325 |
self.image_window = None |
| 326 |
|
| 327 |
@QtCore.pyqtSlot() |
| 328 |
def show_quickstart(self): |
| 329 |
""" |
| 330 |
If the user requests to re-activate the quickstart guide display at startup, change the |
| 331 |
corresponding gobal parameter. |
| 332 |
|
| 333 |
:return: |
| 334 |
""" |
| 335 |
|
| 336 |
self.configuration.global_parameters_display_quickstart = \ |
| 337 |
self.ui.actionShow_Quickstart.isChecked() |
| 338 |
|
| 339 |
@QtCore.pyqtSlot() |
| 340 |
def create_image_window(self): |
| 341 |
""" |
| 342 |
On request of the workflow thread open a separate window for displaying images in debugging. |
| 343 |
|
| 344 |
:return: - |
| 345 |
""" |
| 346 |
self.image_window = DisplayImage() |
| 347 |
|
| 348 |
@QtCore.pyqtSlot(object) |
| 349 |
def update_image_window(self, image): |
| 350 |
""" |
| 351 |
Replace the image in the image window with a new one sent from the workflow thread. |
| 352 |
|
| 353 |
:param image: Numpy array of type uint8 or uint16, size: (size_y, size_x [, 3]) |
| 354 |
:return: - |
| 355 |
""" |
| 356 |
self.image_window.update_image(image) |
| 357 |
|
| 358 |
@QtCore.pyqtSlot() |
| 359 |
def terminate_image_window(self): |
| 360 |
""" |
| 361 |
Close the separate image window on request of the workflow thread. |
| 362 |
|
| 363 |
:return: - |
| 364 |
""" |
| 365 |
del self.image_window |
| 366 |
|
| 367 |
def automatic_changed(self): |
| 368 |
""" |
| 369 |
If the user checks / unchecks the "Automatic" checkbox, change the corresponding status |
| 370 |
variable. |
| 371 |
|
| 372 |
:return: - |
| 373 |
""" |
| 374 |
|
| 375 |
self.automatic = not self.automatic |
| 376 |
|
| 377 |
def edit_configuration(self): |
| 378 |
""" |
| 379 |
This method is invoked by selecting the "edit stacking config" menu entry. |
| 380 |
|
| 381 |
:return: - |
| 382 |
""" |
| 383 |
|
| 384 |
|
| 385 |
self.display_widget(ConfigurationEditor(self)) |
| 386 |
|
| 387 |
def change_go_back_activity(self, phases): |
| 388 |
""" |
| 389 |
This method is triggered in the configuration editor if at least one parameter has been |
| 390 |
changed. In this case it may be necessary to reset the workflow to an earlier phase. This |
| 391 |
phase is set as the current activity, and later phases are deleted from the "go back" |
| 392 |
button list. |
| 393 |
|
| 394 |
:param phases: List of phases after which the workflow has to be invalidated. The |
| 395 |
earliest phase on the list is relevant. |
| 396 |
:return: - |
| 397 |
""" |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
phases.append(self.activity) |
| 402 |
|
| 403 |
|
| 404 |
earliest_phase = self.activities[min([self.activities.index(item) for item in phases])] |
| 405 |
|
| 406 |
|
| 407 |
if self.configuration.global_parameters_protocol_level > 1 and self.activity != 'Read frames': |
| 408 |
|
| 409 |
if self.workflow.attached_log_file is not None: |
| 410 |
if self.configuration.global_parameters_store_protocol_with_result and \ |
| 411 |
self.workflow.attached_log_file.closed: |
| 412 |
try: |
| 413 |
self.workflow.attached_log_file = open(self.workflow.attached_log_name, 'a') |
| 414 |
except: |
| 415 |
pass |
| 416 |
Miscellaneous.protocol( |
| 417 |
"+++ Parameter change: all phases after '" + earliest_phase + "' are invalidated +++", |
| 418 |
self.workflow.attached_log_file, precede_with_timestamp=True) |
| 419 |
|
| 420 |
|
| 421 |
self.activity = earliest_phase |
| 422 |
self.set_previous_actions_button() |
| 423 |
|
| 424 |
def display_widget(self, widget, display=True): |
| 425 |
""" |
| 426 |
Display a widget in the central main GUI location, or remove it from there. |
| 427 |
|
| 428 |
:param widget: Widget object to be displayed. |
| 429 |
:param display: If "True", display the widget. If "False", remove the current widget from |
| 430 |
the GUI. |
| 431 |
:return: - |
| 432 |
""" |
| 433 |
|
| 434 |
if display: |
| 435 |
if self.widget_saved: |
| 436 |
self.ui.verticalLayout_2.removeWidget(self.widget_saved) |
| 437 |
self.widget_saved.deleteLater() |
| 438 |
self.widget_saved.close() |
| 439 |
self.widget_saved = widget |
| 440 |
self.ui.verticalLayout_2.insertWidget(0, widget) |
| 441 |
self.ui.verticalLayout_2.setStretch(1, 0) |
| 442 |
else: |
| 443 |
if self.widget_saved: |
| 444 |
self.ui.verticalLayout_2.removeWidget(self.widget_saved) |
| 445 |
self.widget_saved.deleteLater() |
| 446 |
self.widget_saved.close() |
| 447 |
self.widget_saved = None |
| 448 |
|
| 449 |
def load_config_file(self): |
| 450 |
""" |
| 451 |
Load a stacking configuration file (extension ".pss"). |
| 452 |
:return: - |
| 453 |
""" |
| 454 |
|
| 455 |
options = QtWidgets.QFileDialog.Options() |
| 456 |
filename = QtWidgets.QFileDialog.getOpenFileName(self, "Load configuration file", |
| 457 |
self.configuration.hidden_parameters_current_dir, |
| 458 |
"*.pss", options=options) |
| 459 |
file_name = filename[0] |
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
if file_name != '': |
| 464 |
|
| 465 |
self.configuration.read_config(file_name=file_name) |
| 466 |
|
| 467 |
self.configuration.current_dir = str(Path(file_name).parents[0]) |
| 468 |
self.display_widget(ConfigurationEditor(self)) |
| 469 |
|
| 470 |
if self.configuration.global_parameters_protocol_level > 1 and \ |
| 471 |
self.configuration.global_parameters_version_imported_from != \ |
| 472 |
self.configuration.global_parameters_version: |
| 473 |
Miscellaneous.protocol("+++ Importing configuration from older version: " + |
| 474 |
self.configuration.global_parameters_version_imported_from + " +++", |
| 475 |
self.workflow.attached_log_file) |
| 476 |
|
| 477 |
def save_config_file(self): |
| 478 |
""" |
| 479 |
Save all stacking parameters in a configuration file. |
| 480 |
|
| 481 |
:return: - |
| 482 |
""" |
| 483 |
|
| 484 |
|
| 485 |
options = QtWidgets.QFileDialog.Options() |
| 486 |
filename = QtWidgets.QFileDialog.getSaveFileName(self, "Save configuration file", |
| 487 |
self.configuration.hidden_parameters_current_dir, |
| 488 |
"Config file (*.pss)", |
| 489 |
options=options) |
| 490 |
|
| 491 |
|
| 492 |
file_name = filename[0] |
| 493 |
if file_name != "": |
| 494 |
my_file = Path(file_name) |
| 495 |
|
| 496 |
|
| 497 |
self.configuration.hidden_parameters_current_dir = str(my_file.parents[0]) |
| 498 |
|
| 499 |
|
| 500 |
if my_file.is_file(): |
| 501 |
remove(str(my_file)) |
| 502 |
self.configuration.write_config(file_name=str(my_file)) |
| 503 |
|
| 504 |
def load_video_directory(self): |
| 505 |
""" |
| 506 |
This method is invoked by selecting "Open" from the "file" menu. |
| 507 |
|
| 508 |
:return: - |
| 509 |
""" |
| 510 |
|
| 511 |
|
| 512 |
self.display_widget(JobEditor(self)) |
| 513 |
|
| 514 |
def reset_masters(self): |
| 515 |
""" |
| 516 |
This method is invoked by selecting "Reset master frames" from the "Calibration" menu. The |
| 517 |
signal is caught on the workflow thread, where the method "execute_reset_masters" is |
| 518 |
executed. |
| 519 |
|
| 520 |
:return: - |
| 521 |
""" |
| 522 |
|
| 523 |
self.signal_reset_masters.emit() |
| 524 |
if self.configuration.global_parameters_protocol_level > 0: |
| 525 |
Miscellaneous.protocol("+++ De-activating master dark / flat frames +++", |
| 526 |
self.workflow.attached_log_file) |
| 527 |
|
| 528 |
def create_master_dark(self): |
| 529 |
""" |
| 530 |
Open a file dialog for entering a video file or image directory with dark frames. When the |
| 531 |
FileDialog closes, it sends a signal (with the path name as payload) to the corresponding |
| 532 |
slot on the workflow thread. There the actual master dark computation is performed. |
| 533 |
|
| 534 |
:return: - |
| 535 |
""" |
| 536 |
|
| 537 |
options = QtWidgets.QFileDialog.Options() |
| 538 |
message = "Select a video file / a folder with image files for computing a master dark" \ |
| 539 |
" frame" |
| 540 |
|
| 541 |
file_dialog = FileDialog(self, message, |
| 542 |
self.configuration.hidden_parameters_current_dir, |
| 543 |
"Videos (*.avi *.mov *.mp4 *.ser)", options=options) |
| 544 |
file_dialog.setNameFilters(["Still image folder / video file (*.avi *.mov *.mp4 *.ser)"]) |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
file_dialog.signal_dialog_ready.connect(self.workflow.execute_create_master_dark) |
| 549 |
file_dialog.exec_() |
| 550 |
|
| 551 |
@QtCore.pyqtSlot(bool) |
| 552 |
def master_dark_created(self, success): |
| 553 |
""" |
| 554 |
Called from the workflow thread when it has created a master dark frame. This method is |
| 555 |
used to save the image to disk. |
| 556 |
|
| 557 |
:param success: A flag indicating if the dark frame was created successfully. Only if true, |
| 558 |
write the resulting image to disk. |
| 559 |
:return: - |
| 560 |
""" |
| 561 |
|
| 562 |
if success: |
| 563 |
options = QtWidgets.QFileDialog.Options() |
| 564 |
master_dark_file = QtWidgets.QFileDialog.getSaveFileName(self, |
| 565 |
"Choose a file name for the new master dark frame", |
| 566 |
self.configuration.hidden_parameters_current_dir, |
| 567 |
"Images (*png *.tiff *.fits)", |
| 568 |
options=options) |
| 569 |
|
| 570 |
if master_dark_file[0]: |
| 571 |
Frames.save_image(master_dark_file[0], |
| 572 |
self.workflow.calibration.master_dark_frame, |
| 573 |
color=self.workflow.calibration.dark_color, |
| 574 |
avoid_overwriting=False, |
| 575 |
header=self.configuration.global_parameters_version) |
| 576 |
|
| 577 |
self.configuration.hidden_parameters_current_dir = \ |
| 578 |
str(Path(master_dark_file[0]).parents[0]) |
| 579 |
if self.configuration.global_parameters_protocol_level > 1: |
| 580 |
Miscellaneous.protocol(" The master dark frame was written to: " + |
| 581 |
str(master_dark_file[0]), |
| 582 |
self.workflow.attached_log_file, |
| 583 |
precede_with_timestamp=False) |
| 584 |
|
| 585 |
|
| 586 |
self.gui_set_busy(False) |
| 587 |
|
| 588 |
def load_master_dark(self): |
| 589 |
""" |
| 590 |
This method is invoked by selecting "Load master dark" from the "Calibration" menu. It |
| 591 |
opens a file dialog for selecting a Png, Tiff or Fits file with a master dark frame. |
| 592 |
|
| 593 |
:return: - |
| 594 |
""" |
| 595 |
|
| 596 |
options = QtWidgets.QFileDialog.Options() |
| 597 |
master_dark_file = QtWidgets.QFileDialog.getOpenFileName(self, |
| 598 |
"Select image file containing a master dark frame", |
| 599 |
self.configuration.hidden_parameters_current_dir, |
| 600 |
"Images (*png *.tiff *.fits)", options=options) |
| 601 |
|
| 602 |
if master_dark_file[0]: |
| 603 |
self.signal_load_master_dark.emit(master_dark_file[0]) |
| 604 |
|
| 605 |
self.configuration.hidden_parameters_current_dir = str( |
| 606 |
Path(master_dark_file[0]).parents[0]) |
| 607 |
if self.configuration.global_parameters_protocol_level > 0: |
| 608 |
Miscellaneous.protocol("+++ Loading master dark frame +++", |
| 609 |
self.workflow.attached_log_file) |
| 610 |
|
| 611 |
def create_master_flat(self): |
| 612 |
""" |
| 613 |
Open a file dialog for entering a video file or image directory with flat frames. When the |
| 614 |
FileDialog closes, it sends a signal (with the path name as payload) to the corresponding |
| 615 |
slot on the workflow thread. There the actual master flat computation is performed. |
| 616 |
|
| 617 |
:return: - |
| 618 |
""" |
| 619 |
|
| 620 |
options = QtWidgets.QFileDialog.Options() |
| 621 |
message = "Select a video file / a folder with image files for computing a master flat" \ |
| 622 |
" frame" |
| 623 |
|
| 624 |
file_dialog = FileDialog(self, message, |
| 625 |
self.configuration.hidden_parameters_current_dir, |
| 626 |
"Videos (*.avi *.mov *.mp4 *.ser)", options=options) |
| 627 |
file_dialog.setNameFilters(["Still image folder / video file (*.avi *.mov *.mp4 *.ser)"]) |
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
file_dialog.signal_dialog_ready.connect(self.workflow.execute_create_master_flat) |
| 632 |
file_dialog.exec_() |
| 633 |
|
| 634 |
@QtCore.pyqtSlot(bool) |
| 635 |
def master_flat_created(self, success): |
| 636 |
""" |
| 637 |
Called from the workflow thread when it has created a master flat frame. This method is |
| 638 |
used to save the image to disk. |
| 639 |
|
| 640 |
:param success: A flag indicating if the dark frame was created successfully. Only if true, |
| 641 |
write the resulting image to disk. |
| 642 |
:return: - |
| 643 |
""" |
| 644 |
|
| 645 |
if success: |
| 646 |
options = QtWidgets.QFileDialog.Options() |
| 647 |
master_flat_file = QtWidgets.QFileDialog.getSaveFileName(self, |
| 648 |
"Choose a file name for the new master flat frame", |
| 649 |
self.configuration.hidden_parameters_current_dir, |
| 650 |
"Images (*png *.tiff *.fits)", |
| 651 |
options=options) |
| 652 |
|
| 653 |
if master_flat_file[0]: |
| 654 |
Frames.save_image(master_flat_file[0], |
| 655 |
self.workflow.calibration.master_flat_frame, |
| 656 |
color=self.workflow.calibration.flat_color, |
| 657 |
avoid_overwriting=False, |
| 658 |
header=self.configuration.global_parameters_version) |
| 659 |
|
| 660 |
self.configuration.hidden_parameters_current_dir = str( |
| 661 |
Path(master_flat_file[0]).parents[0]) |
| 662 |
if self.configuration.global_parameters_protocol_level > 1: |
| 663 |
Miscellaneous.protocol(" The master flat frame was written to: " + |
| 664 |
str(master_flat_file[0]), |
| 665 |
self.workflow.attached_log_file, |
| 666 |
precede_with_timestamp=False) |
| 667 |
|
| 668 |
|
| 669 |
self.gui_set_busy(False) |
| 670 |
|
| 671 |
def load_master_flat(self): |
| 672 |
""" |
| 673 |
This method is invoked by selecting "Load master flat" from the "Calibration" menu. It |
| 674 |
opens a file dialog for selecting a Png, Tiff or Fits file with a master flat frame. |
| 675 |
|
| 676 |
:return: - |
| 677 |
""" |
| 678 |
|
| 679 |
options = QtWidgets.QFileDialog.Options() |
| 680 |
master_flat_file = QtWidgets.QFileDialog.getOpenFileName(self, |
| 681 |
"Select image file containing a master dark frame", |
| 682 |
self.configuration.hidden_parameters_current_dir, |
| 683 |
"Images (*png *.tiff *.fits)", options=options) |
| 684 |
|
| 685 |
if master_flat_file[0]: |
| 686 |
self.signal_load_master_flat.emit(master_flat_file[0]) |
| 687 |
|
| 688 |
self.configuration.hidden_parameters_current_dir = str( |
| 689 |
Path(master_flat_file[0]).parents[0]) |
| 690 |
if self.configuration.global_parameters_protocol_level > 0: |
| 691 |
Miscellaneous.protocol("+++ Loading master flat frame +++", |
| 692 |
self.workflow.attached_log_file) |
| 693 |
|
| 694 |
@QtCore.pyqtSlot(str) |
| 695 |
def report_calibration_error(self, message): |
| 696 |
if self.configuration.global_parameters_protocol_level > 0: |
| 697 |
Miscellaneous.protocol(" " + message, |
| 698 |
self.workflow.attached_log_file, precede_with_timestamp=False) |
| 699 |
|
| 700 |
def go_back(self): |
| 701 |
""" |
| 702 |
Repeat processing steps as specified via the choice of the "comboBox_back" button. The user |
| 703 |
can either repeat steps of the current job, or go back to the previous job. |
| 704 |
|
| 705 |
:return: - |
| 706 |
""" |
| 707 |
|
| 708 |
|
| 709 |
task = self.ui.comboBox_back.currentText() |
| 710 |
if self.configuration.global_parameters_protocol_level > 0: |
| 711 |
if self.workflow.attached_log_file is not None: |
| 712 |
if self.configuration.global_parameters_store_protocol_with_result and \ |
| 713 |
self.workflow.attached_log_file.closed: |
| 714 |
try: |
| 715 |
self.workflow.attached_log_file = open(self.workflow.attached_log_name, 'a') |
| 716 |
except: |
| 717 |
pass |
| 718 |
Miscellaneous.protocol("", self.workflow.attached_log_file, |
| 719 |
precede_with_timestamp=False) |
| 720 |
Miscellaneous.protocol("+++ Repeating from task: " + task + " +++", |
| 721 |
self.workflow.attached_log_file) |
| 722 |
|
| 723 |
|
| 724 |
if self.job_index == self.job_number and self.job_index > 0: |
| 725 |
self.job_index -= 1 |
| 726 |
|
| 727 |
|
| 728 |
if task in ['Read frames', 'Rank frames', 'Select frames', 'Align frames', |
| 729 |
'Select stack size', 'Set ROI', 'Set alignment points', |
| 730 |
'Compute frame qualities', 'Stack frames', 'Save stacked image', |
| 731 |
'Postprocessing', 'Save postprocessed image']: |
| 732 |
|
| 733 |
self.display_widget(None, display=False) |
| 734 |
self.work_next_task(task) |
| 735 |
|
| 736 |
|
| 737 |
elif task == 'Previous job': |
| 738 |
self.job_index -= 1 |
| 739 |
self.display_widget(None, display=False) |
| 740 |
self.work_next_task("Read frames") |
| 741 |
|
| 742 |
def play(self): |
| 743 |
""" |
| 744 |
This method is invoked when the "Start / Cont." button is pressed. |
| 745 |
:return: - |
| 746 |
""" |
| 747 |
|
| 748 |
|
| 749 |
self.work_next_task(self.activity) |
| 750 |
|
| 751 |
def pause(self): |
| 752 |
""" |
| 753 |
This method is invoked when the "Pause" button is pressed. |
| 754 |
:return: - |
| 755 |
""" |
| 756 |
|
| 757 |
|
| 758 |
if not self.pause: |
| 759 |
self.append_status_bar(" Execution will be suspended after current phase.") |
| 760 |
|
| 761 |
self.pause = True |
| 762 |
|
| 763 |
def go_next(self): |
| 764 |
""" |
| 765 |
This method is invoked when the "Next Job" button is pressed. |
| 766 |
:return: - |
| 767 |
""" |
| 768 |
|
| 769 |
self.work_next_task("Next job") |
| 770 |
|
| 771 |
@QtCore.pyqtSlot(str) |
| 772 |
def work_next_task(self, next_activity): |
| 773 |
""" |
| 774 |
This is the central place where all activities are scheduled. Depending on the |
| 775 |
"next_activity" chosen, the appropriate activity is started on the workflow thread. |
| 776 |
|
| 777 |
:param next_activity: Activity to be performed next. |
| 778 |
:return: - |
| 779 |
""" |
| 780 |
|
| 781 |
|
| 782 |
if not self.jobs or self.job_index >= self.job_number: |
| 783 |
return |
| 784 |
|
| 785 |
self.activity = next_activity |
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
self.show_current_progress_widgets(False) |
| 790 |
|
| 791 |
|
| 792 |
if self.pause: |
| 793 |
self.pause = False |
| 794 |
self.busy = False |
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
elif self.activity == "Read frames": |
| 799 |
|
| 800 |
|
| 801 |
self.signal_frames.emit(self.jobs[self.job_index]) |
| 802 |
self.busy = True |
| 803 |
|
| 804 |
elif self.activity == "Rank frames": |
| 805 |
if not self.automatic: |
| 806 |
pass |
| 807 |
|
| 808 |
self.signal_rank_frames.emit() |
| 809 |
self.busy = True |
| 810 |
|
| 811 |
elif self.activity == "Select frames": |
| 812 |
|
| 813 |
|
| 814 |
if not self.automatic and self.configuration.frames_add_selection_dialog: |
| 815 |
|
| 816 |
if self.workflow.frames.index_translation_active: |
| 817 |
self.workflow.frames.reset_index_translation() |
| 818 |
self.workflow.rank_frames.reset_index_translation() |
| 819 |
|
| 820 |
fsw = FrameSelectorWidget(self, self.configuration, self.workflow.frames, |
| 821 |
self.workflow.rank_frames, |
| 822 |
self.workflow.attached_log_file, |
| 823 |
self.signal_select_frames) |
| 824 |
|
| 825 |
self.display_widget(fsw) |
| 826 |
fsw.listWidget.setFocus() |
| 827 |
|
| 828 |
else: |
| 829 |
|
| 830 |
|
| 831 |
self.signal_select_frames.emit() |
| 832 |
|
| 833 |
elif self.activity == "Align frames": |
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
if not self.automatic and not self.configuration.align_frames_automation and \ |
| 838 |
self.configuration.align_frames_mode == 'Surface': |
| 839 |
border = self.configuration.align_frames_search_width |
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
rpew = RectangularPatchEditorWidget(self, self.workflow.frames.frames_mono( |
| 844 |
self.workflow.rank_frames.frame_ranks_max_index)[border:-border, |
| 845 |
border:-border], |
| 846 |
"With 'ctrl' and the left mouse button pressed, draw a " |
| 847 |
"rectangular patch to be used for frame alignment. Or just " |
| 848 |
"press 'OK / Cancel' (automatic selection).", |
| 849 |
self.signal_align_frames) |
| 850 |
|
| 851 |
self.display_widget(rpew) |
| 852 |
rpew.viewer.setFocus() |
| 853 |
|
| 854 |
else: |
| 855 |
|
| 856 |
|
| 857 |
self.signal_align_frames.emit(0, 0, 0, 0) |
| 858 |
|
| 859 |
self.busy = True |
| 860 |
|
| 861 |
elif self.activity == "Select stack size": |
| 862 |
|
| 863 |
if not self.automatic: |
| 864 |
|
| 865 |
|
| 866 |
self.workflow.align_frames.reset_roi() |
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
fvw = FrameViewerWidget(self, self.workflow.configuration, self.workflow.frames, |
| 871 |
self.workflow.rank_frames, self.workflow.align_frames, |
| 872 |
self.workflow.attached_log_file, |
| 873 |
self.workflow.work_next_task_signal, "Set ROI") |
| 874 |
|
| 875 |
self.display_widget(fvw) |
| 876 |
fvw.frame_viewer.setFocus() |
| 877 |
|
| 878 |
else: |
| 879 |
|
| 880 |
|
| 881 |
self.workflow.work_next_task_signal.emit("Set ROI") |
| 882 |
self.busy = True |
| 883 |
|
| 884 |
elif self.activity == "Set ROI": |
| 885 |
|
| 886 |
|
| 887 |
self.workflow.align_frames.reset_roi() |
| 888 |
|
| 889 |
if not self.automatic: |
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
rpew = RectangularPatchEditorWidget(self, self.workflow.align_frames.mean_frame, |
| 894 |
"With 'ctrl' and the left mouse button pressed, draw a rectangle to set the" |
| 895 |
" ROI, or just press 'OK' (no ROI).", self.signal_set_roi) |
| 896 |
|
| 897 |
self.display_widget(rpew) |
| 898 |
rpew.viewer.setFocus() |
| 899 |
|
| 900 |
else: |
| 901 |
|
| 902 |
self.signal_set_roi.emit(0, 0, 0, 0) |
| 903 |
self.busy = True |
| 904 |
|
| 905 |
elif self.activity == "Set alignment points": |
| 906 |
|
| 907 |
|
| 908 |
if self.automatic: |
| 909 |
self.signal_set_alignment_points.emit() |
| 910 |
self.busy = True |
| 911 |
else: |
| 912 |
|
| 913 |
|
| 914 |
if self.configuration.global_parameters_protocol_level > 0: |
| 915 |
Miscellaneous.protocol("+++ Start creating alignment points +++", |
| 916 |
self.workflow.attached_log_file) |
| 917 |
|
| 918 |
self.workflow.my_timer.create_no_check('Initialize alignment point object') |
| 919 |
self.workflow.alignment_points = AlignmentPoints(self.workflow.configuration, |
| 920 |
self.workflow.frames, self.workflow.rank_frames, self.workflow.align_frames, |
| 921 |
progress_signal=self.workflow.work_current_progress_signal) |
| 922 |
self.workflow.my_timer.stop('Initialize alignment point object') |
| 923 |
|
| 924 |
apew = AlignmentPointEditorWidget(self, self.workflow.configuration, |
| 925 |
self.workflow.align_frames, |
| 926 |
self.workflow.alignment_points, |
| 927 |
self.signal_set_alignment_points) |
| 928 |
|
| 929 |
self.display_widget(apew) |
| 930 |
apew.viewer.setFocus() |
| 931 |
|
| 932 |
elif self.activity == "Compute frame qualities": |
| 933 |
if not self.automatic: |
| 934 |
pass |
| 935 |
self.signal_compute_frame_qualities.emit() |
| 936 |
self.busy = True |
| 937 |
|
| 938 |
elif self.activity == "Stack frames": |
| 939 |
if not self.automatic: |
| 940 |
pass |
| 941 |
self.signal_stack_frames.emit() |
| 942 |
self.busy = True |
| 943 |
|
| 944 |
elif self.activity == "Save stacked image": |
| 945 |
if self.automatic: |
| 946 |
self.signal_save_stacked_image.emit() |
| 947 |
else: |
| 948 |
sdv = ShiftDistributionViewerWidget(self, |
| 949 |
self.workflow.stack_frames.shift_distribution, |
| 950 |
self.workflow.stack_frames.shift_failure_percent, |
| 951 |
self.signal_save_stacked_image) |
| 952 |
self.display_widget(sdv) |
| 953 |
self.busy = True |
| 954 |
|
| 955 |
elif self.activity == "Postprocessing": |
| 956 |
if self.automatic: |
| 957 |
self.signal_postprocess_image.emit() |
| 958 |
else: |
| 959 |
if self.configuration.global_parameters_protocol_level > 0: |
| 960 |
Miscellaneous.protocol("+++ Start postprocessing +++", |
| 961 |
self.workflow.attached_log_file) |
| 962 |
|
| 963 |
|
| 964 |
pew = PostprocEditorWidget(self.workflow.configuration, |
| 965 |
self.workflow.postproc_input_image, |
| 966 |
self.workflow.postproc_input_name, |
| 967 |
self.write_status_bar, |
| 968 |
self.signal_save_postprocessed_image) |
| 969 |
self.display_widget(pew) |
| 970 |
pew.frame_viewer.setFocus() |
| 971 |
self.busy = True |
| 972 |
|
| 973 |
elif self.activity == "Save postprocessed image": |
| 974 |
if not self.automatic: |
| 975 |
pass |
| 976 |
|
| 977 |
|
| 978 |
if self.workflow.postprocessed_image is not None: |
| 979 |
self.signal_save_postprocessed_image.emit(self.workflow.postprocessed_image) |
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
else: |
| 984 |
self.signal_save_postprocessed_image.emit( |
| 985 |
self.workflow.configuration.postproc_data_object.versions[ |
| 986 |
self.workflow.configuration.postproc_data_object.version_selected].image) |
| 987 |
self.busy = True |
| 988 |
|
| 989 |
elif self.activity == "Next job": |
| 990 |
self.job_index += 1 |
| 991 |
if self.job_index < self.job_number: |
| 992 |
|
| 993 |
self.activity = "Read frames" |
| 994 |
|
| 995 |
self.error_occurred = False |
| 996 |
if not self.automatic: |
| 997 |
pass |
| 998 |
self.signal_frames.emit(self.jobs[self.job_index]) |
| 999 |
else: |
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
if self.error_occurred: |
| 1004 |
self.activity = "Read frames" |
| 1005 |
self.error_occurred = False |
| 1006 |
|
| 1007 |
self.busy = False |
| 1008 |
|
| 1009 |
|
| 1010 |
self.update_status() |
| 1011 |
|
| 1012 |
@QtCore.pyqtSlot(str) |
| 1013 |
def next_job_after_error(self, message): |
| 1014 |
""" |
| 1015 |
This method is triggered by the workflow thread via a signal when an error causes a job to |
| 1016 |
be aborted. In interactive mode a message window shows the error message and asks the user |
| 1017 |
for confirmation. Depending on the protocol level, the error message is also written to the |
| 1018 |
protocol (file). |
| 1019 |
|
| 1020 |
:param message: Error message to be displayed |
| 1021 |
:return: - |
| 1022 |
""" |
| 1023 |
|
| 1024 |
|
| 1025 |
self.report_error(message) |
| 1026 |
|
| 1027 |
|
| 1028 |
self.error_occurred = True |
| 1029 |
|
| 1030 |
|
| 1031 |
self.work_next_task("Next job") |
| 1032 |
|
| 1033 |
@QtCore.pyqtSlot(str) |
| 1034 |
def report_error(self, message): |
| 1035 |
""" |
| 1036 |
This method is triggered by the workflow thread via a signal when an error is to be |
| 1037 |
reported. In interactive mode a message window shows the error message and asks the user |
| 1038 |
for confirmation. Depending on the protocol level, the error message is also written to the |
| 1039 |
protocol (file). |
| 1040 |
|
| 1041 |
:param message: Error message to be displayed |
| 1042 |
:return: - |
| 1043 |
""" |
| 1044 |
|
| 1045 |
|
| 1046 |
if not self.automatic: |
| 1047 |
msg = QtWidgets.QMessageBox() |
| 1048 |
msg.setText(message) |
| 1049 |
msg.setIcon(QtWidgets.QMessageBox.Critical) |
| 1050 |
msg.setWindowTitle(self.configuration.global_parameters_version) |
| 1051 |
msg.setWindowIcon(QtGui.QIcon(self.configuration.window_icon)) |
| 1052 |
msg.setStandardButtons(QtWidgets.QMessageBox.Ok) |
| 1053 |
msg.exec_() |
| 1054 |
|
| 1055 |
if self.configuration.global_parameters_protocol_level > 0: |
| 1056 |
Miscellaneous.protocol(message + "\n", self.workflow.attached_log_file,) |
| 1057 |
|
| 1058 |
def save_result(self): |
| 1059 |
""" |
| 1060 |
save the result as 16bit Png, Tiff or Fits file at the standard location. |
| 1061 |
|
| 1062 |
:return: - |
| 1063 |
""" |
| 1064 |
|
| 1065 |
Frames.save_image(self.workflow.stacked_image_name, |
| 1066 |
self.workflow.stack_frames.stacked_image, |
| 1067 |
color=self.workflow.frames.color, avoid_overwriting=False, |
| 1068 |
header=self.configuration.global_parameters_version) |
| 1069 |
|
| 1070 |
def save_result_as(self): |
| 1071 |
""" |
| 1072 |
save the result as 16bit Png, Tiff or Fits file at a location selected by the user. |
| 1073 |
|
| 1074 |
:return: - |
| 1075 |
""" |
| 1076 |
|
| 1077 |
options = QtWidgets.QFileDialog.Options() |
| 1078 |
filename, extension = QtWidgets.QFileDialog.getSaveFileName(self, |
| 1079 |
"Save result as 16bit Png, Tiff or Fits image", self.workflow.stacked_image_name, |
| 1080 |
"Image Files (*png *.tiff *.fits)", options=options) |
| 1081 |
|
| 1082 |
if filename and extension: |
| 1083 |
Frames.save_image(filename, self.workflow.stack_frames.stacked_image, |
| 1084 |
color=self.workflow.frames.color, avoid_overwriting=False, |
| 1085 |
header=self.configuration.global_parameters_version) |
| 1086 |
|
| 1087 |
def place_holder_manual_activity(self, activity): |
| 1088 |
|
| 1089 |
quit_msg = "This is a placeholder for the manual activity " + activity + \ |
| 1090 |
". press OK when you want to continue with the workflow." |
| 1091 |
QtWidgets.QMessageBox.question(self, 'Message', quit_msg, |
| 1092 |
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No, |
| 1093 |
QtWidgets.QMessageBox.No) |
| 1094 |
|
| 1095 |
def show_current_progress_widgets(self, show): |
| 1096 |
""" |
| 1097 |
Show or hide the GUI widgets showing the progress of the current job. |
| 1098 |
|
| 1099 |
:param show: If "True", show the widgets, otherwise hide them. |
| 1100 |
:return: - |
| 1101 |
""" |
| 1102 |
|
| 1103 |
if show: |
| 1104 |
self.ui.progressBar_current.show() |
| 1105 |
self.ui.label_current_progress.show() |
| 1106 |
else: |
| 1107 |
self.ui.progressBar_current.hide() |
| 1108 |
self.ui.label_current_progress.hide() |
| 1109 |
|
| 1110 |
@QtCore.pyqtSlot(str, int) |
| 1111 |
def set_current_progress(self, activity, percent): |
| 1112 |
""" |
| 1113 |
Triggered by signal "work_current_progress_signal" on the workflow thread, show the progress |
| 1114 |
of the current activity in a progress bar on the main GUI. |
| 1115 |
|
| 1116 |
:param activity: Textual description of the current activity. |
| 1117 |
:param percent: Fraction of task finished (in %) |
| 1118 |
:return: - |
| 1119 |
""" |
| 1120 |
|
| 1121 |
self.show_current_progress_widgets(True) |
| 1122 |
self.ui.label_current_progress.setText(activity) |
| 1123 |
self.ui.progressBar_current.setValue(percent) |
| 1124 |
|
| 1125 |
def show_batch_progress_widgets(self, show, value=0): |
| 1126 |
""" |
| 1127 |
Show or hide the GUI widgets showing the progress of the batch. |
| 1128 |
|
| 1129 |
:param show: If "True", show the widgets, otherwise hide them. |
| 1130 |
:return: - |
| 1131 |
""" |
| 1132 |
|
| 1133 |
if show: |
| 1134 |
self.ui.progressBar_batch.show() |
| 1135 |
self.ui.progressBar_batch.setValue(value) |
| 1136 |
self.ui.label_batch_progress.show() |
| 1137 |
else: |
| 1138 |
self.ui.progressBar_batch.hide() |
| 1139 |
self.ui.label_batch_progress.hide() |
| 1140 |
|
| 1141 |
def set_previous_actions_button(self): |
| 1142 |
""" |
| 1143 |
Initialize the "backwards" combobox: The user can only go back to those program steps |
| 1144 |
which have been executed already. |
| 1145 |
|
| 1146 |
:return: - |
| 1147 |
""" |
| 1148 |
|
| 1149 |
standard_activities = ['Read frames', 'Rank frames', 'Select frames', 'Align frames', |
| 1150 |
'Select stack size', 'Set ROI', 'Set alignment points', |
| 1151 |
'Compute frame qualities', 'Stack frames', 'Save stacked image', |
| 1152 |
'Postprocessing', 'Save postprocessed image'] |
| 1153 |
|
| 1154 |
|
| 1155 |
if self.workflow.activity == 'stacking': |
| 1156 |
|
| 1157 |
if not self.configuration.frames_add_selection_dialog: |
| 1158 |
standard_activities.remove('Select frames') |
| 1159 |
start_index = standard_activities.index('Read frames') |
| 1160 |
if self.configuration.global_parameters_include_postprocessing: |
| 1161 |
stop_index = standard_activities.index('Save postprocessed image') |
| 1162 |
else: |
| 1163 |
stop_index = standard_activities.index('Save stacked image') |
| 1164 |
elif self.workflow.activity == 'postproc': |
| 1165 |
start_index = standard_activities.index('Postprocessing') |
| 1166 |
stop_index = standard_activities.index('Save postprocessed image') |
| 1167 |
else: |
| 1168 |
start_index = None |
| 1169 |
stop_index = None |
| 1170 |
|
| 1171 |
self.ui.comboBox_back.currentTextChanged.disconnect(self.go_back) |
| 1172 |
self.ui.comboBox_back.clear() |
| 1173 |
self.ui.comboBox_back.addItem('Go back to:') |
| 1174 |
|
| 1175 |
|
| 1176 |
if self.job_index > 0 and self.job_number > 1: |
| 1177 |
self.ui.comboBox_back.addItem('Previous job') |
| 1178 |
|
| 1179 |
|
| 1180 |
if self.activity in standard_activities: |
| 1181 |
self.ui.comboBox_back.addItems( |
| 1182 |
standard_activities[start_index:standard_activities.index(self.activity) + 1]) |
| 1183 |
|
| 1184 |
|
| 1185 |
elif self.activity == "Next job": |
| 1186 |
self.ui.comboBox_back.addItems(standard_activities[start_index:stop_index + 1]) |
| 1187 |
|
| 1188 |
self.ui.comboBox_back.setCurrentIndex(0) |
| 1189 |
self.ui.comboBox_back.currentTextChanged.connect(self.go_back) |
| 1190 |
|
| 1191 |
@QtCore.pyqtSlot(bool) |
| 1192 |
def gui_set_busy(self, busy): |
| 1193 |
""" |
| 1194 |
Set the main GUI busy / not busy, and update its status. |
| 1195 |
|
| 1196 |
:param busy: Flag indicating if the main GUI is to be set busy. |
| 1197 |
:return: - |
| 1198 |
""" |
| 1199 |
self.busy = busy |
| 1200 |
self.update_status() |
| 1201 |
|
| 1202 |
def update_status(self): |
| 1203 |
""" |
| 1204 |
Activate / Deactivate GUI elements depending on the current processing status. |
| 1205 |
|
| 1206 |
:return: - |
| 1207 |
""" |
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
self.set_previous_actions_button() |
| 1212 |
|
| 1213 |
|
| 1214 |
if self.busy: |
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
if self.activity in ['Align frames', 'Select frames', 'Select stack size', 'Set ROI', |
| 1220 |
'Set alignment points']: |
| 1221 |
self.activate_gui_elements([self.ui.comboBox_back], True) |
| 1222 |
else: |
| 1223 |
self.activate_gui_elements([self.ui.comboBox_back], False) |
| 1224 |
self.activate_gui_elements([self.ui.pushButton_start, |
| 1225 |
self.ui.pushButton_next_job, self.ui.menuFile, |
| 1226 |
self.ui.menuEdit, self.ui.menuCalibrate], False) |
| 1227 |
self.activate_gui_elements([self.ui.pushButton_pause], True) |
| 1228 |
if self.job_index < self.job_number: |
| 1229 |
if self.activity == 'Select frames': |
| 1230 |
self.write_status_bar("Select / de-select frames to be used for stacking.", |
| 1231 |
"red") |
| 1232 |
elif self.activity == 'Select stack size': |
| 1233 |
self.write_status_bar("Select the number / percentage of frames to be stacked.", |
| 1234 |
"red") |
| 1235 |
elif self.activity == 'Rank frames': |
| 1236 |
self.write_status_bar("Select / de-select frames to be used for stacking.", |
| 1237 |
"red") |
| 1238 |
else: |
| 1239 |
self.write_status_bar("Busy processing " + self.jobs[self.job_index].file_name, |
| 1240 |
"black") |
| 1241 |
|
| 1242 |
|
| 1243 |
else: |
| 1244 |
self.activate_gui_elements([self.ui.menuFile, self.ui.menuEdit, self.ui.menuCalibrate], |
| 1245 |
True) |
| 1246 |
self.activate_gui_elements([self.ui.pushButton_pause], False) |
| 1247 |
if self.activity == "Next job": |
| 1248 |
self.activate_gui_elements([self.ui.actionSave, self.ui.actionSave_as], True) |
| 1249 |
else: |
| 1250 |
self.activate_gui_elements([self.ui.actionSave, self.ui.actionSave_as], False) |
| 1251 |
activated_buttons = [] |
| 1252 |
if self.job_index < self.job_number: |
| 1253 |
activated_buttons.append(self.ui.pushButton_start) |
| 1254 |
if self.job_index > 0 or self.activity != "Read frames": |
| 1255 |
activated_buttons.append(self.ui.comboBox_back) |
| 1256 |
if self.job_index < self.job_number - 1: |
| 1257 |
activated_buttons.append(self.ui.pushButton_next_job) |
| 1258 |
if activated_buttons: |
| 1259 |
self.activate_gui_elements(activated_buttons, True) |
| 1260 |
message = "To continue, press '" + \ |
| 1261 |
self.button_get_description(activated_buttons[0]) + "'" |
| 1262 |
if len(activated_buttons) > 1: |
| 1263 |
for button in activated_buttons[1:]: |
| 1264 |
message += ", or '" + self.button_get_description(button) + "'" |
| 1265 |
message += ", define new jobs (File/Open), or exit the program with 'Quit'." |
| 1266 |
self.write_status_bar(message, "red") |
| 1267 |
else: |
| 1268 |
self.write_status_bar("Load new jobs, or quit.", "red") |
| 1269 |
|
| 1270 |
|
| 1271 |
self.show_batch_progress_widgets(self.job_number > 1) |
| 1272 |
if self.job_number > 0: |
| 1273 |
self.ui.progressBar_batch.setValue(int(100 * self.job_index / self.job_number)) |
| 1274 |
|
| 1275 |
def button_get_description(self, element): |
| 1276 |
""" |
| 1277 |
Get the textual description of a button. Different methods must be used for "pushButton" and |
| 1278 |
comboBoxes. |
| 1279 |
|
| 1280 |
:param element: Button object |
| 1281 |
:return: Text written on the button (string) |
| 1282 |
""" |
| 1283 |
|
| 1284 |
try: |
| 1285 |
description = element.text() |
| 1286 |
return description |
| 1287 |
except: |
| 1288 |
pass |
| 1289 |
try: |
| 1290 |
description = element.currentText() |
| 1291 |
return description |
| 1292 |
except: |
| 1293 |
return "" |
| 1294 |
|
| 1295 |
def activate_gui_elements(self, elements, enable): |
| 1296 |
""" |
| 1297 |
Enable / Disable selected GUI elements. |
| 1298 |
|
| 1299 |
:param elements: List of GUI elements. |
| 1300 |
:param enable: If "True", enable the elements; otherwise disable them. |
| 1301 |
:return: - |
| 1302 |
""" |
| 1303 |
|
| 1304 |
for element in elements: |
| 1305 |
element.setEnabled(enable) |
| 1306 |
|
| 1307 |
@QtCore.pyqtSlot(str, str) |
| 1308 |
def write_status_bar(self, message, color): |
| 1309 |
""" |
| 1310 |
Set the text in the status bar to "message". |
| 1311 |
|
| 1312 |
:param message: Text to be displayed |
| 1313 |
:param color: Color in which the text is to be displayed (string) |
| 1314 |
:return: - |
| 1315 |
""" |
| 1316 |
|
| 1317 |
self.ui.statusBar.showMessage(message) |
| 1318 |
self.ui.statusBar.setStyleSheet('color: ' + color) |
| 1319 |
|
| 1320 |
def append_status_bar(self, append_message): |
| 1321 |
""" |
| 1322 |
Append text to the current status bar message. |
| 1323 |
|
| 1324 |
:param append_message: Text to be appended. |
| 1325 |
:return: - |
| 1326 |
""" |
| 1327 |
|
| 1328 |
self.ui.statusBar.showMessage(self.ui.statusBar.currentMessage() + append_message) |
| 1329 |
|
| 1330 |
def about_pss(self): |
| 1331 |
|
| 1332 |
USER = getpass.getuser() |
| 1333 |
PC = platform.node() |
| 1334 |
OS = '{0} {1} {2}'.format(platform.system(), platform.release(), platform.architecture()[0]) |
| 1335 |
PYTHON_VERSION = platform.python_version() |
| 1336 |
QT_VERSION = QtCore.qVersion() |
| 1337 |
CPU = psutil.cpu_count() |
| 1338 |
MEMORY = psutil.virtual_memory()[0] / 1024 ** 3 |
| 1339 |
MATPLOTLIB_VERSION = matplotlib.__version__ |
| 1340 |
OPENCV_VERSION = cv2.__version__ |
| 1341 |
NUMPY_VERSION = np.__version__ |
| 1342 |
PSUTIL_VERSION = psutil.__version__ |
| 1343 |
SCIPY_VERSION = scipy.__version__ |
| 1344 |
SKIMAGE_VERSION = skimage.__version__ |
| 1345 |
ASTROPY_VERSION = astropy.__version__ |
| 1346 |
PIP_VERSION = pip.__version__ |
| 1347 |
|
| 1348 |
CONTENT = (''' <b>{0}</b><br><br> |
| 1349 |
Produce a sharp image of a planetary system object (moon, sun, planets) |
| 1350 |
from many seeing-affected frames using the "lucky imaging" technique. |
| 1351 |
<br><br> |
| 1352 |
The program is mainly targeted at extended objects (moon, sun), |
| 1353 |
but it has been applied successfully to the imaging of planets as well. |
| 1354 |
<br><br> |
| 1355 |
Homepage: <a href='https://github.com/Rolf-Hempel/PlanetarySystemStacker'> |
| 1356 |
https://github.com/Rolf-Hempel/PlanetarySystemStacker</a> |
| 1357 |
<br><br> |
| 1358 |
System overview:<br> |
| 1359 |
PC: {2}<br> |
| 1360 |
CPU Cores: {4}<br> |
| 1361 |
Memory: {5:.1f} [GB]<br> |
| 1362 |
OS: {3}<br> |
| 1363 |
User: {1}<br> |
| 1364 |
<br> |
| 1365 |
Software versions used:<br> |
| 1366 |
Python: {6}<br> |
| 1367 |
Qt: {7}<br> |
| 1368 |
Matplotlib: {8}<br> |
| 1369 |
OpenCV: {9}<br> |
| 1370 |
Numpy: {10}<br> |
| 1371 |
Psutil: {11}<br> |
| 1372 |
Scipy: {12}<br> |
| 1373 |
Scikit-image: {13}<br> |
| 1374 |
Astropy: {14}<br> |
| 1375 |
Pip: {15}'''.format(self.configuration.global_parameters_version, USER, PC, OS, CPU, MEMORY, |
| 1376 |
PYTHON_VERSION, |
| 1377 |
QT_VERSION, |
| 1378 |
MATPLOTLIB_VERSION, |
| 1379 |
OPENCV_VERSION, |
| 1380 |
NUMPY_VERSION, |
| 1381 |
PSUTIL_VERSION, |
| 1382 |
SCIPY_VERSION, |
| 1383 |
SKIMAGE_VERSION, |
| 1384 |
ASTROPY_VERSION, |
| 1385 |
PIP_VERSION |
| 1386 |
)) |
| 1387 |
|
| 1388 |
msgBox = QtWidgets.QMessageBox() |
| 1389 |
msgBox.setIcon(QtWidgets.QMessageBox.Information) |
| 1390 |
msgBox.setWindowIcon(QtGui.QIcon(self.configuration.window_icon)) |
| 1391 |
msgBox.setText(CONTENT) |
| 1392 |
msgBox.setWindowTitle("About PlanetarySystemStacker") |
| 1393 |
msgBox.setStandardButtons(QtWidgets.QMessageBox.Ok) |
| 1394 |
msgBox.exec() |
| 1395 |
|
| 1396 |
def closeEvent(self, event=None): |
| 1397 |
""" |
| 1398 |
This event is triggered when the user closes the main window by clicking on the cross in |
| 1399 |
the window corner, or selects 'Quit' in the file menu. |
| 1400 |
|
| 1401 |
:param event: event object |
| 1402 |
:return: - |
| 1403 |
""" |
| 1404 |
|
| 1405 |
|
| 1406 |
quit_msg = "Are you sure you want to exit?" |
| 1407 |
reply = QtWidgets.QMessageBox.question(self, self.configuration.global_parameters_version, |
| 1408 |
quit_msg, |
| 1409 |
QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No, |
| 1410 |
QtWidgets.QMessageBox.No) |
| 1411 |
|
| 1412 |
if reply == QtWidgets.QMessageBox.Yes: |
| 1413 |
if event: |
| 1414 |
event.accept() |
| 1415 |
|
| 1416 |
|
| 1417 |
if self.workflow.attached_log_file: |
| 1418 |
self.workflow.attached_log_file.close() |
| 1419 |
|
| 1420 |
|
| 1421 |
if self.windowState() == QtCore.Qt.WindowMaximized: |
| 1422 |
self.configuration.hidden_parameters_main_window_maximized = True |
| 1423 |
else: |
| 1424 |
(x0, y0, width, height) = self.geometry().getRect() |
| 1425 |
self.configuration.hidden_parameters_main_window_x0 = x0 |
| 1426 |
self.configuration.hidden_parameters_main_window_y0 = y0 |
| 1427 |
self.configuration.hidden_parameters_main_window_width = width |
| 1428 |
self.configuration.hidden_parameters_main_window_height = height |
| 1429 |
self.configuration.hidden_parameters_main_window_maximized = False |
| 1430 |
|
| 1431 |
|
| 1432 |
self.configuration.write_config() |
| 1433 |
exit(0) |
| 1434 |
else: |
| 1435 |
|
| 1436 |
if event: |
| 1437 |
event.ignore() |
| 1438 |
|
| 1439 |
|
| 1440 |
def main(): |
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
if len(argv) <= 1: |
| 1457 |
app = QtWidgets.QApplication(argv) |
| 1458 |
|
| 1459 |
myapp = PlanetarySystemStacker() |
| 1460 |
myapp.show() |
| 1461 |
exit(app.exec_()) |
| 1462 |
|
| 1463 |
|
| 1464 |
else: |
| 1465 |
app = QtCore.QCoreApplication(argv) |
| 1466 |
PssConsole() |
| 1467 |
exit(app.exec_()) |
| 1468 |
|
| 1469 |
if __name__ == "__main__": |
| 1470 |
main() |