def createEditor(self, parent: QWidget, option: QStyleOptionViewItem, index: QModelIndex) -> QWidget: editor = QDoubleSpinBox(parent) editor.setFrame(False) editor.setRange(self.__min, self.__max) editor.setDecimals(self.__decimals) return editor
class GaussianFuzzierSetting(QFrame): def __init__(self): super().__init__() layout = QHBoxLayout() layout.setContentsMargins(0, 0, 0, 0) self.setLayout(layout) self.mean = QDoubleSpinBox() self.mean.setRange(-100, 100) self.mean.setStatusTip("The mean (mu) value for Gaussian function.") self.sd = QDoubleSpinBox() self.sd.setDecimals(3) self.sd.setValue(5) self.sd.setMinimum(0.1) self.sd.setStatusTip("The standard deviation (sigma) value for " "Gaussian function.") self.ascending = QCheckBox() self.ascending.setIcon(QIcon(':/icons/ascending_icon.png')) self.ascending.setStatusTip("Make the fuzzier strictly ascending.") self.descending = QCheckBox() self.descending.setIcon(QIcon(':/icons/descending_icon.png')) self.descending.setStatusTip("Make the fuzzier strictly descending.") layout.addWidget(QLabel("Mean")) layout.addWidget(self.mean, 1) layout.addWidget(QLabel("Standard Deviation")) layout.addWidget(self.sd, 1) layout.addWidget(self.ascending) layout.addWidget(self.descending) def get_values(self): return (self.mean.value(), self.sd.value(), self.ascending.isChecked(), self.descending.isChecked())
class Slider(): def update_offset(self, pos): offset = round(pos, 2) return offset def __init__(self, tool, text, tooltip, precision, minimal, maximal, interval): self.tool = tool self.scale = precision self.name_label = QLabel(text) self.name_label.setAlignment(Qt.AlignLeft) if not tooltip == None: self.name_label.setToolTip(tooltip) self.offset_edit = QDoubleSpinBox() self.offset_edit.setRange(minimal, maximal) self.offset_edit.setSingleStep(precision) self.offset_edit.setDecimals(2) self.offset_edit.setAlignment(Qt.AlignRight) self.offset_edit.valueChanged.connect( lambda pos: self.slider.setSliderPosition( self.update_offset(pos) / self.scale)) self.slider = QSlider(Qt.Horizontal) self.slider.setRange(minimal / self.scale, maximal) self.slider.setTickInterval(interval / self.scale) self.slider.setTickPosition(QSlider.TicksAbove) self.slider.valueChanged.connect(lambda pos: self.offset_edit.setValue( self.update_offset(pos * self.scale))) self.update_offset(0)
def __init__(self, parent, patient): QDialog.__init__(self) self.patient = patient self.parent = parent self.save_button = QPushButton("Zapisz") self.save_button.clicked.connect(self.saveForm) self.spin_boxes = [] self.form_layout = QFormLayout() treatment_layout = QHBoxLayout() self.date_field = QDateEdit() spin_box = QDoubleSpinBox() spin_box.setRange(0.0, 1.0) spin_box.setSingleStep(0.1) self.treatment_field = spin_box treatment_layout.addWidget(self.date_field) treatment_layout.addWidget(self.treatment_field) self.form_layout.addRow(treatment_layout) buttons_layout = QHBoxLayout() buttons_layout.addWidget(self.save_button) self.layout = QVBoxLayout(self) self.layout.addLayout(self.form_layout) self.layout.addLayout(buttons_layout)
class MainWindow(QMainWindow): def __init__(self): super().__init__() form = QFormLayout() self.track_id = QSpinBox() self.track_id.setRange(0, 2147483647) self.track_id.setDisabled(True) self.name = QLineEdit() self.album = QComboBox() self.media_type = QComboBox() self.genre = QComboBox() self.composer = QLineEdit() self.milliseconds = QSpinBox() self.milliseconds.setRange(0, 2147483647) # <1> self.milliseconds.setSingleStep(1) self.bytes = QSpinBox() self.bytes.setRange(0, 2147483647) self.bytes.setSingleStep(1) self.unit_price = QDoubleSpinBox() self.unit_price.setRange(0, 999) self.unit_price.setSingleStep(0.01) self.unit_price.setPrefix("$") form.addRow(QLabel("Track ID"), self.track_id) form.addRow(QLabel("Track name"), self.name) form.addRow(QLabel("Composer"), self.composer) form.addRow(QLabel("Milliseconds"), self.milliseconds) form.addRow(QLabel("Bytes"), self.bytes) form.addRow(QLabel("Unit Price"), self.unit_price) self.model = QSqlTableModel(db=db) self.mapper = QDataWidgetMapper() # <2> self.mapper.setModel(self.model) self.mapper.addMapping(self.track_id, 0) # <3> self.mapper.addMapping(self.name, 1) self.mapper.addMapping(self.composer, 5) self.mapper.addMapping(self.milliseconds, 6) self.mapper.addMapping(self.bytes, 7) self.mapper.addMapping(self.unit_price, 8) self.model.setTable("Track") self.model.select() # <4> self.mapper.toFirst() # <5> self.setMinimumSize(QSize(400, 400)) widget = QWidget() widget.setLayout(form) self.setCentralWidget(widget)
def change_n_components(self, n_components: int): for widget in self.control_widgets: self.control_layout.removeWidget(widget) widget.hide() self.control_widgets.clear() self.input_widgets.clear() widgets = [] slider_range = (0, 1000) input_widgets = [] mean_range = (-5, 15) std_range = (0.0, 10) weight_range = (0, 10) names = [self.tr("Mean"), self.tr("STD"), self.tr("Weight")] ranges = [mean_range, std_range, weight_range] slider_values = [500, 100, 100] input_values = [0.0, 1.0, 1.0] for i in range(n_components): group = QGroupBox(f"C{i+1}") group.setMinimumWidth(200) group_layout = QGridLayout(group) inputs = [] for j, (name, range_, slider_value, input_value) in enumerate( zip(names, ranges, slider_values, input_values)): label = QLabel(name) slider = QSlider() slider.setRange(*slider_range) slider.setValue(slider_value) slider.setOrientation(Qt.Horizontal) input_ = QDoubleSpinBox() input_.setRange(*range_) input_.setDecimals(3) input_.setSingleStep(0.01) input_.setValue(input_value) slider.valueChanged.connect(self.on_value_changed) input_.valueChanged.connect(self.on_value_changed) slider.valueChanged.connect( lambda x, input_=input_, range_=range_: input_.setValue( x / 1000 * (range_[-1] - range_[0]) + range_[0])) input_.valueChanged.connect( lambda x, slider=slider, range_=range_: slider.setValue( (x - range_[0]) / (range_[-1] - range_[0]) * 1000)) group_layout.addWidget(label, j, 0) group_layout.addWidget(slider, j, 1) group_layout.addWidget(input_, j, 2) inputs.append(input_) self.control_layout.addWidget(group, i + 5, 0, 1, 4) widgets.append(group) input_widgets.append(inputs) self.control_widgets = widgets self.input_widgets = input_widgets
def init_font_size_layout(text_item): font_size_input = QDoubleSpinBox() font_size_input.setDecimals(2) font_size_input.setRange(0.5, 900.) font_size_input.setValue(text_item.font().pointSizeF()) font_size_input.valueChanged.connect(lambda: set_font_size( font_size_input.value(), text_item)) font_size_layout = QHBoxLayout() font_size_layout.addWidget(font_size_input) font_size_layout.addWidget(QLabel("pt")) return font_size_layout
def set_object(self, controller): for idx, h in enumerate(self._layouts): h.removeWidget(self._sliders[idx]) self._sliders[idx].deleteLater() h.removeWidget(self._spin_boxes[idx]) self._spin_boxes[idx].deleteLater() h.removeWidget(self._labels[idx]) self._labels[idx].deleteLater() self.parameterSliderLayout.removeItem(h) h.deleteLater() self._layouts = [] self._sliders = [] self._spin_boxes = [] self._labels = [] self._controller = controller if self._controller is not None: self.activatePlayerControls() n_frames = controller.getNumberOfFrames() self.setFrameRange(0, n_frames - 1) self.neighborLineEdit.setText(str(controller.n_neighbors)) parameter = controller.current_parameter min_pos, max_pos = controller.get_parameter_range() for idx, label in enumerate(controller.get_parameter_labels()): print("add parameter", label, min_pos[idx], max_pos[idx]) h_layout = QHBoxLayout() parameter_label = QLabel() parameter_label.setText(label) h_layout.addWidget(parameter_label) parameter_slider = QSlider() parameter_slider.setOrientation(Qt.Horizontal) parameter_slider.setRange(min_pos[idx] * 100, max_pos[idx] * 100) if parameter is not None: parameter_slider.setValue(parameter[idx] * 100) h_layout.addWidget(parameter_slider) parameter_spin_box = QDoubleSpinBox() parameter_spin_box.setRange(min_pos[idx], max_pos[idx]) if parameter is not None: parameter_spin_box.setValue(parameter[idx]) h_layout.addWidget(parameter_spin_box) self.parameterSliderLayout.addLayout(h_layout) parameter_slider.valueChanged.connect(self.parameter_changed) parameter_spin_box.valueChanged.connect( self.update_parameter_from_spin_box) self._sliders.append(parameter_slider) self._spin_boxes.append(parameter_spin_box) self._labels.append(parameter_label) self._layouts.append(h_layout) self.update_animation_controller_list()
def get_settings_window(self, parent=None): gcodesender_settings = QGroupBox("GCodeSender Settings:", parent) buildplate_label = QLabel("Buildplate Motor:", gcodesender_settings) nodes = [str(item) for item in self.__g_code_sender.get_motor_nodes_id_list()] self.buildplate_combo_box = MyQComboBox(gcodesender_settings) self.buildplate_combo_box.addItems(nodes) self.buildplate_combo_box.setCurrentIndex(self.__g_code_sender.get_buildplate_node()) self.buildplate_combo_box.combo_box_clicked.connect(self.__update_nodes_list) self.buildplate_combo_box.currentIndexChanged.connect(self.__set_buildplate_node) wiper_label = QLabel("Wiper Motor:", gcodesender_settings) self.wiper_combo_box = MyQComboBox(gcodesender_settings) self.wiper_combo_box.addItems(nodes) self.wiper_combo_box.setCurrentIndex(self.__g_code_sender.get_wiper_node()) self.wiper_combo_box.combo_box_clicked.connect(self.__update_nodes_list) self.wiper_combo_box.currentIndexChanged.connect(self.__set_wiper_node) buildplate_recoat_offset_label = QLabel("Buildplate recoating offset (mm):", gcodesender_settings) buildplate_recoat_offset_spin = QDoubleSpinBox(gcodesender_settings) buildplate_recoat_offset_spin.setRange(0, 99999) buildplate_recoat_offset_spin.setDecimals(3) buildplate_recoat_offset_spin.setSingleStep(0.001) buildplate_recoat_offset_spin.setValue(self.__g_code_sender.get_building_plate_recoating_offset()) buildplate_recoat_offset_spin.valueChanged.connect(self.__g_code_sender.set_building_plate_recoat_offset) buildplate_recoat_feedrate_label = QLabel("Buildplate recoating feedrate (mm/min):", gcodesender_settings) buildplate_recoat_feedrate_spin = QDoubleSpinBox(gcodesender_settings) buildplate_recoat_feedrate_spin.setRange(0, 99999) buildplate_recoat_feedrate_spin.setDecimals(3) buildplate_recoat_feedrate_spin.setSingleStep(0.001) buildplate_recoat_feedrate_spin.setValue(self.__g_code_sender.get_building_plate_recoating_feedrate()) buildplate_recoat_feedrate_spin.valueChanged.connect(self.__g_code_sender.set_building_plate_recoat_feedrate) wiper_recoat_offset_label = QLabel("Wiper recoating offset: (mm)", gcodesender_settings) wiper_recoat_offset_spin = QDoubleSpinBox(gcodesender_settings) wiper_recoat_offset_spin.setRange(0, 99999) wiper_recoat_offset_spin.setDecimals(3) wiper_recoat_offset_spin.setSingleStep(0.001) wiper_recoat_offset_spin.setValue(self.__g_code_sender.get_wiper_recoating_offset()) wiper_recoat_offset_spin.valueChanged.connect(self.__g_code_sender.set_wiper_recoat_offset) wiper_recoat_feedrate_label = QLabel("Wiper recoating feedrate (mm/min):", gcodesender_settings) wiper_recoat_feedrate_spin = QDoubleSpinBox(gcodesender_settings) wiper_recoat_feedrate_spin.setRange(0, 99999) wiper_recoat_feedrate_spin.setDecimals(3) wiper_recoat_feedrate_spin.setSingleStep(0.001) wiper_recoat_feedrate_spin.setValue(self.__g_code_sender.get_wiper_recoating_feedrate()) wiper_recoat_feedrate_spin.valueChanged.connect(self.__g_code_sender.set_wiper_recoat_feedrate) motor_layout = QGridLayout(gcodesender_settings) motor_layout.addWidget(buildplate_label, 0, 0) motor_layout.addWidget(self.buildplate_combo_box, 0, 1) motor_layout.addWidget(buildplate_recoat_offset_label, 1, 0) motor_layout.addWidget(buildplate_recoat_offset_spin, 1, 1) motor_layout.addWidget(buildplate_recoat_feedrate_label, 2, 0) motor_layout.addWidget(buildplate_recoat_feedrate_spin, 2, 1) motor_layout.addWidget(wiper_label, 3, 0) motor_layout.addWidget(self.wiper_combo_box, 3, 1) motor_layout.addWidget(wiper_recoat_offset_label, 4, 0) motor_layout.addWidget(wiper_recoat_offset_spin, 4, 1) motor_layout.addWidget(wiper_recoat_feedrate_label, 5, 0) motor_layout.addWidget(wiper_recoat_feedrate_spin, 5, 1) return gcodesender_settings
def __init__(self, parent, patient): QDialog.__init__(self) self.parent = parent self.patient = patient self.resize(480, 100) self.setWindowTitle("Stwórz predykcję") self.choose_method = QComboBox() self.choose_method.setItemDelegate(QStyledItemDelegate()) self.choose_method.addItem(self.base_esn_method) self.choose_method.addItem(self.deep_esn_method) self.choose_method.addItem(self.sub_reservoir_method) self.choose_method.addItem(self.abc_smc_method) spin_box = QDoubleSpinBox() spin_box.setRange(0.0, 1000.0) spin_box.setSingleStep(0.1) self.choose_prediction_length = spin_box self.predict_button = QPushButton("Stwórz predykcję") self.predict_button.clicked.connect(self.generatePrediction) self.add_future_treatment_button = QPushButton( "Dodaj podanie lekarstwa") self.add_future_treatment_button.clicked.connect( self.addFutureTreatment) self.form_layout = QFormLayout() self.form_layout.addRow("Wybierz metodę", self.choose_method) self.form_layout.addRow("Długość predykcji (dni)", self.choose_prediction_length) self.treatments = [] buttons_layout = QHBoxLayout() buttons_layout.addWidget(self.add_future_treatment_button) buttons_layout.addWidget(self.predict_button) layout = QVBoxLayout(self) layout.addLayout(self.form_layout) layout.addLayout(buttons_layout)
def __init__(self, parent, patient): QDialog.__init__(self) self.patient = patient self.parent = parent self.setWindowTitle("Dodaj pomiar") self.save_button = QPushButton("Zapisz") self.save_button.clicked.connect(self.saveForm) self.exit_button = QPushButton("Wyjdź") self.exit_button.clicked.connect(self.accept) self.spin_boxes = [] self.form_layout = QFormLayout() measurement_layout = QHBoxLayout() self.date_field = QDateEdit() spin_box = QDoubleSpinBox() spin_box.setRange(0.0, 10000.0) spin_box.setSingleStep(0.1) self.measurement_field = spin_box measurement_layout.addWidget(self.date_field) measurement_layout.addWidget(self.measurement_field) self.form_layout.addRow(measurement_layout) buttons_layout = QHBoxLayout() buttons_layout.addWidget(self.exit_button) buttons_layout.addWidget(self.save_button) self.layout = QVBoxLayout(self) self.layout.addLayout(self.form_layout) self.layout.addLayout(buttons_layout)
class Canvas (QWidget): def __init__(self): QWidget.__init__(self) self.file = "mug.webp" self.__img = cv2.imread(self.file) self.__mask = np.zeros(1) self.original = cv2.imread(self.file) self.__thirdChannelMask = np.dstack((self.__mask, self.__mask, self.__mask)) self.__nseg = 1 self.__sig = 1 self.__comp = 1 self.nSlider = QSlider(orientation=Qt.Horizontal) self.sigSlider = QSlider(orientation=Qt.Horizontal) self.thicSlider = QSlider(orientation=Qt.Horizontal) self.resize_spinbox = QSpinBox(self) self.resize_spinbox.setRange(1, 100) self.resize_spinbox.setValue(100) self.resize_spinbox.setSuffix(" %") self.double_spin_width = QDoubleSpinBox(self) self.double_spin_width.setSuffix(" px") self.double_spin_width.setValue(0) self.double_spin_width.setRange(1, 2000) self.double_spin_height = QDoubleSpinBox(self) self.double_spin_height.setSuffix(" px") self.double_spin_height.setValue(0) self.double_spin_height.setRange(1, 2000) self.zeroModeCheck = QCheckBox("Usar SLIC0") self.__highlightcolor = QColor(255, 255, 255) self.__transparency = 0.5 self.__AllColors = [self.__highlightcolor.toTuple()[:3]] nLabel = QLabel("Numero de segmentos:") sigLabel = QLabel("Sigma:") thicLabel = QLabel("Compactação:") resizeLabel = QLabel("Fator de resize da image:") makssizeLabel = QLabel("Dimensão da mascara de saída:") self.__label = QLabel() nLabel.setToolTip("O número aproximado de labels da imagem segmentada") sigLabel.setToolTip("A largura da Gaussiana") thicLabel.setToolTip("Equilibra a proximidade das cores e a proximidade do espaço, maiores valores tornam os Superpixels mais quadrados") self.nSlider.setMinimum(1) self.nSlider.setMaximum(100) self.sigSlider.setMinimum(1) self.sigSlider.setMaximum(100) self.thicSlider.setMinimum(1) self.thicSlider.setMaximum(100) glayout1 = QGridLayout() glayout1.addWidget(nLabel, 0, 0) glayout1.addWidget(self.nSlider, 0, 1) glayout1.addWidget(sigLabel, 1, 0) glayout1.addWidget(self.sigSlider, 1, 1) glayout1.addWidget(thicLabel, 2, 0) glayout1.addWidget(self.thicSlider, 2, 1) glayout2 = QGridLayout() glayout2.addWidget(resizeLabel, 0, 0) glayout2.addWidget(self.resize_spinbox, 0, 1) glayout2.addWidget(self.zeroModeCheck, 0, 2) glayout2.addWidget(makssizeLabel, 1, 0) glayout2.addWidget(self.double_spin_width, 1, 1) glayout2.addWidget(self.double_spin_height, 1, 2) glayout2.setColumnStretch(3, 1) self.__label.setAlignment(Qt.AlignLeft | Qt.AlignTop) mainlayout = QVBoxLayout() mainlayout.addLayout(glayout1) mainlayout.addLayout(glayout2) mainlayout.addStretch(1) mainlayout.addWidget(self.__label) mainlayout.addStretch(1) mainlayout.setAlignment(Qt.AlignCenter) self.setLayout(mainlayout) self.nSlider.sliderReleased.connect(self.onNsegChange) self.sigSlider.sliderReleased.connect(self.onSigChange) self.thicSlider.sliderReleased.connect(self.onCompChange) self.__label.mousePressEvent = self.Highlight self.resize_spinbox.valueChanged.connect(self.Resize) self.open_image(self.__img) def getBackground(self): mask = self.__thirdChannelMask.copy() mask_r = mask[:, :, 2] mask_g = mask[:, :, 1] mask_b = mask[:, :, 0] offImage = list() for color in self.__AllColors: b_off = mask_b != color[2] g_off = mask_g != color[1] r_off = mask_r != color[0] aux = np.logical_and(b_off, g_off) offImage.append(np.logical_and(aux, r_off)) final = offImage[0] for cut in offImage: final = np.logical_or(final, cut) return final def changeImage(self): self.__mask = slic(self.__img, n_segments=self.__nseg, compactness=self.__comp, sigma=self.__sig, convert2lab=True, slic_zero=self.zeroModeCheck.isChecked()) mask = self.__mask.copy() mask = np.dstack((mask, mask, mask)) mask = img_as_ubyte(mask) self.__thirdChannelMask = mask img = cv2.addWeighted(self.__img, 1, mask, 0.5, 0) marc_img = mark_boundaries(img, self.__mask) self.open_image(marc_img) def load_image(self): self.__img = cv2.imread(self.file) self.original = self.__img self.double_spin_width.setValue(self.__img.shape[1]) self.double_spin_height.setValue(self.__img.shape[0]) val = self.resize_spinbox.value() newDim = int(self.__img.shape[1]*val/100), int(self.__img.shape[0]*val/100) self.__img = cv2.resize(self.__img, newDim) self.open_image(self.__img) def open_image(self, img): if img.shape[2] == 4: qformat = QImage.Format_RGBA8888 else: qformat = QImage.Format_RGB888 copy = img_as_ubyte(img) qimg = QImage(copy.data, copy.shape[1], copy.shape[0], copy.strides[0], qformat).rgbSwapped() pixmap = QPixmap.fromImage(qimg) self.__label.setPixmap(pixmap) self.__label.adjustSize() @Slot() def onNsegChange(self): self.__nseg = self.nSlider.value() self.changeImage() @Slot() def onSigChange(self): self.__sig = self.sigSlider.value() self.changeImage() @Slot() def onCompChange(self): self.__comp = self.thicSlider.value() self.changeImage() @Slot() def onFileOpen(self): self.thicSlider.setValue(1) self.nSlider.setValue(1) self.sigSlider.setValue(1) diag = QFileDialog() file = diag.getOpenFileName()[0] if file != "": self.file = file self.load_image() @Slot() def onSaveFile(self): diag = QFileDialog() file = diag.getSaveFileName()[0] if self.file != "": self.__label.pixmap().save(file) @Slot() def onSaveMask(self): diag = QFileDialog() file = diag.getSaveFileName()[0] final_img = cv2.resize(self.__mask, (self.double_spin_width.value(), self.double_spin_height.value())) if file != "": cv2.imwrite(file, final_img) @Slot() def Highlight(self, e): if e.x() < 0 or e.x() > self.__img.shape[1] or e.y() < 0 or e.y() > self.__img.shape[0]: return self.__mask = flood_fill(self.__mask, (e.y(), e.x()), 255) self.__thirdChannelMask[:, :, 2] = flood_fill(self.__thirdChannelMask[:, :, 2], (e.y(), e.x()), self.__highlightcolor.red()) self.__thirdChannelMask[:, :, 1] = flood_fill(self.__thirdChannelMask[:, :, 1], (e.y(), e.x()), self.__highlightcolor.green()) self.__thirdChannelMask[:, :, 0] = flood_fill(self.__thirdChannelMask[:, :, 0], (e.y(), e.x()), self.__highlightcolor.blue()) img = cv2.addWeighted(self.__img, 1, self.__thirdChannelMask, self.__transparency, 0) marc_img = mark_boundaries(img, self.__mask) self.open_image(marc_img) @Slot() def exportBinary(self): diag = QFileDialog() file = diag.getSaveFileName()[0] mask = self.__thirdChannelMask.copy() final = self.getBackground() b = mask[:, :, 0] g = mask[:, :, 1] r = mask[:, :, 2] b[final] = 0 g[final] = 0 r[final] = 0 final_img = cv2.resize(mask, (int(self.double_spin_width.value()), int(self.double_spin_height.value()))) if file != "": cv2.imwrite(file, final_img) @Slot() def onRemoveBackgroud(self): box = QMessageBox() box.setText("Selecione a cor do background") box.setIcon(QMessageBox.Information) box.exec() diag = QColorDialog() backColor = diag.getColor() final = self.getBackground() b = self.__img[:, :, 0] g = self.__img[:, :, 1] r = self.__img[:, :, 2] b[final] = backColor.blue() g[final] = backColor.green() r[final] = backColor.red() self.open_image(self.__img) @Slot() def Resize(self): val = self.resize_spinbox.value() newDim = int(self.original.shape[1] * val / 100), int(self.original.shape[0] * val / 100) self.__img = cv2.resize(self.original, newDim) self.open_image(self.__img) @Slot() def setHighlightColor(self, color): self.__highlightcolor = color @Slot() def getAllColors(self, colors): self.__AllColors = colors @Slot() def setTran(self, value): self.__transparency = 1- value/100 @Slot() def onUndo(self): self.thicSlider.setValue(1) self.nSlider.setValue(1) self.sigSlider.setValue(1) self.onNsegChange() self.onSigChange() self.onCompChange() self.__img = self.original self.open_image(self.__img)
class ColorDialog(QDialog): def __init__(self, model, font_metric, parent=None): super().__init__(parent) self.setWindowTitle('Color Options') self.model = model self.font_metric = font_metric self.main_window = parent self.createDialogLayout() def createDialogLayout(self): self.createGeneralTab() self.cellTable = self.createDomainTable(self.main_window.cellsModel) self.matTable = self.createDomainTable(self.main_window.materialsModel) self.tabs = { 'cell': self.createDomainTab(self.cellTable), 'material': self.createDomainTab(self.matTable), 'temperature': self.createPropertyTab('temperature'), 'density': self.createPropertyTab('density') } self.tab_bar = QTabWidget() self.tab_bar.setMaximumHeight(800) self.tab_bar.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.tab_bar.addTab(self.generalTab, 'General') self.tab_bar.addTab(self.tabs['cell'], 'Cells') self.tab_bar.addTab(self.tabs['material'], 'Materials') self.tab_bar.addTab(self.tabs['temperature'], 'Temperature') self.tab_bar.addTab(self.tabs['density'], 'Density') self.createButtonBox() self.colorDialogLayout = QVBoxLayout() self.colorDialogLayout.addWidget(self.tab_bar) self.colorDialogLayout.addWidget(self.buttonBox) self.setLayout(self.colorDialogLayout) def createGeneralTab(self): main_window = self.main_window # Masking options self.maskingCheck = QCheckBox('') self.maskingCheck.stateChanged.connect(main_window.toggleMasking) button_width = self.font_metric.boundingRect("XXXXXXXXXX").width() self.maskColorButton = QPushButton() self.maskColorButton.setCursor(QtCore.Qt.PointingHandCursor) self.maskColorButton.setFixedWidth(button_width) self.maskColorButton.setFixedHeight(self.font_metric.height() * 1.5) self.maskColorButton.clicked.connect(main_window.editMaskingColor) # Highlighting options self.hlCheck = QCheckBox('') self.hlCheck.stateChanged.connect(main_window.toggleHighlighting) self.hlColorButton = QPushButton() self.hlColorButton.setCursor(QtCore.Qt.PointingHandCursor) self.hlColorButton.setFixedWidth(button_width) self.hlColorButton.setFixedHeight(self.font_metric.height() * 1.5) self.hlColorButton.clicked.connect(main_window.editHighlightColor) self.alphaBox = QDoubleSpinBox() self.alphaBox.setRange(0, 1) self.alphaBox.setSingleStep(.05) self.alphaBox.valueChanged.connect(main_window.editAlpha) self.seedBox = QSpinBox() self.seedBox.setRange(1, 999) self.seedBox.valueChanged.connect(main_window.editSeed) # General options self.bgButton = QPushButton() self.bgButton.setCursor(QtCore.Qt.PointingHandCursor) self.bgButton.setFixedWidth(button_width) self.bgButton.setFixedHeight(self.font_metric.height() * 1.5) self.bgButton.clicked.connect(main_window.editBackgroundColor) self.colorbyBox = QComboBox(self) self.colorbyBox.addItem("material") self.colorbyBox.addItem("cell") self.colorbyBox.addItem("temperature") self.colorbyBox.addItem("density") self.colorbyBox.currentTextChanged[str].connect( main_window.editColorBy) self.universeLevelBox = QComboBox(self) self.universeLevelBox.addItem('all') for i in range(self.model.max_universe_levels): self.universeLevelBox.addItem(str(i)) self.universeLevelBox.currentTextChanged[str].connect( main_window.editUniverseLevel) # Overlap plotting self.overlapCheck = QCheckBox('', self) overlap_connector = partial(main_window.toggleOverlaps) self.overlapCheck.stateChanged.connect(overlap_connector) self.overlapColorButton = QPushButton() self.overlapColorButton.setCursor(QtCore.Qt.PointingHandCursor) self.overlapColorButton.setFixedWidth(button_width) self.overlapColorButton.setFixedHeight(self.font_metric.height() * 1.5) self.overlapColorButton.clicked.connect(main_window.editOverlapColor) self.colorResetButton = QPushButton("&Reset Colors") self.colorResetButton.setCursor(QtCore.Qt.PointingHandCursor) self.colorResetButton.clicked.connect(main_window.resetColors) formLayout = QFormLayout() formLayout.setAlignment(QtCore.Qt.AlignHCenter) formLayout.setFormAlignment(QtCore.Qt.AlignHCenter) formLayout.setLabelAlignment(QtCore.Qt.AlignLeft) formLayout.addRow('Masking:', self.maskingCheck) formLayout.addRow('Mask Color:', self.maskColorButton) formLayout.addRow(HorizontalLine()) formLayout.addRow('Highlighting:', self.hlCheck) formLayout.addRow('Highlight Color:', self.hlColorButton) formLayout.addRow('Highlight Alpha:', self.alphaBox) formLayout.addRow('Highlight Seed:', self.seedBox) formLayout.addRow(HorizontalLine()) formLayout.addRow('Background Color: ', self.bgButton) formLayout.addRow(HorizontalLine()) formLayout.addRow('Show Overlaps:', self.overlapCheck) formLayout.addRow('Overlap Color:', self.overlapColorButton) formLayout.addRow(HorizontalLine()) formLayout.addRow('Color Plot By:', self.colorbyBox) formLayout.addRow('Universe Level:', self.universeLevelBox) formLayout.addRow(self.colorResetButton, None) generalLayout = QHBoxLayout() innerWidget = QWidget() generalLayout.setAlignment(QtCore.Qt.AlignVCenter) innerWidget.setLayout(formLayout) generalLayout.addStretch(1) generalLayout.addWidget(innerWidget) generalLayout.addStretch(1) self.generalTab = QWidget() self.generalTab.setLayout(generalLayout) def createDomainTable(self, domainmodel): domainTable = QTableView() domainTable.setModel(domainmodel) domainTable.setItemDelegate(DomainDelegate(domainTable)) domainTable.verticalHeader().setVisible(False) domainTable.resizeColumnsToContents() domainTable.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) domainTable.horizontalHeader().setSectionResizeMode( 1, QHeaderView.Stretch) return domainTable def createDomainTab(self, domaintable): domainTab = QWidget() domainTab.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) domainLayout = QVBoxLayout() domainLayout.addWidget(domaintable) domainTab.setLayout(domainLayout) return domainTab def createPropertyTab(self, property_kind): propertyTab = QWidget() propertyTab.property_kind = property_kind propertyTab.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) propertyLayout = QVBoxLayout() propertyTab.minMaxCheckBox = QCheckBox() propertyTab.minMaxCheckBox.setCheckable(True) connector1 = partial(self.main_window.toggleUserMinMax, property=property_kind) propertyTab.minMaxCheckBox.stateChanged.connect(connector1) propertyTab.minBox = ScientificDoubleSpinBox(self) propertyTab.minBox.setMaximum(1E9) propertyTab.minBox.setMinimum(0) propertyTab.maxBox = ScientificDoubleSpinBox(self) propertyTab.maxBox.setMaximum(1E9) propertyTab.maxBox.setMinimum(0) connector2 = partial(self.main_window.editColorbarMin, property_type=property_kind) propertyTab.minBox.valueChanged.connect(connector2) connector3 = partial(self.main_window.editColorbarMax, property_type=property_kind) propertyTab.maxBox.valueChanged.connect(connector3) propertyTab.colormapBox = QComboBox(self) cmaps = sorted(m for m in mcolormaps.datad if not m.endswith("_r")) for cmap in cmaps: propertyTab.colormapBox.addItem(cmap) connector = partial(self.main_window.editColorMap, property_type=property_kind) propertyTab.colormapBox.currentTextChanged[str].connect(connector) propertyTab.dataIndicatorCheckBox = QCheckBox() propertyTab.dataIndicatorCheckBox.setCheckable(True) connector4 = partial(self.main_window.toggleDataIndicatorCheckBox, property=property_kind) propertyTab.dataIndicatorCheckBox.stateChanged.connect(connector4) propertyTab.colorBarScaleCheckBox = QCheckBox() propertyTab.colorBarScaleCheckBox.setCheckable(True) connector5 = partial(self.main_window.toggleColorbarScale, property=property_kind) propertyTab.colorBarScaleCheckBox.stateChanged.connect(connector5) formLayout = QFormLayout() formLayout.setAlignment(QtCore.Qt.AlignHCenter) formLayout.setFormAlignment(QtCore.Qt.AlignHCenter) formLayout.setLabelAlignment(QtCore.Qt.AlignLeft) formLayout.addRow('Colormap:', propertyTab.colormapBox) formLayout.addRow('Custom Min/Max', propertyTab.minMaxCheckBox) formLayout.addRow('Data Indicator', propertyTab.dataIndicatorCheckBox) formLayout.addRow('Log Scale', propertyTab.colorBarScaleCheckBox) formLayout.addRow(HorizontalLine()) formLayout.addRow('Max: ', propertyTab.maxBox) formLayout.addRow('Min: ', propertyTab.minBox) propertyTab.setLayout(formLayout) return propertyTab def updateDataIndicatorVisibility(self): av = self.model.activeView for key, val in av.data_indicator_enabled.items(): self.tabs[key].dataIndicatorCheckBox.setChecked(val) def updateColorMaps(self): cmaps = self.model.activeView.colormaps for key, val in cmaps.items(): idx = self.tabs[key].colormapBox.findText( val, QtCore.Qt.MatchFixedString) if idx >= 0: self.tabs[key].colormapBox.setCurrentIndex(idx) def updateColorMinMax(self): minmax = self.model.activeView.user_minmax for key, val in minmax.items(): self.tabs[key].minBox.setValue(val[0]) self.tabs[key].maxBox.setValue(val[1]) custom_minmax = self.model.activeView.use_custom_minmax for key, val, in custom_minmax.items(): self.tabs[key].minMaxCheckBox.setChecked(val) self.tabs[key].minBox.setEnabled(val) self.tabs[key].maxBox.setEnabled(val) def updateColorbarScale(self): av = self.model.activeView for key, val in av.color_scale_log.items(): self.tabs[key].colorBarScaleCheckBox.setChecked(val) def createButtonBox(self): applyButton = QPushButton("Apply Changes") applyButton.clicked.connect(self.main_window.applyChanges) closeButton = QPushButton("Close") closeButton.clicked.connect(self.hide) buttonLayout = QHBoxLayout() buttonLayout.addStretch(1) buttonLayout.addWidget(applyButton) buttonLayout.addWidget(closeButton) self.buttonBox = QWidget() self.buttonBox.setLayout(buttonLayout) def updateDialogValues(self): self.updateMasking() self.updateMaskingColor() self.updateColorMaps() self.updateColorMinMax() self.updateColorbarScale() self.updateDataIndicatorVisibility() self.updateHighlighting() self.updateHighlightColor() self.updateAlpha() self.updateSeed() self.updateBackgroundColor() self.updateColorBy() self.updateUniverseLevel() self.updateDomainTabs() self.updateOverlap() self.updateOverlapColor() def updateMasking(self): masking = self.model.activeView.masking self.maskingCheck.setChecked(masking) self.maskColorButton.setDisabled(not masking) if masking: self.cellTable.showColumn(4) self.matTable.showColumn(4) else: self.cellTable.hideColumn(4) self.matTable.hideColumn(4) def updateMaskingColor(self): color = self.model.activeView.maskBackground style_values = "border-radius: 8px; background-color: rgb{}" self.maskColorButton.setStyleSheet(style_values.format(str(color))) def updateHighlighting(self): highlighting = self.model.activeView.highlighting self.hlCheck.setChecked(highlighting) self.hlColorButton.setDisabled(not highlighting) self.alphaBox.setDisabled(not highlighting) self.seedBox.setDisabled(not highlighting) if highlighting: self.cellTable.showColumn(5) self.cellTable.hideColumn(2) self.cellTable.hideColumn(3) self.matTable.showColumn(5) self.matTable.hideColumn(2) self.matTable.hideColumn(3) else: self.cellTable.hideColumn(5) self.cellTable.showColumn(2) self.cellTable.showColumn(3) self.matTable.hideColumn(5) self.matTable.showColumn(2) self.matTable.showColumn(3) def updateHighlightColor(self): color = self.model.activeView.highlightBackground style_values = "border-radius: 8px; background-color: rgb{}" self.hlColorButton.setStyleSheet(style_values.format(str(color))) def updateAlpha(self): self.alphaBox.setValue(self.model.activeView.highlightAlpha) def updateSeed(self): self.seedBox.setValue(self.model.activeView.highlightSeed) def updateBackgroundColor(self): color = self.model.activeView.domainBackground self.bgButton.setStyleSheet("border-radius: 8px;" "background-color: rgb%s" % (str(color))) def updateOverlapColor(self): color = self.model.activeView.overlap_color self.overlapColorButton.setStyleSheet("border-radius: 8px;" "background-color: rgb%s" % (str(color))) def updateOverlap(self): colorby = self.model.activeView.colorby overlap_val = self.model.activeView.color_overlaps if colorby in ('cell', 'material'): self.overlapCheck.setChecked(overlap_val) def updateColorBy(self): colorby = self.model.activeView.colorby self.colorbyBox.setCurrentText(colorby) self.overlapCheck.setEnabled(colorby in ("cell", "material")) self.universeLevelBox.setEnabled(colorby == 'cell') def updateUniverseLevel(self): level = self.model.activeView.level if level == -1: self.universeLevelBox.setCurrentText('all') else: self.universeLevelBox.setCurrentText(str(level)) def updateDomainTabs(self): self.cellTable.setModel(self.main_window.cellsModel) self.matTable.setModel(self.main_window.materialsModel)
class Design(QDialog): def __init__(self): super(spur_design, self).__init__() self.createForm() self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) self.buttonBox.accepted.connect(self.submit) self.buttonBox.rejected.connect(self.reject) mainLayout = QVBoxLayout() mainLayout.addWidget(self.formGroupBox) mainLayout.addWidget(self.buttonBox) self.setLayout(mainLayout) self.setWindowTitle("OctoCAD\N{COPYRIGHT SIGN}") self.setGeometry(0, 0, 600, 400) self.setIcon() self.centering() self.show() def setIcon(self): icon = QIcon( "/home/ubuntu/OctoCAD/icons/logo_transparent_enlarged.png") self.setWindowIcon(icon) def centering(self): window = self.frameGeometry() center = QDesktopWidget().availableGeometry().center() window.moveCenter(center) self.move(window.topLeft()) def createForm(self): self.formGroupBox = QGroupBox("Design Configuration") self.layout = QFormLayout() self.Sutp = QDoubleSpinBox() self.Sutp.setRange(1, 10000) self.layout.addRow( QLabel("Enter ultimate tensile strength of pinion N.mm^-2"), self.Sutp) self.Sutg = QDoubleSpinBox() self.Sutg.setRange(1, 10000) self.layout.addRow( QLabel("Enter ultimate tensile strength of gear N.mm^-2"), self.Sutg) self.Ep = QDoubleSpinBox() self.Ep.setRange(1, 10000000) self.layout.addRow( QLabel("Enter modulus of elasticity of pinion N.mm^-2"), self.Ep) self.Eg = QDoubleSpinBox() self.Eg.setRange(1, 10000000) self.layout.addRow( QLabel("Enter modulus of elasticity of gear N.mm^-2"), self.Eg) self.grade = QComboBox() self.grade.addItem("1") self.grade.addItem("2") self.grade.addItem("3") self.grade.addItem("4") self.grade.addItem("5") self.grade.addItem("6") self.grade.addItem("7") self.grade.addItem("8") self.grade.addItem("9") self.grade.addItem("10") self.grade.addItem("11") self.grade.addItem("12") self.layout.addRow(QLabel("Select grade of gear pair"), self.grade) self.y = QComboBox() self.y.addItem("14.5 degree full depth involute tooth") self.y.addItem("20 degree full depth involute tooth") self.y.addItem("20 degree stub involute tooth") self.layout.addRow(QLabel("Select type of gear pair"), self.y) self.zp = QSpinBox() self.zp.setRange(14, 300) self.layout.addRow(QLabel("Enter number of pinion teeth"), self.zp) self.zg = QSpinBox() self.zg.setRange(14, 300) self.layout.addRow(QLabel("Enter number of gear teeth"), self.zg) self.P = QDoubleSpinBox() self.P.setRange(1, 1000000) self.layout.addRow(QLabel("Enter power transmitted by gear pair W"), self.P) self.Np = QDoubleSpinBox() self.Np.setRange(1, 10000) self.layout.addRow(QLabel("Enter speed of pinion rpm"), self.Np) self.Q = QComboBox() self.Q.addItem("Internal gearing") self.Q.addItem("External gearing") self.layout.addRow(QLabel("Enter type of gearing"), self.Q) self.FOSr = QDoubleSpinBox() self.FOSr.setRange(1, 10) self.layout.addRow(QLabel("Enter factor of safety"), self.FOSr) self.cs = QDoubleSpinBox() self.layout.addRow(QLabel("Enter service factor"), self.cs) self.cs.setRange(1, 10) self.formGroupBox.setLayout(self.layout) def submit(self): os.makedirs("/home/ubuntu/.OctoCAD/spur-gear", exist_ok=True) with open("/home/ubuntu/.OctoCAD/spur-gear/user_input_design.txt", "w") as user_input_design_f: user_input_design_f.write("# name: Eg\n# type: scalar\n") user_input_design_f.write(self.Eg.text() + "\n\n\n") user_input_design_f.write("# name: Ep\n# type: scalar\n") user_input_design_f.write(self.Ep.text() + "\n\n\n") user_input_design_f.write("# name: FOSr\n# type: scalar\n") user_input_design_f.write(self.FOSr.text() + "\n\n\n") user_input_design_f.write("# name: Np\n# type: scalar\n") user_input_design_f.write(self.Np.text() + "\n\n\n") user_input_design_f.write("# name: P\n# type: scalar\n") user_input_design_f.write(self.P.text() + "\n\n\n") user_input_design_f.write("# name: Q\n# type: scalar\n") if self.Q.currentText() == "Internal gearing": user_input_design_f.write("1" + "\n\n\n") if self.Q.currentText() == "External gearing": user_input_design_f.write("2" + "\n\n\n") user_input_design_f.write("# name: Sutg\n# type: scalar\n") user_input_design_f.write(self.Sutg.text() + "\n\n\n") user_input_design_f.write("# name: Sutp\n# type: scalar\n") user_input_design_f.write(self.Sutp.text() + "\n\n\n") user_input_design_f.write("# name: cs\n# type: scalar\n") user_input_design_f.write(self.cs.text() + "\n\n\n") user_input_design_f.write("# name: grade\n# type: scalar\n") user_input_design_f.write(self.grade.currentText() + "\n\n\n") user_input_design_f.write("# name: y\n# type: scalar\n") if self.y.currentText() == "14.5 degree full depth involute tooth": user_input_design_f.write("1" + "\n\n\n") if self.y.currentText() == "20 degree full depth involute tooth": user_input_design_f.write("2" + "\n\n\n") if self.y.currentText() == "20 degree stub involute tooth": user_input_design_f.write("3" + "\n\n\n") user_input_design_f.write("# name: zg\n# type: scalar\n") user_input_design_f.write(self.zg.text() + "\n\n\n") user_input_design_f.write("# name: zp\n# type: scalar\n") user_input_design_f.write(self.zp.text() + "\n\n\n") os.system("octave /home/ubuntu/OctoCAD/bis/gear/spur-gear/io.m") self.createResult() def createResult(self): with open("/home/ubuntu/.OctoCAD/spur-gear/result.txt", "r") as result_f: str_Fd = result_f.readline().split("=")[1].rstrip("\n") str_Fap = result_f.readline().split("=")[1].rstrip("\n") str_Fag = result_f.readline().split("=")[1].rstrip("\n") str_Fw = result_f.readline().split("=")[1].rstrip("\n") str_BHN = result_f.readline().split("=")[1].rstrip("\n") str_m = result_f.readline().split("=")[1].rstrip("\n") str_FOS = result_f.readline().split("=")[1].rstrip("\n") str_zp = result_f.readline().split("=")[1].rstrip("\n") str_zg = result_f.readline().split("=")[1].rstrip("\n") str_result = "Module of gear pair is " + str_m + " mm\n\nEffective load on the gear pair is " + str_Fd + " N\n\nBending load capacity of pinion is " + str_Fap + " N\n\nBending load capacity of gear " + str_Fag + " N\n\nWear load capacity of gear pair is " + str_Fw + " N\n\nSurface hardness of the gear pair is " + str_BHN + " BHN\n\nAvailabe factor of safety is " + str_FOS + "\n\n" result_box = QMessageBox() result_box.setWindowTitle("Design of gear pair") result_box.setText(str_result) result_box.exec_() obj_QApplication.exit()
class ColorForm(QWidget): """ Class for handling a field with a colormap, alpha, and visibility Attributes ---------- model : PlotModel The model instance used when updating information on the form. colormapBox : QComboBox Holds the string of the matplotlib colorbar being used visibilityBox : QCheckBox Indicator for whether or not the field should be visible alphaBox : QDoubleSpinBox Holds the alpha value for the displayed field data colormapBox : QComboBox Selector for colormap dataIndicatorCheckBox : QCheckBox Inidcates whether or not the data indicator will appear on the colorbar userMinMaxBox : QCheckBox Indicates whether or not the user defined values in the min and max will be used to set the bounds of the colorbar. maxBox : ScientificDoubleSpinBox Max value of the colorbar. If the userMinMaxBox is checked, this will be the user's input. If the userMinMaxBox is not checked, this box will hold the max value of the visible data. minBox : ScientificDoubleSpinBox Min value of the colorbar. If the userMinMaxBox is checked, this will be the user's input. If the userMinMaxBox is not checked, this box will hold the max value of the visible data. scaleBox : QCheckBox Indicates whether or not the data is displayed on a log or linear scale maskZeroBox : QCheckBox Indicates whether or not values equal to zero are displayed clipDataBox : QCheckBox Indicates whether or not values outside the min/max are displayed contoursBox : QCheckBox Inidicates whether or not data is displayed as contours contourLevelsLine : QLineEdit Controls the contours of the data. If this line contains a single integer, that number of levels is used to display the data. If a comma-separated set of values is entered, those values will be used as levels in the contour plot. """ def __init__(self, model, main_window, field, colormaps=None): super().__init__() self.model = model self.main_window = main_window self.field = field self.layout = QFormLayout() # Visibility check box self.visibilityBox = QCheckBox() visible_connector = partial(main_window.toggleTallyVisibility) self.visibilityBox.stateChanged.connect(visible_connector) # Alpha value self.alphaBox = QDoubleSpinBox() self.alphaBox.setDecimals(2) self.alphaBox.setRange(0, 1) self.alphaBox.setSingleStep(0.05) alpha_connector = partial(main_window.editTallyAlpha) self.alphaBox.valueChanged.connect(alpha_connector) # Color map selector self.colormapBox = QComboBox() if colormaps is None: colormaps = sorted(m for m in mcolormaps.datad if not m.endswith("_r")) for colormap in colormaps: self.colormapBox.addItem(colormap) cmap_connector = partial(main_window.editTallyDataColormap) self.colormapBox.currentTextChanged[str].connect(cmap_connector) # Data indicator line check box self.dataIndicatorCheckBox = QCheckBox() data_indicator_connector = partial(main_window.toggleTallyDataIndicator) self.dataIndicatorCheckBox.stateChanged.connect(data_indicator_connector) # User specified min/max check box self.userMinMaxBox = QCheckBox() minmax_connector = partial(main_window.toggleTallyDataUserMinMax) self.userMinMaxBox.stateChanged.connect(minmax_connector) # Data min spin box self.minBox = ScientificDoubleSpinBox() self.minBox.setMinimum(0.0) min_connector = partial(main_window.editTallyDataMin) self.minBox.valueChanged.connect(min_connector) # Data max spin box self.maxBox = ScientificDoubleSpinBox() self.maxBox.setMinimum(0.0) max_connector = partial(main_window.editTallyDataMax) self.maxBox.valueChanged.connect(max_connector) # Linear/Log scaling check box self.scaleBox = QCheckBox() scale_connector = partial(main_window.toggleTallyLogScale) self.scaleBox.stateChanged.connect(scale_connector) # Masking of zero values check box self.maskZeroBox = QCheckBox() zero_connector = partial(main_window.toggleTallyMaskZero) self.maskZeroBox.stateChanged.connect(zero_connector) # Clip data to min/max check box self.clipDataBox = QCheckBox() clip_connector = partial(main_window.toggleTallyDataClip) self.clipDataBox.stateChanged.connect(clip_connector) # Display data as contour plot check box self.contoursBox = QCheckBox() self.contoursBox.stateChanged.connect(main_window.toggleTallyContours) self.contourLevelsLine = QLineEdit() self.contourLevelsLine.textChanged.connect( main_window.editTallyContourLevels) # Organize widgets on layout self.layout.addRow("Visible:", self.visibilityBox) self.layout.addRow("Alpha: ", self.alphaBox) self.layout.addRow("Colormap: ", self.colormapBox) self.layout.addRow("Data Indicator: ", self.dataIndicatorCheckBox) self.layout.addRow("Custom Min/Max: ", self.userMinMaxBox) self.layout.addRow("Min: ", self.minBox) self.layout.addRow("Max: ", self.maxBox) self.layout.addRow("Log Scale: ", self.scaleBox) self.layout.addRow("Clip Data: ", self.clipDataBox) self.layout.addRow("Mask Zeros: ", self.maskZeroBox) self.layout.addRow("Contours: ", self.contoursBox) self.layout.addRow("Contour Levels:", self.contourLevelsLine) self.setLayout(self.layout) def updateTallyContours(self): cv = self.model.currentView self.contoursBox.setChecked(cv.tallyContours) self.contourLevelsLine.setText(cv.tallyContourLevels) def updateDataIndicator(self): cv = self.model.currentView self.dataIndicatorCheckBox.setChecked(cv.tallyDataIndicator) def setMinMaxEnabled(self, enable): enable = bool(enable) self.minBox.setEnabled(enable) self.maxBox.setEnabled(enable) def updateMinMax(self): cv = self.model.currentView self.minBox.setValue(cv.tallyDataMin) self.maxBox.setValue(cv.tallyDataMax) self.setMinMaxEnabled(cv.tallyDataUserMinMax) def updateTallyVisibility(self): cv = self.model.currentView self.visibilityBox.setChecked(cv.tallyDataVisible) def updateMaskZeros(self): cv = self.model.currentView self.maskZeroBox.setChecked(cv.tallyMaskZeroValues) def updateDataClip(self): cv = self.model.currentView self.clipDataBox.setChecked(cv.clipTallyData) def update(self): cv = self.model.currentView # set colormap value in selector cmap = cv.tallyDataColormap idx = self.colormapBox.findText(cmap, QtCore.Qt.MatchFixedString) self.colormapBox.setCurrentIndex(idx) self.alphaBox.setValue(cv.tallyDataAlpha) self.visibilityBox.setChecked(cv.tallyDataVisible) self.userMinMaxBox.setChecked(cv.tallyDataUserMinMax) self.scaleBox.setChecked(cv.tallyDataLogScale) self.updateMinMax() self.updateMaskZeros() self.updateDataClip() self.updateDataIndicator() self.updateTallyContours()
class PlotsWidget(ToolWidget): def __init__(self, image, parent=None): super(PlotsWidget, self).__init__(parent) choices = ['Red', 'Green', 'Blue', 'Hue', 'Saturation', 'Value'] self.xaxis_combo = QComboBox() self.xaxis_combo.addItems(choices) self.xaxis_combo.setCurrentIndex(3) self.yaxis_combo = QComboBox() self.yaxis_combo.addItems(choices) self.yaxis_combo.setCurrentIndex(4) self.sampling_spin = QSpinBox() levels = int(np.log2(min(image.shape[:-1]))) self.sampling_spin.setRange(0, levels) self.sampling_spin.setSpecialValueText(self.tr('Off')) self.sampling_spin.setValue(1) self.sampling_spin.setSuffix(self.tr(' level(s)')) self.size_spin = QSpinBox() self.size_spin.setRange(1, 10) self.size_spin.setValue(2) self.size_spin.setSuffix(self.tr(' pt')) self.style_combo = QComboBox() self.markers = [ ',', '.', 'o', '8', 's', 'p', 'P', '*', 'h', 'H', 'X', 'D' ] self.style_combo.addItems([ 'pixel', 'point', 'circle', 'octa', 'square', 'penta', 'plus', 'star', 'hexa1', 'hexa2', 'cross', 'diamond' ]) self.alpha_spin = QDoubleSpinBox() self.alpha_spin.setRange(0, 1) self.alpha_spin.setDecimals(2) self.alpha_spin.setSingleStep(0.05) self.alpha_spin.setValue(1) self.colors_check = QCheckBox(self.tr('Show colors')) self.grid_check = QCheckBox(self.tr('Show grid')) self.norm_check = QCheckBox(self.tr('Normalized')) self.total_label = QLabel() img = np.copy(image) self.colors = [None] * (levels + 1) for scale in range(levels + 1): rgb = cv.cvtColor(img.astype(np.float32) / 255, cv.COLOR_BGR2RGB) hsv = cv.cvtColor(rgb, cv.COLOR_RGB2HSV) hsv[:, :, 0] /= 360 shape = (img.shape[0] * img.shape[1], img.shape[2]) self.colors[scale] = np.concatenate( (np.reshape(rgb, shape), np.reshape(hsv, shape)), axis=1) img = cv.pyrDown(img) figure = Figure() plot_canvas = FigureCanvas(figure) self.axes = plot_canvas.figure.subplots() self.redraw() figure.set_tight_layout(True) self.xaxis_combo.currentIndexChanged.connect(self.redraw) self.yaxis_combo.currentIndexChanged.connect(self.redraw) self.sampling_spin.valueChanged.connect(self.redraw) self.size_spin.valueChanged.connect(self.redraw) self.style_combo.currentIndexChanged.connect(self.redraw) self.alpha_spin.valueChanged.connect(self.redraw) self.colors_check.stateChanged.connect(self.redraw) self.grid_check.stateChanged.connect(self.redraw) self.norm_check.stateChanged.connect(self.redraw) bottom_layout = QGridLayout() bottom_layout.addWidget(NavigationToolbar(plot_canvas, self), 0, 0, 1, 5) bottom_layout.addWidget(QLabel(self.tr('X axis:')), 1, 0) bottom_layout.addWidget(self.xaxis_combo, 1, 1) bottom_layout.addWidget(QLabel(self.tr('Y Axis:')), 2, 0) bottom_layout.addWidget(self.yaxis_combo, 2, 1) bottom_layout.addWidget(QLabel(self.tr('Subsampling:')), 3, 0) bottom_layout.addWidget(self.sampling_spin, 3, 1) bottom_layout.addWidget(QLabel(self.tr('Point size:')), 1, 2) bottom_layout.addWidget(self.size_spin, 1, 3) bottom_layout.addWidget(QLabel(self.tr('Point style:')), 2, 2) bottom_layout.addWidget(self.style_combo, 2, 3) bottom_layout.addWidget(QLabel(self.tr('Point alpha:')), 3, 2) bottom_layout.addWidget(self.alpha_spin, 3, 3) bottom_layout.addWidget(self.colors_check, 1, 4) bottom_layout.addWidget(self.grid_check, 2, 4) bottom_layout.addWidget(self.total_label, 3, 4) main_layout = QVBoxLayout() main_layout.addWidget(plot_canvas) main_layout.addLayout(bottom_layout) self.setLayout(main_layout) def redraw(self): start = time() v = self.sampling_spin.value() x = self.colors[v][:, self.xaxis_combo.currentIndex()] y = self.colors[v][:, self.yaxis_combo.currentIndex()] xlim = self.axes.get_xlim() ylim = self.axes.get_ylim() s = self.size_spin.value()**2 c = None if not self.colors_check.isChecked( ) else self.colors[v][:, :3] a = self.alpha_spin.value() m = self.markers[self.style_combo.currentIndex()] self.axes.clear() self.axes.set_facecolor([0.5] * 3 if c is not None else [1.0] * 3) self.axes.scatter(x, y, s, c, m, alpha=a) self.axes.set_xlabel(self.xaxis_combo.currentText()) self.axes.set_ylabel(self.yaxis_combo.currentText()) self.axes.grid(self.grid_check.isChecked(), which='both') self.axes.set_xlim(xlim) self.axes.set_ylim(ylim) self.axes.figure.canvas.draw() self.total_label.setText(self.tr('[{} points]'.format(len(x)))) self.info_message.emit('Plot redraw = {}'.format(elapsed_time(start)))
class MainWindow(QMainWindow): def __init__(self): super().__init__() layout = QVBoxLayout() form = QFormLayout() self.track_id = QSpinBox() self.track_id.setDisabled(True) self.name = QLineEdit() self.album = QComboBox() self.media_type = QComboBox() self.genre = QComboBox() self.composer = QLineEdit() self.milliseconds = QSpinBox() self.milliseconds.setRange(0, 2147483647) self.milliseconds.setSingleStep(1) self.bytes = QSpinBox() self.bytes.setRange(0, 2147483647) self.bytes.setSingleStep(1) self.unit_price = QDoubleSpinBox() self.unit_price.setRange(0, 999) self.unit_price.setSingleStep(0.01) self.unit_price.setPrefix("$") form.addRow(QLabel("Track ID"), self.track_id) form.addRow(QLabel("Track name"), self.name) form.addRow(QLabel("Composer"), self.composer) form.addRow(QLabel("Milliseconds"), self.milliseconds) form.addRow(QLabel("Bytes"), self.bytes) form.addRow(QLabel("Unit Price"), self.unit_price) self.model = QSqlTableModel(db=db) self.mapper = QDataWidgetMapper() self.mapper.setModel(self.model) self.mapper.addMapping(self.track_id, 0) self.mapper.addMapping(self.name, 1) self.mapper.addMapping(self.composer, 5) self.mapper.addMapping(self.milliseconds, 6) self.mapper.addMapping(self.bytes, 7) self.mapper.addMapping(self.unit_price, 8) self.model.setTable("Track") self.model.select() self.mapper.toFirst() self.setMinimumSize(QSize(400, 400)) controls = QHBoxLayout() # tag::controls[] prev_rec = QPushButton("Previous") prev_rec.clicked.connect(self.mapper.toPrevious) next_rec = QPushButton("Next") next_rec.clicked.connect(self.mapper.toNext) save_rec = QPushButton("Save Changes") save_rec.clicked.connect(self.mapper.submit) # end::controls[] controls.addWidget(prev_rec) controls.addWidget(next_rec) controls.addWidget(save_rec) layout.addLayout(form) layout.addLayout(controls) widget = QWidget() widget.setLayout(layout) self.setCentralWidget(widget)
class NGL_HKLViewer(QWidget): def __init__(self, parent=None): super(NGL_HKLViewer, self).__init__(parent) self.verbose = 0 self.UseOSbrowser = False self.jscriptfname = "" self.devmode = False for e in sys.argv: if "verbose" in e: self.verbose = e.split("verbose=")[1] if "UseOSbrowser" in e: self.UseOSbrowser = e.split("UseOSbrowser=")[1] if "jscriptfname" in e: self.jscriptfname = e.split("jscriptfname=")[1] if "devmode" in e: self.devmode = True self.zmq_context = None self.bufsize = 20000 self.originalPalette = QApplication.palette() self.openFileNameButton = QPushButton("Load reflection file") self.openFileNameButton.setDefault(True) self.openFileNameButton.clicked.connect(self.OpenReflectionsFile) self.debugbutton = QPushButton("Debug") self.debugbutton.clicked.connect(self.DebugInteractively) self.settingsbtn = QPushButton("Settings") self.settingsbtn.clicked.connect(self.SettingsDialog) self.mousemoveslider = QSlider(Qt.Horizontal) self.mousemoveslider.setMinimum(0) self.mousemoveslider.setMaximum(300) self.mousemoveslider.setValue(0) self.mousemoveslider.sliderReleased.connect( self.onFinalMouseSensitivity) self.mousemoveslider.valueChanged.connect(self.onMouseSensitivity) self.mousesensitxtbox = QLineEdit('') self.mousesensitxtbox.setReadOnly(True) self.fontspinBox = QDoubleSpinBox() self.fontspinBox.setSingleStep(1) self.fontspinBox.setRange(4, 50) self.font = QFont() self.font.setFamily(self.font.defaultFamily()) self.fontspinBox.setValue(self.font.pointSize()) #self.fontspinBox.setValue(self.font.pixelSize()) self.fontspinBox.valueChanged.connect(self.onFontsizeChanged) self.Fontsize_labeltxt = QLabel() self.Fontsize_labeltxt.setText("Font size:") self.cameraPerspectCheckBox = QCheckBox() self.cameraPerspectCheckBox.setText("Perspective camera") self.cameraPerspectCheckBox.clicked.connect(self.onCameraPerspect) self.cameraPerspectCheckBox.setCheckState(Qt.Unchecked) self.settingsform = SettingsForm(self) self.MillerComboBox = QComboBox() self.MillerComboBox.activated.connect(self.onMillerComboSelchange) #self.MillerComboBox.setSizeAdjustPolicy(QComboBox.AdjustToContents) self.MillerLabel = QLabel() self.MillerLabel.setText("Selected HKL Scene") self.HKLnameedit = QLineEdit('') self.HKLnameedit.setReadOnly(True) self.textInfo = QTextEdit() self.textInfo.setLineWrapMode(QTextEdit.NoWrap) self.textInfo.setReadOnly(True) labels = [ "Label", "Type", "no. of HKLs", "Span of HKLs", "Min Max data", "Min Max sigmas", "d_min, d_max", "Symmetry unique", "Anomalous" ] self.millertable = QTableWidget(0, len(labels)) self.millertable.setHorizontalHeaderLabels(labels) self.millertable.horizontalHeader().setDefaultAlignment(Qt.AlignLeft) # don't allow editing this table self.millertable.setEditTriggers(QTableWidget.NoEditTriggers) self.createExpansionBox() self.createFileInfoBox() self.CreateSliceTabs() self.createRadiiScaleGroupBox() self.createBinsBox() self.CreateFunctionTabs() mainLayout = QGridLayout() mainLayout.addWidget(self.FileInfoBox, 0, 0) mainLayout.addWidget(self.MillerLabel, 1, 0) mainLayout.addWidget(self.MillerComboBox, 2, 0) mainLayout.addWidget(self.functionTabWidget, 3, 0) mainLayout.addWidget(self.settingsbtn, 4, 0, 1, 1) #import code, traceback; code.interact(local=locals(), banner="".join( traceback.format_stack(limit=10) ) ) if self.UseOSbrowser == False: self.BrowserBox = QWebEngineView() mainLayout.addWidget(self.BrowserBox, 0, 1, 5, 3) self.BrowserBox.setUrl("https://cctbx.github.io/") #self.BrowserBox.setUrl("https://webglreport.com/") #self.BrowserBox.loadFinished.connect(self.onLoadFinished) mainLayout.setColumnStretch(2, 1) mainLayout.setRowStretch(0, 1) mainLayout.setRowStretch(1, 0) mainLayout.setRowStretch(2, 1) mainLayout.setRowStretch(3, 1) mainLayout.setColumnStretch(4, 0) self.setLayout(mainLayout) self.setWindowTitle("HKL-Viewer") self.cctbxproc = None self.LaunchCCTBXPython() self.out = None self.err = None self.comboviewwidth = 0 self.hklscenes_arrays = [] self.array_infotpls = [] self.matching_arrays = [] self.bin_infotpls = None self.bin_opacities = None self.html_url = "" self.spacegroups = [] self.info = [] self.infostr = "" self.fileisvalid = False self.NewFileLoaded = False self.NewHKLscenes = False self.updatingNbins = False self.binstableitemchanges = False self.show() def SettingsDialog(self): self.settingsform.show() def update(self): if self.cctbxproc: if self.cctbxproc.stdout: print(self.cctbxproc.stdout.read().decode("utf-8")) if self.cctbxproc.stderr: print(self.cctbxproc.stderr.read().decode("utf-8")) if self.out: print(self.out.decode("utf-8")) if self.err: print(self.err.decode("utf-8")) if self.zmq_context: try: msg = self.socket.recv( flags=zmq.NOBLOCK ) #To empty the socket from previous messages msgstr = msg.decode() self.infodict = eval(msgstr) #print("received from cctbx: " + str(self.infodict)) if self.infodict: if self.infodict.get("hklscenes_arrays"): self.hklscenes_arrays = self.infodict.get( "hklscenes_arrays", []) if self.infodict.get("array_infotpls"): self.array_infotpls = self.infodict.get( "array_infotpls", []) if self.infodict.get("bin_data_label"): self.BinDataComboBox.setCurrentText( self.infodict["bin_data_label"]) if self.infodict.get("bin_infotpls"): self.bin_infotpls = self.infodict["bin_infotpls"] self.nbins = len(self.bin_infotpls) self.updatingNbins = True self.Nbins_spinBox.setValue(self.nbins) self.updatingNbins = False self.binstable.clearContents() self.binstable.setRowCount(self.nbins) for row, bin_infotpl in enumerate(self.bin_infotpls): for col, elm in enumerate(bin_infotpl): # only allow changing the last column with opacity values if col != 3: item = QTableWidgetItem(str(elm)) else: item = QTableWidgetItem() item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled) item.setCheckState(Qt.Checked) item.setFlags(item.flags() ^ Qt.ItemIsEditable) self.binstable.setItem(row, col, item) if self.bin_opacities: self.update_table_opacities() if self.infodict.get("bin_opacities"): self.bin_opacities = self.infodict["bin_opacities"] if self.binstable.rowCount() > 0: self.update_table_opacities() if self.infodict.get("html_url"): self.html_url = self.infodict["html_url"] if self.UseOSbrowser == False: self.BrowserBox.setUrl(self.html_url) # workaround for background colour bug in chromium # https://bugreports.qt.io/browse/QTBUG-41960 self.BrowserBox.page().setBackgroundColor( QColor(100, 100, 100, 1.0)) if self.infodict.get("spacegroups"): self.spacegroups = self.infodict.get("spacegroups", []) self.SpaceGroupComboBox.clear() self.SpaceGroupComboBox.addItems(self.spacegroups) if self.infodict.get("merge_data"): self.mergedata = self.infodict["merge_data"] currentinfostr = "" if self.infodict.get("info"): currentinfostr = self.infodict.get("info", []) if self.infodict.get("NewFileLoaded"): self.NewFileLoaded = self.infodict.get( "NewFileLoaded", False) if self.infodict.get("NewHKLscenes"): self.NewHKLscenes = self.infodict.get( "NewHKLscenes", False) self.fileisvalid = True #print("ngl_hkl_infodict: " + str(ngl_hkl_infodict)) if currentinfostr: #print(currentinfostr) self.infostr += currentinfostr + "\n" # display no more than self.bufsize bytes of text self.infostr = self.infostr[-self.bufsize:] self.textInfo.setPlainText(self.infostr) self.textInfo.verticalScrollBar().setValue( self.textInfo.verticalScrollBar().maximum()) if self.NewFileLoaded and self.NewHKLscenes: #if self.mergedata == True : val = Qt.CheckState.Checked #if self.mergedata == None : val = Qt.CheckState.PartiallyChecked #if self.mergedata == False : val = Qt.CheckState.Unchecked #self.mergecheckbox.setCheckState(val ) #print("got hklscenes: " + str(self.hklscenes_arrays)) self.MillerComboBox.clear() self.MillerComboBox.addItems( [e[3] for e in self.hklscenes_arrays]) self.MillerComboBox.setCurrentIndex( -1) # unselect the first item in the list self.comboviewwidth = 0 for e in self.hklscenes_arrays: self.comboviewwidth = max( self.comboviewwidth, self.MillerComboBox.fontMetrics().width(e[3])) self.MillerComboBox.view().setMinimumWidth( self.comboviewwidth) self.millertable.clearContents() self.millertable.setRowCount(len( self.hklscenes_arrays)) for n, millarr in enumerate(self.array_infotpls): for m, elm in enumerate(millarr): self.millertable.setItem( n, m, QTableWidgetItem(str(elm))) self.functionTabWidget.setDisabled(True) self.NewFileLoaded = False if self.NewHKLscenes: self.BinDataComboBox.clear() self.BinDataComboBox.addItems( ["Resolution"] + [e[3] for e in self.hklscenes_arrays]) self.BinDataComboBox.view().setMinimumWidth( self.comboviewwidth) #self.BinDataComboBox.setCurrentIndex(-1) # unselect the first item in the list self.NewHKLscenes = False except Exception as e: errmsg = str(e) if "Resource temporarily unavailable" not in errmsg: print(errmsg + traceback.format_exc(limit=10)) pass def onFinalMouseSensitivity(self): val = self.mousemoveslider.value() / 100.0 self.NGL_HKL_command( 'NGL_HKLviewer.viewer.NGL.mouse_sensitivity = %f' % val) def onMouseSensitivity(self): val = self.mousemoveslider.value() / 100.0 self.mousesensitxtbox.setText("%2.2f" % val) def onFontsizeChanged(self, val): font = app.font() font.setPointSize(val) app.setFont(font) self.settingsform.setFixedSize(self.settingsform.sizeHint()) def onCameraPerspect(self, val): if self.cameraPerspectCheckBox.isChecked(): self.NGL_HKL_command("NGL_HKLviewer.camera_type = perspective") else: self.NGL_HKL_command("NGL_HKLviewer.camera_type = orthographic") def MergeData(self): if self.mergecheckbox.checkState() == Qt.CheckState.Checked: self.NGL_HKL_command('NGL_HKLviewer.mergedata = True') if self.mergecheckbox.checkState() == Qt.CheckState.PartiallyChecked: self.NGL_HKL_command('NGL_HKLviewer.mergedata = None') if self.mergecheckbox.checkState() == Qt.CheckState.Unchecked: self.NGL_HKL_command('NGL_HKLviewer.mergedata = False') def ExpandToP1(self): if self.expandP1checkbox.isChecked(): self.NGL_HKL_command('NGL_HKLviewer.viewer.expand_to_p1 = True') else: self.NGL_HKL_command('NGL_HKLviewer.viewer.expand_to_p1 = False') def ExpandAnomalous(self): if self.expandAnomalouscheckbox.isChecked(): self.NGL_HKL_command( 'NGL_HKLviewer.viewer.expand_anomalous = True') else: self.NGL_HKL_command( 'NGL_HKLviewer.viewer.expand_anomalous = False') def showSysAbsent(self): if self.sysabsentcheckbox.isChecked(): self.NGL_HKL_command( 'NGL_HKLviewer.viewer.show_systematic_absences = True') else: self.NGL_HKL_command( 'NGL_HKLviewer.viewer.show_systematic_absences = False') def showMissing(self): if self.missingcheckbox.isChecked(): self.NGL_HKL_command('NGL_HKLviewer.viewer.show_missing = True') else: self.NGL_HKL_command('NGL_HKLviewer.viewer.show_missing = False') def showOnlyMissing(self): if self.onlymissingcheckbox.isChecked(): self.NGL_HKL_command( 'NGL_HKLviewer.viewer.show_only_missing = True') else: self.NGL_HKL_command( 'NGL_HKLviewer.viewer.show_only_missing = False') def showSlice(self): if self.showslicecheckbox.isChecked(): self.NGL_HKL_command('NGL_HKLviewer.viewer.slice_mode = True') if self.expandP1checkbox.isChecked(): self.NGL_HKL_command("""NGL_HKLviewer.viewer { expand_to_p1 = True inbrowser = False } """) if self.expandAnomalouscheckbox.isChecked(): self.NGL_HKL_command("""NGL_HKLviewer.viewer { expand_anomalous = True inbrowser = False } """) else: self.NGL_HKL_command("""NGL_HKLviewer.viewer { slice_mode = False inbrowser = True } """) def onSliceComboSelchange(self, i): rmin = self.array_infotpls[self.MillerComboBox.currentIndex()][3][0][i] rmax = self.array_infotpls[self.MillerComboBox.currentIndex()][3][1][i] self.sliceindexspinBox.setRange(rmin, rmax) self.NGL_HKL_command("NGL_HKLviewer.viewer.slice_axis = %s" % self.sliceaxis[i]) def onSliceIndexChanged(self, val): self.sliceindex = val self.NGL_HKL_command("NGL_HKLviewer.viewer.slice_index = %d" % self.sliceindex) def onBindataComboSelchange(self, i): if self.BinDataComboBox.currentText(): if self.BinDataComboBox.currentIndex() > 0: bin_scene_label = str(self.BinDataComboBox.currentIndex() - 1) else: bin_scene_label = "Resolution" self.NGL_HKL_command("NGL_HKLviewer.bin_scene_label = %s" % bin_scene_label) def update_table_opacities(self, allalpha=None): bin_opacitieslst = eval(self.bin_opacities) self.binstable_isready = False for binopacity in bin_opacitieslst: if not allalpha: alpha = float(binopacity.split(",")[0]) else: alpha = allalpha bin = int(binopacity.split(",")[1]) item = QTableWidgetItem() item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled) if alpha < 0.5: item.setCheckState(Qt.Unchecked) else: item.setCheckState(Qt.Checked) item.setFlags(item.flags() ^ Qt.ItemIsEditable) self.binstable.setItem(bin, 3, item) self.binstable_isready = True def SetOpaqueAll(self): if self.binstableitemchanges: return bin_opacitieslst = eval(self.bin_opacities) nbins = len(bin_opacitieslst) sum = 0 for binopacity in bin_opacitieslst: sum += float(binopacity.split(",")[0]) if sum >= nbins: self.OpaqueAllCheckbox.setCheckState(Qt.Checked) if sum == 0: self.OpaqueAllCheckbox.setCheckState(Qt.Unchecked) if sum > 0.0 and sum < nbins: self.OpaqueAllCheckbox.setCheckState(Qt.PartiallyChecked) def onBinsTableItemChanged(self, item): row = item.row() column = item.column() try: if item.checkState() == Qt.Unchecked: newval = 0 else: newval = 1.0 if column == 3 and self.binstable_isready: # changing opacity assert (newval <= 1.0 and newval >= 0.0) bin_opacitieslst = eval(self.bin_opacities) bin_opacitieslst[row] = str(newval) + ', ' + str(row) self.bin_opacities = str(bin_opacitieslst) self.SetOpaqueAll() self.NGL_HKL_command( 'NGL_HKLviewer.viewer.NGL.bin_opacities = "%s"' % self.bin_opacities) except Exception as e: print(str(e)) #self.binstable.currentItem().setText( self.currentSelectedBinsTableVal) def onBinsTableItemSelectionChanged(self): row = self.binstable.currentItem().row() column = self.binstable.currentItem().column() self.currentSelectedBinsTableVal = self.binstable.currentItem().text() #print( "in itemSelectionChanged " + self.currentSelectedBinsTableVal) def onOpaqueAll(self): self.binstableitemchanges = True bin_opacitieslst = eval(self.bin_opacities) nbins = len(bin_opacitieslst) bin_opacitieslst = [] self.binstable_isready = False if self.OpaqueAllCheckbox.isChecked(): for i in range(nbins): bin_opacitieslst.append("1.0, %d" % i) else: for i in range(nbins): bin_opacitieslst.append("0.0, %d" % i) self.bin_opacities = str(bin_opacitieslst) self.NGL_HKL_command('NGL_HKLviewer.viewer.NGL.bin_opacities = "%s"' % self.bin_opacities) self.binstableitemchanges = False self.binstable_isready = True """ def onLoadFinished(self, val): pass #print("web page finished loading now") def onBinsTableitemActivated(self, item): row = item.row() column = item.column() currentval = item.text() #print( "in itemActivated " + currentval) def onBinsTableCellentered(self, row, col): pass #print( "in Cellentered " + self.binstable.currentItem().text() ) def onBinsTableCellPressed(self, row, col): pass #print( "in CellPressed " + self.binstable.currentItem().text() ) """ def onNbinsChanged(self, val): self.nbins = val if not self.updatingNbins: # avoid possible endless loop to cctbx self.NGL_HKL_command("NGL_HKLviewer.nbins = %d" % self.nbins) def onRadiiScaleChanged(self, val): self.radii_scale = val self.NGL_HKL_command(""" NGL_HKLviewer.viewer { nth_power_scale_radii = %f scale = %f } """ % (self.nth_power_scale, self.radii_scale)) def onPowerScaleChanged(self, val): self.nth_power_scale = val self.NGL_HKL_command(""" NGL_HKLviewer.viewer { nth_power_scale_radii = %f scale = %f } """ % (self.nth_power_scale, self.radii_scale)) def onManualPowerScale(self): if self.ManualPowerScalecheckbox.isChecked(): self.NGL_HKL_command( 'NGL_HKLviewer.viewer.nth_power_scale_radii = %f' % self.nth_power_scale) self.power_scale_spinBox.setEnabled(True) else: self.NGL_HKL_command( 'NGL_HKLviewer.viewer.nth_power_scale_radii = -1.0') self.power_scale_spinBox.setEnabled(False) self.nth_power_scale = -1.0 def OpenReflectionsFile(self): options = QFileDialog.Options() fileName, filtr = QFileDialog.getOpenFileName( self, "Load reflections file", "", "All Files (*);;MTZ Files (*.mtz);;CIF (*.cif)", "", options) if fileName: self.HKLnameedit.setText(fileName) #self.infostr = "" self.textInfo.setPlainText("") self.fileisvalid = False self.NGL_HKL_command('NGL_HKLviewer.filename = "%s"' % fileName) self.MillerComboBox.clear() self.BinDataComboBox.clear() def createExpansionBox(self): self.SpaceGroupComboBox = QComboBox() self.SpaceGroupComboBox.activated.connect(self.SpacegroupSelchange) self.SpacegroupLabel = QLabel() self.SpacegroupLabel.setText("Space Subgroups") self.mergecheckbox = QCheckBox() self.mergecheckbox.setText("Merge data") #self.mergecheckbox.setTristate (True) self.mergecheckbox.clicked.connect(self.MergeData) self.expandP1checkbox = QCheckBox() self.expandP1checkbox.setText("Expand to P1") self.expandP1checkbox.clicked.connect(self.ExpandToP1) self.expandAnomalouscheckbox = QCheckBox() self.expandAnomalouscheckbox.setText("Show Friedel pairs") self.expandAnomalouscheckbox.clicked.connect(self.ExpandAnomalous) self.sysabsentcheckbox = QCheckBox() self.sysabsentcheckbox.setText("Show Systematic Absences") self.sysabsentcheckbox.clicked.connect(self.showSysAbsent) self.missingcheckbox = QCheckBox() self.missingcheckbox.setText("Show Missing") self.missingcheckbox.clicked.connect(self.showMissing) self.onlymissingcheckbox = QCheckBox() self.onlymissingcheckbox.setText("Only Show Missing") self.onlymissingcheckbox.clicked.connect(self.showOnlyMissing) self.ExpansionBox = QGroupBox("Expansions") layout = QGridLayout() layout.addWidget(self.SpacegroupLabel, 0, 0) layout.addWidget(self.SpaceGroupComboBox, 0, 1) #layout.addWidget(self.mergecheckbox, 1, 0) layout.addWidget(self.expandP1checkbox, 1, 0) layout.addWidget(self.expandAnomalouscheckbox, 1, 1) layout.addWidget(self.sysabsentcheckbox, 2, 0) layout.addWidget(self.missingcheckbox, 3, 0) layout.addWidget(self.onlymissingcheckbox, 3, 1) layout.setRowStretch(0, 0) layout.setRowStretch(1, 0) layout.setRowStretch(2, 0) layout.setRowStretch(3, 1) self.ExpansionBox.setLayout(layout) def CreateSliceTabs(self): self.showslicecheckbox = QCheckBox() self.showslicecheckbox.setText("Show Slice") self.showslicecheckbox.clicked.connect(self.showSlice) self.sliceindexspinBox = QDoubleSpinBox() self.sliceindex = 0 self.sliceindexspinBox.setValue(self.sliceindex) self.sliceindexspinBox.setDecimals(0) self.sliceindexspinBox.setSingleStep(1) self.sliceindexspinBox.setRange(0, 20) self.sliceindexspinBox.valueChanged.connect(self.onSliceIndexChanged) self.SliceLabelComboBox = QComboBox() self.SliceLabelComboBox.activated.connect(self.onSliceComboSelchange) self.sliceaxis = ["h", "k", "l"] self.SliceLabelComboBox.addItems(self.sliceaxis) self.sliceTabWidget = QTabWidget() tab1 = QWidget() layout1 = QGridLayout() layout1.addWidget(self.showslicecheckbox, 0, 0, 1, 1) layout1.addWidget(self.SliceLabelComboBox, 0, 1, 1, 1) layout1.addWidget(self.sliceindexspinBox, 0, 2, 1, 1) tab1.setLayout(layout1) tab2 = QWidget() layout2 = QGridLayout() self.hvec_spinBox = QDoubleSpinBox(self.sliceTabWidget) self.hvecval = 2.0 self.hvec_spinBox.setValue(self.hvecval) self.hvec_spinBox.setDecimals(2) self.hvec_spinBox.setSingleStep(0.5) self.hvec_spinBox.setRange(-100.0, 10.0) self.hvec_spinBox.valueChanged.connect(self.onHvecChanged) self.hvec_Label = QLabel() self.hvec_Label.setText("H") layout2.addWidget(self.hvec_Label, 0, 0, 1, 1) layout2.addWidget(self.hvec_spinBox, 0, 1, 1, 1) self.kvec_spinBox = QDoubleSpinBox(self.sliceTabWidget) self.kvecval = 0.0 self.kvec_spinBox.setValue(self.kvecval) self.kvec_spinBox.setDecimals(2) self.kvec_spinBox.setSingleStep(0.5) self.kvec_spinBox.setRange(-100.0, 100.0) self.kvec_spinBox.valueChanged.connect(self.onKvecChanged) self.kvec_Label = QLabel() self.kvec_Label.setText("K") layout2.addWidget(self.kvec_Label, 1, 0, 1, 1) layout2.addWidget(self.kvec_spinBox, 1, 1, 1, 1) self.lvec_spinBox = QDoubleSpinBox(self.sliceTabWidget) self.lvecval = 0.0 self.lvec_spinBox.setValue(self.lvecval) self.lvec_spinBox.setDecimals(2) self.lvec_spinBox.setSingleStep(0.5) self.lvec_spinBox.setRange(-100.0, 100.0) self.lvec_spinBox.valueChanged.connect(self.onLvecChanged) self.lvec_Label = QLabel() self.lvec_Label.setText("L") layout2.addWidget(self.lvec_Label, 2, 0, 1, 1) layout2.addWidget(self.lvec_spinBox, 2, 1, 1, 1) self.hkldist_spinBox = QDoubleSpinBox(self.sliceTabWidget) self.hkldistval = 0.0 self.hkldist_spinBox.setValue(self.hkldistval) self.hkldist_spinBox.setDecimals(2) self.hkldist_spinBox.setSingleStep(0.5) self.hkldist_spinBox.setRange(-100.0, 100.0) self.hkldist_spinBox.valueChanged.connect(self.onHKLdistChanged) self.hkldist_Label = QLabel() self.hkldist_Label.setText("Distance from Origin") layout2.addWidget(self.hkldist_Label, 3, 0, 1, 1) layout2.addWidget(self.hkldist_spinBox, 3, 1, 1, 1) self.clipwidth_spinBox = QDoubleSpinBox(self.sliceTabWidget) self.clipwidthval = 0.5 self.clipwidth_spinBox.setValue(self.clipwidthval) self.clipwidth_spinBox.setDecimals(2) self.clipwidth_spinBox.setSingleStep(0.05) self.clipwidth_spinBox.setRange(0.0, 100.0) self.clipwidth_spinBox.valueChanged.connect(self.onClipwidthChanged) self.clipwidth_Label = QLabel() self.clipwidth_Label.setText("Clip Plane Width") layout2.addWidget(self.clipwidth_Label, 4, 0, 1, 1) layout2.addWidget(self.clipwidth_spinBox, 4, 1, 1, 1) self.ClipBox = QGroupBox("Normal Vector to Clip Plane") self.ClipBox.setLayout(layout2) layout3 = QGridLayout() self.ClipPlaneChkBox = QCheckBox(self.sliceTabWidget) self.ClipPlaneChkBox.setText( "Use clip plane normal to HKL vector pointing out") self.ClipPlaneChkBox.clicked.connect(self.onClipPlaneChkBox) layout3.addWidget(self.ClipPlaneChkBox, 0, 0) layout3.addWidget(self.ClipBox, 1, 0) tab2.setLayout(layout3) self.sliceTabWidget.addTab(tab1, "Explicit Slicing") self.sliceTabWidget.addTab(tab2, "Clip Plane Slicing") self.ClipBox.setDisabled(True) def onClipPlaneChkBox(self): if self.ClipPlaneChkBox.isChecked(): self.ClipBox.setDisabled(False) philstr = """NGL_HKLviewer.normal_clip_plane { h = %s k = %s l = %s hkldist = %s clipwidth = %s } NGL_HKLviewer.viewer.NGL.fixorientation = %s """ %(self.hvecval, self.kvecval, self.lvecval, self.hkldistval, self.clipwidthval, \ str(self.fixedorientcheckbox.isChecked()) ) self.NGL_HKL_command(philstr) else: self.ClipBox.setDisabled(True) self.NGL_HKL_command( "NGL_HKLviewer.normal_clip_plane.clipwidth = None") def onClipwidthChanged(self, val): self.clipwidthval = val self.NGL_HKL_command("NGL_HKLviewer.normal_clip_plane.clipwidth = %f" % self.clipwidthval) def onHKLdistChanged(self, val): self.hkldistval = val self.NGL_HKL_command("NGL_HKLviewer.normal_clip_plane.hkldist = %f" % self.hkldistval) def onHvecChanged(self, val): self.hvecval = val self.NGL_HKL_command("NGL_HKLviewer.normal_clip_plane.h = %f" % self.hvecval) def onKvecChanged(self, val): self.kvecval = val self.NGL_HKL_command("NGL_HKLviewer.normal_clip_plane.k = %f" % self.kvecval) def onLvecChanged(self, val): self.lvecval = val self.NGL_HKL_command("NGL_HKLviewer.normal_clip_plane.l = %f" % self.lvecval) def onFixedorient(self): self.NGL_HKL_command('NGL_HKLviewer.viewer.NGL.fixorientation = %s' \ %str(self.fixedorientcheckbox.isChecked())) def onMillerComboSelchange(self, i): self.NGL_HKL_command("NGL_HKLviewer.scene_id = %d" % i) #self.MillerComboBox.setCurrentIndex(i) if self.MillerComboBox.currentText(): self.functionTabWidget.setEnabled(True) self.expandAnomalouscheckbox.setEnabled(True) # don' allow anomalous expansion for data that's already anomalous for arrayinfo in self.array_infotpls: isanomalous = arrayinfo[-1] label = arrayinfo[0] if isanomalous and label == self.MillerComboBox.currentText( )[:len(label)]: self.expandAnomalouscheckbox.setDisabled(True) else: self.functionTabWidget.setDisabled(True) self.SpaceGroupComboBox.clear() self.SpaceGroupComboBox.addItems(self.spacegroups) # need to supply issymunique flag in infotuple #if self.hklscenes_arrays[ i ][6] == 0: # self.mergecheckbox.setEnabled(True) #else: # self.mergecheckbox.setEnabled(False) def createFileInfoBox(self): self.FileInfoBox = QGroupBox("Reflection File Information") layout = QGridLayout() layout.addWidget(self.openFileNameButton, 0, 0, 1, 2) if self.devmode: layout.addWidget(self.debugbutton, 0, 2, 1, 1) layout.addWidget(self.HKLnameedit, 1, 0, 1, 3) layout.addWidget(self.millertable, 2, 0, 1, 3) layout.addWidget(self.textInfo, 3, 0, 1, 3) #layout.setColumnStretch(1, 2) self.FileInfoBox.setLayout(layout) def createRadiiScaleGroupBox(self): self.RadiiScaleGroupBox = QGroupBox("Radii Size of HKL Spheres") self.ManualPowerScalecheckbox = QCheckBox() self.ManualPowerScalecheckbox.setText( "Manual Power Scaling of Sphere Radii") self.ManualPowerScalecheckbox.clicked.connect(self.onManualPowerScale) self.power_scale_spinBox = QDoubleSpinBox(self.RadiiScaleGroupBox) self.nth_power_scale = 0.5 self.power_scale_spinBox.setValue(self.nth_power_scale) self.power_scale_spinBox.setDecimals(2) self.power_scale_spinBox.setSingleStep(0.05) self.power_scale_spinBox.setRange(0.0, 1.0) self.power_scale_spinBox.valueChanged.connect(self.onPowerScaleChanged) self.power_scale_spinBox.setEnabled(False) self.powerscaleLabel = QLabel() self.powerscaleLabel.setText("Power scale Factor") self.radii_scale_spinBox = QDoubleSpinBox(self.RadiiScaleGroupBox) self.radii_scale = 1.0 self.radii_scale_spinBox.setValue(self.radii_scale) self.radii_scale_spinBox.setDecimals(1) self.radii_scale_spinBox.setSingleStep(0.1) self.radii_scale_spinBox.setRange(0.2, 2.0) self.radii_scale_spinBox.valueChanged.connect(self.onRadiiScaleChanged) self.radiiscaleLabel = QLabel() self.radiiscaleLabel.setText("Linear Scale Factor") layout = QGridLayout() layout.addWidget(self.ManualPowerScalecheckbox, 1, 0, 1, 2) layout.addWidget(self.powerscaleLabel, 2, 0, 1, 2) layout.addWidget(self.power_scale_spinBox, 2, 1, 1, 2) layout.addWidget(self.radiiscaleLabel, 3, 0, 1, 2) layout.addWidget(self.radii_scale_spinBox, 3, 1, 1, 2) layout.setColumnStretch(0, 1) layout.setColumnStretch(1, 0) self.RadiiScaleGroupBox.setLayout(layout) def createBinsBox(self): self.binstable = QTableWidget(0, 4) self.binstable_isready = False labels = [ "no. of HKLs", "lower bin value", "upper bin value", "opacity" ] self.binstable.setHorizontalHeaderLabels(labels) self.binstable.horizontalHeader().setDefaultAlignment(Qt.AlignLeft) self.bindata_labeltxt = QLabel() self.bindata_labeltxt.setText("Data binned:") self.Nbins_spinBox = QSpinBox() self.Nbins_spinBox.setSingleStep(1) self.Nbins_spinBox.setRange(1, 40) self.Nbins_spinBox.valueChanged.connect(self.onNbinsChanged) self.Nbins_labeltxt = QLabel() self.Nbins_labeltxt.setText("Number of bins:") self.OpaqueAllCheckbox = QCheckBox() #self.OpaqueAllCheckbox.setTristate() self.OpaqueAllCheckbox.setText("Show all data in bins") self.OpaqueAllCheckbox.clicked.connect(self.onOpaqueAll) self.binstable.itemChanged.connect(self.onBinsTableItemChanged) self.binstable.itemSelectionChanged.connect( self.onBinsTableItemSelectionChanged) self.BinDataComboBox = QComboBox() self.BinDataComboBox.activated.connect(self.onBindataComboSelchange) self.BinsGroupBox = QGroupBox("Bins") layout = QGridLayout() layout.addWidget(self.bindata_labeltxt, 0, 0) layout.addWidget(self.BinDataComboBox, 0, 1) layout.addWidget(self.Nbins_labeltxt, 0, 2) layout.addWidget(self.Nbins_spinBox, 0, 3) layout.addWidget(self.OpaqueAllCheckbox, 1, 2) layout.addWidget(self.binstable, 2, 0, 1, 4) layout.setColumnStretch(0, 0) layout.setColumnStretch(1, 2) layout.setColumnStretch(3, 1) self.BinsGroupBox.setLayout(layout) def DebugInteractively(self): import code, traceback code.interact(local=locals(), banner="".join(traceback.format_stack(limit=10))) def CreateFunctionTabs(self): self.functionTabWidget = QTabWidget() tab1 = QWidget() layout1 = QGridLayout() layout1.addWidget(self.ExpansionBox, 0, 0) layout1.setRowStretch(0, 0) tab1.setLayout(layout1) tab2 = QWidget() layout2 = QGridLayout() self.fixedorientcheckbox = QCheckBox(self.sliceTabWidget) self.fixedorientcheckbox.setText( "Fix orientation but allow zoom and translation") self.fixedorientcheckbox.clicked.connect(self.onFixedorient) layout2.addWidget(self.fixedorientcheckbox, 0, 0) layout2.addWidget(self.sliceTabWidget, 1, 0) tab2.setLayout(layout2) tab3 = QWidget() layout3 = QGridLayout() layout3.addWidget(self.RadiiScaleGroupBox, 0, 0) tab3.setLayout(layout3) tab4 = QWidget() layout4 = QGridLayout() layout4.addWidget(self.BinsGroupBox, 0, 0) tab4.setLayout(layout4) self.functionTabWidget.addTab(tab1, "Expand") self.functionTabWidget.addTab(tab2, "Slice") self.functionTabWidget.addTab(tab3, "Size") self.functionTabWidget.addTab(tab4, "Bins") self.functionTabWidget.setDisabled(True) def SpacegroupSelchange(self, i): self.NGL_HKL_command("NGL_HKLviewer.spacegroup_choice = %d" % i) def find_free_port(self): import socket s = socket.socket() s.bind(('', 0)) # Bind to a free port provided by the host. port = s.getsockname()[1] s.close() return port def LaunchCCTBXPython(self): self.sockport = self.find_free_port() self.zmq_context = zmq.Context() self.socket = self.zmq_context.socket(zmq.PAIR) self.socket.bind("tcp://127.0.0.1:%s" % self.sockport) try: msg = self.socket.recv( flags=zmq.NOBLOCK) #To empty the socket from previous messages except Exception as e: pass cmdargs = 'cctbx.python.bat -i -c "from crys3d.hklview import cmdlineframes;' \ + ' myHKLview = cmdlineframes.HKLViewFrame(useGuiSocket=%s, high_quality=True,' %self.sockport \ + ' jscriptfname = \'%s\', ' %self.jscriptfname \ + ' verbose=%s, UseOSBrowser= %s )"\n' %(self.verbose, str(self.UseOSbrowser)) self.cctbxproc = subprocess.Popen(cmdargs, shell=True, stdin=subprocess.PIPE, stdout=sys.stdout, stderr=sys.stderr) #time.sleep(1) def NGL_HKL_command(self, cmdstr): #print("sending:\n" + cmdstr) self.socket.send(bytes(cmdstr, "utf-8"))
class DomainDock(PlotterDock): """ Domain options dock """ def __init__(self, model, font_metric, parent=None): super().__init__(model, font_metric, parent) self.setAllowedAreas(QtCore.Qt.LeftDockWidgetArea) # Create Controls self._createOriginBox() self._createOptionsBox() self._createResolutionBox() # Create submit button self.applyButton = QPushButton("Apply Changes") # Mac bug fix self.applyButton.setMinimumHeight(self.font_metric.height() * 1.6) self.applyButton.clicked.connect(self.main_window.applyChanges) # Create Zoom box self.zoomBox = QSpinBox() self.zoomBox.setSuffix(' %') self.zoomBox.setRange(25, 2000) self.zoomBox.setValue(100) self.zoomBox.setSingleStep(25) self.zoomBox.valueChanged.connect(self.main_window.editZoom) self.zoomLayout = QHBoxLayout() self.zoomLayout.addWidget(QLabel('Zoom:')) self.zoomLayout.addWidget(self.zoomBox) self.zoomLayout.setContentsMargins(0, 0, 0, 0) self.zoomWidget = QWidget() self.zoomWidget.setLayout(self.zoomLayout) # Create Layout self.dockLayout = QVBoxLayout() self.dockLayout.addWidget(QLabel("Geometry/Properties")) self.dockLayout.addWidget(HorizontalLine()) self.dockLayout.addWidget(self.originGroupBox) self.dockLayout.addWidget(self.optionsGroupBox) self.dockLayout.addWidget(self.resGroupBox) self.dockLayout.addWidget(HorizontalLine()) self.dockLayout.addWidget(self.zoomWidget) self.dockLayout.addWidget(HorizontalLine()) self.dockLayout.addStretch() self.dockLayout.addWidget(self.applyButton) self.dockLayout.addWidget(HorizontalLine()) self.optionsWidget = QWidget() self.optionsWidget.setLayout(self.dockLayout) self.setWidget(self.optionsWidget) def _createOriginBox(self): # X Origin self.xOrBox = QDoubleSpinBox() self.xOrBox.setDecimals(9) self.xOrBox.setRange(-99999, 99999) xbox_connector = partial(self.main_window.editSingleOrigin, dimension=0) self.xOrBox.valueChanged.connect(xbox_connector) # Y Origin self.yOrBox = QDoubleSpinBox() self.yOrBox.setDecimals(9) self.yOrBox.setRange(-99999, 99999) ybox_connector = partial(self.main_window.editSingleOrigin, dimension=1) self.yOrBox.valueChanged.connect(ybox_connector) # Z Origin self.zOrBox = QDoubleSpinBox() self.zOrBox.setDecimals(9) self.zOrBox.setRange(-99999, 99999) zbox_connector = partial(self.main_window.editSingleOrigin, dimension=2) self.zOrBox.valueChanged.connect(zbox_connector) # Origin Form Layout self.orLayout = QFormLayout() self.orLayout.addRow('X:', self.xOrBox) self.orLayout.addRow('Y:', self.yOrBox) self.orLayout.addRow('Z:', self.zOrBox) self.orLayout.setLabelAlignment(QtCore.Qt.AlignLeft) self.orLayout.setFieldGrowthPolicy(QFormLayout.AllNonFixedFieldsGrow) # Origin Group Box self.originGroupBox = QGroupBox('Origin') self.originGroupBox.setLayout(self.orLayout) def _createOptionsBox(self): # Width self.widthBox = QDoubleSpinBox(self) self.widthBox.setRange(.1, 99999) self.widthBox.setDecimals(9) self.widthBox.valueChanged.connect(self.main_window.editWidth) # Height self.heightBox = QDoubleSpinBox(self) self.heightBox.setRange(.1, 99999) self.heightBox.setDecimals(9) self.heightBox.valueChanged.connect(self.main_window.editHeight) # ColorBy self.colorbyBox = QComboBox(self) self.colorbyBox.addItem("material") self.colorbyBox.addItem("cell") self.colorbyBox.addItem("temperature") self.colorbyBox.addItem("density") self.colorbyBox.currentTextChanged[str].connect( self.main_window.editColorBy) # Universe level (applies to cell coloring only) self.universeLevelBox = QComboBox(self) self.universeLevelBox.addItem('all') for i in range(self.model.max_universe_levels): self.universeLevelBox.addItem(str(i)) self.universeLevelBox.currentTextChanged[str].connect( self.main_window.editUniverseLevel) # Alpha self.domainAlphaBox = QDoubleSpinBox(self) self.domainAlphaBox.setValue(self.model.activeView.domainAlpha) self.domainAlphaBox.setSingleStep(0.05) self.domainAlphaBox.setDecimals(2) self.domainAlphaBox.setRange(0.0, 1.0) self.domainAlphaBox.valueChanged.connect(self.main_window.editPlotAlpha) # Visibility self.visibilityBox = QCheckBox(self) self.visibilityBox.stateChanged.connect( self.main_window.editPlotVisibility) # Outlines self.outlinesBox = QCheckBox(self) self.outlinesBox.stateChanged.connect(self.main_window.toggleOutlines) # Basis self.basisBox = QComboBox(self) self.basisBox.addItem("xy") self.basisBox.addItem("xz") self.basisBox.addItem("yz") self.basisBox.currentTextChanged.connect(self.main_window.editBasis) # Advanced Color Options self.colorOptionsButton = QPushButton('Color Options...') self.colorOptionsButton.setMinimumHeight(self.font_metric.height() * 1.6) self.colorOptionsButton.clicked.connect(self.main_window.showColorDialog) # Options Form Layout self.opLayout = QFormLayout() self.opLayout.addRow('Width:', self.widthBox) self.opLayout.addRow('Height:', self.heightBox) self.opLayout.addRow('Basis:', self.basisBox) self.opLayout.addRow('Color By:', self.colorbyBox) self.opLayout.addRow('Universe Level:', self.universeLevelBox) self.opLayout.addRow('Plot alpha:', self.domainAlphaBox) self.opLayout.addRow('Visible:', self.visibilityBox) self.opLayout.addRow('Outlines:', self.outlinesBox) self.opLayout.addRow(self.colorOptionsButton) self.opLayout.setLabelAlignment(QtCore.Qt.AlignLeft) self.opLayout.setFieldGrowthPolicy(QFormLayout.AllNonFixedFieldsGrow) # Options Group Box self.optionsGroupBox = QGroupBox('Options') self.optionsGroupBox.setLayout(self.opLayout) def _createResolutionBox(self): # Horizontal Resolution self.hResBox = QSpinBox(self) self.hResBox.setRange(1, 99999) self.hResBox.setSingleStep(25) self.hResBox.setSuffix(' px') self.hResBox.valueChanged.connect(self.main_window.editHRes) # Vertical Resolution self.vResLabel = QLabel('Pixel Height:') self.vResBox = QSpinBox(self) self.vResBox.setRange(1, 99999) self.vResBox.setSingleStep(25) self.vResBox.setSuffix(' px') self.vResBox.valueChanged.connect(self.main_window.editVRes) # Ratio checkbox self.ratioCheck = QCheckBox("Fixed Aspect Ratio", self) self.ratioCheck.stateChanged.connect(self.main_window.toggleAspectLock) # Resolution Form Layout self.resLayout = QFormLayout() self.resLayout.addRow(self.ratioCheck) self.resLayout.addRow('Pixel Width:', self.hResBox) self.resLayout.addRow(self.vResLabel, self.vResBox) self.resLayout.setLabelAlignment(QtCore.Qt.AlignLeft) self.resLayout.setFieldGrowthPolicy(QFormLayout.AllNonFixedFieldsGrow) # Resolution Group Box self.resGroupBox = QGroupBox("Resolution") self.resGroupBox.setLayout(self.resLayout) def updateDock(self): self.updateOrigin() self.updateWidth() self.updateHeight() self.updateColorBy() self.updateUniverseLevel() self.updatePlotAlpha() self.updatePlotVisibility() self.updateOutlines() self.updateBasis() self.updateAspectLock() self.updateHRes() self.updateVRes() def updateOrigin(self): self.xOrBox.setValue(self.model.activeView.origin[0]) self.yOrBox.setValue(self.model.activeView.origin[1]) self.zOrBox.setValue(self.model.activeView.origin[2]) def updateWidth(self): self.widthBox.setValue(self.model.activeView.width) def updateHeight(self): self.heightBox.setValue(self.model.activeView.height) def updateColorBy(self): self.colorbyBox.setCurrentText(self.model.activeView.colorby) if self.model.activeView.colorby != 'cell': self.universeLevelBox.setEnabled(False) else: self.universeLevelBox.setEnabled(True) def updateUniverseLevel(self): self.universeLevelBox.setCurrentIndex(self.model.activeView.level + 1) def updatePlotAlpha(self): self.domainAlphaBox.setValue(self.model.activeView.domainAlpha) def updatePlotVisibility(self): self.visibilityBox.setChecked(self.model.activeView.domainVisible) def updateOutlines(self): self.outlinesBox.setChecked(self.model.activeView.outlines) def updateBasis(self): self.basisBox.setCurrentText(self.model.activeView.basis) def updateAspectLock(self): aspect_lock = bool(self.model.activeView.aspectLock) self.ratioCheck.setChecked(aspect_lock) self.vResBox.setDisabled(aspect_lock) self.vResLabel.setDisabled(aspect_lock) def updateHRes(self): self.hResBox.setValue(self.model.activeView.h_res) def updateVRes(self): self.vResBox.setValue(self.model.activeView.v_res) def revertToCurrent(self): cv = self.model.currentView self.xOrBox.setValue(cv.origin[0]) self.yOrBox.setValue(cv.origin[1]) self.zOrBox.setValue(cv.origin[2]) self.widthBox.setValue(cv.width) self.heightBox.setValue(cv.height) def resizeEvent(self, event): self.main_window.resizeEvent(event) hideEvent = showEvent = moveEvent = resizeEvent
class OptionsDock(QDockWidget): def __init__(self, model, FM, parent=None): super(OptionsDock, self).__init__(parent) self.model = model self.FM = FM self.mw = parent self.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Expanding) # Doesn't work? self.setAllowedAreas(QtCore.Qt.LeftDockWidgetArea | QtCore.Qt.RightDockWidgetArea) # Create Controls self.createOriginBox() self.createOptionsBox() self.createResolutionBox() # Create submit button self.applyButton = QPushButton("Apply Changes") self.applyButton.setMinimumHeight(self.FM.height() * 1.6) # Mac bug fix self.applyButton.clicked.connect(self.mw.applyChanges) # Create Zoom box self.zoomBox = QSpinBox() self.zoomBox.setSuffix(' %') self.zoomBox.setRange(25, 2000) self.zoomBox.setValue(100) self.zoomBox.setSingleStep(25) self.zoomBox.valueChanged.connect(self.mw.editZoom) self.zoomLayout = QHBoxLayout() self.zoomLayout.addWidget(QLabel('Zoom:')) self.zoomLayout.addWidget(self.zoomBox) self.zoomLayout.setContentsMargins(0, 0, 0, 0) self.zoomWidget = QWidget() self.zoomWidget.setLayout(self.zoomLayout) # Create Layout self.dockLayout = QVBoxLayout() self.dockLayout.addWidget(self.originGroupBox) self.dockLayout.addWidget(self.optionsGroupBox) self.dockLayout.addWidget(self.resGroupBox) self.dockLayout.addWidget(self.applyButton) self.dockLayout.addStretch() self.dockLayout.addWidget(HorizontalLine()) self.dockLayout.addWidget(self.zoomWidget) self.optionsWidget = QWidget() self.optionsWidget.setLayout(self.dockLayout) self.setWidget(self.optionsWidget) def createOriginBox(self): # X Origin self.xOrBox = QDoubleSpinBox() self.xOrBox.setDecimals(9) self.xOrBox.setRange(-99999, 99999) self.xOrBox.valueChanged.connect( lambda value: self.mw.editSingleOrigin(value, 0)) # Y Origin self.yOrBox = QDoubleSpinBox() self.yOrBox.setDecimals(9) self.yOrBox.setRange(-99999, 99999) self.yOrBox.valueChanged.connect( lambda value: self.mw.editSingleOrigin(value, 1)) # Z Origin self.zOrBox = QDoubleSpinBox() self.zOrBox.setDecimals(9) self.zOrBox.setRange(-99999, 99999) self.zOrBox.valueChanged.connect( lambda value: self.mw.editSingleOrigin(value, 2)) # Origin Form Layout self.orLayout = QFormLayout() self.orLayout.addRow('X:', self.xOrBox) self.orLayout.addRow('Y:', self.yOrBox) self.orLayout.addRow('Z:', self.zOrBox) #self.orLayout.setVerticalSpacing(4) self.orLayout.setLabelAlignment(QtCore.Qt.AlignLeft) self.orLayout.setFieldGrowthPolicy(QFormLayout.AllNonFixedFieldsGrow) # Origin Group Box self.originGroupBox = QGroupBox('Origin') self.originGroupBox.setLayout(self.orLayout) def createOptionsBox(self): # Width self.widthBox = QDoubleSpinBox(self) self.widthBox.setRange(.1, 99999) self.widthBox.valueChanged.connect(self.mw.editWidth) # Height self.heightBox = QDoubleSpinBox(self) self.heightBox.setRange(.1, 99999) self.heightBox.valueChanged.connect(self.mw.editHeight) # ColorBy self.colorbyBox = QComboBox(self) self.colorbyBox.addItem("material") self.colorbyBox.addItem("cell") self.colorbyBox.currentTextChanged[str].connect(self.mw.editColorBy) # Alpha self.plotAlphaBox = QDoubleSpinBox(self) self.plotAlphaBox.setValue(self.model.activeView.plotAlpha) self.plotAlphaBox.setSingleStep(0.05) self.plotAlphaBox.setDecimals(2) self.plotAlphaBox.setRange(0.0, 1.0) self.plotAlphaBox.valueChanged.connect(self.mw.editPlotAlpha) # Basis self.basisBox = QComboBox(self) self.basisBox.addItem("xy") self.basisBox.addItem("xz") self.basisBox.addItem("yz") self.basisBox.currentTextChanged.connect(self.mw.editBasis) # Advanced Color Options self.colorOptionsButton = QPushButton('Color Options...') self.colorOptionsButton.setMinimumHeight(self.FM.height() * 1.6) self.colorOptionsButton.clicked.connect(self.mw.showColorDialog) # Options Form Layout self.opLayout = QFormLayout() self.opLayout.addRow('Width:', self.widthBox) self.opLayout.addRow('Height:', self.heightBox) self.opLayout.addRow('Basis:', self.basisBox) self.opLayout.addRow('Color By:', self.colorbyBox) self.opLayout.addRow('Plot alpha:', self.plotAlphaBox) self.opLayout.addRow(self.colorOptionsButton) self.opLayout.setLabelAlignment(QtCore.Qt.AlignLeft) self.opLayout.setFieldGrowthPolicy(QFormLayout.AllNonFixedFieldsGrow) # Options Group Box self.optionsGroupBox = QGroupBox('Options') self.optionsGroupBox.setLayout(self.opLayout) def createResolutionBox(self): # Horizontal Resolution self.hResBox = QSpinBox(self) self.hResBox.setRange(1, 99999) self.hResBox.setSingleStep(25) self.hResBox.setSuffix(' px') self.hResBox.valueChanged.connect(self.mw.editHRes) # Vertical Resolution self.vResLabel = QLabel('Pixel Height:') self.vResBox = QSpinBox(self) self.vResBox.setRange(1, 99999) self.vResBox.setSingleStep(25) self.vResBox.setSuffix(' px') self.vResBox.valueChanged.connect(self.mw.editVRes) # Ratio checkbox self.ratioCheck = QCheckBox("Fixed Aspect Ratio", self) self.ratioCheck.stateChanged.connect(self.mw.toggleAspectLock) # Resolution Form Layout self.resLayout = QFormLayout() self.resLayout.addRow(self.ratioCheck) self.resLayout.addRow('Pixel Width:', self.hResBox) self.resLayout.addRow(self.vResLabel, self.vResBox) self.resLayout.setLabelAlignment(QtCore.Qt.AlignLeft) self.resLayout.setFieldGrowthPolicy(QFormLayout.AllNonFixedFieldsGrow) # Resolution Group Box self.resGroupBox = QGroupBox("Resolution") self.resGroupBox.setLayout(self.resLayout) def updateDock(self): self.updateOrigin() self.updateWidth() self.updateHeight() self.updateColorBy() self.updatePlotAlpha() self.updateBasis() self.updateAspectLock() self.updateHRes() self.updateVRes() def updateOrigin(self): self.xOrBox.setValue(self.model.activeView.origin[0]) self.yOrBox.setValue(self.model.activeView.origin[1]) self.zOrBox.setValue(self.model.activeView.origin[2]) def updateWidth(self): self.widthBox.setValue(self.model.activeView.width) def updateHeight(self): self.heightBox.setValue(self.model.activeView.height) def updateColorBy(self): self.colorbyBox.setCurrentText(self.model.activeView.colorby) def updatePlotAlpha(self): self.plotAlphaBox.setValue(self.model.activeView.plotAlpha) def updateBasis(self): self.basisBox.setCurrentText(self.model.activeView.basis) def updateAspectLock(self): if self.model.activeView.aspectLock: self.ratioCheck.setChecked(True) self.vResBox.setDisabled(True) self.vResLabel.setDisabled(True) else: self.ratioCheck.setChecked(False) self.vResBox.setDisabled(False) self.vResLabel.setDisabled(False) def updateHRes(self): self.hResBox.setValue(self.model.activeView.h_res) def updateVRes(self): self.vResBox.setValue(self.model.activeView.v_res) def revertToCurrent(self): cv = self.model.currentView self.xOrBox.setValue(cv.origin[0]) self.yOrBox.setValue(cv.origin[1]) self.zOrBox.setValue(cv.origin[2]) self.widthBox.setValue(cv.width) self.heightBox.setValue(cv.height) def resizeEvent(self, event): self.mw.resizeEvent(event) def hideEvent(self, event): self.mw.resizeEvent(event) def showEvent(self, event): self.mw.resizeEvent(event) def moveEvent(self, event): self.mw.resizeEvent(event)
class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): QToolTip.setFont(QFont('SansSerif', 12)) Title = QLabel('Выберите параметры обучения нейросети') Title.setToolTip('Для корректной работы функций обучения и оптимизации \n' 'в каталоге с программой должны находится подкаталоги \n' 'train, val, test. В каждом из этих каталогов должно \n' 'быть столько подкаталогов с изображениями, сколько \n' 'необходимо распознавать классов') Title1 = QLabel('Выберите параметры исследования изображения') Title1.setToolTip('Для корректной работы функции классификации и локализации \n' 'изображения для исследования и модель нейросети должны \n' 'находиться в одном каталоге с программой, формат изображения \n' '.jpg, формат модели нейросети .h5, указывать формат в полях не нужно') TitleEp = QLabel('Эпохи:') TitleEp.setToolTip('Эпоха — один «проход» данных через нейросеть. \n' 'Изменение этого параметра позволяет избежать \n' 'недообученности или переобученности нейросети') TitleBa = QLabel('Размер мини-выборки:') TitleBa.setToolTip('Количество изображений загружаемых в нейросеть ' 'за раз') TitleTr = QLabel('Количество изображений для обучения:') TitleTr.setToolTip('Количество изображений во всех подкаталогах ' 'каталога train') TitleVa = QLabel('Количество изображений для проверки:') TitleVa.setToolTip('Количество изображений во всех подкаталогах ' 'каталога val') TitleTe = QLabel('Количество изображений для тестирования:') TitleTe.setToolTip('Количество изображений во всех подкаталогах ' 'каталога test') TitleSc = QLabel('Масштабирование пирамиды изображений:') TitleSc.setToolTip('Коэффициент масштабирования изображения') TitleSs = QLabel('Шаг скользящего окна:') TitleSs.setToolTip('Расстояние в пикселях, на которое смещается \n' 'окно классификатора по изображению за одну итерацию') TitleNi = QLabel('Имя изображения для исследования:') TitleNi.setToolTip('Наименование изображения без расширения. \n' 'Изображение должно иметь формат jpg и \n' 'находится в одном каталоге с программой, иначе \n' 'необходимо указывать полный путь до изображения') TitleNm = QLabel('Имя модели нейросети:') TitleNm.setToolTip('Наименование модели нейросети без расширения. \n' 'Модель должна иметь формат h5 и находится \n' 'в одном каталоге с программой, иначе необходимо \n' 'указывать полный путь до модели нейросети') TitleCl = QLabel('Количество классов:') self.InputEpochs = QSpinBox(self) self.InputEpochs.setRange(5,50) self.InputEpochs.setValue(10) self.InputBatch = QSpinBox(self) self.InputBatch.setRange(5,100) self.InputBatch.setValue(15) self.InputTrain = QSpinBox(self) self.InputTrain.setRange(100,100000) self.InputTrain.setValue(1050) self.InputValidation = QSpinBox(self) self.InputValidation.setRange(100,100000) self.InputValidation.setValue(225) self.InputTest = QSpinBox(self) self.InputTest.setRange(100,100000) self.InputTest.setValue(225) self.InputScale = QDoubleSpinBox(self) self.InputScale.setRange(1.1,5) self.InputScale.setValue(1.5) self.InputStep = QSpinBox(self) self.InputStep.setRange(1,150) self.InputStep.setValue(30) self.InputClass = QSpinBox(self) self.InputClass.setRange(3,20) self.InputClass.setValue(3) self.ImageName = QLineEdit("ImageName", self) self.ImageName.setMaxLength(20) self.ModelName = QLineEdit("TestModel", self) self.ModelName.setMaxLength(20) btn = QPushButton('Обучить нейросеть', self) btn.setToolTip('В результате обучения модель нейросети ' 'будет сохранена в каталоге с программой') btn.resize(btn.sizeHint()) btn.clicked.connect(self.buttonClicked) btn1 = QPushButton('Оптимизировать нейросеть', self) btn1.setToolTip('В результате оптимизации 3 лучшие модели ' 'будут сохранены в каталоге с программой') btn1.resize(btn1.sizeHint()) btn1.clicked.connect(self.buttonClicked1) btn2 = QPushButton('Поиск и распознавание', self) btn2.resize(btn2.sizeHint()) btn2.clicked.connect(self.buttonClicked2) grid = QGridLayout(self) grid.setSpacing(10) grid.addWidget(Title, 1, 0, 1, 4) grid.addWidget(TitleEp, 2, 0) grid.addWidget(TitleBa, 3, 0) grid.addWidget(TitleCl, 4, 0) grid.addWidget(self.InputEpochs, 2, 1) grid.addWidget(self.InputBatch, 3, 1) grid.addWidget(self.InputClass, 4, 1) grid.addWidget(TitleTr, 2, 2) grid.addWidget(TitleVa, 3, 2) grid.addWidget(TitleTe, 4, 2) grid.addWidget(self.InputTrain, 2, 3) grid.addWidget(self.InputValidation, 3, 3) grid.addWidget(self.InputTest, 4, 3) grid.addWidget(btn, 5, 0, 1, 2) grid.addWidget(btn1, 5, 2, 1, 2) grid.addWidget(Title1, 6, 0, 1, 4) grid.addWidget(TitleSc, 7, 0) grid.addWidget(self.InputScale, 7, 1) grid.addWidget(TitleSs, 7, 2) grid.addWidget(self.InputStep, 7, 3) grid.addWidget(TitleNi, 8, 0) grid.addWidget(self.ImageName, 8, 1) grid.addWidget(TitleNm, 8, 2) grid.addWidget(self.ModelName, 8, 3) grid.addWidget(btn2, 9, 1, 1, 2) self.setLayout(grid) self.setWindowTitle('Система поиска и распознавания сорных растений') self.setWindowIcon(QIcon('icon.png')) self.show() def buttonClicked(self): train_dir = 'train' # Каталог с данными для обучения val_dir = 'val' # Каталог с данными для проверки test_dir = 'test' # Каталог с данными для тестирования img_width, img_height = 150, 150 # Размеры изображения input_shape = (img_width, img_height, 3) # Размерность тензора на основе изображения для входных данных в нейронную сеть epochs = self.InputEpochs.value() Nclasses = self.InputClass.value() batch_size = self.InputBatch.value() nb_train_samples = self.InputTrain.value() nb_validation_samples = self.InputValidation.value() nb_test_samples = self.InputTest.value() model = Sequential() model.add(Conv2D(32, (3, 3), input_shape=input_shape)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(64, (3, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(64, (3, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(128, (3, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) model.add(Dense(768)) model.add(Activation('selu')) model.add(Dropout(0.5)) model.add(Dense(Nclasses)) model.add(Activation('softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) datagen = ImageDataGenerator(rescale=1. / 255) train_generator = datagen.flow_from_directory( train_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode='categorical') val_generator = datagen.flow_from_directory( val_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode='categorical') test_generator = datagen.flow_from_directory( test_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode='categorical') model.fit_generator( train_generator, steps_per_epoch=nb_train_samples // batch_size, epochs=epochs, validation_data=val_generator, validation_steps=nb_validation_samples // batch_size) scores = model.evaluate_generator(test_generator, nb_test_samples // batch_size) print("Аккуратность на тестовых данных: %.2f%%" % (scores[1]*100)) model.save('TestModel.h5') def buttonClicked1(self): train_dir = 'train' val_dir = 'val' test_dir = 'test' img_width, img_height = 150, 150 input_shape = (img_width, img_height, 3) epochs = self.InputEpochs.value() Nclasses = self.InputClass.value() batch_size = self.InputBatch.value() nb_train_samples = self.InputTrain.value() nb_validation_samples = self.InputValidation.value() nb_test_samples = self.InputTest.value() l=0 def build_model(hp): model = Sequential() num_hidden_layers = hp.Int('num_hidden_layers', 1, 3, default=1) num_conv_layers = hp.Int('num_conv_layers', 2, 6, default=2) model.add(Conv2D(32, (3, 3), input_shape=input_shape)) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) for i in range(num_conv_layers): filters = hp.Int('filters'+str(i), 32, 64, step=16) model.add(Conv2D(filters,(3, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Conv2D(128, (3, 3))) model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Flatten()) for j in range(num_hidden_layers): model.add(Dense(units=hp.Int('units_hiddenNeurons_'+str(j), min_value=128, max_value=1024, step=64), activation=hp.Choice('activation'+str(j),values=['relu','tanh','elu','selu']))) model.add(Dropout(0.5)) model.add(Dense(Nclasses)) model.add(Activation('softmax')) model.compile( loss='categorical_crossentropy', optimizer=hp.Choice('optimizer', values=['adam','rmsprop','SGD'],default='adam'), metrics=['accuracy']) return model tuner = RandomSearch( build_model, objective='val_accuracy', max_trials=15, directory='test_directory') tuner.search_space_summary() datagen = ImageDataGenerator(rescale=1. / 255) train_generator = datagen.flow_from_directory( train_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode='categorical') val_generator = datagen.flow_from_directory( val_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode='categorical') test_generator = datagen.flow_from_directory( test_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode='categorical') tuner.search( train_generator, steps_per_epoch=nb_train_samples // batch_size, epochs=epochs, validation_data=val_generator, validation_steps=nb_validation_samples // batch_size) tuner.results_summary() models = tuner.get_best_models(num_models=3) for model in models: model.summary() l=l+1 scores = model.evaluate_generator(test_generator, nb_test_samples // batch_size) model.save('bestmodel_'+str(l)+'.h5') print("Аккуратность на тестовых данных: %.2f%%" % (scores[1]*100)) def buttonClicked2(self): def pyramid(image, scale, minSize=(150, 150)): yield image while True: w = int(image.shape[1] / scale) image = imutils.resize(image, width=w) if image.shape[0] < minSize[1] or image.shape[1] < minSize[0]: break yield image def sliding_window(image, stepSize, windowSize): # сдвиг окна по изображению for y in range(0, image.shape[0], stepSize): for x in range(0, image.shape[1], stepSize): yield (x, y, image[y:y + windowSize[1], x:x + windowSize[0]]) # расстояние между точками def dist(x,y): return np.sqrt(np.sum(np.square(np.array(center(x))-np.array(center(y))))) # вычисляем точку-центр рамки def center(frame): (x, y, x2, y2, _ , _ , _ ) = frame return [ x+((x2-x)//2), y2+((y-y2)//2) ] # очистка списка найденных рамок def clean(X, i=0, max_dist=150): # выбираем основной элемент X[i] j = i+1 # и, следующий за ним, элемент X[j] if i <= len(X) and i > 0: max_dist *= X[i][6] while(j<len(X)): # для всех элементов j (!= i) d = dist( X[i], X[j] ) # считаем расстояние между точками i,j if ( d < max_dist ): # i,j в одном кластере if ( X[i][4] >= X[j][4] ): # сравниваем рейтинг del X[j] # удаляем элемент j else: del X[i] # удаляем основной элемент i X = clean(X, i=i, max_dist=max_dist) # рекурсивно повторяем уже без элемента i break else: j+=1 if ( i<(len(X)-1) ): # если есть ещё точки в других, относительно X[i], кластерах X = clean(X, i=i+1, max_dist=max_dist) # то выполняем проверку return X if __name__ == "__main__": k = 1 path = os.getcwd() classes = os.listdir(path + "/train") boxes = [] scale = self.InputScale.value() stepSize = self.InputStep.value() imagename = self.ImageName.text() modelname = self.ModelName.text() font = cv2.FONT_HERSHEY_SIMPLEX model = load_model(modelname+'.h5') img = cv2.imread(imagename+'.jpg') width1, height1, channels1 = img.shape if (width1 > 1000) or (height1 > 1000): img = cv2.resize(img, (0, 0), fx = 0.5, fy = 0.5) (winW, winH) = (150, 150) clone = img.copy() for resized in pyramid(img, scale): for (x, y, window) in sliding_window(resized, stepSize, windowSize=(winW, winH)): # если окно не соответствует нашему желаемому размеру окна, игнорируем его if window.shape[0] != winH or window.shape[1] != winW: continue img_tensor = image.img_to_array(window) # (height, width, channels) img_tensor = np.expand_dims(img_tensor, axis=0) # (1, height, width, channels), добавим измерение, потому что модель ожидает эту форму: (batch_size, height, width, channels) img_tensor /= 255. # imshow ожидает значений в диапазоне [0, 1] pred = model.predict(img_tensor) nclass = np.argmax(pred) pred = np.amax(pred) if pred >= 0.9: boxes.append([x*k, y*k, (x + winW)*k, (y + winH)*k, pred, classes[nclass], k]) k *= scale boxes = clean(boxes) for (x, y, x2, y2, score, classname, scales) in boxes: cv2.rectangle(clone, (int(x),int(y)),(int(x2), int(y2)),(0, 255, 0), 2) cv2.putText(clone, classname + " %.3f" % score, (int(x),int(y2)), font, 0.7, (0, 255, 0), thickness=2) cv2.imshow("Result", clone) cv2.waitKey(1) def closeEvent(self, event): reply = QMessageBox.question(self, 'Выход', "Вы уверены что хотите закрыть программу?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if reply == QMessageBox.Yes: event.accept() else: event.ignore()
def __init__(self, prefs, icon=None): super().__init__() self.prefs = prefs self.hotkey_format_regex = r'^\<ctrl\>\+\<alt\>(\+\<shift\>)?\+[a-zA-Z0-9]$' if icon: self.setWindowIcon(icon) # checkbox: Include Minimized include_minimized = QCheckBox() include_minimized.setFixedWidth(35) include_minimized.setChecked(self.prefs["include_minimized"]["value"]) include_minimized.stateChanged.connect( lambda: self.include_minimized(include_minimized.isChecked())) include_minimized_descr = QLabel( self.prefs["include_minimized"]["description"]) include_minimized_descr.setWordWrap(True) include_minimized_layout = QHBoxLayout() include_minimized_layout.addWidget(include_minimized) include_minimized_layout.addWidget(include_minimized_descr) include_minimized_groupbox = QGroupBox("Include Minimized") include_minimized_groupbox.setLayout(include_minimized_layout) # checkbox: snap to grid snap_to_grid = QCheckBox() snap_to_grid.setFixedWidth(35) snap_to_grid.setChecked(self.prefs["snap_to_grid"]["value"]) snap_to_grid.stateChanged.connect( lambda: self.snap_to_grid(snap_to_grid.isChecked())) snap_to_grid_descr = QLabel(self.prefs["snap_to_grid"]["description"]) snap_to_grid_descr.setWordWrap(True) snap_to_grid_layout = QHBoxLayout() snap_to_grid_layout.addWidget(snap_to_grid) snap_to_grid_layout.addWidget(snap_to_grid_descr) snap_to_grid_groupbox = QGroupBox("Snap To Grid") snap_to_grid_groupbox.setLayout(snap_to_grid_layout) # checkbox: fit into screen fit_into_screen = QCheckBox() fit_into_screen.setFixedWidth(35) fit_into_screen.setChecked(self.prefs["fit_into_screen"]["value"]) fit_into_screen.stateChanged.connect( lambda: self.fit_into_screen(fit_into_screen.isChecked())) fit_into_screen_descr = QLabel( self.prefs["fit_into_screen"]["description"]) fit_into_screen_descr.setWordWrap(True) fit_into_screen_layout = QHBoxLayout() fit_into_screen_layout.addWidget(fit_into_screen) fit_into_screen_layout.addWidget(fit_into_screen_descr) fit_into_screen_groupbox = QGroupBox("Fit Into Screen") fit_into_screen_groupbox.setLayout(fit_into_screen_layout) # doublespinBox: match cutoff match_cutoff = QDoubleSpinBox() match_cutoff.setFixedWidth(35) match_cutoff.setValue(self.prefs["match_cutoff"]["value"]) match_cutoff.setRange(0.1, 1.0) match_cutoff.setSingleStep(0.1) match_cutoff.setDecimals(1) match_cutoff.valueChanged.connect( lambda: self.match_cutoff(match_cutoff.value())) match_cutoff_descr = QLabel(self.prefs["match_cutoff"]["description"]) match_cutoff_descr.setWordWrap(True) match_cutoff_layout = QHBoxLayout() match_cutoff_layout.addWidget(match_cutoff) match_cutoff_layout.addWidget(match_cutoff_descr) match_cutoff_groupbox = QGroupBox("Match Cutoff") match_cutoff_groupbox.setLayout(match_cutoff_layout) # checkbox: enable hotkeys enable_hotkeys = QCheckBox() enable_hotkeys.setFixedWidth(35) enable_hotkeys.setChecked(self.prefs["enable_hotkeys"]["value"]) enable_hotkeys.stateChanged.connect( lambda: self.enable_hotkeys(enable_hotkeys.isChecked())) enable_hotkeys_descr = QLabel( self.prefs["enable_hotkeys"]["description"]) enable_hotkeys_descr.setWordWrap(True) enable_hotkeys_layout = QHBoxLayout() enable_hotkeys_layout.addWidget(enable_hotkeys) enable_hotkeys_layout.addWidget(enable_hotkeys_descr) # lineedit: hotkeys shortcuts hotkey_freeze_new_name = QLabel("Freeze New:") hotkey_freeze_new_name.setFixedWidth(75) self.hotkey_freeze_new_status = QLabel() self.hotkey_freeze_new = QLineEdit( self.prefs["hotkeys"]["value"]["freeze_new"]) self.hotkey_freeze_new.setFixedWidth(140) self.hotkey_freeze_new.editingFinished.connect( lambda: self.hotkey_freeze_new_update(self.hotkey_freeze_new.text( ))) self.hotkey_freeze_new.cursorPositionChanged.connect( self.hotkey_freeze_new_status.clear) hotkey_freeze_new_layout = QHBoxLayout() hotkey_freeze_new_layout.addWidget(hotkey_freeze_new_name) hotkey_freeze_new_layout.addWidget(self.hotkey_freeze_new) hotkey_freeze_new_layout.addWidget(self.hotkey_freeze_new_status) hotkey_freeze_all_name = QLabel("Freeze All:") hotkey_freeze_all_name.setFixedWidth(75) self.hotkey_freeze_all_status = QLabel() self.hotkey_freeze_all = QLineEdit( self.prefs["hotkeys"]["value"]["freeze_all"]) self.hotkey_freeze_all.setFixedWidth(140) self.hotkey_freeze_all.editingFinished.connect( lambda: self.hotkey_freeze_all_update(self.hotkey_freeze_all.text( ))) self.hotkey_freeze_all.cursorPositionChanged.connect( self.hotkey_freeze_all_status.clear) hotkey_freeze_all_layout = QHBoxLayout() hotkey_freeze_all_layout.addWidget(hotkey_freeze_all_name) hotkey_freeze_all_layout.addWidget(self.hotkey_freeze_all) hotkey_freeze_all_layout.addWidget(self.hotkey_freeze_all_status) hotkey_restore_name = QLabel("Restore:") hotkey_restore_name.setFixedWidth(75) self.hotkey_restore_status = QLabel() self.hotkey_restore = QLineEdit( self.prefs["hotkeys"]["value"]["restore"]) self.hotkey_restore.setFixedWidth(140) self.hotkey_restore.editingFinished.connect( lambda: self.hotkey_restore_update(self.hotkey_restore.text())) self.hotkey_restore.cursorPositionChanged.connect( self.hotkey_restore_status.clear) hotkey_restore_layout = QHBoxLayout() hotkey_restore_layout.addWidget(hotkey_restore_name) hotkey_restore_layout.addWidget(self.hotkey_restore) hotkey_restore_layout.addWidget(self.hotkey_restore_status) self.hotkeys_statusbar = QLabel() self.hotkeys_statusbar.setWordWrap(True) self.hotkeys_statusbar.setText(hotkeys_statusbar_map[ self.prefs['enable_hotkeys']['value']]['text']) self.hotkeys_statusbar.setStyleSheet(hotkeys_statusbar_map[ self.prefs['enable_hotkeys']['value']]['style']) close_button = QPushButton("Close") close_button.setMaximumWidth(75) close_button.clicked.connect(self.close) hotkeys_statusbar_layout = QHBoxLayout() hotkeys_statusbar_layout.addWidget(self.hotkeys_statusbar) hotkeys_statusbar_layout.addWidget(close_button) hotkeys_layout = QVBoxLayout() hotkeys_layout.addLayout(hotkey_freeze_new_layout) hotkeys_layout.addLayout(hotkey_freeze_all_layout) hotkeys_layout.addLayout(hotkey_restore_layout) self.hotkeys_groupbox = QGroupBox() self.hotkeys_groupbox.setFlat(True) self.hotkeys_groupbox.setDisabled( not self.prefs["enable_hotkeys"]["value"]) self.hotkeys_groupbox.setLayout(hotkeys_layout) enable_hotkeys_outer_layout = QVBoxLayout() enable_hotkeys_outer_layout.addLayout(enable_hotkeys_layout) enable_hotkeys_outer_layout.addWidget(self.hotkeys_groupbox) enable_hotkeys_groupbox = QGroupBox("Enable Hotkeys") enable_hotkeys_groupbox.setLayout(enable_hotkeys_outer_layout) # Create main layout and add widgets main_layout = QVBoxLayout() main_layout.addWidget(include_minimized_groupbox) main_layout.addWidget(snap_to_grid_groupbox) main_layout.addWidget(fit_into_screen_groupbox) main_layout.addWidget(match_cutoff_groupbox) main_layout.addWidget(enable_hotkeys_groupbox) main_layout.addWidget(self.hotkeys_groupbox) #main_layout.addWidget(hotkeys_statusbar_groupbox) main_layout.addLayout(hotkeys_statusbar_layout) self.setLayout(main_layout)
class SSUTypicalComponentChart(QDialog): def __init__(self, parent=None, toolbar=False): flags = Qt.Window | Qt.WindowTitleHint | Qt.CustomizeWindowHint | Qt.WindowCloseButtonHint super().__init__(parent=parent, f=flags) self.setWindowTitle(self.tr("SSU Typical Component Chart")) self.figure = plt.figure(figsize=(6, 3)) self.clustering_axes = self.figure.add_subplot(1, 2, 1) self.component_axes = self.figure.add_subplot(1, 2, 2) self.canvas = FigureCanvas(self.figure) self.toolbar = NavigationToolbar(self.canvas, self) self.main_layout = QGridLayout(self) self.main_layout.addWidget(self.toolbar, 0, 0, 1, 4) self.main_layout.addWidget(self.canvas, 1, 0, 1, 4) if not toolbar: self.toolbar.hide() self.supported_scales = [("log-linear", self.tr("Log-linear")), ("log", self.tr("Log")), ("phi", self.tr("φ")), ("linear", self.tr("Linear"))] self.AXIS_LIST = [ self.tr("Mean [φ]"), self.tr("Standard deviation [φ]"), self.tr("Skewness"), self.tr("Kurtosis") ] self.x_axis_label = QLabel(self.tr("X Axis")) self.x_axis_combo_box = QComboBox() self.x_axis_combo_box.addItems(self.AXIS_LIST) self.y_axis_label = QLabel(self.tr("Y Axis")) self.y_axis_combo_box = QComboBox() self.y_axis_combo_box.addItems(self.AXIS_LIST) self.y_axis_combo_box.setCurrentIndex(1) self.main_layout.addWidget(self.x_axis_label, 2, 0) self.main_layout.addWidget(self.x_axis_combo_box, 2, 1) self.main_layout.addWidget(self.y_axis_label, 3, 0) self.main_layout.addWidget(self.y_axis_combo_box, 3, 1) self.scale_label = QLabel(self.tr("Scale")) self.scale_combo_box = QComboBox() self.scale_combo_box.addItems( [name for key, name in self.supported_scales]) self.main_layout.addWidget(self.scale_label, 2, 2) self.main_layout.addWidget(self.scale_combo_box, 2, 3) self.min_samples_label = QLabel(self.tr("Minimum Samples")) self.min_samples_input = QDoubleSpinBox() self.min_samples_input.setRange(0.0001, 0.9999) self.min_samples_input.setDecimals(4) self.min_samples_input.setSingleStep(0.001) self.min_samples_input.setValue(0.03) self.min_cluster_size_label = QLabel(self.tr("Minimum Cluster Size")) self.min_cluster_size_input = QDoubleSpinBox() self.min_cluster_size_input.setRange(0.0001, 0.9999) self.min_cluster_size_input.setDecimals(4) self.min_cluster_size_input.setSingleStep(0.001) self.min_cluster_size_input.setValue(0.1) self.xi_label = QLabel(self.tr("xi")) self.xi_input = QDoubleSpinBox() self.xi_input.setRange(0.0001, 0.9999) self.xi_input.setDecimals(4) self.xi_input.setSingleStep(0.001) self.xi_input.setValue(0.05) self.main_layout.addWidget(self.min_samples_label, 3, 2) self.main_layout.addWidget(self.min_samples_input, 3, 3) self.main_layout.addWidget(self.min_cluster_size_label, 4, 0) self.main_layout.addWidget(self.min_cluster_size_input, 4, 1) self.main_layout.addWidget(self.xi_label, 4, 2) self.main_layout.addWidget(self.xi_input, 4, 3) self.update_chart_button = QPushButton(self.tr("Update Chart")) self.update_chart_button.clicked.connect(self.update_chart) self.save_typical_button = QPushButton(self.tr("Save Typical")) self.save_typical_button.setEnabled(False) self.save_typical_button.clicked.connect(self.on_save_clicked) self.main_layout.addWidget(self.update_chart_button, 5, 0, 1, 2) self.main_layout.addWidget(self.save_typical_button, 5, 2, 1, 2) self.last_results = None # type: list[SSUResult] self.data_to_clustering = None self.stacked_components = None self.normal_msg = QMessageBox(self) self.file_dialog = QFileDialog(parent=self) @property def scale(self) -> str: index = self.scale_combo_box.currentIndex() key, name = self.supported_scales[index] return key @property def transfer(self) -> typing.Callable: if self.scale == "log-linear": return lambda classes_φ: convert_φ_to_μm(classes_φ) elif self.scale == "log": return lambda classes_φ: np.log(convert_φ_to_μm(classes_φ)) elif self.scale == "phi": return lambda classes_φ: classes_φ elif self.scale == "linear": return lambda classes_φ: convert_φ_to_μm(classes_φ) @property def xlabel(self) -> str: if self.scale == "log-linear": return self.tr("Grain-size [μm]") elif self.scale == "log": return self.tr("Ln(grain-size in μm)") elif self.scale == "phi": return self.tr("Grain-size [φ]") elif self.scale == "linear": return self.tr("Grain-size [μm]") @property def ylabel(self) -> str: return self.tr("Frequency") @property def xlog(self) -> bool: if self.scale == "log-linear": return True else: return False 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) def update_chart(self): if self.last_results is None: return x = self.transfer(self.last_results[0].classes_φ) self.save_typical_button.setEnabled(True) cluster = OPTICS(min_samples=self.min_samples_input.value(), min_cluster_size=self.min_cluster_size_input.value(), xi=self.xi_input.value()) flags = cluster.fit_predict(self.data_to_clustering) cmap = plt.get_cmap() self.clustering_axes.clear() flag_set = set(flags) for flag in flag_set: key = np.equal(flags, flag) if flag == -1: c = "#7a7374" label = self.tr("Not clustered") else: c = cmap(flag) label = self.tr("EM{0}").format(flag + 1) self.clustering_axes.plot(self.data_to_clustering[key] [:, self.x_axis_combo_box.currentIndex()], self.data_to_clustering[key] [:, self.y_axis_combo_box.currentIndex()], c="#ffffff00", marker=".", ms=8, mfc=c, mew=0.0, zorder=flag, label=label) if len(flag_set) < 6: self.clustering_axes.legend(loc="upper left") self.clustering_axes.set_xlabel(self.x_axis_combo_box.currentText()) self.clustering_axes.set_ylabel(self.y_axis_combo_box.currentText()) self.clustering_axes.set_title(self.tr("Clustering of end-members")) self.component_axes.clear() if self.xlog: self.component_axes.set_xscale("log") for flag in flag_set: if flag == -1: c = "#7a7374" else: c = cmap(flag) key = np.equal(flags, flag) for distribution in self.stacked_components[key]: self.component_axes.plot(x, distribution, c=c, zorder=flag) if flag != -1: typical = np.mean(self.stacked_components[key], axis=0) self.component_axes.plot(x, typical, c="black", zorder=1e10, ls="--", linewidth=1) self.component_axes.set_title(self.tr("Typical end-members")) self.component_axes.set_xlabel(self.xlabel) self.component_axes.set_ylabel(self.ylabel) self.component_axes.set_xlim(x[0], x[-1]) self.component_axes.set_ylim(0, None) self.figure.tight_layout() self.canvas.draw() def show_typical(self, results: typing.Iterable[GrainSizeSample]): if len(results) == 0: return keys_to_clustering = ["mean", "std", "skewness", "kurtosis"] data_to_clustering = [] stacked_components = [] for result in results: for component in result.components: has_nan = False for key in keys_to_clustering: if np.isnan( component.logarithmic_moments[key]) or np.isinf( component.logarithmic_moments[key]): has_nan = True break if has_nan: continue data_to_clustering.append([ component.logarithmic_moments[key] for key in keys_to_clustering ]) stacked_components.append(component.distribution) # convert to numpy array data_to_clustering = np.array(data_to_clustering) stacked_components = np.array(stacked_components) self.last_results = results self.data_to_clustering = data_to_clustering self.stacked_components = stacked_components self.update_chart() def save_typical(self, filename): assert self.last_results is not None if len(self.last_results) == 0: return cluster = OPTICS(min_samples=self.min_samples_input.value(), min_cluster_size=self.min_cluster_size_input.value(), xi=self.xi_input.value()) classes_μm = self.last_results[0].classes_μm flags = cluster.fit_predict(self.data_to_clustering) flag_set = set(flags) typicals = [] for flag in flag_set: if flag != -1: key = np.equal(flags, flag) typical = np.mean(self.stacked_components[key], axis=0) typicals.append(typical) 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 2 + N_clusters sheets: 1. The first sheet is the sum distributions of all component clusters. 2. The second sheet is used to put the component distributions that not in any cluster. 3. The left sheet is the component distributions of each cluster, separately. The clustering algorithm is OPTICS, implemented by scikit-learn. https://scikit-learn.org/stable/modules/generated/sklearn.cluster.OPTICS.html Clustering algorithm details min_samples={1} min_cluster_size={2} xi={3} others=default """.format(QGRAIN_VERSION, self.min_samples_input.value(), self.min_cluster_size_input.value(), self.xi_input.value()) 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("Typical Components")) write(0, 0, self.tr("Typical Component"), 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, distribution in enumerate(typicals, 1): if row % 2 == 0: style = "normal_dark" else: style = "normal_light" write(row, 0, self.tr("Component{0}").format(row), style=style) for col, value in enumerate(distribution, 1): write(row, col, value, style=style) QCoreApplication.processEvents() for flag in flag_set: if flag == -1: ws = wb.create_sheet(self.tr("Not Clustered"), 2) else: ws = wb.create_sheet(self.tr("Cluster{0}").format(flag + 1)) write(0, 0, self.tr("Index"), 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 key = np.equal(flags, flag) for row, component in enumerate(self.stacked_components[key], 1): if row % 2 == 0: style = "normal_dark" else: style = "normal_light" write(row, 0, str(row), style=style) for col, value in enumerate(component, 1): write(row, col, value, style=style) QCoreApplication.processEvents() wb.save(filename) wb.close() def on_save_clicked(self): if len(self.last_results) == 0: self.show_warning(self.tr("There is not an SSU result.")) return filename, _ = self.file_dialog.getSaveFileName( self, self. tr("Choose a filename to save the typical components of SSU results" ), None, f"{self.tr('Microsoft Excel')} (*.xlsx)") if filename is None or filename == "": return try: self.save_typical(filename) self.show_info(self.tr("The typical components have been saved.")) except Exception as e: self.show_error( self.tr("Error raised while saving it to Excel file.\n {0}" ).format(e.__str__())) return
class ColorDialog(QDialog): def __init__(self, model, FM, parent=None): super(ColorDialog, self).__init__(parent) self.setWindowTitle('Color Options') self.model = model self.FM = FM self.mw = parent self.createDialogLayout() def createDialogLayout(self): self.createGeneralTab() self.cellTable = self.createDomainTable(self.mw.cellsModel) self.matTable = self.createDomainTable(self.mw.materialsModel) self.cellTab = self.createDomainTab(self.cellTable) self.matTab = self.createDomainTab(self.matTable) self.tabs = QTabWidget() self.tabs.setMaximumHeight(800) self.tabs.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.tabs.addTab(self.generalTab, 'General') self.tabs.addTab(self.cellTab, 'Cells') self.tabs.addTab(self.matTab, 'Materials') self.createButtonBox() self.colorDialogLayout = QVBoxLayout() #self.colorDialogLayout.setContentsMargins(0, 0, 0, 0) self.colorDialogLayout.addWidget(self.tabs) self.colorDialogLayout.addWidget(self.buttonBox) self.setLayout(self.colorDialogLayout) def createGeneralTab(self): # Masking options self.maskingCheck = QCheckBox('') self.maskingCheck.stateChanged.connect(self.mw.toggleMasking) self.maskColorButton = QPushButton() self.maskColorButton.setCursor(QtCore.Qt.PointingHandCursor) self.maskColorButton.setFixedWidth(self.FM.width("XXXXXXXXXX")) self.maskColorButton.setFixedHeight(self.FM.height() * 1.5) self.maskColorButton.clicked.connect(self.mw.editMaskingColor) # Highlighting options self.hlCheck = QCheckBox('') self.hlCheck.stateChanged.connect(self.mw.toggleHighlighting) self.hlColorButton = QPushButton() self.hlColorButton.setCursor(QtCore.Qt.PointingHandCursor) self.hlColorButton.setFixedWidth(self.FM.width("XXXXXXXXXX")) self.hlColorButton.setFixedHeight(self.FM.height() * 1.5) self.hlColorButton.clicked.connect(self.mw.editHighlightColor) self.alphaBox = QDoubleSpinBox() self.alphaBox.setRange(0, 1) self.alphaBox.setSingleStep(.05) self.alphaBox.valueChanged.connect(self.mw.editAlpha) self.seedBox = QSpinBox() self.seedBox.setRange(1, 999) self.seedBox.valueChanged.connect(self.mw.editSeed) # General options self.bgButton = QPushButton() self.bgButton.setCursor(QtCore.Qt.PointingHandCursor) self.bgButton.setFixedWidth(self.FM.width("XXXXXXXXXX")) self.bgButton.setFixedHeight(self.FM.height() * 1.5) self.bgButton.clicked.connect(self.mw.editBackgroundColor) self.colorbyBox = QComboBox(self) self.colorbyBox.addItem("material") self.colorbyBox.addItem("cell") self.colorbyBox.currentTextChanged[str].connect(self.mw.editColorBy) formLayout = QFormLayout() formLayout.setAlignment(QtCore.Qt.AlignHCenter) formLayout.setFormAlignment(QtCore.Qt.AlignHCenter) formLayout.setLabelAlignment(QtCore.Qt.AlignLeft) #formLayout.setFieldGrowthPolicy(QFormLayout.AllNonFixedFieldsGrow) formLayout.addRow('Masking:', self.maskingCheck) formLayout.addRow('Mask Color:', self.maskColorButton) formLayout.addRow(HorizontalLine()) formLayout.addRow('Highlighting:', self.hlCheck) formLayout.addRow('Highlight Color:', self.hlColorButton) formLayout.addRow('Highlight Alpha:', self.alphaBox) formLayout.addRow('Highlight Seed:', self.seedBox) formLayout.addRow(HorizontalLine()) formLayout.addRow('Background Color: ', self.bgButton) formLayout.addRow('Color Plot By:', self.colorbyBox) generalLayout = QHBoxLayout() innerWidget = QWidget() generalLayout.setAlignment(QtCore.Qt.AlignVCenter) innerWidget.setLayout(formLayout) generalLayout.addStretch(1) generalLayout.addWidget(innerWidget) generalLayout.addStretch(1) self.generalTab = QWidget() self.generalTab.setLayout(generalLayout) def createDomainTable(self, domainmodel): domainTable = QTableView() domainTable.setModel(domainmodel) domainTable.setItemDelegate(DomainDelegate(domainTable)) domainTable.verticalHeader().setVisible(False) domainTable.resizeColumnsToContents() domainTable.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) domainTable.horizontalHeader().setSectionResizeMode( 1, QHeaderView.Stretch) return domainTable def createDomainTab(self, domaintable): domainTab = QWidget() domainTab.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) domainLayout = QVBoxLayout() domainLayout.addWidget(domaintable) domainTab.setLayout(domainLayout) return domainTab def createButtonBox(self): applyButton = QPushButton("Apply Changes") applyButton.clicked.connect(self.mw.applyChanges) closeButton = QPushButton("Close") closeButton.clicked.connect(self.hide) buttonLayout = QHBoxLayout() buttonLayout.addStretch(1) buttonLayout.addWidget(applyButton) buttonLayout.addWidget(closeButton) self.buttonBox = QWidget() self.buttonBox.setLayout(buttonLayout) def updateDialogValues(self): self.updateMasking() self.updateMaskingColor() self.updateHighlighting() self.updateHighlightColor() self.updateAlpha() self.updateSeed() self.updateBackgroundColor() self.updateColorBy() self.updateDomainTabs() def updateMasking(self): masking = self.model.activeView.masking self.maskingCheck.setChecked(masking) self.maskColorButton.setDisabled(not masking) if masking: self.cellTable.showColumn(4) self.matTable.showColumn(4) else: self.cellTable.hideColumn(4) self.matTable.hideColumn(4) def updateMaskingColor(self): color = self.model.activeView.maskBackground self.maskColorButton.setStyleSheet("border-radius: 8px;" "background-color: rgb%s" % (str(color))) def updateHighlighting(self): highlighting = self.model.activeView.highlighting self.hlCheck.setChecked(highlighting) self.hlColorButton.setDisabled(not highlighting) self.alphaBox.setDisabled(not highlighting) self.seedBox.setDisabled(not highlighting) if highlighting: self.cellTable.showColumn(5) self.cellTable.hideColumn(2) self.cellTable.hideColumn(3) self.matTable.showColumn(5) self.matTable.hideColumn(2) self.matTable.hideColumn(3) else: self.cellTable.hideColumn(5) self.cellTable.showColumn(2) self.cellTable.showColumn(3) self.matTable.hideColumn(5) self.matTable.showColumn(2) self.matTable.showColumn(3) def updateHighlightColor(self): color = self.model.activeView.highlightBackground self.hlColorButton.setStyleSheet("border-radius: 8px;" "background-color: rgb%s" % (str(color))) def updateAlpha(self): self.alphaBox.setValue(self.model.activeView.highlightAlpha) def updateSeed(self): self.seedBox.setValue(self.model.activeView.highlightSeed) def updateBackgroundColor(self): color = self.model.activeView.plotBackground self.bgButton.setStyleSheet("border-radius: 8px;" "background-color: rgb%s" % (str(color))) def updateColorBy(self): self.colorbyBox.setCurrentText(self.model.activeView.colorby) def updateDomainTabs(self): self.cellTable.setModel(self.mw.cellsModel) self.matTable.setModel(self.mw.materialsModel)
class TrainingPanel(Panel): def __init__(self, datasets, testing_panel, threads): super().__init__() if isinstance(testing_panel, TestingPanel): self.testing_panel = testing_panel else: raise TypeError('"testing_panel" must be the instance of ' '"TestingPanel"') self.datasets = datasets self.threads = threads self.__set_execution_ui() self.__set_options_ui() self.__set_outputs_ui() self.__set_graphic_ui() def __set_execution_ui(self): group_box = QGroupBox('Training Execution') inner_layout = QHBoxLayout() group_box.setLayout(inner_layout) self.data_selector = QComboBox() self.data_selector.addItems(list(self.datasets.keys())) self.data_selector.setStatusTip('Select the training dataset.') self.start_btn = QPushButton('Train') self.start_btn.setStatusTip('Start training.') self.start_btn.clicked.connect(self.__run) self.stop_btn = QPushButton('Stop') self.stop_btn.setStatusTip('Force the training stop running.') self.stop_btn.setDisabled(True) self.multicore_cb = QCheckBox('Multicore') self.multicore_cb.setStatusTip('Use multiprocessing in calculating ' 'fitting for populations.') self.multicore_cb.setChecked(True) inner_layout.addWidget(self.data_selector, 1) inner_layout.addWidget(self.start_btn) inner_layout.addWidget(self.stop_btn) inner_layout.addWidget(self.multicore_cb) self._layout.addWidget(group_box) def __set_options_ui(self): group_box = QGroupBox('Training Options') inner_layout = QFormLayout() group_box.setLayout(inner_layout) self.iter_times = QSpinBox() self.iter_times.setRange(1, 1000000) self.iter_times.setValue(200) self.iter_times.setStatusTip('The total iterating times for training.') self.population_size = QSpinBox() self.population_size.setRange(1, 100000) self.population_size.setValue(100) self.population_size.setStatusTip('The population size for the PSO.') self.inertia_weight = QDoubleSpinBox() self.inertia_weight.setRange(0, 50) self.inertia_weight.setValue(1) self.inertia_weight.setSingleStep(0.1) self.inertia_weight.setStatusTip('The inertia weight of the velocity ' ' for each individual.') self.cognitive_const_rand_upper = QDoubleSpinBox() self.cognitive_const_rand_upper.setRange(0, 50) self.cognitive_const_rand_upper.setValue(2) self.cognitive_const_rand_upper.setSingleStep(0.1) self.cognitive_const_rand_upper.setStatusTip( 'The random upper bound for cognitive accelerate constant.') self.social_const_rand_upper = QDoubleSpinBox() self.social_const_rand_upper.setRange(0, 50) self.social_const_rand_upper.setValue(3) self.social_const_rand_upper.setSingleStep(0.1) self.social_const_rand_upper.setStatusTip( 'The random upper bound for social accelerate constant.') self.v_max = QDoubleSpinBox() self.v_max.setRange(0.5, 100) self.v_max.setValue(5) self.v_max.setSingleStep(1) self.v_max.setStatusTip('The maximum of velocity for each individual.') self.nneuron = QSpinBox() self.nneuron.setRange(1, 100) self.nneuron.setValue(6) self.nneuron.setStatusTip('The number of RBFN neuron.') self.sd_max = QDoubleSpinBox() self.sd_max.setRange(0.01, 20) self.sd_max.setValue(10) self.sd_max.setSingleStep(0.1) self.sd_max.setStatusTip('The random range maximum of standard ' 'deviation of each neuron in RBFN (only for ' 'initialization).') inner_layout.addRow('Iterating Times:', self.iter_times) inner_layout.addRow('Population Size:', self.population_size) inner_layout.addRow('Inertia Weight:', self.inertia_weight) inner_layout.addRow('Cognitive Const Upper:', self.cognitive_const_rand_upper) inner_layout.addRow('Social Const Upper:', self.social_const_rand_upper) inner_layout.addRow('Maximum of Velocity:', self.v_max) inner_layout.addRow('Number of Neuron:', self.nneuron) inner_layout.addRow('Maximum of SD:', self.sd_max) self._layout.addWidget(group_box) def __set_outputs_ui(self): group_box = QGroupBox('Training Details') inner_layout = QFormLayout() group_box.setLayout(inner_layout) self.current_iter_time = QLabel('--') self.current_error = QLabel('--') self.avg_error = QLabel('--') self.global_best_error = QLabel('--') self.total_best_error = QLabel('--') self.progressbar = QProgressBar() self.current_iter_time.setAlignment(Qt.AlignCenter) self.current_error.setAlignment(Qt.AlignCenter) self.avg_error.setAlignment(Qt.AlignCenter) self.global_best_error.setAlignment(Qt.AlignCenter) self.total_best_error.setAlignment(Qt.AlignCenter) self.current_iter_time.setStatusTip('The current iterating time of ' 'the PSO.') self.current_error.setStatusTip('The current error from the fitting ' 'function. ("( )": normalized error)') self.avg_error.setStatusTip('The average error from the fitting ' 'function in current iteration. ("( )": ' 'normalized error)') self.global_best_error.setStatusTip( 'The error of global best individual from the fitting function in ' 'current iteration. ("( )": normalized error)') self.total_best_error.setStatusTip( 'The error of total best individual from the fitting function in ' 'training. ("( )": normalized error)') inner_layout.addRow('Current Iterating Time:', self.current_iter_time) inner_layout.addRow('Current Error:', self.current_error) inner_layout.addRow('Average Error:', self.avg_error) inner_layout.addRow('Global Best Error:', self.global_best_error) inner_layout.addRow('Total Best Error:', self.total_best_error) inner_layout.addRow(self.progressbar) self._layout.addWidget(group_box) def __set_graphic_ui(self): group_box = QGroupBox('Error Line Charts:') inner_layout = QVBoxLayout() group_box.setLayout(inner_layout) self.err_chart = ErrorLineChart(1) self.err_chart.setStatusTip('The history of error from the fitting ' 'of the PSO for each data.') self.__err_x = 1 self.iter_err_chart = ErrorLineChart( 3, ('Avg', 'Global Best', 'Total Best')) self.iter_err_chart.setStatusTip('The history of average and least ' 'error from the fitting of the PSO ' 'for each iteration.') self.iter_err_chart.setMinimumHeight(150) inner_layout.addWidget(QLabel('Current Error')) inner_layout.addWidget(self.err_chart) inner_layout.addWidget(QLabel('Average Error')) inner_layout.addWidget(self.iter_err_chart) self._layout.addWidget(group_box) @Slot() def __init_widgets(self): self.start_btn.setDisabled(True) self.stop_btn.setEnabled(True) self.multicore_cb.setDisabled(True) self.data_selector.setDisabled(True) self.iter_times.setDisabled(True) self.population_size.setDisabled(True) self.inertia_weight.setDisabled(True) self.cognitive_const_rand_upper.setDisabled(True) self.social_const_rand_upper.setDisabled(True) self.v_max.setDisabled(True) self.nneuron.setDisabled(True) self.sd_max.setDisabled(True) self.err_chart.clear() self.iter_err_chart.clear() self.__err_x = 1 @Slot() def __reset_widgets(self): self.start_btn.setEnabled(True) self.stop_btn.setDisabled(True) self.multicore_cb.setEnabled(True) self.data_selector.setEnabled(True) self.iter_times.setEnabled(True) self.population_size.setEnabled(True) self.inertia_weight.setEnabled(True) self.cognitive_const_rand_upper.setEnabled(True) self.social_const_rand_upper.setEnabled(True) self.v_max.setEnabled(True) self.nneuron.setEnabled(True) self.sd_max.setEnabled(True) self.progressbar.setMinimum(0) self.progressbar.setMaximum(100) @Slot() def __indicate_busy(self): self.progressbar.setMinimum(0) self.progressbar.setMaximum(0) @Slot(int) def __show_current_iter_time(self, value): self.current_iter_time.setText(str(value + 1)) self.progressbar.setValue(value + 1) @Slot(float) def __show_current_error(self, value): self.current_error.setText('{:.5f} ({:.5f})'.format(value, value / 40)) self.err_chart.append_point(self.__err_x, value) self.__err_x += 1 @Slot(float, float, float) def __show_iter_error(self, avg, glob, total): self.avg_error.setText('{:.5f} ({:.5f})'.format(avg, avg / 40)) self.global_best_error.setText( '{:.5f} ({:.5f})'.format(glob, glob / 40)) self.total_best_error.setText( '{:.5f} ({:.5f})'.format(total, total / 40)) self.iter_err_chart.append_point( int(self.current_iter_time.text()), total, 2) self.iter_err_chart.append_point( int(self.current_iter_time.text()), glob, 1) self.iter_err_chart.append_point( int(self.current_iter_time.text()), avg, 0) def __run(self): self.progressbar.setMaximum(self.iter_times.value()) self.__current_dataset = self.datasets[ self.data_selector.currentText()] self.__pso = PSO(self.iter_times.value(), self.population_size.value(), self.inertia_weight.value(), self.cognitive_const_rand_upper.value(), self.social_const_rand_upper.value(), self.v_max.value(), self.nneuron.value(), self.__current_dataset, self.sd_max.value(), is_multicore=self.multicore_cb.isChecked()) self.threads.append(self.__pso) self.stop_btn.clicked.connect(self.__pso.stop) self.__pso.started.connect(self.__init_widgets) self.__pso.finished.connect(self.__reset_widgets) self.__pso.sig_current_iter_time.connect(self.__show_current_iter_time) self.__pso.sig_current_error.connect(self.__show_current_error) self.__pso.sig_iter_error.connect(self.__show_iter_error) self.__pso.sig_indicate_busy.connect(self.__indicate_busy) self.__pso.sig_console.connect(self.testing_panel.print_console) self.__pso.sig_rbfn.connect(self.testing_panel.load_rbfn) self.__pso.start()
class BlockView(QWidget): """Config widget for a single experiment block. In a model-view paradigm, this is a view, and block is a model. A new block can be set using setModel. """ statistics_type_to_name = { "max": "Min/Max", "meanstd": "Standardise" } statistics_name_to_type = {v: k for k, v in statistics_type_to_name.items()} def __init__(self, parent=None): super().__init__(parent) layout = QFormLayout() self.setLayout(layout) self._model = None # Block properties --------------------------------------------------------------------------------------------- self.duration = QWidget() self.duration.setContentsMargins(0, 0, 0, 0) ly = QHBoxLayout() ly.setContentsMargins(0, 0, 0, 0) self.duration.setLayout(ly) self.duration_base = QDoubleSpinBox() self.duration_base.setRange(0, 1000) self.duration_base.setValue(10) self.duration_deviation = QDoubleSpinBox() self.duration_deviation.setRange(0, 10) self.duration_deviation.setValue(0) self.duration_deviation.setSingleStep(0.1) self.duration_deviation.setSuffix(" s") self.duration_base.valueChanged.connect(self.duration_deviation.setMaximum) ly.addWidget(self.duration_base) ly.addWidget(QLabel("±")) ly.addWidget(self.duration_deviation) self.feedback_source = QLineEdit("All") self.feedback_type = QComboBox() self.feedback_type.addItem("Baseline") self.feedback_type.addItem("Feedback") self.mock_signal_path = QLineEdit() self.mock_signal_dataset = QLineEdit() self.mock_previous = QSpinBox() self.mock_previous_reverse = QCheckBox() self.mock_previous_random = QCheckBox() self.pause = QCheckBox() self.beep = QCheckBox() self.start_data_driven_filter_designer = QCheckBox() self.update_statistics = QCheckBox() self.update_statistics.stateChanged.connect(lambda state: self.statistics_type.setEnabled(bool(state))) self.statistics_type = QComboBox() self.statistics_type.setEnabled(False) for name in self.statistics_name_to_type: self.statistics_type.addItem(name) self.statistics_type.setCurrentText("Standardise") self.random_bound = QComboBox() self.random_bound.addItem("SimCircle") self.random_bound.addItem("RandomCircle") self.random_bound.addItem("Bar") self.video_path = QLineEdit() self.message = QLineEdit() self.feedback_type.currentTextChanged.connect(lambda ftype: self.message.setEnabled(ftype == "Baseline")) self.voiceover = QCheckBox() # Grouped properties ------------------------------------------------------------------------------------------- # Mock signal mock_signal_groupbox = QGroupBox("Mock signal") mock_signal_gblayout = QFormLayout() mock_signal_groupbox.setLayout(mock_signal_gblayout) mock_signal_gblayout.addRow("Mock signal file path", self.mock_signal_path) mock_signal_gblayout.addRow("Mock signal file dataset", self.mock_signal_dataset) mock_signal_gblayout.addRow("Mock previous", self.mock_previous) mock_signal_gblayout.addRow("Reverse mock previous", self.mock_previous_reverse) mock_signal_gblayout.addRow("Random mock previous", self.mock_previous_random) # After block actions after_block_groupbox = QGroupBox("After block actions") after_block_gblayout = QFormLayout() after_block_groupbox.setLayout(after_block_gblayout) after_block_gblayout.addRow("Start data driven filter designer", self.start_data_driven_filter_designer) after_block_gblayout.addRow("Pause", self.pause) after_block_gblayout.addRow("Beep", self.beep) after_block_gblayout.addRow("Update statistics", self.update_statistics) after_block_gblayout.addRow("Statistics type", self.statistics_type) # Adding properties to the widget ------------------------------------------------------------------------------ layout.addRow("Duration", self.duration) layout.addRow("Source", self.feedback_source) layout.addRow("FB Type", self.feedback_type) layout.addRow("Random bound", self.random_bound) layout.addRow("Video path", self.video_path) layout.addRow("Message for test subject", self.message) layout.addRow("Voiceover for message", self.voiceover) layout.addRow(mock_signal_groupbox) layout.addRow(after_block_groupbox) def model(self): return self._model def setModel(self, block, /): """Set the model block for this view. Data in the view will be updated to reflect the new block. """ self._model = block self.updateView() def updateModel(self): """Copy data from this view to the block model. A similarly named function in the block copies data the opposite way. Use one or the other depending on where data was changed. """ model = self.model() if model is None: return model.duration = self.duration_base.value() model.duration_deviation = self.duration_deviation.value() model.feedback_source = self.feedback_source.text() model.feedback_type = self.feedback_type.currentText() model.random_bound = self.random_bound.currentText() model.video_path = self.video_path.text() model.message = self.message.text() model.voiceover = self.voiceover.isChecked() model.mock_signal_path = self.mock_signal_path.text() model.mock_signal_dataset = self.mock_signal_dataset.text() model.mock_previous = self.mock_previous.value() model.mock_previous_reverse = self.mock_previous_reverse.isChecked() model.mock_previous_random = self.mock_previous_random.isChecked() model.start_data_driven_filter_designer = self.start_data_driven_filter_designer.isChecked() model.pause = self.pause.isChecked() model.beep = self.beep.isChecked() model.update_statistics = self.update_statistics.isChecked() model.statistics_type = self.statistics_name_to_type[self.statistics_type.currentText()] def updateView(self): model = self.model() if model is None: return self.duration_base.setValue(model.duration) self.duration_deviation.setValue(model.duration_deviation) self.feedback_source.setText(model.feedback_source) self.feedback_type.setCurrentText(model.feedback_type) self.mock_signal_path.setText(model.mock_signal_path) self.mock_signal_dataset.setText(model.mock_signal_dataset) self.mock_previous.setValue(model.mock_previous) self.mock_previous_reverse.setChecked(model.mock_previous_reverse) self.mock_previous_random.setChecked(model.mock_previous_random) self.pause.setChecked(model.pause) self.beep.setChecked(model.beep) self.start_data_driven_filter_designer.setChecked(model.start_data_driven_filter_designer) self.update_statistics.setChecked(model.update_statistics) self.statistics_type.setCurrentText(self.statistics_type_to_name[model.statistics_type]) self.random_bound.setCurrentText(model.random_bound) self.video_path.setText(model.video_path) self.message.setText(model.message) self.voiceover.setChecked(model.voiceover)
class WidgetConfig(QGroupBox): def __init__(self): super(WidgetConfig, self).__init__() HEIGHT = 40 grid = QGridLayout() # 使用默认摄像头复选框 self.check_camera = QCheckBox('Use default camera') self.check_camera.setChecked(False) self.check_camera.stateChanged.connect(self.slot_check_camera) grid.addWidget(self.check_camera, 0, 0, 1, 3) # 一行三列 # 选择视频文件 label_video = QLabel('Detect File') self.line_video = QLineEdit() if 'video' in GLOBAL.config: self.line_video.setText(GLOBAL.config['video']) self.line_video.setFixedHeight(HEIGHT) self.line_video.setEnabled(False) self.line_video.editingFinished.connect( lambda: GLOBAL.record_config({'video': self.line_video.text()})) self.btn_video = QPushButton('Choose') self.btn_video.setFixedHeight(HEIGHT) self.btn_video.setEnabled(False) self.btn_video.clicked.connect(self.choose_video_file) self.slot_check_camera() grid.addWidget(label_video, 1, 0) grid.addWidget(self.line_video, 1, 1) grid.addWidget(self.btn_video, 1, 2) # 选择权重文件 label_weights = QLabel('Weights File') self.line_weights = QLineEdit() if 'weights' in GLOBAL.config: self.line_weights.setText(GLOBAL.config['weights']) self.line_weights.setFixedHeight(HEIGHT) self.line_weights.editingFinished.connect(lambda: GLOBAL.record_config( {'weights': self.line_weights.text()})) self.btn_weights = QPushButton('Choose') self.btn_weights.setFixedHeight(HEIGHT) self.btn_weights.clicked.connect(self.choose_weights_file) grid.addWidget(label_weights, 2, 0) grid.addWidget(self.line_weights, 2, 1) grid.addWidget(self.btn_weights, 2, 2) # 是否使用GPU label_device = QLabel('CUDA device') self.line_device = QLineEdit('gpu') if 'device' in GLOBAL.config: self.line_device.setText(GLOBAL.config['device']) else: self.line_device.setText('cpu') self.line_device.setPlaceholderText('cpu or 0 or 0,1,2,3') self.line_device.setFixedHeight(HEIGHT) self.line_device.editingFinished.connect( lambda: GLOBAL.record_config({'device': self.line_device.text()})) grid.addWidget(label_device, 3, 0) grid.addWidget(self.line_device, 3, 1, 1, 2) # 设置图像大小 label_size = QLabel('Img Size') self.combo_size = QComboBox() self.combo_size.setFixedHeight(HEIGHT) self.combo_size.setStyleSheet( 'QAbstractItemView::item {height: 40px;}') self.combo_size.setView(QListView()) self.combo_size.addItem('320', 320) self.combo_size.addItem('416', 416) self.combo_size.addItem('480', 480) self.combo_size.addItem('544', 544) self.combo_size.addItem('640', 640) self.combo_size.setCurrentIndex(2) self.combo_size.currentIndexChanged.connect( lambda: GLOBAL.record_config( {'img_size': self.combo_size.currentData()})) grid.addWidget(label_size, 4, 0) grid.addWidget(self.combo_size, 4, 1, 1, 2) #choose net camera label_stream = QLabel('NetVedioStream') self.combo_stream = QComboBox() self.combo_stream.setFixedHeight(HEIGHT) self.combo_stream.setStyleSheet( 'QAbstractItemView::item {height: 40px;}') self.combo_stream.setView(QListView()) self.combo_stream.addItem('rtsp://*****:*****@192.168.0.65/', 'rtsp://*****:*****@192.168.0.65/') self.combo_stream.addItem('rtsp://*****:*****@192.168.0.66/', 'rtsp://*****:*****@192.168.0.66/') self.combo_stream.addItem('rtsp://*****:*****@192.168.0.67/', 'rtsp://*****:*****@192.168.0.67/') self.combo_stream.addItem('rtsp://*****:*****@192.168.0.68/', 'rtsp://*****:*****@192.168.0.68/') self.combo_stream.addItem('rtsp://*****:*****@192.168.0.65/', 'rtsp://*****:*****@192.168.0.65/') self.combo_stream.setCurrentIndex(0) self.combo_stream.currentIndexChanged.connect( lambda: GLOBAL.record_config( {'netstreamvedio': self.combo_stream.currentData()})) grid.addWidget(label_stream, 5, 0) grid.addWidget(self.combo_stream, 5, 1, 1, 2) # 设置置信度阈值 label_conf = QLabel('Confidence') self.spin_conf = QDoubleSpinBox() self.spin_conf.setFixedHeight(HEIGHT) self.spin_conf.setDecimals(1) self.spin_conf.setRange(0.1, 0.9) self.spin_conf.setSingleStep(0.1) if 'conf_thresh' in GLOBAL.config: self.spin_conf.setValue(GLOBAL.config['conf_thresh']) else: self.spin_conf.setValue(0.4) # 默认值 GLOBAL.record_config({'conf_thresh': 0.4}) self.spin_conf.valueChanged.connect(lambda: GLOBAL.record_config( {'conf_thresh': round(self.spin_conf.value(), 1)})) grid.addWidget(label_conf, 6, 0) grid.addWidget(self.spin_conf, 6, 1, 1, 2) # 设置IOU阈值 label_iou = QLabel('IOU') self.spin_iou = QDoubleSpinBox() self.spin_iou.setFixedHeight(HEIGHT) self.spin_iou.setDecimals(1) self.spin_iou.setRange(0.1, 0.9) self.spin_iou.setSingleStep(0.1) if 'iou_thresh' in GLOBAL.config: self.spin_iou.setValue(GLOBAL.config['iou_thresh']) else: self.spin_iou.setValue(0.5) # 默认值 GLOBAL.record_config({'iou_thresh': 0.5}) self.spin_iou.valueChanged.connect(lambda: GLOBAL.record_config( {'iou_thresh': round(self.spin_iou.value(), 1)})) grid.addWidget(label_iou, 7, 0) grid.addWidget(self.spin_iou, 7, 1, 1, 2) # class-agnostic NMS self.check_agnostic = QCheckBox('Agnostic') if 'agnostic' in GLOBAL.config: self.check_agnostic.setChecked(GLOBAL.config['agnostic']) else: self.check_agnostic.setChecked(True) self.check_agnostic.stateChanged.connect(lambda: GLOBAL.record_config( {'agnostic': self.check_agnostic.isChecked()})) grid.addWidget(self.check_agnostic, 8, 0, 1, 3) # 一行三列 # augmented inference self.check_augment = QCheckBox('Augment') if 'augment' in GLOBAL.config: self.check_augment.setChecked(GLOBAL.config['augment']) else: self.check_augment.setChecked(True) self.check_augment.stateChanged.connect(lambda: GLOBAL.record_config( {'augment': self.check_augment.isChecked()})) grid.addWidget(self.check_augment, 9, 0, 1, 3) # 一行三列 self.setLayout(grid) # 设置布局 def slot_check_camera(self): check = self.check_camera.isChecked() GLOBAL.record_config({'use_camera': check}) # 保存配置 if check: self.line_video.setEnabled(False) self.btn_video.setEnabled(False) else: self.line_video.setEnabled(True) self.btn_video.setEnabled(True) def choose_weights_file(self): """从系统中选择权重文件""" file = QFileDialog.getOpenFileName( self, "Pre-trained YOLOv5 Weights", "./", "Weights Files (*.pt);;All Files (*)") if file[0] != '': self.line_weights.setText(file[0]) GLOBAL.record_config({'weights': file[0]}) def choose_video_file(self): """从系统中选择视频文件""" file = QFileDialog.getOpenFileName(self, "Video Files", "./", "Video Files (*)") if file[0] != '': self.line_video.setText(file[0]) GLOBAL.record_config({'video': file[0]}) def save_config(self): """保存当前的配置到配置文件""" config = { 'use_camera': self.check_camera.isChecked(), 'video': self.line_video.text(), 'weights': self.line_weights.text(), 'device': self.line_device.text(), 'img_size': self.combo_size.currentData(), 'conf_thresh': round(self.spin_conf.value(), 1), 'iou_thresh': round(self.spin_iou.value(), 1), 'agnostic': self.check_agnostic.isChecked(), 'augment': self.check_augment.isChecked(), 'netstreamvedio': self.combo_stream.currentData() } GLOBAL.record_config(config)
class ClassicResolverSettingWidget(QDialog): def __init__(self, parent=None, filename=None, group=None): super().__init__(parent=parent, f=Qt.Window) self.setWindowTitle(self.tr("Classic Resolver Setting")) if filename is not None: self.setting_file = QSettings(filename, QSettings.Format.IniFormat) if group is not None: self.setting_file.beginGroup(group) else: self.setting_file = None self.setAttribute(Qt.WA_StyledBackground, True) self.init_ui() def init_ui(self): self.main_layout = QGridLayout(self) # distance self.distance_label = QLabel(self.tr("Distance (Loss) Function")) 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( self.on_distance_changed) self.main_layout.addWidget(self.distance_label, 0, 0) self.main_layout.addWidget(self.distance_combo_box, 0, 1) # minimizer self.minimizer_label = QLabel(self.tr("Minimizer")) self.minimizer_label.setToolTip( self. tr("The algorithm to find the minimum value of the distance function." )) self.minimizer_combo_box = QComboBox() self.minimizer_combo_box.addItems(built_in_minimizers) self.minimizer_combo_box.setCurrentText("SLSQP") self.main_layout.addWidget(self.minimizer_label, 1, 0) self.main_layout.addWidget(self.minimizer_combo_box, 1, 1) # try_GO self.try_GO_checkbox = QCheckBox( self.tr("Try Global Optimization (GO)")) self.try_GO_checkbox.setChecked(False) self.try_GO_checkbox.setToolTip( self.tr("Whether to try global optimization.")) self.main_layout.addWidget(self.try_GO_checkbox, 2, 0, 1, 2) # GO_max_niter self.GO_max_niter_label = QLabel( self.tr("[GO] Maximum N<sub>iteration</sub>")) self.GO_max_niter_label.setToolTip( self. tr("Maximum number of iterations of global optimization for termination." )) self.GO_max_niter_input = QSpinBox() self.GO_max_niter_input.setRange(0, 10000) self.GO_max_niter_input.setValue(100) self.main_layout.addWidget(self.GO_max_niter_label, 3, 0) self.main_layout.addWidget(self.GO_max_niter_input, 3, 1) # GO_success_niter self.GO_success_niter_label = QLabel( self.tr("[GO] Success N<sub>iteration</sub>")) self.GO_success_niter_label.setToolTip( self. tr("The number of iteration that reaching the same local optimal value for termination." )) self.GO_success_niter_input = QSpinBox() self.GO_success_niter_input.setRange(1, 100) self.GO_success_niter_input.setValue(5) self.main_layout.addWidget(self.GO_success_niter_label, 4, 0) self.main_layout.addWidget(self.GO_success_niter_input, 4, 1) # GO_step self.GO_step_label = QLabel(self.tr("[GO] Step Size")) self.GO_step_label.setToolTip( self.tr("The step size of searching global optimal value.")) self.GO_step_input = QDoubleSpinBox() self.GO_step_input.setRange(0.01, 10) self.GO_step_input.setValue(0.1) self.main_layout.addWidget(self.GO_step_label, 5, 0) self.main_layout.addWidget(self.GO_step_input, 5, 1) # GO_minimizer_tol self.GO_minimizer_tol_label = QLabel( self.tr("[GO] Minimizer -lg(loss<sub>tolerance</sub>)")) self.GO_minimizer_tol_label.setToolTip( self. tr("Controls the tolerance of the loss function for termination.")) self.GO_minimizer_tol_input = QSpinBox() self.GO_minimizer_tol_input.setRange(1, 100) self.GO_minimizer_tol_input.setValue(6) self.main_layout.addWidget(self.GO_minimizer_tol_label, 6, 0) self.main_layout.addWidget(self.GO_minimizer_tol_input, 6, 1) # GO_minimizer_ftol self.GO_minimizer_ftol_label = QLabel( self.tr("[GO] Minimizer -lg(δ<sub>loss</sub>)")) self.GO_minimizer_ftol_label.setToolTip( self. tr("Controls the precision goal for the value of loss function in the stopping criterion." )) self.GO_minimizer_ftol_input = QSpinBox() self.GO_minimizer_ftol_input.setRange(1, 100) self.GO_minimizer_ftol_input.setValue(8) self.main_layout.addWidget(self.GO_minimizer_ftol_label, 7, 0) self.main_layout.addWidget(self.GO_minimizer_ftol_input, 7, 1) # GO_minimizer_max_niter self.GO_minimizer_max_niter_label = QLabel( self.tr("[GO] Minimizer Maximum N<sub>iteration</sub>")) self.GO_minimizer_max_niter_label.setToolTip( self. tr("Maximum number of iterations of the minimizer of global optimization." )) self.GO_minimizer_max_niter_input = QSpinBox() self.GO_minimizer_max_niter_input.setRange(0, 100000) self.GO_minimizer_max_niter_input.setValue(500) self.main_layout.addWidget(self.GO_minimizer_max_niter_label, 8, 0) self.main_layout.addWidget(self.GO_minimizer_max_niter_input, 8, 1) # FLO_tol self.FLO_tol_label = QLabel( self.tr("[FLO] -lg(loss<sub>tolerance</sub>)")) self.FLO_tol_label.setToolTip( self. tr("Controls the tolerance of the loss function for termination.")) self.FLO_tol_input = QSpinBox() self.FLO_tol_input.setRange(1, 100) self.FLO_tol_input.setValue(8) self.main_layout.addWidget(self.FLO_tol_label, 9, 0) self.main_layout.addWidget(self.FLO_tol_input, 9, 1) # FLO_ftol self.FLO_ftol_label = QLabel(self.tr("[FLO] -lg(δ<sub>loss</sub>)")) self.FLO_ftol_label.setToolTip( self. tr("Controls the precision goal for the value of loss function in the stopping criterion." )) self.FLO_ftol_input = QSpinBox() self.FLO_ftol_input.setRange(1, 100) self.FLO_ftol_input.setValue(10) self.main_layout.addWidget(self.FLO_ftol_label, 10, 0) self.main_layout.addWidget(self.FLO_ftol_input, 10, 1) # FLO_max_niter self.FLO_max_niter_label = QLabel( self.tr("[FLO] Maximum N<sub>iteration</sub>")) self.FLO_max_niter_label.setToolTip( self. tr("Maximum number of iterations of the minimizer of final local optimization." )) self.FLO_max_niter_input = QSpinBox() self.FLO_max_niter_input.setRange(100, 100000) self.FLO_max_niter_input.setValue(1000) self.main_layout.addWidget(self.FLO_max_niter_label, 11, 0) self.main_layout.addWidget(self.FLO_max_niter_input, 11, 1) def on_distance_changed(self, distance: str): if distance == "log10MSE": self.GO_minimizer_tol_label.setText( self.tr("[GO] Minimizer -loss<sub>tolerance</sub>")) self.FLO_tol_label.setText( self.tr("[FLO] -loss<sub>tolerance</sub>")) else: self.GO_minimizer_tol_label.setText( self.tr("[GO] Minimizer -lg(loss<sub>tolerance</sub>)")) self.FLO_tol_label.setText( self.tr("[FLO] -lg(loss<sub>tolerance</sub>)")) @property def setting(self): distance = self.distance_combo_box.currentText() minimizer = self.minimizer_combo_box.currentText() try_GO = self.try_GO_checkbox.isChecked() GO_max_niter = self.GO_max_niter_input.value() GO_success_niter = self.GO_success_niter_input.value() GO_step_size = self.GO_step_input.value() GO_minimizer_tol = -self.GO_minimizer_tol_input.value( ) if distance == "log10MSE" else 10**( -self.GO_minimizer_tol_input.value()) GO_minimizer_ftol = 10**(-self.GO_minimizer_ftol_input.value()) GO_minimizer_max_niter = self.GO_minimizer_max_niter_input.value() FLO_tol = -self.FLO_tol_input.value( ) if distance == "log10MSE" else 10**(-self.FLO_tol_input.value()) FLO_ftol = 10**(-self.FLO_ftol_input.value()) FLO_max_niter = self.FLO_max_niter_input.value() setting = ClassicResolverSetting( distance=distance, minimizer=minimizer, try_GO=try_GO, GO_max_niter=GO_max_niter, GO_success_niter=GO_success_niter, GO_step=GO_step_size, GO_minimizer_tol=GO_minimizer_tol, GO_minimizer_ftol=GO_minimizer_ftol, GO_minimizer_max_niter=GO_minimizer_max_niter, FLO_tol=FLO_tol, FLO_ftol=FLO_ftol, FLO_max_niter=FLO_max_niter) return setting @setting.setter def setting(self, setting: ClassicResolverSetting): self.distance_combo_box.setCurrentText(setting.distance) self.minimizer_combo_box.setCurrentText(setting.minimizer) self.try_GO_checkbox.setChecked(setting.try_GO) self.GO_max_niter_input.setValue(setting.GO_max_niter) self.GO_success_niter_input.setValue(setting.GO_success_niter) self.GO_step_input.setValue(setting.GO_step) if setting.distance == "log10MSE": self.GO_minimizer_tol_input.setValue(-setting.GO_minimizer_tol) self.FLO_tol_input.setValue(-setting.FLO_tol) else: self.GO_minimizer_tol_input.setValue( -np.log10(setting.GO_minimizer_tol)) self.FLO_ftol_input.setValue(-np.log10(setting.FLO_tol)) self.GO_minimizer_ftol_input.setValue( -np.log10(setting.GO_minimizer_ftol)) self.GO_minimizer_max_niter_input.setValue( setting.GO_minimizer_max_niter) self.FLO_ftol_input.setValue(-np.log10(setting.FLO_ftol)) self.FLO_max_niter_input.setValue(setting.FLO_max_niter) def save(self): if self.setting_file is not None: setting_bytes = pickle.dumps(self.setting) self.setting_file.setValue("classic_resolver_setting", setting_bytes) def restore(self): if self.setting_file is not None: setting_bytes = self.setting_file.value("classic_resolver_setting", defaultValue=None) if setting_bytes is not None: setting = pickle.loads(setting_bytes) self.setting = setting else: self.setting = ClassicResolverSetting()
def addArrowsBox(title, indexOffset, scale): layout = QGridLayout() deltaSB = QDoubleSpinBox() deltaSB.setSizePolicy(QSizePolicy.Maximum, QSizePolicy.MinimumExpanding) if title == "Translation": deltaSB.setRange(-7, 7) deltaSB.setDecimals(3) deltaSB.setValue(1.0) else: deltaSB.setRange(-60, 60) deltaSB.setDecimals(2) deltaSB.setValue(10.0) setattr(self, "delta_" + title, deltaSB) deltaF = QFormLayout() deltaF.addRow("Delta", deltaSB) layout.addLayout(deltaF, 0, 0, 1, 3) layout.addWidget( positionButton( style.standardIcon(QStyle.SP_ArrowUp), "X+", 0 + indexOffset, deltaSB, scale, ), 1, 1, ) layout.addWidget( positionButton( style.standardIcon(QStyle.SP_ArrowDown), "X-", 0 + indexOffset, deltaSB, -scale, ), 3, 1, ) layout.addWidget( positionButton( style.standardIcon(QStyle.SP_ArrowLeft), "Y-", 1 + indexOffset, deltaSB, -scale, ), 2, 0, ) layout.addWidget( positionButton( style.standardIcon(QStyle.SP_ArrowRight), "Y+", 1 + indexOffset, deltaSB, scale, ), 2, 2, ) layout.addWidget( positionButton( style.standardIcon(QStyle.SP_ArrowUp), "Z+", 2 + indexOffset, deltaSB, scale, ), 1, 4, ) layout.addWidget( positionButton( style.standardIcon(QStyle.SP_ArrowDown), "Z-", 2 + indexOffset, deltaSB, -scale, ), 3, 4, ) ret = QGroupBox(title) ret.setLayout(layout) return ret
class CleanSkinUI(QWidget): def __init__(self): QWidget.__init__(self) self.setParent(uiUtils.getTopLevelWidget(), 1) self.setAttribute(Qt.WA_DeleteOnClose) self.setWindowTitle("Clean Skin") self.setWindowIcon(QIcon(uiUtils.getIconFolder()+"arikara_CleanSkin.png")) lLayout = QFormLayout() # Max Weight self.dsb_maxWeight = QDoubleSpinBox() self.dsb_maxWeight.setRange(0.0, 1.0) self.dsb_maxWeight.setDecimals(4) self.dsb_maxWeight.setSingleStep(0.001) weight = 0.001 if cmds.optionVar(exists="ariCleanSkin_MW"): weight = cmds.optionVar(q="ariCleanSkin_MW") else: cmds.optionVar(fv=("ariCleanSkin_MW", weight)) self.dsb_maxWeight.setValue(weight) self.dsb_maxWeight.editingFinished.connect(self.dsb_maxWeightEdited) # Max Influence self.sb_maxInfluence = QSpinBox() self.sb_maxInfluence.setRange(1, 12) infl = 4 if cmds.optionVar(exists="ariCleanSkin_MI"): infl = cmds.optionVar(q="ariCleanSkin_MI") else: cmds.optionVar(iv=("ariCleanSkin_MI", infl)) self.sb_maxInfluence.setValue(infl) self.sb_maxInfluence.editingFinished.connect(self.sb_maxInfluenceEdited) self.pb_cleanSkin = QPushButton("Clean Skin") self.pb_cleanSkin.clicked.connect(self.cleanSkinCallback) lLayout.addRow("Max Weight:", self.dsb_maxWeight) lLayout.addRow("Max Influence:", self.sb_maxInfluence) lLayout.addRow(self.pb_cleanSkin) self.setLayout(lLayout) if cmds.optionVar(exists="ariCleanSkin_Geo"): geo = cmds.optionVar(q="ariCleanSkin_Geo") self.setGeometry(*geo) def cleanSkinCallback(self): cmds.undoInfo(openChunk=True, chunkName="Arikara Clean Skin") selection = cmds.ls(selection=True) maxWeight = self.dsb_maxWeight.value() maxInfluence = self.sb_maxInfluence.value() for obj in selection: try: cmds.arikaraInfluence(obj, maxInfluence=maxInfluence) except Exception as e: print e try: cmds.arikaraInfluence(obj, ru=maxWeight) except Exception as e: print e cmds.undoInfo(closeChunk=True) def updateGeometry(self): geo = self.geometry() cmds.optionVar(iv=("ariCleanSkin_Geo", geo.x())) cmds.optionVar(iva=("ariCleanSkin_Geo", geo.y())) cmds.optionVar(iva=("ariCleanSkin_Geo", geo.width())) cmds.optionVar(iva=("ariCleanSkin_Geo", geo.height())) def moveEvent(self, event): self.updateGeometry() def resizeEvent(self, event): self.updateGeometry() def dsb_maxWeightEdited(self): val = self.dsb_maxWeight.value() cmds.optionVar(fv=("ariCleanSkin_MW", val)) def sb_maxInfluenceEdited(self): val = self.sb_maxInfluence.value() cmds.optionVar(iv=("ariCleanSkin_MI", val))