def test_calibration_dialog(qtbot: QtBot): cals = { "A": Calibration.from_points([[0, 2], [1, 4]], unit="ppb"), "B": Calibration(), } dialog = dialogs.CalibrationDialog(cals, "B") qtbot.addWidget(dialog) dialog.open() assert dialog.combo_element.currentText() == "B" assert dialog.points.model.array.size == 0 assert not dialog.button_plot.isEnabled() assert not dialog.points.button_remove.isEnabled() dialog.lineedit_gradient.setText("1") dialog.lineedit_intercept.setText("2") dialog.lineedit_unit.setText("ppm") dialog.combo_element.setCurrentIndex(0) assert dialog.combo_element.currentText() == "A" assert dialog.points.model.array.size == 6 assert dialog.button_plot.isEnabled() # Points enabled on remove / add dialog.points.removeCalibrationLevel() assert not dialog.button_plot.isEnabled() dialog.points.addCalibrationLevel() assert not dialog.button_plot.isEnabled() dialog.points.model.setData(dialog.points.model.index(0, 1), 1.0) dialog.points.model.setData(dialog.points.model.index(1, 1), 6.0) assert dialog.button_plot.isEnabled() assert np.isclose(dialog.calibrations["A"].gradient, 4.0) # Points weightings assert dialog.calibrations["A"].weighting == "Equal" assert np.all(dialog.calibrations["A"].weights == 1.0) dialog.points.combo_weighting.setCurrentText("y") assert dialog.calibrations["A"].weighting == "y" assert np.all( dialog.calibrations["A"].weights == dialog.calibrations["A"].y) # Restored on change assert dialog.calibrations["B"].gradient == 1.0 assert dialog.calibrations["B"].intercept == 2.0 assert dialog.calibrations["B"].unit == "ppm" # Just run code as can't test clipboard dialog.copyToClipboard() dialog.copyAllToClipboard() dialog.combo_element.setCurrentIndex(0) dialog.showCurve() dialog.apply() dialog.check_all.setChecked(True) dialog.apply()
def test_calibration_points_table_model(): calibration = Calibration.from_points(np.array([[1.0, 1.0], [2.0, 2.0]])) model = CalibrationPointsTableModel(calibration, axes=(0, 1), counts_editable=True) # Check starting shape assert model.columnCount() == 3 assert model.rowCount() == 2 assert np.all(model.array == [[1.0, 1.0, 1.0], [2.0, 2.0, 1.0]]) model.setRowCount(3) assert model.rowCount() == 3 assert calibration.points.shape == (3, 2) # Add data model.setData(model.index(0, 0), "0.0") model.setData(model.index(0, 1), "1.0") model.setData(model.index(1, 0), "1.0") model.setData(model.index(1, 1), "2.0") model.setData(model.index(2, 0), "2.0") model.setData(model.index(2, 1), "3.0") assert model.data(model.index(0, 1)) == "1.0" assert calibration.points.shape == (3, 2) assert np.all(calibration.points == [[0.0, 1.0], [1.0, 2.0], [2.0, 3.0]]) assert np.isclose(calibration.intercept, 1.0) assert np.isclose(calibration.gradient, 1.0) model.setData(model.index(0, 1), "nan") assert calibration.points.shape == (3, 2) assert np.isnan(model.array[0, 1]) assert model.data(model.index(0, 1)) == "" # Change calibration model.setCalibration( Calibration.from_points(np.array([[0.0, 0.0], [2.0, 4.0]]))) assert model.columnCount() == 3 assert model.rowCount() == 2 # Check header assert (model.headerData(0, QtCore.Qt.Horizontal, QtCore.Qt.DisplayRole) == "Concentration") assert (model.headerData(1, QtCore.Qt.Horizontal, QtCore.Qt.DisplayRole) == "Response") assert model.headerData(0, QtCore.Qt.Vertical, QtCore.Qt.DisplayRole) == "A" assert model.flags(model.index(0, 0)) & QtCore.Qt.ItemIsEditable assert model.flags(model.index(0, 1)) & QtCore.Qt.ItemIsEditable
def test_laser_widget(qtbot: QtBot): x = rand_data(["A1", "B2"]) y = x["A1"].copy() viewspace = LaserViewSpace() qtbot.addWidget(viewspace) viewspace.show() view = viewspace.activeView() view.addLaser(Laser(x)) widget = view.activeWidget() widget.applyConfig(Config(1.0, 1.0, 1.0)) assert widget.laser.config.spotsize == 1.0 widget.applyCalibration({"B2": Calibration(2.0, 2.0)}) assert widget.laser.calibration["B2"].intercept == 2.0 widget.updateNames({"A1": "A1", "B2": "2B"}) assert np.all(viewspace.uniqueElements() == ["2B", "A1"]) widget.transform(flip="horizontal") assert np.all(widget.laser.get("A1") == np.flip(y, axis=1)) widget.transform(flip="horizontal") widget.transform(flip="vertical") assert np.all(widget.laser.get("A1") == np.flip(y, axis=0)) widget.transform(flip="vertical") assert np.all(widget.laser.get("A1") == y) widget.transform(rotate="right") assert np.all(widget.laser.get("A1") == np.rot90(y, k=1, axes=(1, 0))) widget.transform(rotate="left") assert np.all(widget.laser.get("A1") == y)
def keyPressEvent(self, event: QtGui.QKeyEvent) -> None: if event.matches(QtGui.QKeySequence.Cancel): self.graphics.endSelection() self.graphics.endWidget() elif event.matches(QtGui.QKeySequence.Paste): mime = QtWidgets.QApplication.clipboard().mimeData() if mime.hasFormat("arplication/x-pew2config"): with BytesIO(mime.data("application/x-pew2config")) as fp: array = np.load(fp) if self.is_srr: config = SRRConfig.from_array(array) else: config = Config.from_array(array) self.applyConfig(config) elif mime.hasFormat("application/x-pew2calibration"): with BytesIO(mime.data("application/x-pew2calibration")) as fp: npy = np.load(fp) calibrations = {k: Calibration.from_array(npy[k]) for k in npy} self.applyCalibration(calibrations) super().keyPressEvent(event)
def test_calibration_points_table_model_weights(): calibration = Calibration.from_points( np.array([[1.0, 1.0], [2.0, 4.0], [3.0, 5.0]]), weights=("test", np.array([0.4, 0.4, 0.2])), ) model = CalibrationPointsTableModel(calibration, axes=(0, 1), counts_editable=False) assert np.all( model.array == [[1.0, 1.0, 0.4], [2.0, 4.0, 0.4], [3.0, 5.0, 0.2]]) assert model.flags(model.index(0, 2)) & QtCore.Qt.ItemIsEditable model.setData(model.index(0, 2), "0.6") assert calibration._weights[0] == 0.6 model.setWeighting("x") assert np.all( model.array == [[1.0, 1.0, 1.0], [2.0, 4.0, 2.0], [3.0, 5.0, 3.0]]) assert not model.flags(model.index(0, 2)) & QtCore.Qt.ItemIsEditable model.setWeighting("test") assert model.flags(model.index(0, 2)) & QtCore.Qt.ItemIsEditable
def test_laser_view_space(qtbot: QtBot): viewspace = LaserViewSpace() qtbot.addWidget(viewspace) viewspace.show() viewspace.toggleCalibrate(False) viewspace.toggleColorbar(False) viewspace.toggleLabel(False) viewspace.toggleScalebar(False) viewspace.toggleSmooth(False) assert not viewspace.options.calibrate assert not viewspace.options.items["colorbar"] assert not viewspace.options.items["label"] assert not viewspace.options.items["scalebar"] assert not viewspace.options.smoothing viewspace.splitActiveHorizontal() assert viewspace.currentElement() is None viewspace.views[0].addLaser(Laser(rand_data(["A1", "B2"]))) viewspace.views[0].addLaser(Laser(rand_data(["A1", "C3"]))) viewspace.views[1].addLaser(Laser(rand_data(["A1", "C3"]))) viewspace.views[1].addLaser(Laser(rand_data(["B2", "D4"]))) assert viewspace.uniqueElements() == ["A1", "B2", "C3", "D4"] assert viewspace.currentElement() == "A1" # Apply config viewspace.applyConfig(Config(10, 10, 10)) for view in viewspace.views: for widget in view.widgets(): assert widget.laser.config.spotsize == 10 assert widget.laser.config.speed == 10 assert widget.laser.config.scantime == 10 # Try to apply calibraiton viewspace.applyCalibration({ "A1": Calibration(1.0, 1.0), "B2": Calibration(2.0, 2.0) }) qtbot.waitExposed(viewspace) for view in viewspace.views: for widget in view.widgets(): if "A1" in widget.laser.elements: assert widget.laser.calibration["A1"].intercept == 1.0 assert widget.laser.calibration["A1"].gradient == 1.0 if "B2" in widget.laser.elements: assert widget.laser.calibration["B2"].intercept == 2.0 assert widget.laser.calibration["B2"].gradient == 2.0 if "C3" in widget.laser.elements: assert widget.laser.calibration["C3"].intercept == 0.0 assert widget.laser.calibration["C3"].gradient == 1.0 # Check element changed if avilable assert viewspace.views[0].activeWidget().combo_element.currentText( ) == "A1" assert viewspace.views[1].activeWidget().combo_element.currentText( ) == "A1" viewspace.setCurrentElement("B2") assert viewspace.views[0].activeWidget().combo_element.currentText( ) == "B2" assert viewspace.views[1].activeWidget().combo_element.currentText( ) == "A1" # Close all for view in viewspace.views: for widget in view.widgets(): widget.close()
def test_calibration_curve_dialog(qtbot: QtBot): dialog = dialogs.CalibrationCurveDialog( "A", Calibration.from_points([[0, 1], [1, 2], [2, 3], [4, 4]])) qtbot.addWidget(dialog) dialog.open()