class Config(SignalNode.Config): """Config widget displayed for LSLInput.""" def __init__(self, parent=None): super().__init__(parent=parent) self.data_source = QComboBox() for source in LSLInput.data_sources: self.data_source.addItem(source.name) self.data_source.currentTextChanged.connect(self._adjust) self.channel_count = QLabel() self.channel_count.setFrameStyle(QFrame.Panel | QFrame.Sunken) self.channel_count.setAlignment(Qt.AlignCenter) self.channel_count.setMargin(5) self.frequency = QLabel() self.frequency.setFrameStyle(QFrame.Panel | QFrame.Sunken) self.frequency.setAlignment(Qt.AlignCenter) self.frequency.setMargin(5) layout = QFormLayout() self.setLayout(layout) layout.addRow("Data source:", self.data_source) layout.addRow("Channel count:", self.channel_count) layout.addRow("Frequency:", self.frequency) self._adjust() def _adjust(self): """Adjust displayed values after a change.""" # Sync changes with the node self.updateModel() # Find the data_source with the selected name for data_source in LSLInput.data_sources: if data_source.name == self.data_source.currentText(): break else: data_source = None # Update displays to show new information self.channel_count.setText(str(data_source.channel_count)) self.frequency.setText(str(data_source.frequency) + " Hz") def updateModel(self): n = self.node() if n is None: return n.setDataSource(self.data_source.currentText()) def updateView(self): n = self.node() if n is None: return self.data_source.blockSignals(True) self.data_source.setCurrentText(n.dataSource()) self.data_source.blockSignals(False)
def comboBoxSelectItemByText(combobox: QtWidgets.QComboBox, value, block=False): index = combobox.findText(value) if index >= 0: if block: block_state = combobox.blockSignals(True) combobox.setCurrentIndex(index) if block: combobox.blockSignals(block_state)
def setEditorData(self, editor: QComboBox, index: QModelIndex): """Update the editor.""" items = [artist.name for artist in self._manager.context.artists] current = index.model().data(index, RoleTaskArtist) try: row = items.index(current) except ValueError: # None is not in list row = -1 editor.blockSignals(True) editor.setCurrentIndex(row) editor.blockSignals(False)
class Config(SignalNode.Config): """Config widget displayed for LSLInput.""" def __init__(self, parent=None): super().__init__(parent=parent) self.smoothing_factor = QDoubleSpinBox() self.smoothing_factor.setMinimum(0) self.smoothing_factor.setMaximum(1) self.smoothing_factor.setSingleStep(0.1) self.smoothing_factor.setPrefix("x") self.smoothing_factor.valueChanged.connect(self.updateModel) self.method = QComboBox() self.method.addItem("Rectification") self.method.addItem("Fourier Transform") self.method.addItem("Hilbert Transform") self.method.addItem("cFIR") self.method.currentTextChanged.connect(self.updateModel) self.smoother_type = QComboBox() for name in EnvelopeDetector.smoother_name_to_type: self.smoother_type.addItem(name) self.smoother_type.currentTextChanged.connect(self.updateModel) layout = QFormLayout() self.setLayout(layout) layout.addRow("Smoothing factor", self.smoothing_factor) layout.addRow("Method", self.method) layout.addRow("Smoother type", self.smoother_type) def updateModel(self): n = self.node() if n is None: return smoothing_factor = self.smoothing_factor.value() method = self.method.currentText() smoother_type = n.smoother_name_to_type[self.smoother_type.currentText()] n.setSmoothingFactor(smoothing_factor) n.setMethod(method) n.setSmootherType(smoother_type) def updateView(self): n = self.node() if n is None: return self.smoothing_factor.blockSignals(True) self.method.blockSignals(True) self.smoother_type.blockSignals(True) self.smoothing_factor.setValue(n.smoothingFactor()) self.method.setCurrentText(n.method()) self.smoother_type.setCurrentText(n.smoother_type_to_name[n.smootherType()]) self.smoothing_factor.blockSignals(False) self.method.blockSignals(False) self.smoother_type.blockSignals(False)
class LabeledComboBox(QWidget): currentTextChanged = Signal(str) def __init__(self, label_string, items=[]): super(LabeledComboBox, self).__init__() self.label = QLabel(label_string) self.combo_box = QComboBox(self) self.items = items for item in self.items: self.combo_box.addItem(item) self.previous_item = [None, None] self.combo_box.currentTextChanged.connect(self._currentTextChanged) ComboBoxLayout = QVBoxLayout(self) ComboBoxLayout.addWidget(self.label) ComboBoxLayout.addWidget(self.combo_box) def currentText(self): return self.combo_box.currentText() def _currentTextChanged(self, text): self.previous_item = [ self.previous_item[1], self.combo_box.currentText() ] self.currentTextChanged.emit(text) def revert(self): self.setCurrentText(self.previous_item[0], quiet=True) self.previous_item = [None, self.previous_item[0]] def setCurrentText(self, value, quiet=False): if quiet: self.combo_box.blockSignals(True) self.combo_box.setCurrentText(value) self.combo_box.blockSignals(False) else: self.combo_box.setCurrentText(value)
def update_combobox(box: QComboBox, labels: List[str]) -> None: """Update the combobox menu.""" box.blockSignals(True) box.clear() box.insertItems(0, labels) box.blockSignals(False)
def set_combobox_index(box: QComboBox, index: int) -> None: """Update the index on the given QComboBox without sending a signal.""" box.blockSignals(True) box.setCurrentIndex(index) box.blockSignals(False)
class Config(SignalNode.Config): """Config widget displayed for BandpassFilter.""" def __init__(self, parent=None): super().__init__(parent=parent) # Upper bound ---------------------------------------------------------------------------------------------- self.lower_bound_enable = QCheckBox() self.lower_bound_enable.setChecked(True) self.lower_bound_enable.stateChanged.connect(self.updateModel) self.lower_bound = QDoubleSpinBox() self.lower_bound.valueChanged.connect(self.updateModel) self.lower_bound.setMinimum(0) self.lower_bound.setMaximum(250) self.lower_bound.setSuffix(" Hz") layout = QHBoxLayout() layout.setContentsMargins(0, 0, 0, 0) lower_bound_widget = QWidget() lower_bound_widget.setContentsMargins(0, 0, 0, 0) lower_bound_widget.setLayout(layout) layout.addWidget(self.lower_bound_enable) layout.addWidget(self.lower_bound) # Lower bound ---------------------------------------------------------------------------------------------- self.upper_bound_enable = QCheckBox() self.upper_bound_enable.setChecked(True) self.upper_bound_enable.stateChanged.connect(self.updateModel) self.upper_bound = QDoubleSpinBox() self.upper_bound.valueChanged.connect(self.updateModel) self.upper_bound.setMinimum(0) self.upper_bound.setMaximum(250) self.upper_bound.setSuffix(" Hz") layout = QHBoxLayout() layout.setContentsMargins(0, 0, 0, 0) upper_bound_widget = QWidget() upper_bound_widget.setContentsMargins(0, 0, 0, 0) upper_bound_widget.setLayout(layout) layout.addWidget(self.upper_bound_enable) layout.addWidget(self.upper_bound) # Filter type and length ----------------------------------------------------------------------------------- self.filter_type = QComboBox() for name in BandpassFilter.filter_name_to_type: self.filter_type.addItem(name) self.filter_type.currentTextChanged.connect(self.updateModel) self.filter_length = QSpinBox() self.filter_length.setMinimum(2) self.filter_length.setMaximum(1000000) self.filter_length.setValue(1000) self.filter_length.valueChanged.connect(self.updateModel) self.filter_order = QSpinBox() self.filter_order.setRange(1, 4) self.filter_order.valueChanged.connect(self.updateModel) # ---------------------------------------------------------------------------------------------------------- layout = QFormLayout() layout.addRow("Lower bound:", lower_bound_widget) layout.addRow("Upper bound:", upper_bound_widget) layout.addRow("Filter type:", self.filter_type) layout.addRow("Filter order:", self.filter_order) layout.addRow("Filter length:", self.filter_length) self.setLayout(layout) def updateModel(self): n = self.node() if n is None: return if self.lower_bound_enable.isChecked(): lower_bound = self.lower_bound.value() else: lower_bound = None if self.upper_bound_enable.isChecked(): upper_bound = self.upper_bound.value() else: upper_bound = None filter_type = n.filter_name_to_type[self.filter_type.currentText()] filter_length = self.filter_length.value() filter_order = self.filter_order.value() n.setLowerBound(lower_bound) n.setUpperBound(upper_bound) n.setFilterType(filter_type) n.setFilterLength(filter_length) n.setFilterOrder(filter_order) def updateView(self): n = self.node() if n is None: return # Prevent view fields from emitting signals while they are updated self.lower_bound.blockSignals(True) self.upper_bound.blockSignals(True) self.filter_type.blockSignals(True) self.filter_length.blockSignals(True) self.filter_order.blockSignals(True) if n.upperBound() is None: self.upper_bound_enable.setChecked(False) else: self.upper_bound_enable.setChecked(True) self.upper_bound.setValue(n.upperBound()) if n.lowerBound() is None: self.lower_bound_enable.setChecked(False) else: self.lower_bound_enable.setChecked(True) self.lower_bound.setValue(n.lowerBound()) self.filter_type.setCurrentText( n.filter_type_to_name[n.filterType()]) self.filter_length.setValue(n.filterLength()) self.filter_order.setValue(n.filterOrder()) # Release the block and call adjust self.lower_bound.blockSignals(False) self.upper_bound.blockSignals(False) self.filter_type.blockSignals(False) self.filter_length.blockSignals(False) self.filter_order.blockSignals(False) self._adjust() def _adjust(self): """Adjust displayed values and limits in response to changes.""" # Enable spinbox widgets based on their checkbox self.lower_bound.setEnabled(self.lower_bound_enable.isChecked()) self.upper_bound.setEnabled(self.upper_bound_enable.isChecked()) # Adjust min and max so that lower_bound is never higher than upper_bound if self.lower_bound_enable.isChecked(): self.upper_bound.setMinimum(self.lower_bound.value()) else: self.upper_bound.setMinimum(0) if self.upper_bound_enable.isChecked(): self.lower_bound.setMaximum(self.upper_bound.value()) else: self.lower_bound.setMaximum(250) if self.filter_type.currentText() == "Butterworth": self.filter_order.setEnabled(True) else: self.filter_order.setEnabled(False)
class MayaNodeWidget(QWidget): """Widget allowing Maya node selection with a persistent cache. Users should connect to the node_changed signal. """ node_changed = Signal(str) def __init__(self, label=None, name=None, parent=None): """Constructor :param label: Optional label text. :param name: Unique name used to query persistent data. :param parent: Parent QWidget. """ super(MayaNodeWidget, self).__init__(parent) self.cache = StringCache("cmt.mayanodewidget.{}".format(name), parent=self) self._layout = QHBoxLayout(self) self._layout.setContentsMargins(0, 0, 0, 0) self.setLayout(self._layout) if label: label = QLabel(label, self) label.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) self._layout.addWidget(label) self._combo_box = QComboBox(self) self._combo_box.setEditable(True) self._combo_box.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed) self._combo_box.setInsertPolicy(QComboBox.InsertAtTop) self._combo_box.setModel(self.cache) self._combo_box.editTextChanged.connect(self.edit_changed) self._layout.addWidget(self._combo_box) button = QPushButton("<", self) button.released.connect(self.use_selected) button.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) self._layout.addWidget(button) @property def node(self): return self._combo_box.currentText() @node.setter def node(self, value): self._combo_box.setEditText(value) def edit_changed(self, text): """Slot called whenever the text changes in the combobox. :param text: New text. """ if not text: return if cmds.objExists(text): self.node_changed.emit(text) self._combo_box.blockSignals(True) self._push(text) self._combo_box.blockSignals(False) def use_selected(self): """Populate the combobox with the name of the selected node.""" selected = cmds.ls(sl=True) if selected: self._push(selected[0]) def _push(self, node): """Push a new node onto the cache. :param path: Node name. """ self.cache.push(node) self._combo_box.setCurrentIndex(0)
class WMatSelectV(QGroupBox): """ Material related widget including a Label, a Combobox to select a material and a Button to edit a material libary. WMatSelect is instantiated to empty material data, so it has to be referenced to actual material data with the update method prior to its first usage. """ # Signal to W_MachineSetup to know that the save popup is needed saveNeeded = Signal() def __init__(self, parent=None): """ Set a reference to a material libray and material data path, updates the Combobox by the material names of the libary and set a referenced material by name. Parameters ---------- self : A WMatSelect object parent : A reference to the widgets parent Returns ------- """ # Build the interface according to the .ui file QGroupBox.__init__(self, parent) self.verticalLayout = QVBoxLayout(self) self.c_mat_type = QComboBox(self) self.c_mat_type.setObjectName(u"c_mat_type") self.verticalLayout.addWidget(self.c_mat_type) self.b_matlib = QPushButton(self) self.b_matlib.setObjectName(u"b_matlib") self.b_matlib.setText("Edit Materials") self.verticalLayout.addWidget(self.b_matlib) # Create the property of the widget self.mat_win = None # DMatLib widget self.obj = None # object that has a material attribute self.mat_attr_name = "" # material attribute name self.matlib = list() # Matlib self.matlib_path = "" # Path to save the matlib self.def_mat = "M400-50A" # Default material self.is_hide_button = False # To hide the "Edit material" button # Connect the signals self.c_mat_type.currentIndexChanged.connect(self.set_mat_type) self.b_matlib.clicked.connect(self.s_open_matlib) def update(self, obj, mat_attr_name, matlib, matlib_path=""): """ Set a reference to a material libray and material data path, updates the Combobox by the material names of the libary and set a referenced material by name. Parameters ---------- self : A WMatSelect object obj : A pyleecan object that has a material attribute mat_attr_name : A string of the material attribute name matlib : A material libary, i.e. a list of Material objects matlib_path : A string containing the path of material data Returns ------- """ self.c_mat_type.blockSignals(True) # Set material combobox according to matlib names self.obj = obj self.mat_attr_name = mat_attr_name self.matlib = matlib self.matlib_path = matlib_path if self.is_hide_button: self.b_matlib.hide() else: self.b_matlib.show() # Update the list of materials self.c_mat_type.clear() items_to_add = [] # Add RefMatLib materials items_to_add.extend([mat.name for mat in matlib.dict_mat["RefMatLib"]]) # Add machine-specific materials items_to_add.extend( [mat.name for mat in matlib.dict_mat["MachineMatLib"]]) self.c_mat_type.addItems(items_to_add) mat = getattr(self.obj, mat_attr_name, None) if mat is None or mat.name is None: # Default lamination material: M400-50A index = self.c_mat_type.findText(self.def_mat) if index != -1: # self.mat.__init__(init_dict=self.matlib[index].as_dict()) setattr( self.obj, self.mat_attr_name, self.matlib.dict_mat["RefMatLib"][index], ) else: index = self.c_mat_type.findText(mat.name) self.c_mat_type.setCurrentIndex(index) self.c_mat_type.blockSignals(False) def setText(self, txt): """ Set the Label's text Parameters ---------- self : A WMatSelect object txt : A text string Returns ------- """ self.setTitle(txt) def set_mat_type(self, index): """ Signal to set the referenced material from the material libary by the selected Combobox index Parameters ---------- self : A WMatSelect object index : Current index of the combobox Returns ------- """ if index >= len(self.matlib.dict_mat["RefMatLib"]): index -= len(self.matlib.dict_mat["RefMatLib"]) dict_key = "MachineMatLib" else: dict_key = "RefMatLib" setattr(self.obj, self.mat_attr_name, self.matlib.dict_mat[dict_key][index]) # Notify the machine GUI that the machine has changed self.saveNeeded.emit() def s_open_matlib(self): """ Open the GUI (DMatLib widget) to Edit the Material library Parameters ---------- self : A WMatSelect object Returns ------- """ if self.c_mat_type.currentIndex() >= len( self.matlib.dict_mat["RefMatLib"]): index = self.c_mat_type.currentIndex() - len( self.matlib.dict_mat["RefMatLib"]) key = "MachineMatLib" else: index = self.c_mat_type.currentIndex() key = "RefMatLib" self.mat_win = DMatLib(self.matlib, key, index) self.mat_win.accepted.connect(self.set_matlib) self.mat_win.saveNeeded.connect(self.emit_save) self.mat_win.show() def emit_save(self): """ Emit saveNeeded if a material has been edited """ self.saveNeeded.emit() def set_matlib(self): """Update the matlib with the new value Parameters ---------- self : A WMatSelect object Returns ------- """ # Empty and fill the list to keep the same object (to change it everywhere) # del self.matlib[:] # self.matlib.extend(self.mat_win.matlib) # Update the material # index = int(self.mat_win.nav_mat.currentItem().text()[:3]) - 1 # not needed if machine materials are "connected" properly # mat_dict = (self.mat_win.matlib[index]).as_dict() # self.mat.__init__(init_dict=mat_dict) # Do not clear for now to keep editor (DMatLib) open # # Clear the window # self.mat_win.deleteLater() # self.mat_win = None # Update the widget # Avoid trigger signal currentIndexChanged self.c_mat_type.blockSignals(True) self.c_mat_type.clear() items_to_add = [] # Add RefMatLib materials items_to_add.extend( [mat.name for mat in self.matlib.dict_mat["RefMatLib"]]) # Add machine-specific materials items_to_add.extend( [mat.name for mat in self.matlib.dict_mat["MachineMatLib"]]) self.c_mat_type.addItems(items_to_add) index = self.c_mat_type.findText( getattr(self.obj, self.mat_attr_name).name) self.c_mat_type.setCurrentIndex(index) self.c_mat_type.blockSignals(False)
class FittingResultViewer(QDialog): PAGE_ROWS = 20 logger = logging.getLogger("root.QGrain.ui.FittingResultViewer") result_marked = Signal(SSUResult) def __init__(self, reference_viewer: ReferenceResultViewer, parent=None): super().__init__(parent=parent, f=Qt.Window) self.setWindowTitle(self.tr("SSU Fitting Result Viewer")) self.__fitting_results = [] # type: list[SSUResult] self.retry_tasks = {} # type: dict[UUID, SSUTask] self.__reference_viewer = reference_viewer self.init_ui() self.boxplot_chart = BoxplotChart(parent=self, toolbar=True) self.typical_chart = SSUTypicalComponentChart(parent=self, toolbar=True) self.distance_chart = DistanceCurveChart(parent=self, toolbar=True) self.mixed_distribution_chart = MixedDistributionChart( parent=self, toolbar=True, use_animation=True) self.file_dialog = QFileDialog(parent=self) self.async_worker = AsyncWorker() self.async_worker.background_worker.task_succeeded.connect( self.on_fitting_succeeded) self.async_worker.background_worker.task_failed.connect( self.on_fitting_failed) self.update_page_list() self.update_page(self.page_index) self.normal_msg = QMessageBox(self) self.remove_warning_msg = QMessageBox(self) self.remove_warning_msg.setStandardButtons(QMessageBox.No | QMessageBox.Yes) self.remove_warning_msg.setDefaultButton(QMessageBox.No) self.remove_warning_msg.setWindowTitle(self.tr("Warning")) self.remove_warning_msg.setText( self.tr("Are you sure to remove all SSU results?")) self.outlier_msg = QMessageBox(self) self.outlier_msg.setStandardButtons(QMessageBox.Discard | QMessageBox.Retry | QMessageBox.Ignore) self.outlier_msg.setDefaultButton(QMessageBox.Ignore) self.retry_progress_msg = QMessageBox() self.retry_progress_msg.addButton(QMessageBox.Ok) self.retry_progress_msg.button(QMessageBox.Ok).hide() self.retry_progress_msg.setWindowTitle(self.tr("Progress")) self.retry_timer = QTimer(self) self.retry_timer.setSingleShot(True) self.retry_timer.timeout.connect( lambda: self.retry_progress_msg.exec_()) def init_ui(self): self.data_table = QTableWidget(100, 100) self.data_table.setEditTriggers(QAbstractItemView.NoEditTriggers) self.data_table.setSelectionBehavior(QAbstractItemView.SelectRows) self.data_table.setAlternatingRowColors(True) self.data_table.setContextMenuPolicy(Qt.CustomContextMenu) self.main_layout = QGridLayout(self) self.main_layout.addWidget(self.data_table, 0, 0, 1, 3) self.previous_button = QPushButton( qta.icon("mdi.skip-previous-circle"), self.tr("Previous")) self.previous_button.setToolTip( self.tr("Click to back to the previous page.")) self.previous_button.clicked.connect(self.on_previous_button_clicked) self.current_page_combo_box = QComboBox() self.current_page_combo_box.addItem(self.tr("Page {0}").format(1)) self.current_page_combo_box.currentIndexChanged.connect( self.update_page) self.next_button = QPushButton(qta.icon("mdi.skip-next-circle"), self.tr("Next")) self.next_button.setToolTip(self.tr("Click to jump to the next page.")) self.next_button.clicked.connect(self.on_next_button_clicked) self.main_layout.addWidget(self.previous_button, 1, 0) self.main_layout.addWidget(self.current_page_combo_box, 1, 1) self.main_layout.addWidget(self.next_button, 1, 2) self.distance_label = QLabel(self.tr("Distance")) self.distance_label.setToolTip( self. tr("It's the function to calculate the difference (on the contrary, similarity) between two samples." )) self.distance_combo_box = QComboBox() self.distance_combo_box.addItems(built_in_distances) self.distance_combo_box.setCurrentText("log10MSE") self.distance_combo_box.currentTextChanged.connect( lambda: self.update_page(self.page_index)) self.main_layout.addWidget(self.distance_label, 2, 0) self.main_layout.addWidget(self.distance_combo_box, 2, 1, 1, 2) self.menu = QMenu(self.data_table) self.menu.setShortcutAutoRepeat(True) self.mark_action = self.menu.addAction( qta.icon("mdi.marker-check"), self.tr("Mark Selection(s) as Reference")) self.mark_action.triggered.connect(self.mark_selections) self.remove_selection_action = self.menu.addAction( qta.icon("fa.remove"), self.tr("Remove Selection(s)")) self.remove_selection_action.triggered.connect(self.remove_selections) self.remove_all_action = self.menu.addAction(qta.icon("fa.remove"), self.tr("Remove All")) self.remove_all_action.triggered.connect(self.remove_all_results) self.plot_loss_chart_action = self.menu.addAction( qta.icon("mdi.chart-timeline-variant"), self.tr("Plot Loss Chart")) self.plot_loss_chart_action.triggered.connect(self.show_distance) self.plot_distribution_chart_action = self.menu.addAction( qta.icon("fa5s.chart-area"), self.tr("Plot Distribution Chart")) self.plot_distribution_chart_action.triggered.connect( self.show_distribution) self.plot_distribution_animation_action = self.menu.addAction( qta.icon("fa5s.chart-area"), self.tr("Plot Distribution Chart (Animation)")) self.plot_distribution_animation_action.triggered.connect( self.show_history_distribution) self.detect_outliers_menu = self.menu.addMenu( qta.icon("mdi.magnify"), self.tr("Detect Outliers")) self.check_nan_and_inf_action = self.detect_outliers_menu.addAction( self.tr("Check NaN and Inf")) self.check_nan_and_inf_action.triggered.connect(self.check_nan_and_inf) self.check_final_distances_action = self.detect_outliers_menu.addAction( self.tr("Check Final Distances")) self.check_final_distances_action.triggered.connect( self.check_final_distances) self.check_component_mean_action = self.detect_outliers_menu.addAction( self.tr("Check Component Mean")) self.check_component_mean_action.triggered.connect( lambda: self.check_component_moments("mean")) self.check_component_std_action = self.detect_outliers_menu.addAction( self.tr("Check Component STD")) self.check_component_std_action.triggered.connect( lambda: self.check_component_moments("std")) self.check_component_skewness_action = self.detect_outliers_menu.addAction( self.tr("Check Component Skewness")) self.check_component_skewness_action.triggered.connect( lambda: self.check_component_moments("skewness")) self.check_component_kurtosis_action = self.detect_outliers_menu.addAction( self.tr("Check Component Kurtosis")) self.check_component_kurtosis_action.triggered.connect( lambda: self.check_component_moments("kurtosis")) self.check_component_fractions_action = self.detect_outliers_menu.addAction( self.tr("Check Component Fractions")) self.check_component_fractions_action.triggered.connect( self.check_component_fractions) self.degrade_results_action = self.detect_outliers_menu.addAction( self.tr("Degrade Results")) self.degrade_results_action.triggered.connect(self.degrade_results) self.try_align_components_action = self.detect_outliers_menu.addAction( self.tr("Try Align Components")) self.try_align_components_action.triggered.connect( self.try_align_components) self.analyse_typical_components_action = self.menu.addAction( qta.icon("ei.tags"), self.tr("Analyse Typical Components")) self.analyse_typical_components_action.triggered.connect( self.analyse_typical_components) self.load_dump_action = self.menu.addAction( qta.icon("fa.database"), self.tr("Load Binary Dump")) self.load_dump_action.triggered.connect(self.load_dump) self.save_dump_action = self.menu.addAction( qta.icon("fa.save"), self.tr("Save Binary Dump")) self.save_dump_action.triggered.connect(self.save_dump) self.save_excel_action = self.menu.addAction( qta.icon("mdi.microsoft-excel"), self.tr("Save Excel")) self.save_excel_action.triggered.connect( lambda: self.on_save_excel_clicked(align_components=False)) self.save_excel_align_action = self.menu.addAction( qta.icon("mdi.microsoft-excel"), self.tr("Save Excel (Force Alignment)")) self.save_excel_align_action.triggered.connect( lambda: self.on_save_excel_clicked(align_components=True)) self.data_table.customContextMenuRequested.connect(self.show_menu) # necessary to add actions of menu to this widget itself, # otherwise, the shortcuts will not be triggered self.addActions(self.menu.actions()) def show_menu(self, pos: QPoint): self.menu.popup(QCursor.pos()) def show_message(self, title: str, message: str): self.normal_msg.setWindowTitle(title) self.normal_msg.setText(message) self.normal_msg.exec_() def show_info(self, message: str): self.show_message(self.tr("Info"), message) def show_warning(self, message: str): self.show_message(self.tr("Warning"), message) def show_error(self, message: str): self.show_message(self.tr("Error"), message) @property def distance_name(self) -> str: return self.distance_combo_box.currentText() @property def distance_func(self) -> typing.Callable: return get_distance_func_by_name(self.distance_combo_box.currentText()) @property def page_index(self) -> int: return self.current_page_combo_box.currentIndex() @property def n_pages(self) -> int: return self.current_page_combo_box.count() @property def n_results(self) -> int: return len(self.__fitting_results) @property def selections(self): start = self.page_index * self.PAGE_ROWS temp = set() for item in self.data_table.selectedRanges(): for i in range(item.topRow(), min(self.PAGE_ROWS + 1, item.bottomRow() + 1)): temp.add(i + start) indexes = list(temp) indexes.sort() return indexes def update_page_list(self): last_page_index = self.page_index if self.n_results == 0: n_pages = 1 else: n_pages, left = divmod(self.n_results, self.PAGE_ROWS) if left != 0: n_pages += 1 self.current_page_combo_box.blockSignals(True) self.current_page_combo_box.clear() self.current_page_combo_box.addItems( [self.tr("Page {0}").format(i + 1) for i in range(n_pages)]) if last_page_index >= n_pages: self.current_page_combo_box.setCurrentIndex(n_pages - 1) else: self.current_page_combo_box.setCurrentIndex(last_page_index) self.current_page_combo_box.blockSignals(False) def update_page(self, page_index: int): def write(row: int, col: int, value: str): if isinstance(value, str): pass elif isinstance(value, int): value = str(value) elif isinstance(value, float): value = f"{value: 0.4f}" else: value = value.__str__() item = QTableWidgetItem(value) item.setTextAlignment(Qt.AlignCenter) self.data_table.setItem(row, col, item) # necessary to clear self.data_table.clear() if page_index == self.n_pages - 1: start = page_index * self.PAGE_ROWS end = self.n_results else: start, end = page_index * self.PAGE_ROWS, (page_index + 1) * self.PAGE_ROWS self.data_table.setRowCount(end - start) self.data_table.setColumnCount(7) self.data_table.setHorizontalHeaderLabels([ self.tr("Resolver"), self.tr("Distribution Type"), self.tr("N_components"), self.tr("N_iterations"), self.tr("Spent Time [s]"), self.tr("Final Distance"), self.tr("Has Reference") ]) sample_names = [ result.sample.name for result in self.__fitting_results[start:end] ] self.data_table.setVerticalHeaderLabels(sample_names) for row, result in enumerate(self.__fitting_results[start:end]): write(row, 0, result.task.resolver) write(row, 1, self.get_distribution_name(result.task.distribution_type)) write(row, 2, result.task.n_components) write(row, 3, result.n_iterations) write(row, 4, result.time_spent) write( row, 5, self.distance_func(result.sample.distribution, result.distribution)) has_ref = result.task.initial_guess is not None or result.task.reference is not None write(row, 6, self.tr("Yes") if has_ref else self.tr("No")) self.data_table.resizeColumnsToContents() def on_previous_button_clicked(self): if self.page_index > 0: self.current_page_combo_box.setCurrentIndex(self.page_index - 1) def on_next_button_clicked(self): if self.page_index < self.n_pages - 1: self.current_page_combo_box.setCurrentIndex(self.page_index + 1) def get_distribution_name(self, distribution_type: DistributionType): if distribution_type == DistributionType.Normal: return self.tr("Normal") elif distribution_type == DistributionType.Weibull: return self.tr("Weibull") elif distribution_type == DistributionType.SkewNormal: return self.tr("Skew Normal") else: raise NotImplementedError(distribution_type) def add_result(self, result: SSUResult): if self.n_results == 0 or \ (self.page_index == self.n_pages - 1 and \ divmod(self.n_results, self.PAGE_ROWS)[-1] != 0): need_update = True else: need_update = False self.__fitting_results.append(result) self.update_page_list() if need_update: self.update_page(self.page_index) def add_results(self, results: typing.List[SSUResult]): if self.n_results == 0 or \ (self.page_index == self.n_pages - 1 and \ divmod(self.n_results, self.PAGE_ROWS)[-1] != 0): need_update = True else: need_update = False self.__fitting_results.extend(results) self.update_page_list() if need_update: self.update_page(self.page_index) def mark_selections(self): for index in self.selections: self.result_marked.emit(self.__fitting_results[index]) def remove_results(self, indexes): results = [] for i in reversed(indexes): res = self.__fitting_results.pop(i) results.append(res) self.update_page_list() self.update_page(self.page_index) def remove_selections(self): indexes = self.selections self.remove_results(indexes) def remove_all_results(self): res = self.remove_warning_msg.exec_() if res == QMessageBox.Yes: self.__fitting_results.clear() self.update_page_list() self.update_page(0) def show_distance(self): results = [self.__fitting_results[i] for i in self.selections] if results is None or len(results) == 0: return result = results[0] self.distance_chart.show_distance_series(result.get_distance_series( self.distance_name), title=result.sample.name) self.distance_chart.show() def show_distribution(self): results = [self.__fitting_results[i] for i in self.selections] if results is None or len(results) == 0: return result = results[0] self.mixed_distribution_chart.show_model(result.view_model) self.mixed_distribution_chart.show() def show_history_distribution(self): results = [self.__fitting_results[i] for i in self.selections] if results is None or len(results) == 0: return result = results[0] self.mixed_distribution_chart.show_result(result) self.mixed_distribution_chart.show() def load_dump(self): filename, _ = self.file_dialog.getOpenFileName( self, self.tr("Select a binary dump file of SSU results"), None, self.tr("Binary dump (*.dump)")) if filename is None or filename == "": return with open(filename, "rb") as f: results = pickle.load(f) # type: list[SSUResult] valid = True if isinstance(results, list): for result in results: if not isinstance(result, SSUResult): valid = False break else: valid = False if valid: if self.n_results != 0 and len(results) != 0: old_classes = self.__fitting_results[0].classes_φ new_classes = results[0].classes_φ classes_inconsistent = False if len(old_classes) != len(new_classes): classes_inconsistent = True else: classes_error = np.abs(old_classes - new_classes) if not np.all(np.less_equal(classes_error, 1e-8)): classes_inconsistent = True if classes_inconsistent: self.show_error( self. tr("The results in the dump file has inconsistent grain-size classes with that in your list." )) return self.add_results(results) else: self.show_error(self.tr("The binary dump file is invalid.")) def save_dump(self): if self.n_results == 0: self.show_warning(self.tr("There is not any result in the list.")) return filename, _ = self.file_dialog.getSaveFileName( self, self.tr("Save the SSU results to binary dump file"), None, self.tr("Binary dump (*.dump)")) if filename is None or filename == "": return with open(filename, "wb") as f: pickle.dump(self.__fitting_results, f) def save_excel(self, filename, align_components=False): if self.n_results == 0: return results = self.__fitting_results.copy() classes_μm = results[0].classes_μm n_components_list = [ result.n_components for result in self.__fitting_results ] count_dict = Counter(n_components_list) max_n_components = max(count_dict.keys()) self.logger.debug( f"N_components: {count_dict}, Max N_components: {max_n_components}" ) flags = [] if not align_components: for result in results: flags.extend(range(result.n_components)) else: n_components_desc = "\n".join([ self.tr("{0} Component(s): {1}").format(n_components, count) for n_components, count in count_dict.items() ]) self.show_info( self.tr("N_components distribution of Results:\n{0}").format( n_components_desc)) stacked_components = [] for result in self.__fitting_results: for component in result.components: stacked_components.append(component.distribution) stacked_components = np.array(stacked_components) cluser = KMeans(n_clusters=max_n_components) flags = cluser.fit_predict(stacked_components) # check flags to make it unique flag_index = 0 for i, result in enumerate(self.__fitting_results): result_flags = set() for component in result.components: if flags[flag_index] in result_flags: if flags[flag_index] == max_n_components: flags[flag_index] = max_n_components - 1 else: flag_index[flag_index] += 1 result_flags.add(flags[flag_index]) flag_index += 1 flag_set = set(flags) picked = [] for target_flag in flag_set: for i, flag in enumerate(flags): if flag == target_flag: picked.append( (target_flag, logarithmic(classes_μm, stacked_components[i])["mean"])) break picked.sort(key=lambda x: x[1]) flag_map = {flag: index for index, (flag, _) in enumerate(picked)} flags = np.array([flag_map[flag] for flag in flags]) wb = openpyxl.Workbook() prepare_styles(wb) ws = wb.active ws.title = self.tr("README") description = \ """ This Excel file was generated by QGrain ({0}). Please cite: Liu, Y., Liu, X., Sun, Y., 2021. QGrain: An open-source and easy-to-use software for the comprehensive analysis of grain size distributions. Sedimentary Geology 423, 105980. https://doi.org/10.1016/j.sedgeo.2021.105980 It contanins 4 + max(N_components) sheets: 1. The first sheet is the sample distributions of SSU results. 2. The second sheet is used to put the infomation of fitting. 3. The third sheet is the statistic parameters calculated by statistic moment method. 4. The fouth sheet is the distributions of unmixed components and their sum of each sample. 5. Other sheets are the unmixed end-member distributions which were discretely stored. The SSU algorithm is implemented by QGrain. """.format(QGRAIN_VERSION) def write(row, col, value, style="normal_light"): cell = ws.cell(row + 1, col + 1, value=value) cell.style = style lines_of_desc = description.split("\n") for row, line in enumerate(lines_of_desc): write(row, 0, line, style="description") ws.column_dimensions[column_to_char(0)].width = 200 ws = wb.create_sheet(self.tr("Sample Distributions")) write(0, 0, self.tr("Sample Name"), style="header") ws.column_dimensions[column_to_char(0)].width = 16 for col, value in enumerate(classes_μm, 1): write(0, col, value, style="header") ws.column_dimensions[column_to_char(col)].width = 10 for row, result in enumerate(results, 1): if row % 2 == 0: style = "normal_dark" else: style = "normal_light" write(row, 0, result.sample.name, style=style) for col, value in enumerate(result.sample.distribution, 1): write(row, col, value, style=style) QCoreApplication.processEvents() ws = wb.create_sheet(self.tr("Information of Fitting")) write(0, 0, self.tr("Sample Name"), style="header") ws.column_dimensions[column_to_char(0)].width = 16 headers = [ self.tr("Distribution Type"), self.tr("N_components"), self.tr("Resolver"), self.tr("Resolver Settings"), self.tr("Initial Guess"), self.tr("Reference"), self.tr("Spent Time [s]"), self.tr("N_iterations"), self.tr("Final Distance [log10MSE]") ] for col, value in enumerate(headers, 1): write(0, col, value, style="header") if col in (4, 5, 6): ws.column_dimensions[column_to_char(col)].width = 10 else: ws.column_dimensions[column_to_char(col)].width = 10 for row, result in enumerate(results, 1): if row % 2 == 0: style = "normal_dark" else: style = "normal_light" write(row, 0, result.sample.name, style=style) write(row, 1, result.distribution_type.name, style=style) write(row, 2, result.n_components, style=style) write(row, 3, result.task.resolver, style=style) write(row, 4, self.tr("Default") if result.task.resolver_setting is None else result.task.resolver_setting.__str__(), style=style) write(row, 5, self.tr("None") if result.task.initial_guess is None else result.task.initial_guess.__str__(), style=style) write(row, 6, self.tr("None") if result.task.reference is None else result.task.reference.__str__(), style=style) write(row, 7, result.time_spent, style=style) write(row, 8, result.n_iterations, style=style) write(row, 9, result.get_distance("log10MSE"), style=style) ws = wb.create_sheet(self.tr("Statistic Moments")) write(0, 0, self.tr("Sample Name"), style="header") ws.merge_cells(start_row=1, start_column=1, end_row=2, end_column=1) ws.column_dimensions[column_to_char(0)].width = 16 headers = [] sub_headers = [ self.tr("Proportion"), self.tr("Mean [φ]"), self.tr("Mean [μm]"), self.tr("STD [φ]"), self.tr("STD [μm]"), self.tr("Skewness"), self.tr("Kurtosis") ] for i in range(max_n_components): write(0, i * len(sub_headers) + 1, self.tr("C{0}").format(i + 1), style="header") ws.merge_cells(start_row=1, start_column=i * len(sub_headers) + 2, end_row=1, end_column=(i + 1) * len(sub_headers) + 1) headers.extend(sub_headers) for col, value in enumerate(headers, 1): write(1, col, value, style="header") ws.column_dimensions[column_to_char(col)].width = 10 flag_index = 0 for row, result in enumerate(results, 2): if row % 2 == 0: style = "normal_light" else: style = "normal_dark" write(row, 0, result.sample.name, style=style) for component in result.components: index = flags[flag_index] write(row, index * len(sub_headers) + 1, component.fraction, style=style) write(row, index * len(sub_headers) + 2, component.logarithmic_moments["mean"], style=style) write(row, index * len(sub_headers) + 3, component.geometric_moments["mean"], style=style) write(row, index * len(sub_headers) + 4, component.logarithmic_moments["std"], style=style) write(row, index * len(sub_headers) + 5, component.geometric_moments["std"], style=style) write(row, index * len(sub_headers) + 6, component.logarithmic_moments["skewness"], style=style) write(row, index * len(sub_headers) + 7, component.logarithmic_moments["kurtosis"], style=style) flag_index += 1 ws = wb.create_sheet(self.tr("Unmixed Components")) ws.merge_cells(start_row=1, start_column=1, end_row=1, end_column=2) write(0, 0, self.tr("Sample Name"), style="header") ws.column_dimensions[column_to_char(0)].width = 16 for col, value in enumerate(classes_μm, 2): write(0, col, value, style="header") ws.column_dimensions[column_to_char(col)].width = 10 row = 1 for result_index, result in enumerate(results, 1): if result_index % 2 == 0: style = "normal_dark" else: style = "normal_light" write(row, 0, result.sample.name, style=style) ws.merge_cells(start_row=row + 1, start_column=1, end_row=row + result.n_components + 1, end_column=1) for component_i, component in enumerate(result.components, 1): write(row, 1, self.tr("C{0}").format(component_i), style=style) for col, value in enumerate( component.distribution * component.fraction, 2): write(row, col, value, style=style) row += 1 write(row, 1, self.tr("Sum"), style=style) for col, value in enumerate(result.distribution, 2): write(row, col, value, style=style) row += 1 ws_dict = {} flag_set = set(flags) for flag in flag_set: ws = wb.create_sheet(self.tr("Unmixed EM{0}").format(flag + 1)) write(0, 0, self.tr("Sample Name"), style="header") ws.column_dimensions[column_to_char(0)].width = 16 for col, value in enumerate(classes_μm, 1): write(0, col, value, style="header") ws.column_dimensions[column_to_char(col)].width = 10 ws_dict[flag] = ws flag_index = 0 for row, result in enumerate(results, 1): if row % 2 == 0: style = "normal_dark" else: style = "normal_light" for component in result.components: flag = flags[flag_index] ws = ws_dict[flag] write(row, 0, result.sample.name, style=style) for col, value in enumerate(component.distribution, 1): write(row, col, value, style=style) flag_index += 1 wb.save(filename) wb.close() def on_save_excel_clicked(self, align_components=False): if self.n_results == 0: self.show_warning(self.tr("There is not any SSU result.")) return filename, _ = self.file_dialog.getSaveFileName( None, self.tr("Choose a filename to save SSU Results"), None, "Microsoft Excel (*.xlsx)") if filename is None or filename == "": return try: self.save_excel(filename, align_components) self.show_info( self.tr("SSU results have been saved to:\n {0}").format( filename)) except Exception as e: self.show_error( self. tr("Error raised while save SSU results to Excel file.\n {0}" ).format(e.__str__())) def on_fitting_succeeded(self, result: SSUResult): result_replace_index = self.retry_tasks[result.task.uuid] self.__fitting_results[result_replace_index] = result self.retry_tasks.pop(result.task.uuid) self.retry_progress_msg.setText( self.tr("Tasks to be retried: {0}").format(len(self.retry_tasks))) if len(self.retry_tasks) == 0: self.retry_progress_msg.close() self.logger.debug( f"Retried task succeeded, sample name={result.task.sample.name}, distribution_type={result.task.distribution_type.name}, n_components={result.task.n_components}" ) self.update_page(self.page_index) def on_fitting_failed(self, failed_info: str, task: SSUTask): # necessary to remove it from the dict self.retry_tasks.pop(task.uuid) if len(self.retry_tasks) == 0: self.retry_progress_msg.close() self.show_error( self.tr("Failed to retry task, sample name={0}.\n{1}").format( task.sample.name, failed_info)) self.logger.warning( f"Failed to retry task, sample name={task.sample.name}, distribution_type={task.distribution_type.name}, n_components={task.n_components}" ) def retry_results(self, indexes, results): assert len(indexes) == len(results) if len(results) == 0: return self.retry_progress_msg.setText( self.tr("Tasks to be retried: {0}").format(len(results))) self.retry_timer.start(1) for index, result in zip(indexes, results): query = self.__reference_viewer.query_reference(result.sample) ref_result = None if query is None: nearby_results = self.__fitting_results[ index - 5:index] + self.__fitting_results[index + 1:index + 6] ref_result = self.__reference_viewer.find_similar( result.sample, nearby_results) else: ref_result = query keys = ["mean", "std", "skewness"] # reference = [{key: comp.logarithmic_moments[key] for key in keys} for comp in ref_result.components] task = SSUTask( result.sample, ref_result.distribution_type, ref_result.n_components, resolver=ref_result.task.resolver, resolver_setting=ref_result.task.resolver_setting, # reference=reference) initial_guess=ref_result.last_func_args) self.logger.debug( f"Retry task: sample name={task.sample.name}, distribution_type={task.distribution_type.name}, n_components={task.n_components}" ) self.retry_tasks[task.uuid] = index self.async_worker.execute_task(task) def degrade_results(self): degrade_results = [] # type: list[SSUResult] degrade_indexes = [] # type: list[int] for i, result in enumerate(self.__fitting_results): for component in result.components: if component.fraction < 1e-3: degrade_results.append(result) degrade_indexes.append(i) break self.logger.debug( f"Results should be degrade (have a redundant component): {[result.sample.name for result in degrade_results]}" ) if len(degrade_results) == 0: self.show_info( self.tr("No fitting result was evaluated as an outlier.")) return self.show_info( self. tr("The results below should be degrade (have a redundant component:\n {0}" ).format(", ".join( [result.sample.name for result in degrade_results]))) self.retry_progress_msg.setText( self.tr("Tasks to be retried: {0}").format(len(degrade_results))) self.retry_timer.start(1) for index, result in zip(degrade_indexes, degrade_results): reference = [] n_redundant = 0 for component in result.components: if component.fraction < 1e-3: n_redundant += 1 else: reference.append( dict(mean=component.logarithmic_moments["mean"], std=component.logarithmic_moments["std"], skewness=component.logarithmic_moments["skewness"] )) task = SSUTask( result.sample, result.distribution_type, result.n_components - n_redundant if result.n_components > n_redundant else 1, resolver=result.task.resolver, resolver_setting=result.task.resolver_setting, reference=reference) self.logger.debug( f"Retry task: sample name={task.sample.name}, distribution_type={task.distribution_type.name}, n_components={task.n_components}" ) self.retry_tasks[task.uuid] = index self.async_worker.execute_task(task) def ask_deal_outliers(self, outlier_results: typing.List[SSUResult], outlier_indexes: typing.List[int]): assert len(outlier_indexes) == len(outlier_results) if len(outlier_results) == 0: self.show_info( self.tr("No fitting result was evaluated as an outlier.")) else: if len(outlier_results) > 100: self.outlier_msg.setText( self. tr("The fitting results have the component that its fraction is near zero:\n {0}...(total {1} outliers)\nHow to deal with them?" ).format( ", ".join([ result.sample.name for result in outlier_results[:100] ]), len(outlier_results))) else: self.outlier_msg.setText( self. tr("The fitting results have the component that its fraction is near zero:\n {0}\nHow to deal with them?" ).format(", ".join([ result.sample.name for result in outlier_results ]))) res = self.outlier_msg.exec_() if res == QMessageBox.Discard: self.remove_results(outlier_indexes) elif res == QMessageBox.Retry: self.retry_results(outlier_indexes, outlier_results) else: pass def check_nan_and_inf(self): if self.n_results == 0: self.show_warning(self.tr("There is not any result in the list.")) return outlier_results = [] outlier_indexes = [] for i, result in enumerate(self.__fitting_results): if not result.is_valid: outlier_results.append(result) outlier_indexes.append(i) self.logger.debug( f"Outlier results with the nan or inf value(s): {[result.sample.name for result in outlier_results]}" ) self.ask_deal_outliers(outlier_results, outlier_indexes) def check_final_distances(self): if self.n_results == 0: self.show_warning(self.tr("There is not any result in the list.")) return elif self.n_results < 10: self.show_warning(self.tr("The results in list are too less.")) return distances = [] for result in self.__fitting_results: distances.append(result.get_distance(self.distance_name)) distances = np.array(distances) self.boxplot_chart.show_dataset([distances], xlabels=[self.distance_name], ylabel=self.tr("Distance")) self.boxplot_chart.show() # calculate the 1/4, 1/2, and 3/4 postion value to judge which result is invalid # 1. the mean squared errors are much higher in the results which are lack of components # 2. with the component number getting higher, the mean squared error will get lower and finally reach the minimum median = np.median(distances) upper_group = distances[np.greater(distances, median)] lower_group = distances[np.less(distances, median)] value_1_4 = np.median(lower_group) value_3_4 = np.median(upper_group) distance_QR = value_3_4 - value_1_4 outlier_results = [] outlier_indexes = [] for i, (result, distance) in enumerate(zip(self.__fitting_results, distances)): if distance > value_3_4 + distance_QR * 1.5: # which error too small is not outlier # if distance > value_3_4 + distance_QR * 1.5 or distance < value_1_4 - distance_QR * 1.5: outlier_results.append(result) outlier_indexes.append(i) self.logger.debug( f"Outlier results with too greater distances: {[result.sample.name for result in outlier_results]}" ) self.ask_deal_outliers(outlier_results, outlier_indexes) def check_component_moments(self, key: str): if self.n_results == 0: self.show_warning(self.tr("There is not any result in the list.")) return elif self.n_results < 10: self.show_warning(self.tr("The results in list are too less.")) return max_n_components = 0 for result in self.__fitting_results: if result.n_components > max_n_components: max_n_components = result.n_components moments = [] for i in range(max_n_components): moments.append([]) for result in self.__fitting_results: for i, component in enumerate(result.components): if np.isnan(component.logarithmic_moments[key]) or np.isinf( component.logarithmic_moments[key]): pass else: moments[i].append(component.logarithmic_moments[key]) # key_trans = {"mean": self.tr("Mean"), "std": self.tr("STD"), "skewness": self.tr("Skewness"), "kurtosis": self.tr("Kurtosis")} key_label_trans = { "mean": self.tr("Mean [φ]"), "std": self.tr("STD [φ]"), "skewness": self.tr("Skewness"), "kurtosis": self.tr("Kurtosis") } self.boxplot_chart.show_dataset( moments, xlabels=[f"C{i+1}" for i in range(max_n_components)], ylabel=key_label_trans[key]) self.boxplot_chart.show() outlier_dict = {} for i in range(max_n_components): stacked_moments = np.array(moments[i]) # calculate the 1/4, 1/2, and 3/4 postion value to judge which result is invalid # 1. the mean squared errors are much higher in the results which are lack of components # 2. with the component number getting higher, the mean squared error will get lower and finally reach the minimum median = np.median(stacked_moments) upper_group = stacked_moments[np.greater(stacked_moments, median)] lower_group = stacked_moments[np.less(stacked_moments, median)] value_1_4 = np.median(lower_group) value_3_4 = np.median(upper_group) distance_QR = value_3_4 - value_1_4 for j, result in enumerate(self.__fitting_results): if result.n_components > i: distance = result.components[i].logarithmic_moments[key] if distance > value_3_4 + distance_QR * 1.5 or distance < value_1_4 - distance_QR * 1.5: outlier_dict[j] = result outlier_results = [] outlier_indexes = [] for index, result in sorted(outlier_dict.items(), key=lambda x: x[0]): outlier_indexes.append(index) outlier_results.append(result) self.logger.debug( f"Outlier results with abnormal {key} values of their components: {[result.sample.name for result in outlier_results]}" ) self.ask_deal_outliers(outlier_results, outlier_indexes) def check_component_fractions(self): outlier_results = [] outlier_indexes = [] for i, result in enumerate(self.__fitting_results): for component in result.components: if component.fraction < 1e-3: outlier_results.append(result) outlier_indexes.append(i) break self.logger.debug( f"Outlier results with the component that its fraction is near zero: {[result.sample.name for result in outlier_results]}" ) self.ask_deal_outliers(outlier_results, outlier_indexes) def try_align_components(self): if self.n_results == 0: self.show_warning(self.tr("There is not any result in the list.")) return elif self.n_results < 10: self.show_warning(self.tr("The results in list are too less.")) return import matplotlib.pyplot as plt n_components_list = [ result.n_components for result in self.__fitting_results ] count_dict = Counter(n_components_list) max_n_components = max(count_dict.keys()) self.logger.debug( f"N_components: {count_dict}, Max N_components: {max_n_components}" ) n_components_desc = "\n".join([ self.tr("{0} Component(s): {1}").format(n_components, count) for n_components, count in count_dict.items() ]) self.show_info( self.tr("N_components distribution of Results:\n{0}").format( n_components_desc)) x = self.__fitting_results[0].classes_μm stacked_components = [] for result in self.__fitting_results: for component in result.components: stacked_components.append(component.distribution) stacked_components = np.array(stacked_components) cluser = KMeans(n_clusters=max_n_components) flags = cluser.fit_predict(stacked_components) figure = plt.figure(figsize=(6, 4)) cmap = plt.get_cmap("tab10") axes = figure.add_subplot(1, 1, 1) for flag, distribution in zip(flags, stacked_components): plt.plot(x, distribution, c=cmap(flag), zorder=flag) axes.set_xscale("log") axes.set_xlabel(self.tr("Grain-size [μm]")) axes.set_ylabel(self.tr("Frequency")) figure.tight_layout() figure.show() outlier_results = [] outlier_indexes = [] flag_index = 0 for i, result in enumerate(self.__fitting_results): result_flags = set() for component in result.components: if flags[flag_index] in result_flags: outlier_results.append(result) outlier_indexes.append(i) break else: result_flags.add(flags[flag_index]) flag_index += 1 self.logger.debug( f"Outlier results that have two components in the same cluster: {[result.sample.name for result in outlier_results]}" ) self.ask_deal_outliers(outlier_results, outlier_indexes) def analyse_typical_components(self): if self.n_results == 0: self.show_warning(self.tr("There is not any result in the list.")) return elif self.n_results < 10: self.show_warning(self.tr("The results in list are too less.")) return self.typical_chart.show_typical(self.__fitting_results) self.typical_chart.show()
class ReferenceResultViewer(QDialog): PAGE_ROWS = 20 logger = logging.getLogger("root.QGrain.ui.ReferenceResultViewer") def __init__(self, parent=None): super().__init__(parent=parent, f=Qt.Window) self.setWindowTitle(self.tr("SSU Reference Result Viewer")) self.__fitting_results = [] self.__reference_map = {} self.retry_tasks = {} self.init_ui() self.distance_chart = DistanceCurveChart(parent=self, toolbar=True) self.mixed_distribution_chart = MixedDistributionChart( parent=self, toolbar=True, use_animation=True) self.file_dialog = QFileDialog(parent=self) self.update_page_list() self.update_page(self.page_index) self.remove_warning_msg = QMessageBox(self) self.remove_warning_msg.setStandardButtons(QMessageBox.No | QMessageBox.Yes) self.remove_warning_msg.setDefaultButton(QMessageBox.No) self.remove_warning_msg.setWindowTitle(self.tr("Warning")) self.remove_warning_msg.setText( self.tr("Are you sure to remove all SSU results?")) self.normal_msg = QMessageBox(self) def init_ui(self): self.data_table = QTableWidget(100, 100) self.data_table.setEditTriggers(QAbstractItemView.NoEditTriggers) self.data_table.setSelectionBehavior(QAbstractItemView.SelectRows) self.data_table.setAlternatingRowColors(True) self.data_table.setContextMenuPolicy(Qt.CustomContextMenu) self.main_layout = QGridLayout(self) self.main_layout.addWidget(self.data_table, 0, 0, 1, 3) self.previous_button = QPushButton( qta.icon("mdi.skip-previous-circle"), self.tr("Previous")) self.previous_button.setToolTip( self.tr("Click to back to the previous page.")) self.previous_button.clicked.connect(self.on_previous_button_clicked) self.current_page_combo_box = QComboBox() self.current_page_combo_box.addItem(self.tr("Page {0}").format(1)) self.current_page_combo_box.currentIndexChanged.connect( self.update_page) self.next_button = QPushButton(qta.icon("mdi.skip-next-circle"), self.tr("Next")) self.next_button.setToolTip(self.tr("Click to jump to the next page.")) self.next_button.clicked.connect(self.on_next_button_clicked) self.main_layout.addWidget(self.previous_button, 1, 0) self.main_layout.addWidget(self.current_page_combo_box, 1, 1) self.main_layout.addWidget(self.next_button, 1, 2) self.distance_label = QLabel(self.tr("Distance")) self.distance_label.setToolTip( self. tr("It's the function to calculate the difference (on the contrary, similarity) between two samples." )) self.distance_combo_box = QComboBox() self.distance_combo_box.addItems(built_in_distances) self.distance_combo_box.setCurrentText("log10MSE") self.distance_combo_box.currentTextChanged.connect( lambda: self.update_page(self.page_index)) self.main_layout.addWidget(self.distance_label, 2, 0) self.main_layout.addWidget(self.distance_combo_box, 2, 1, 1, 2) self.menu = QMenu(self.data_table) self.mark_action = self.menu.addAction( qta.icon("mdi.marker-check"), self.tr("Mark Selection(s) as Reference")) self.mark_action.triggered.connect(self.mark_selections) self.unmark_action = self.menu.addAction( qta.icon("mdi.do-not-disturb"), self.tr("Unmark Selection(s)")) self.unmark_action.triggered.connect(self.unmark_selections) self.remove_action = self.menu.addAction( qta.icon("fa.remove"), self.tr("Remove Selection(s)")) self.remove_action.triggered.connect(self.remove_selections) self.remove_all_action = self.menu.addAction(qta.icon("fa.remove"), self.tr("Remove All")) self.remove_all_action.triggered.connect(self.remove_all_results) self.plot_loss_chart_action = self.menu.addAction( qta.icon("mdi.chart-timeline-variant"), self.tr("Plot Loss Chart")) self.plot_loss_chart_action.triggered.connect(self.show_distance) self.plot_distribution_chart_action = self.menu.addAction( qta.icon("fa5s.chart-area"), self.tr("Plot Distribution Chart")) self.plot_distribution_chart_action.triggered.connect( self.show_distribution) self.plot_distribution_animation_action = self.menu.addAction( qta.icon("fa5s.chart-area"), self.tr("Plot Distribution Chart (Animation)")) self.plot_distribution_animation_action.triggered.connect( self.show_history_distribution) self.load_dump_action = self.menu.addAction( qta.icon("fa.database"), self.tr("Load Binary Dump")) self.load_dump_action.triggered.connect( lambda: self.load_dump(mark_ref=True)) self.save_dump_action = self.menu.addAction( qta.icon("fa.save"), self.tr("Save Binary Dump")) self.save_dump_action.triggered.connect(self.save_dump) self.data_table.customContextMenuRequested.connect(self.show_menu) def show_menu(self, pos): self.menu.popup(QCursor.pos()) def show_message(self, title: str, message: str): self.normal_msg.setWindowTitle(title) self.normal_msg.setText(message) self.normal_msg.exec_() def show_info(self, message: str): self.show_message(self.tr("Info"), message) def show_warning(self, message: str): self.show_message(self.tr("Warning"), message) def show_error(self, message: str): self.show_message(self.tr("Error"), message) @property def distance_name(self) -> str: return self.distance_combo_box.currentText() @property def distance_func(self) -> typing.Callable: return get_distance_func_by_name(self.distance_combo_box.currentText()) @property def page_index(self) -> int: return self.current_page_combo_box.currentIndex() @property def n_pages(self) -> int: return self.current_page_combo_box.count() @property def n_results(self) -> int: return len(self.__fitting_results) @property def selections(self): start = self.page_index * self.PAGE_ROWS temp = set() for item in self.data_table.selectedRanges(): for i in range(item.topRow(), min(self.PAGE_ROWS + 1, item.bottomRow() + 1)): temp.add(i + start) indexes = list(temp) indexes.sort() return indexes def update_page_list(self): last_page_index = self.page_index if self.n_results == 0: n_pages = 1 else: n_pages, left = divmod(self.n_results, self.PAGE_ROWS) if left != 0: n_pages += 1 self.current_page_combo_box.blockSignals(True) self.current_page_combo_box.clear() self.current_page_combo_box.addItems( [self.tr("Page {0}").format(i + 1) for i in range(n_pages)]) if last_page_index >= n_pages: self.current_page_combo_box.setCurrentIndex(n_pages - 1) else: self.current_page_combo_box.setCurrentIndex(last_page_index) self.current_page_combo_box.blockSignals(False) def update_page(self, page_index: int): def write(row: int, col: int, value: str): if isinstance(value, str): pass elif isinstance(value, int): value = str(value) elif isinstance(value, float): value = f"{value: 0.4f}" else: value = value.__str__() item = QTableWidgetItem(value) item.setTextAlignment(Qt.AlignCenter) self.data_table.setItem(row, col, item) # necessary to clear self.data_table.clear() if page_index == self.n_pages - 1: start = page_index * self.PAGE_ROWS end = self.n_results else: start, end = page_index * self.PAGE_ROWS, (page_index + 1) * self.PAGE_ROWS self.data_table.setRowCount(end - start) self.data_table.setColumnCount(8) self.data_table.setHorizontalHeaderLabels([ self.tr("Resolver"), self.tr("Distribution Type"), self.tr("N_components"), self.tr("N_iterations"), self.tr("Spent Time [s]"), self.tr("Final Distance"), self.tr("Has Reference"), self.tr("Is Reference") ]) sample_names = [ result.sample.name for result in self.__fitting_results[start:end] ] self.data_table.setVerticalHeaderLabels(sample_names) for row, result in enumerate(self.__fitting_results[start:end]): write(row, 0, result.task.resolver) write(row, 1, self.get_distribution_name(result.task.distribution_type)) write(row, 2, result.task.n_components) write(row, 3, result.n_iterations) write(row, 4, result.time_spent) write( row, 5, self.distance_func(result.sample.distribution, result.distribution)) has_ref = result.task.initial_guess is not None or result.task.reference is not None write(row, 6, self.tr("Yes") if has_ref else self.tr("No")) is_ref = result.uuid in self.__reference_map write(row, 7, self.tr("Yes") if is_ref else self.tr("No")) self.data_table.resizeColumnsToContents() def on_previous_button_clicked(self): if self.page_index > 0: self.current_page_combo_box.setCurrentIndex(self.page_index - 1) def on_next_button_clicked(self): if self.page_index < self.n_pages - 1: self.current_page_combo_box.setCurrentIndex(self.page_index + 1) def get_distribution_name(self, distribution_type: DistributionType): if distribution_type == DistributionType.Normal: return self.tr("Normal") elif distribution_type == DistributionType.Weibull: return self.tr("Weibull") elif distribution_type == DistributionType.SkewNormal: return self.tr("Skew Normal") else: raise NotImplementedError(distribution_type) def add_result(self, result: SSUResult): if self.n_results == 0 or \ (self.page_index == self.n_pages - 1 and \ divmod(self.n_results, self.PAGE_ROWS)[-1] != 0): need_update = True else: need_update = False self.__fitting_results.append(result) self.update_page_list() if need_update: self.update_page(self.page_index) def add_results(self, results: typing.List[SSUResult]): if self.n_results == 0 or \ (self.page_index == self.n_pages - 1 and \ divmod(self.n_results, self.PAGE_ROWS)[-1] != 0): need_update = True else: need_update = False self.__fitting_results.extend(results) self.update_page_list() if need_update: self.update_page(self.page_index) def mark_results(self, results: typing.List[SSUResult]): for result in results: self.__reference_map[result.uuid] = result self.update_page(self.page_index) def unmark_results(self, results: typing.List[SSUResult]): for result in results: if result.uuid in self.__reference_map: self.__reference_map.pop(result.uuid) self.update_page(self.page_index) def add_references(self, results: typing.List[SSUResult]): self.add_results(results) self.mark_results(results) def mark_selections(self): results = [ self.__fitting_results[selection] for selection in self.selections ] self.mark_results(results) def unmark_selections(self): results = [ self.__fitting_results[selection] for selection in self.selections ] self.unmark_results(results) def remove_results(self, indexes): results = [] for i in reversed(indexes): res = self.__fitting_results.pop(i) results.append(res) self.unmark_results(results) self.update_page_list() self.update_page(self.page_index) def remove_selections(self): indexes = self.selections self.remove_results(indexes) def remove_all_results(self): res = self.remove_warning_msg.exec_() if res == QMessageBox.Yes: self.__fitting_results.clear() self.update_page_list() self.update_page(0) def show_distance(self): results = [self.__fitting_results[i] for i in self.selections] if results is None or len(results) == 0: return result = results[0] self.distance_chart.show_distance_series(result.get_distance_series( self.distance_name), title=result.sample.name) self.distance_chart.show() def show_distribution(self): results = [self.__fitting_results[i] for i in self.selections] if results is None or len(results) == 0: return result = results[0] self.mixed_distribution_chart.show_model(result.view_model) self.mixed_distribution_chart.show() def show_history_distribution(self): results = [self.__fitting_results[i] for i in self.selections] if results is None or len(results) == 0: return result = results[0] self.mixed_distribution_chart.show_result(result) self.mixed_distribution_chart.show() def load_dump(self, mark_ref=False): filename, _ = self.file_dialog.getOpenFileName( self, self.tr("Select a binary dump file of SSU results"), None, self.tr("Binary dump (*.dump)")) if filename is None or filename == "": return with open(filename, "rb") as f: results = pickle.load(f) valid = True if isinstance(results, list): for result in results: if not isinstance(result, SSUResult): valid = False break else: valid = False if valid: self.add_results(results) if mark_ref: self.mark_results(results) else: self.show_error(self.tr("The binary dump file is invalid.")) def save_dump(self): if self.n_results == 0: self.show_warning(self.tr("There is not any result in the list.")) return filename, _ = self.file_dialog.getSaveFileName( self, self.tr("Save the SSU results to binary dump file"), None, self.tr("Binary dump (*.dump)")) if filename is None or filename == "": return with open(filename, "wb") as f: pickle.dump(self.__fitting_results, f) def find_similar(self, target: GrainSizeSample, ref_results: typing.List[SSUResult]): assert len(ref_results) != 0 # sample_moments = logarithmic(sample.classes_φ, sample.distribution) # keys_to_check = ["mean", "std", "skewness", "kurtosis"] start_time = time.time() from scipy.interpolate import interp1d min_distance = 1e100 min_result = None trans_func = interp1d(target.classes_φ, target.distribution, bounds_error=False, fill_value=0.0) for result in ref_results: # TODO: To scale the classes of result to that of sample # use moments to calculate? MOMENTS MAY NOT BE PERFECT, MAY IGNORE THE MINOR DIFFERENCE # result_moments = logarithmic(result.classes_φ, result.distribution) # distance = sum([(sample_moments[key]-result_moments[key])**2 for key in keys_to_check]) trans_dist = trans_func(result.classes_φ) distance = self.distance_func(result.distribution, trans_dist) if distance < min_distance: min_distance = distance min_result = result self.logger.debug( f"It took {time.time()-start_time:0.4f} s to query the reference from {len(ref_results)} results." ) return min_result def query_reference(self, sample: GrainSizeSample): if len(self.__reference_map) == 0: self.logger.debug("No result is marked as reference.") return None return self.find_similar(sample, self.__reference_map.values())
class QGroundObjectBuyMenu(QDialog): layout_changed_signal = Signal(QTgoLayout) def __init__( self, parent: QWidget, ground_object: TheaterGroundObject, game: Game, current_group_value: int, ) -> None: super().__init__(parent) self.setMinimumWidth(350) self.setWindowTitle("Buy ground object @ " + ground_object.obj_name) self.setWindowIcon(EVENT_ICONS["capture"]) self.mainLayout = QGridLayout() self.setLayout(self.mainLayout) self.force_group_selector = QComboBox() self.force_group_selector.setMinimumWidth(250) self.layout_selector = QComboBox() self.layout_selector.setMinimumWidth(250) # Get the layouts and fill the combobox tasks = [] if isinstance(ground_object, SamGroundObject): role = GroupRole.AIR_DEFENSE elif isinstance(ground_object, VehicleGroupGroundObject): role = GroupRole.GROUND_FORCE elif isinstance(ground_object, EwrGroundObject): role = GroupRole.AIR_DEFENSE tasks.append(GroupTask.EARLY_WARNING_RADAR) else: raise NotImplementedError( f"Unhandled TGO type {ground_object.__class__}") if not tasks: tasks = role.tasks for group in game.blue.armed_forces.groups_for_tasks(tasks): self.force_group_selector.addItem(group.name, userData=group) self.force_group_selector.setEnabled( self.force_group_selector.count() > 1) self.force_group_selector.adjustSize() force_group = self.force_group_selector.itemData( self.force_group_selector.currentIndex()) for layout in force_group.layouts: self.layout_selector.addItem(layout.name, userData=layout) self.layout_selector.adjustSize() self.layout_selector.setEnabled(len(force_group.layouts) > 1) selected_template = self.layout_selector.itemData( self.layout_selector.currentIndex()) self.theater_layout = QTgoLayout(selected_template, force_group) self.layout_selector.currentIndexChanged.connect(self.layout_changed) self.force_group_selector.currentIndexChanged.connect( self.force_group_changed) template_selector_layout = QGridLayout() template_selector_layout.addWidget(QLabel("Armed Forces Group:"), 0, 0, Qt.AlignLeft) template_selector_layout.addWidget(self.force_group_selector, 0, 1, alignment=Qt.AlignRight) template_selector_layout.addWidget(QLabel("Layout:"), 1, 0, Qt.AlignLeft) template_selector_layout.addWidget(self.layout_selector, 1, 1, alignment=Qt.AlignRight) self.mainLayout.addLayout(template_selector_layout, 0, 0) self.template_layout = QGroundObjectTemplateLayout( game, ground_object, self.theater_layout, self.layout_changed_signal, current_group_value, ) self.template_layout.close_dialog_signal.connect(self.close_dialog) self.mainLayout.addWidget(self.template_layout, 1, 0) self.setLayout(self.mainLayout) def force_group_changed(self) -> None: # Prevent ComboBox from firing change Events self.layout_selector.blockSignals(True) unit_group = self.force_group_selector.itemData( self.force_group_selector.currentIndex()) self.layout_selector.clear() for layout in unit_group.layouts: self.layout_selector.addItem(layout.name, userData=layout) self.layout_selector.adjustSize() # Enable if more than one template is available self.layout_selector.setEnabled(len(unit_group.layouts) > 1) # Enable Combobox Signals again self.layout_selector.blockSignals(False) self.layout_changed() def layout_changed(self) -> None: self.layout() self.theater_layout.layout = self.layout_selector.itemData( self.layout_selector.currentIndex()) self.theater_layout.force_group = self.force_group_selector.itemData( self.force_group_selector.currentIndex()) self.layout_changed_signal.emit(self.theater_layout) def close_dialog(self) -> None: self.accept()
class FilePathWidget(QWidget): """Widget allowing file path selection with a persistent cache. Users should connect to the path_changed signal. """ any_file = 0 existing_file = 1 directory = 2 path_changed = Signal(str) def __init__(self, label=None, file_mode=any_file, file_filter=None, name=None, parent=None): """Constructor :param label: Optional label text. :param file_mode: Sets the file dialog mode. One of FilePathWidget.[any_file|existing_file|directory]. :param file_filter: File filter text example 'Python Files (*.py)'. :param name: Unique name used to query persistent data. :param parent: Parent QWidget. """ super(FilePathWidget, self).__init__(parent) self.file_mode = file_mode if file_filter is None: file_filter = "Any File (*)" self.file_filter = file_filter self.cache = StringCache("cmt.filepathwidget.{}".format(name), parent=self) self._layout = QHBoxLayout(self) self._layout.setContentsMargins(0, 0, 0, 0) self.setLayout(self._layout) if label: label = QLabel(label, self) label.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) self._layout.addWidget(label) self._combo_box = QComboBox(self) self._combo_box.setEditable(True) self._combo_box.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Fixed) self._combo_box.setInsertPolicy(QComboBox.InsertAtTop) self._combo_box.setMinimumWidth(50) self._combo_box.setModel(self.cache) self._combo_box.editTextChanged.connect(self.edit_changed) self._layout.addWidget(self._combo_box) button = QPushButton("Browse", self) button.released.connect(self.show_dialog) button.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) self._layout.addWidget(button) @property def path(self): return self._combo_box.currentText() @path.setter def path(self, value): self._combo_box.setEditText(value) def edit_changed(self, text): """Slot called whenever the text changes in the combobox. :param text: New text. """ if not text: return text = text.replace("\\", "/") if (os.path.isfile(text) and self.file_mode != FilePathWidget.directory ) or (os.path.isdir(text) and self.file_mode == FilePathWidget.directory): self.path_changed.emit(text) self._combo_box.blockSignals(True) self._push(text) self._combo_box.blockSignals(False) def show_dialog(self): """Show the file browser dialog.""" dialog = QFileDialog(self) dialog.setNameFilter(self.file_filter) file_mode = [ QFileDialog.AnyFile, QFileDialog.ExistingFile, QFileDialog.Directory, ][self.file_mode] dialog.setFileMode(file_mode) dialog.setModal(True) if self.cache: dialog.setHistory(self.cache.stringList()) for value in self.cache.stringList(): if os.path.exists(value): if os.path.isfile(value): directory = os.path.dirname(value) dialog.selectFile(value) else: directory = value dialog.setDirectory(directory) break if dialog.exec_() == QDialog.Accepted: path = dialog.selectedFiles() if path: self._push(path[0]) def _push(self, path): """Push a new path onto the cache. :param path: Path value. """ self.cache.push(path) self._combo_box.setCurrentIndex(0)