| 1 |
|
| 2 |
""" |
| 3 |
Copyright (c) 2019 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 |
""" |
| 22 |
|
| 23 |
from numpy import arange |
| 24 |
from PyQt5 import QtWidgets |
| 25 |
import matplotlib |
| 26 |
matplotlib.use('Agg') |
| 27 |
import matplotlib.pyplot as plt |
| 28 |
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as Canvas |
| 29 |
from matplotlib.figure import Figure |
| 30 |
|
| 31 |
from shift_distribution_viewer_gui import Ui_shift_distribution_viewer |
| 32 |
|
| 33 |
|
| 34 |
class MatplotlibWidget(Canvas): |
| 35 |
""" |
| 36 |
This widget creates a plot of frame qualities, either sorted chronologically or by quality. |
| 37 |
|
| 38 |
""" |
| 39 |
|
| 40 |
def __init__(self, parent=None): |
| 41 |
""" |
| 42 |
Initialize the widget. |
| 43 |
|
| 44 |
:param parent: Parent object |
| 45 |
""" |
| 46 |
|
| 47 |
super(MatplotlibWidget, self).__init__(Figure()) |
| 48 |
|
| 49 |
self.setParent(parent) |
| 50 |
|
| 51 |
plt.rcParams.update({'font.size': 8}) |
| 52 |
self.fig, self.ax = plt.subplots() |
| 53 |
|
| 54 |
def draw_distribution(self, shift_distribution): |
| 55 |
""" |
| 56 |
Draw the shift distribution. |
| 57 |
|
| 58 |
:param shift_distribution: 1D Numpy array (int) with counts for each shift size. |
| 59 |
:return: - |
| 60 |
""" |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
if max(shift_distribution) > 0: |
| 65 |
max_index = [index for index, item in enumerate(shift_distribution) if item != 0][-1] + 1 |
| 66 |
plt.title('Frequency distribution of local warp sizes at alignment points') |
| 67 |
else: |
| 68 |
max_index = 1 |
| 69 |
plt.title('No alignment points, therefore no warp distribution') |
| 70 |
|
| 71 |
pixels = arange(max_index) |
| 72 |
|
| 73 |
plt.bar(pixels, shift_distribution[:max_index], align='center') |
| 74 |
plt.xticks(pixels, pixels) |
| 75 |
plt.xlabel('Warp size (pixels)') |
| 76 |
plt.ylabel('Frequency') |
| 77 |
self.fig.canvas.draw() |
| 78 |
self.fig.canvas.flush_events() |
| 79 |
|
| 80 |
|
| 81 |
class ShiftDistributionViewerWidget(QtWidgets.QFrame, Ui_shift_distribution_viewer): |
| 82 |
""" |
| 83 |
Display the distribution of frame shifts at alignment points. For each AP in each contributing |
| 84 |
frame the total shift (in pixels) is rounded to the next integer value. For every integer number |
| 85 |
up to the maximum frame shift the number of occurrences is displayed. |
| 86 |
""" |
| 87 |
|
| 88 |
def __init__(self, parent_gui, shift_distribution, shift_failure_percent, signal_finished, |
| 89 |
parent=None): |
| 90 |
""" |
| 91 |
Initialize the Viewer. The widget has a fixed size and is rendered as a QFrame. |
| 92 |
|
| 93 |
:param parent_gui: GUI object by which the viewer is invoked. |
| 94 |
:param parent: Parent object |
| 95 |
:param shift_distribution: 1D Numpy array (int) with counts for each shift size. |
| 96 |
:param shift_failure_percent: Percent of failed warp shift measurements. |
| 97 |
:param signal_finished: Qt signal with signature () telling the workflow thread that the |
| 98 |
viewer has finished, or None (no signalling). |
| 99 |
""" |
| 100 |
|
| 101 |
QtWidgets.QFrame.__init__(self, parent) |
| 102 |
self.setupUi(self) |
| 103 |
|
| 104 |
self.setFrameShape(QtWidgets.QFrame.Panel) |
| 105 |
self.setFrameShadow(QtWidgets.QFrame.Sunken) |
| 106 |
self.setObjectName("shift_distribution_viewer") |
| 107 |
|
| 108 |
self.setFixedSize(900, 600) |
| 109 |
|
| 110 |
self.parent_gui = parent_gui |
| 111 |
self.shift_distribution = shift_distribution |
| 112 |
self.signal_finished = signal_finished |
| 113 |
|
| 114 |
self.buttonBox.accepted.connect(self.accept) |
| 115 |
|
| 116 |
|
| 117 |
self.matplotlib_widget = MatplotlibWidget() |
| 118 |
self.verticalLayout.insertWidget(0, Canvas(self.matplotlib_widget.fig)) |
| 119 |
|
| 120 |
self.matplotlib_widget.draw_distribution(self.shift_distribution) |
| 121 |
|
| 122 |
|
| 123 |
if shift_failure_percent >= 0.: |
| 124 |
self.failedShiftsLabel.setText( |
| 125 |
"Failed shift measurements: " + str(shift_failure_percent) + "%") |
| 126 |
if shift_failure_percent > 5.: |
| 127 |
self.failedShiftsLabel.setStyleSheet('color: red') |
| 128 |
else: |
| 129 |
self.failedShiftsLabel.setStyleSheet('color: black') |
| 130 |
else: |
| 131 |
self.failedShiftsLabel.setText("") |
| 132 |
|
| 133 |
def accept(self): |
| 134 |
""" |
| 135 |
|
| 136 |
:return: - |
| 137 |
""" |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
if self.signal_finished is not None: |
| 142 |
self.signal_finished.emit() |
| 143 |
|
| 144 |
self.close() |
| 145 |
|
| 146 |
def closeEvent(self, event): |
| 147 |
""" |
| 148 |
Remove the widget from the parent GUI and close the viewer. |
| 149 |
|
| 150 |
:param event: Close event object |
| 151 |
:return: - |
| 152 |
""" |
| 153 |
|
| 154 |
|
| 155 |
self.parent_gui.display_widget(None, display=False) |
| 156 |
self.close() |