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 test_main_window_actions_widget(qtbot: QtBot): window = MainWindow() qtbot.addWidget(window) window.viewspace.views[0].addLaser( Laser(rand_data("A1"), info={"File Path": "/home/pewpew/real.npz"})) window.viewspace.refresh() assert window.action_export_all.isEnabled() assert window.action_tool_calculator.isEnabled() assert window.action_tool_drift.isEnabled() assert window.action_tool_filter.isEnabled() assert window.action_tool_standards.isEnabled() assert window.action_tool_overlay.isEnabled() window.actionTransformFlipHorz() window.actionTransformFlipVert() window.actionTransformRotateLeft() window.actionTransformRotateRight() window.actionExportAll() window.actionToolCalculator() window.actionToolDrift() window.actionToolFilter() window.actionToolStandards() window.actionToolOverlay()
def test_export_dialog_names(qtbot: QtBot): viewspace = LaserViewSpace() qtbot.addWidget(viewspace) view = viewspace.activeView() widget = view.addLaser( Laser( rand_data(["A", "\\B", "C>_<"]), info={ "Name": "inv@|d", "File Path": "/invalid.npz" }, )) dlg = ExportDialog(widget) dlg.lineedit_directory.setText(str(Path("."))) assert dlg.isComplete() dlg.lineedit_filename.setText("invalid.csv") assert dlg.isComplete() dlg.check_export_all.setChecked(True) paths = dlg.generatePaths(widget.laser) assert paths[0][0].name == "invalid_A.csv" assert paths[1][0].name == "invalid__B.csv" assert paths[2][0].name == "invalid_C___.csv"
def test_merge_tool(qtbot: QtBot): viewspace = LaserViewSpace() qtbot.addWidget(viewspace) viewspace.show() view = viewspace.activeView() data = rand_data(["A", "B"]) view.addLaser(Laser(data, info={"Name": "Laser 1", "File Path": "/test/laser1"})) data = rand_data(["B", "C"]) view.addLaser(Laser(data, info={"Name": "Laser 2", "File Path": "/test/laser1"})) tool = MergeTool(view.activeWidget()) view.addTab("Tool", tool) qtbot.waitExposed(tool) assert tool.list.count() == 1 # Test load via dialog dlg = tool.addLaserDialog() assert dlg.comboBoxItems() == ["Laser 2"] dlg.accept() assert tool.list.count() == 2 assert [row.offset() == (0, 0) for row in tool.list.rows] assert tool.list.rows[0].combo_element.currentText() == "A" assert tool.list.rows[1].combo_element.currentText() == "B" tool.action_align_horz.trigger() assert tool.list.rows[0].offset() == (0, 0) assert tool.list.rows[1].offset() == (0, 10) tool.action_align_vert.trigger() assert tool.list.rows[0].offset() == (0, 0) assert tool.list.rows[1].offset() == (10, 0) tool.action_align_auto.trigger() assert tool.list.rows[0].offset() == (0, 0) # Second image position unknown tool.apply()
def test_tool_calculator(qtbot: QtBot): viewspace = LaserViewSpace() qtbot.addWidget(viewspace) viewspace.show() view = viewspace.activeView() widget = view.addLaser(Laser(rand_data(["a", "b"]))) tool = CalculatorTool(view.activeWidget()) view.addTab("Tool", tool) qtbot.waitExposed(tool) assert tool.lineedit_name.text() == "calc0" tool.apply() assert "calc0" in widget.laser.elements assert tool.lineedit_name.text() == "calc1" # overwrite tool.lineedit_name.setText("a") tool.apply() assert len(widget.laser.elements) == 3 # Inserters assert tool.formula.toPlainText() == "a" tool.insertFunction(1) assert tool.formula.toPlainText() == "abs(a" tool.insertVariable(2) assert tool.formula.toPlainText() == "abs(ba" # Test output of previewData and output lineedit x = np.array(np.random.random((10, 10)), dtype=[("a", float)]) tool.formula.setPlainText("mean(a)") assert tool.previewData(x) is None assert tool.output.text() == f"{np.mean(x['a']):.10g}" # Array access in output tool.formula.setPlainText("a[0]") assert tool.previewData(x) is None assert tool.output.text() == f"{list(map('{:.4g}'.format, x['a'][0]))}" # Simple op tool.formula.setPlainText("a + 1.0") assert np.all(tool.previewData(x) == x["a"] + 1.0) assert tool.isComplete() # Invalid input tool.formula.setPlainText("fail") assert tool.previewData(x) is None assert not tool.isComplete()
def test_laser_widget_cursor(qtbot: QtBot): main = QtWidgets.QMainWindow() qtbot.addWidget(main) main.statusBar() # Create bar viewspace = LaserViewSpace() main.setCentralWidget(viewspace) view = viewspace.activeView() view.addLaser(Laser(rand_data(["a"]))) widget = view.activeWidget() # Cursor widget.updateCursorStatus(2.0, 2.0, 1.0) assert main.statusBar().currentMessage() == "2,2 [1]" widget.updateCursorStatus(1.0, 3.0, np.nan) assert main.statusBar().currentMessage() == "1,3 [nan]"
def test_laser_widget_actions(qtbot: QtBot): viewspace = LaserViewSpace() qtbot.addWidget(viewspace) viewspace.show() view = viewspace.activeView() view.addLaser( Laser(rand_data(["a", "b"]), info={"File Path": "/home/pewpew/real.npz"})) widget = view.activeWidget() dlg = widget.actionCalibration() dlg.close() dlg = widget.actionConfig() dlg.close() widget.actionDuplicate() widget.actionCopyImage() dlg = widget.actionExport() dlg.close() dlg = widget.actionSave() dlg.close() dlg = widget.actionStatistics() dlg.close() dlg = widget.actionSelectDialog() dlg.close() dlg = widget.actionColocal() dlg.close() widget.contextMenuEvent( QtGui.QContextMenuEvent(QtGui.QContextMenuEvent.Mouse, QtCore.QPoint(0, 0))) # Test contextmenu widget.graphics.mask = np.ones((10, 10), dtype=bool) widget.contextMenuEvent( QtGui.QContextMenuEvent( QtGui.QContextMenuEvent.Mouse, widget.graphics.mapFromScene(QtCore.QPointF(0, 0)), )) widget.actionCopySelectionText() widget.actionCropSelection() dlg = widget.actionStatisticsSelection() dlg.close() dlg = widget.actionColocalSelection() dlg.close()
def test_laser_view(qtbot: QtBot): viewspace = LaserViewSpace() qtbot.addWidget(viewspace) viewspace.show() view = viewspace.activeView() laser = view.addLaser(Laser(rand_data(["A1", "B2", "C3"]))) qtbot.waitExposed(laser) view.tabs.setTabText(0, "newname") assert view.stack.widget(0).laserName() == "newname" view.contextMenuEvent( QtGui.QContextMenuEvent(QtGui.QContextMenuEvent.Mouse, QtCore.QPoint(0, 0))) # Drop event drag_mime = QtCore.QMimeData() path = Path(__file__).parent.joinpath("data", "io", "npz", "test.npz") drag_mime.setUrls([QtCore.QUrl.fromLocalFile(str(path.resolve()))]) drag_event = QtGui.QDragEnterEvent( QtCore.QPoint(0, 0), QtCore.Qt.CopyAction, drag_mime, QtCore.Qt.LeftButton, QtCore.Qt.NoModifier, ) view.dragEnterEvent(drag_event) assert drag_event.isAccepted() drop_event = QtGui.QDropEvent( QtCore.QPoint(0, 0), QtCore.Qt.CopyAction, drag_mime, QtCore.Qt.LeftButton, QtCore.Qt.NoModifier, ) with qtbot.waitSignal(view.numTabsChanged): view.dropEvent(drop_event) assert drop_event.isAccepted() assert len(view.widgets()) == 2 dlg = view.actionOpen() dlg.show() dlg.close()
def test_laser_view_space_apply_dialogs(qtbot: QtBot): viewspace = LaserViewSpace() qtbot.addWidget(viewspace) viewspace.show() qtbot.addWidget(viewspace) viewspace.views[0].addLaser(Laser(rand_data("A1"))) viewspace.refresh() dlg = viewspace.configDialog() dlg.applyPressed.emit(dlg) dlg.close() dlg = viewspace.colortableRangeDialog() dlg.applyPressed.emit(dlg) dlg.close() dlg = viewspace.fontsizeDialog() dlg.intValueSelected.emit(5) dlg.close() assert viewspace.options.font.pointSize() == 5
def test_tool_filter(qtbot: QtBot): viewspace = LaserViewSpace() qtbot.addWidget(viewspace) viewspace.show() view = viewspace.activeView() view.addLaser(Laser(rand_data(["a"]))) tool = FilteringTool(view.activeWidget()) view.addTab("Tool", tool) qtbot.waitExposed(tool) tool.combo_filter.setCurrentText("Mean") tool.combo_filter.activated.emit(0) tool.lineedit_fparams[0].setText("3.0") tool.lineedit_fparams[1].setText("3.0") tool.lineedit_fparams[0].editingFinished.emit() assert np.all(tool.fparams == [3.0, 3.0]) assert tool.isComplete() tool.lineedit_fparams[0].setText("5.0") tool.lineedit_fparams[0].editingFinished.emit() assert np.all(tool.fparams == [5.0, 3.0]) assert tool.isComplete() tool.combo_filter.setCurrentText("Median") tool.combo_filter.activated.emit(0) assert np.all(tool.fparams == [5.0, 3.0]) assert tool.isComplete() tool.lineedit_fparams[0].setText("4.0") tool.lineedit_fparams[0].editingFinished.emit() assert not tool.isComplete() tool.combo_filter.setCurrentText("Mean") tool.combo_filter.activated.emit(0) assert np.all(tool.fparams == [5.0, 3.0]) assert tool.isComplete() tool.apply()
def test_tool_widget(qtbot: QtBot): viewspace = LaserViewSpace() qtbot.addWidget(viewspace) viewspace.show() view = viewspace.activeView() widget = view.addLaser(Laser(rand_data("A1"), info={"Name": "Widget"})) tool = ToolWidget(widget, apply_all=True) index = widget.index widget.view.removeTab(index) widget.view.insertTab(index, "Tool", tool) qtbot.waitExposed(tool) tool.requestClose() view.tabs.tabText(index) == "Widget" with qtbot.wait_signal(tool.applyPressed): button = tool.button_box.button(QtWidgets.QDialogButtonBox.Apply) button.click() with qtbot.wait_signal(tool.applyPressed): button = tool.button_box.button(QtWidgets.QDialogButtonBox.Ok) button.click()
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_export_dialog(qtbot: QtBot): viewspace = LaserViewSpace() qtbot.addWidget(viewspace) view = viewspace.activeView() widget = view.addLaser( Laser(rand_data("A1"), info={ "Name": "laser", "File Path": "/home/user/laser.npz" })) dlg = ExportDialog(widget) dlg.open() assert dlg.lineedit_directory.text() == "/home/user" assert dlg.lineedit_filename.text() == "laser.npz" assert dlg.lineedit_preview.text() == "laser.npz" assert dlg.options.currentExt() == ".npz" assert not dlg.check_export_all.isEnabled() assert not dlg.check_calibrate.isEnabled() dlg.lineedit_filename.setText("laser.png") assert dlg.lineedit_preview.text() == "laser.png" assert dlg.options.currentExt() == ".png" dlg.lineedit_filename.setText("laser") assert dlg.lineedit_preview.text() == "laser.png" assert dlg.options.currentExt() == ".png" dlg.check_export_all.click() assert dlg.lineedit_preview.text() == "laser_<element>.png" dlg.lineedit_filename.setText("laser.npz") assert dlg.options.currentExt() == ".npz" assert not dlg.check_export_all.isEnabled() assert dlg.lineedit_preview.text() == "laser.npz" dlg.lineedit_filename.setText("laser.abc") assert not dlg.isComplete() dlg.lineedit_filename.setText("laser.npz") dir_dlg = dlg.selectDirectory() dir_dlg.close() dlg.lineedit_filename.setText("/fake/directory") assert not dlg.isComplete() with tempfile.TemporaryDirectory() as tempdir: # Test export dlg.lineedit_directory.setText(tempdir) dlg.lineedit_filename.setText("temp.npz") paths = dlg.generatePaths(dlg.widget) assert paths == [(Path(tempdir, "temp.npz"), "A1", None)] dlg.export(paths[0][0], paths[0][1], None, dlg.widget) assert Path(tempdir, "temp.npz").exists() # Test export all elements and png with tempfile.TemporaryDirectory() as tempdir: dlg.lineedit_directory.setText(tempdir) dlg.lineedit_filename.setText("temp.png") dlg.accept() assert Path(tempdir, "temp_A1.png").exists() with tempfile.TemporaryDirectory() as tempdir: dlg.lineedit_directory.setText(tempdir) dlg.lineedit_filename.setText("temp.vti") dlg.accept() assert Path(tempdir, "temp.vti").exists() dlg.close()
def test_export_all_dialog(qtbot: QtBot): viewspace = LaserViewSpace() qtbot.addWidget(viewspace) view = viewspace.activeView() lasers = [ Laser( rand_data("A1"), info={ "Name": "laser1", "File Path": "/fake/directory/laser1.npz" }, ), Laser( rand_data("B2"), info={ "Name": "laser2", "File Path": "/fake/directory/laser2.npz" }, ), Laser( rand_data("C3"), info={ "Name": "laser3", "File Path": "/fake/directory/laser3.npz" }, ), Laser( rand_data(["B2", "C3"]), info={ "Name": "laser4", "File Path": "/fake/directory/laser4.npz" }, ), ] widgets = [view.addLaser(laser) for laser in lasers] dlg = ExportAllDialog(widgets) dlg.open() assert dlg.lineedit_directory.text() == "/fake/directory" assert dlg.lineedit_filename.text() == "<name>.npz" assert dlg.lineedit_preview.text() == "<name>.npz" assert not dlg.isComplete() dlg.lineedit_prefix.setText("01") assert dlg.lineedit_preview.text() == "01_<name>.npz" assert not dlg.check_export_all.isEnabled() assert not dlg.check_calibrate.isEnabled() assert not dlg.combo_element.isEnabled() dlg.options.setCurrentIndex(dlg.options.indexForExt(".csv")) assert dlg.lineedit_preview.text() == "01_<name>.csv" assert dlg.check_export_all.isEnabled() assert dlg.check_calibrate.isEnabled() assert dlg.combo_element.isEnabled() dlg.check_export_all.click() assert dlg.lineedit_preview.text() == "01_<name>_<element>.csv" assert not dlg.combo_element.isEnabled() with tempfile.TemporaryDirectory() as tempdir: dlg.lineedit_directory.setText(tempdir) assert dlg.isComplete() dlg.accept() assert Path(tempdir, "01_laser1_A1.csv").exists() assert Path(tempdir, "01_laser2_B2.csv").exists() assert Path(tempdir, "01_laser3_C3.csv").exists() assert Path(tempdir, "01_laser4_B2.csv").exists() assert Path(tempdir, "01_laser4_C3.csv").exists()