0
0
mirror of https://github.com/bec-project/bec_widgets.git synced 2025-07-13 19:21:50 +02:00

feat(multi-waveform): new widget added

This commit is contained in:
2024-10-15 23:17:47 +02:00
committed by wyzula_j
parent ec39dae273
commit f3a39a69e2
17 changed files with 2073 additions and 10 deletions

View File

@ -65,16 +65,18 @@ def test_add_remove_bec_figure_to_dock(bec_dock_area):
plt = fig.plot(x_name="samx", y_name="bpm4i")
im = fig.image("eiger")
mm = fig.motor_map("samx", "samy")
mw = fig.multi_waveform("waveform1d")
assert len(bec_dock_area.dock_area.docks) == 1
assert len(d0.widgets) == 1
assert len(d0.widget_list) == 1
assert len(fig.widgets) == 3
assert len(fig.widgets) == 4
assert fig.config.widget_class == "BECFigure"
assert plt.config.widget_class == "BECWaveform"
assert im.config.widget_class == "BECImageShow"
assert mm.config.widget_class == "BECMotorMap"
assert mw.config.widget_class == "BECMultiWaveform"
def test_close_docks(bec_dock_area, qtbot):

View File

@ -6,6 +6,7 @@ import pytest
from bec_widgets.widgets.figure import BECFigure
from bec_widgets.widgets.figure.plots.image.image import BECImageShow
from bec_widgets.widgets.figure.plots.motor_map.motor_map import BECMotorMap
from bec_widgets.widgets.figure.plots.multi_waveform.multi_waveform import BECMultiWaveform
from bec_widgets.widgets.figure.plots.waveform.waveform import BECWaveform
from .client_mocks import mocked_client
@ -63,10 +64,12 @@ def test_add_different_types_of_widgets(qtbot, mocked_client):
plt = bec_figure.plot(x_name="samx", y_name="bpm4i")
im = bec_figure.image("eiger")
motor_map = bec_figure.motor_map("samx", "samy")
multi_waveform = bec_figure.multi_waveform("waveform")
assert plt.__class__ == BECWaveform
assert im.__class__ == BECImageShow
assert motor_map.__class__ == BECMotorMap
assert multi_waveform.__class__ == BECMultiWaveform
def test_access_widgets_access_errors(qtbot, mocked_client):

View File

@ -0,0 +1,253 @@
from unittest import mock
import numpy as np
import pytest
from bec_lib.endpoints import messages
from bec_widgets.utils import Colors
from bec_widgets.widgets.figure import BECFigure
from .client_mocks import mocked_client
from .conftest import create_widget
def test_set_monitor(qtbot, mocked_client):
"""Test that setting the monitor connects the appropriate slot."""
# Create a BECFigure
bec_figure = create_widget(qtbot, BECFigure, client=mocked_client)
# Add a multi_waveform plot
multi_waveform = bec_figure.multi_waveform()
multi_waveform.set_monitor("waveform1d")
assert multi_waveform.config.monitor == "waveform1d"
assert multi_waveform.connected is True
data_0 = np.random.rand(100)
msg = messages.DeviceMonitor1DMessage(
device="waveform1d", data=data_0, metadata={"scan_id": "12345"}
)
multi_waveform.on_monitor_1d_update(msg.content, msg.metadata)
data_waveform = multi_waveform.get_all_data()
print(data_waveform)
assert len(data_waveform) == 1
assert np.array_equal(data_waveform["curve_0"]["y"], data_0)
data_1 = np.random.rand(100)
msg = messages.DeviceMonitor1DMessage(
device="waveform1d", data=data_1, metadata={"scan_id": "12345"}
)
multi_waveform.on_monitor_1d_update(msg.content, msg.metadata)
data_waveform = multi_waveform.get_all_data()
assert len(data_waveform) == 2
assert np.array_equal(data_waveform["curve_0"]["y"], data_0)
assert np.array_equal(data_waveform["curve_1"]["y"], data_1)
def test_on_monitor_1d_update(qtbot, mocked_client):
"""Test that data updates add curves to the plot."""
# Create a BECFigure
bec_figure = create_widget(qtbot, BECFigure, client=mocked_client)
# Add a multi_waveform plot
multi_waveform = bec_figure.multi_waveform()
multi_waveform.set_monitor("test_monitor")
# Simulate receiving data updates
test_data = np.array([1, 2, 3, 4, 5])
msg = {"data": test_data}
metadata = {"scan_id": "scan_1"}
# Call the on_monitor_1d_update method
multi_waveform.on_monitor_1d_update(msg, metadata)
# Check that a curve has been added
assert len(multi_waveform.curves) == 1
# Check that the data in the curve is correct
curve = multi_waveform.curves[-1]
x_data, y_data = curve.getData()
assert np.array_equal(y_data, test_data)
# Simulate another data update
test_data_2 = np.array([6, 7, 8, 9, 10])
msg2 = {"data": test_data_2}
metadata2 = {"scan_id": "scan_1"}
multi_waveform.on_monitor_1d_update(msg2, metadata2)
# Check that another curve has been added
assert len(multi_waveform.curves) == 2
# Check that the data in the curve is correct
curve2 = multi_waveform.curves[-1]
x_data2, y_data2 = curve2.getData()
assert np.array_equal(y_data2, test_data_2)
def test_set_curve_limit_no_flush(qtbot, mocked_client):
"""Test set_curve_limit with flush_buffer=False."""
# Create a BECFigure
bec_figure = create_widget(qtbot, BECFigure, client=mocked_client)
# Add a multi_waveform plot
multi_waveform = bec_figure.multi_waveform()
multi_waveform.set_monitor("test_monitor")
# Simulate adding multiple curves
for i in range(5):
test_data = np.array([i, i + 1, i + 2])
msg = {"data": test_data}
metadata = {"scan_id": "scan_1"}
multi_waveform.on_monitor_1d_update(msg, metadata)
# Check that there are 5 curves
assert len(multi_waveform.curves) == 5
# Set curve limit to 3 with flush_buffer=False
multi_waveform.set_curve_limit(3, flush_buffer=False)
# Check that curves are hidden, but not removed
assert len(multi_waveform.curves) == 5
visible_curves = [curve for curve in multi_waveform.curves if curve.isVisible()]
assert len(visible_curves) == 3
# The first two curves should be hidden
assert not multi_waveform.curves[0].isVisible()
assert not multi_waveform.curves[1].isVisible()
assert multi_waveform.curves[2].isVisible()
assert multi_waveform.curves[3].isVisible()
assert multi_waveform.curves[4].isVisible()
def test_set_curve_limit_flush(qtbot, mocked_client):
"""Test set_curve_limit with flush_buffer=True."""
# Create a BECFigure
bec_figure = create_widget(qtbot, BECFigure, client=mocked_client)
# Add a multi_waveform plot
multi_waveform = bec_figure.multi_waveform()
multi_waveform.set_monitor("test_monitor")
# Simulate adding multiple curves
for i in range(5):
test_data = np.array([i, i + 1, i + 2])
msg = {"data": test_data}
metadata = {"scan_id": "scan_1"}
multi_waveform.on_monitor_1d_update(msg, metadata)
# Check that there are 5 curves
assert len(multi_waveform.curves) == 5
# Set curve limit to 3 with flush_buffer=True
multi_waveform.set_curve_limit(3, flush_buffer=True)
# Check that only 3 curves remain
assert len(multi_waveform.curves) == 3
# The curves should be the last 3 added
x_data, y_data = multi_waveform.curves[0].getData()
assert np.array_equal(y_data, [2, 3, 4])
x_data, y_data = multi_waveform.curves[1].getData()
assert np.array_equal(y_data, [3, 4, 5])
x_data, y_data = multi_waveform.curves[2].getData()
assert np.array_equal(y_data, [4, 5, 6])
def test_set_curve_highlight(qtbot, mocked_client):
"""Test that the correct curve is highlighted."""
# Create a BECFigure
bec_figure = create_widget(qtbot, BECFigure, client=mocked_client)
# Add a multi_waveform plot
multi_waveform = bec_figure.multi_waveform()
multi_waveform.set_monitor("test_monitor")
# Simulate adding multiple curves
for i in range(3):
test_data = np.array([i, i + 1, i + 2])
msg = {"data": test_data}
metadata = {"scan_id": "scan_1"}
multi_waveform.on_monitor_1d_update(msg, metadata)
# Set highlight_last_curve to False
multi_waveform.highlight_last_curve = False
multi_waveform.set_curve_highlight(1) # Highlight the second curve (index 1)
# Check that the second curve is highlighted
visible_curves = [curve for curve in multi_waveform.curves if curve.isVisible()]
# Reverse the list to match indexing in set_curve_highlight
visible_curves = list(reversed(visible_curves))
for i, curve in enumerate(visible_curves):
pen = curve.opts["pen"]
width = pen.width()
if i == 1:
# Highlighted curve should have width 5
assert width == 5
else:
assert width == 1
def test_set_opacity(qtbot, mocked_client):
"""Test that setting opacity updates the curves."""
# Create a BECFigure
bec_figure = create_widget(qtbot, BECFigure, client=mocked_client)
# Add a multi_waveform plot
multi_waveform = bec_figure.multi_waveform()
multi_waveform.set_monitor("waveform1d")
# Simulate adding a curve
test_data = np.array([1, 2, 3])
msg = {"data": test_data}
metadata = {"scan_id": "scan_1"}
multi_waveform.on_monitor_1d_update(msg, metadata)
# Set opacity to 30
multi_waveform.set_opacity(30)
assert multi_waveform.config.opacity == 30
def test_set_colormap(qtbot, mocked_client):
"""Test that setting the colormap updates the curve colors."""
# Create a BECFigure
bec_figure = create_widget(qtbot, BECFigure, client=mocked_client)
# Add a multi_waveform plot
multi_waveform = bec_figure.multi_waveform()
multi_waveform.set_monitor("waveform1d")
# Simulate adding multiple curves
for i in range(3):
test_data = np.array([i, i + 1, i + 2])
msg = {"data": test_data}
metadata = {"scan_id": "scan_1"}
multi_waveform.on_monitor_1d_update(msg, metadata)
# Set a new colormap
multi_waveform.set_opacity(100)
multi_waveform.set_colormap("viridis")
# Check that the colors of the curves have changed accordingly
visible_curves = [curve for curve in multi_waveform.curves if curve.isVisible()]
# Get the colors applied
colors = Colors.evenly_spaced_colors(colormap="viridis", num=len(visible_curves), format="HEX")
for i, curve in enumerate(visible_curves):
pen = curve.opts["pen"]
pen_color = pen.color().name()
expected_color = colors[i]
# Compare pen color to expected color
assert pen_color.lower() == expected_color.lower()
def test_export_to_matplotlib(qtbot, mocked_client):
"""Test that export_to_matplotlib can be called without errors."""
try:
import matplotlib
except ImportError:
pytest.skip("Matplotlib not installed")
# Create a BECFigure
bec_figure = create_widget(qtbot, BECFigure, client=mocked_client)
# Add a multi_waveform plot
multi_waveform = bec_figure.multi_waveform()
multi_waveform.set_monitor("test_monitor")
# Simulate adding a curve
test_data = np.array([1, 2, 3])
msg = {"data": test_data}
metadata = {"scan_id": "scan_1"}
multi_waveform.on_monitor_1d_update(msg, metadata)
# Call export_to_matplotlib
with mock.patch("pyqtgraph.exporters.MatplotlibExporter.export") as mock_export:
multi_waveform.export_to_matplotlib()
mock_export.assert_called_once()

View File

@ -0,0 +1,295 @@
from unittest.mock import MagicMock, patch
import pytest
from qtpy.QtGui import QColor
from qtpy.QtWidgets import QApplication
from bec_widgets.qt_utils.settings_dialog import SettingsDialog
from bec_widgets.utils.colors import apply_theme, get_theme_palette, set_theme
from bec_widgets.widgets.figure.plots.axis_settings import AxisSettings
from bec_widgets.widgets.multi_waveform.multi_waveform_widget import BECMultiWaveformWidget
from .client_mocks import mocked_client
@pytest.fixture
def multi_waveform_widget(qtbot, mocked_client):
widget = BECMultiWaveformWidget(client=mocked_client())
qtbot.addWidget(widget)
qtbot.waitExposed(widget)
return widget
@pytest.fixture
def mock_waveform(multi_waveform_widget):
waveform_mock = MagicMock()
multi_waveform_widget.waveform = waveform_mock
return waveform_mock
def test_multi_waveform_widget_init(multi_waveform_widget):
assert multi_waveform_widget is not None
assert multi_waveform_widget.client is not None
assert isinstance(multi_waveform_widget, BECMultiWaveformWidget)
assert multi_waveform_widget.config.widget_class == "BECMultiWaveformWidget"
###################################
# Wrapper methods for Waveform
###################################
def test_multi_waveform_widget_set_monitor(multi_waveform_widget, mock_waveform):
multi_waveform_widget.set_monitor("waveform1d")
mock_waveform.set_monitor.assert_called_once_with("waveform1d")
def test_multi_waveform_widget_set_curve_highlight_last_active(
multi_waveform_widget, mock_waveform
):
multi_waveform_widget.set_curve_highlight(1)
mock_waveform.set_curve_highlight.assert_called_once_with(-1)
def test_multi_waveform_widget_set_curve_highlight_last_not_active(
multi_waveform_widget, mock_waveform
):
multi_waveform_widget.set_highlight_last_curve(False)
multi_waveform_widget.set_curve_highlight(1)
mock_waveform.set_curve_highlight.assert_called_with(1)
def test_multi_waveform_widget_set_opacity(multi_waveform_widget, mock_waveform):
multi_waveform_widget.set_opacity(50)
mock_waveform.set_opacity.assert_called_once_with(50)
def test_multi_waveform_widget_set_curve_limit(multi_waveform_widget, mock_waveform):
multi_waveform_widget.set_curve_limit(10)
mock_waveform.set_curve_limit.assert_called_once_with(
10, multi_waveform_widget.controls.checkbox_flush_buffer.isChecked()
)
def test_multi_waveform_widget_set_buffer_flush(multi_waveform_widget, mock_waveform):
multi_waveform_widget.set_buffer_flush(True)
mock_waveform.set_curve_limit.assert_called_once_with(
multi_waveform_widget.controls.spinbox_max_trace.value(), True
)
def test_multi_waveform_widget_set_highlight_last_curve(multi_waveform_widget, mock_waveform):
multi_waveform_widget.set_highlight_last_curve(True)
assert multi_waveform_widget.waveform.config.highlight_last_curve is True
assert not multi_waveform_widget.controls.slider_index.isEnabled()
assert not multi_waveform_widget.controls.spinbox_index.isEnabled()
mock_waveform.set_curve_highlight.assert_called_once_with(-1)
def test_multi_waveform_widget_set_colormap(multi_waveform_widget, mock_waveform):
multi_waveform_widget.set_colormap("viridis")
mock_waveform.set_colormap.assert_called_once_with("viridis")
def test_multi_waveform_widget_set_base(multi_waveform_widget, mock_waveform):
multi_waveform_widget.set(
title="Test Title",
x_label="X Label",
y_label="Y Label",
x_scale="linear",
y_scale="log",
x_lim=(0, 10),
y_lim=(0, 10),
)
mock_waveform.set.assert_called_once_with(
title="Test Title",
x_label="X Label",
y_label="Y Label",
x_scale="linear",
y_scale="log",
x_lim=(0, 10),
y_lim=(0, 10),
)
###################################
# Toolbar interactions
###################################
def test_toolbar_connect_action_triggered(multi_waveform_widget, qtbot):
action_connect = multi_waveform_widget.toolbar.widgets["connect"].action
device_combobox = multi_waveform_widget.toolbar.widgets["monitor"].device_combobox
device_combobox.addItem("test_monitor")
device_combobox.setCurrentText("test_monitor")
with patch.object(multi_waveform_widget, "set_monitor") as mock_set_monitor:
action_connect.trigger()
mock_set_monitor.assert_called_once_with(monitor="test_monitor")
def test_toolbar_drag_mode_action_triggered(multi_waveform_widget, qtbot):
action_drag = multi_waveform_widget.toolbar.widgets["drag_mode"].action
action_rectangle = multi_waveform_widget.toolbar.widgets["rectangle_mode"].action
action_drag.trigger()
assert action_drag.isChecked() == True
assert action_rectangle.isChecked() == False
def test_toolbar_rectangle_mode_action_triggered(multi_waveform_widget, qtbot):
action_drag = multi_waveform_widget.toolbar.widgets["drag_mode"].action
action_rectangle = multi_waveform_widget.toolbar.widgets["rectangle_mode"].action
action_rectangle.trigger()
assert action_drag.isChecked() == False
assert action_rectangle.isChecked() == True
def test_toolbar_auto_range_action_triggered(multi_waveform_widget, mock_waveform, qtbot):
action = multi_waveform_widget.toolbar.widgets["auto_range"].action
action.trigger()
qtbot.wait(200)
mock_waveform.set_auto_range.assert_called_once_with(True, "xy")
###################################
# Control Panel interactions
###################################
def test_controls_opacity_slider(multi_waveform_widget, mock_waveform):
multi_waveform_widget.controls.slider_opacity.setValue(75)
mock_waveform.set_opacity.assert_called_with(75)
assert multi_waveform_widget.controls.spinbox_opacity.value() == 75
def test_controls_opacity_spinbox(multi_waveform_widget, mock_waveform):
multi_waveform_widget.controls.spinbox_opacity.setValue(25)
mock_waveform.set_opacity.assert_called_with(25)
assert multi_waveform_widget.controls.slider_opacity.value() == 25
def test_controls_max_trace_spinbox(multi_waveform_widget, mock_waveform):
multi_waveform_widget.controls.spinbox_max_trace.setValue(15)
mock_waveform.set_curve_limit.assert_called_with(
15, multi_waveform_widget.controls.checkbox_flush_buffer.isChecked()
)
def test_controls_flush_buffer_checkbox(multi_waveform_widget, mock_waveform):
multi_waveform_widget.controls.checkbox_flush_buffer.setChecked(True)
mock_waveform.set_curve_limit.assert_called_with(
multi_waveform_widget.controls.spinbox_max_trace.value(), True
)
def test_controls_highlight_checkbox(multi_waveform_widget, mock_waveform):
multi_waveform_widget.controls.checkbox_highlight.setChecked(False)
assert multi_waveform_widget.waveform.config.highlight_last_curve is False
assert multi_waveform_widget.controls.slider_index.isEnabled()
assert multi_waveform_widget.controls.spinbox_index.isEnabled()
index = multi_waveform_widget.controls.spinbox_index.value()
mock_waveform.set_curve_highlight.assert_called_with(index)
###################################
# Axis Settings Dialog Tests
###################################
def show_axis_dialog(qtbot, multi_waveform_widget):
axis_dialog = SettingsDialog(
multi_waveform_widget,
settings_widget=AxisSettings(),
window_title="Axis Settings",
config=multi_waveform_widget.waveform._config_dict["axis"],
)
qtbot.addWidget(axis_dialog)
qtbot.waitExposed(axis_dialog)
return axis_dialog
def test_axis_dialog_with_axis_limits(qtbot, multi_waveform_widget):
multi_waveform_widget.set(
title="Test Title",
x_label="X Label",
y_label="Y Label",
x_scale="linear",
y_scale="log",
x_lim=(0, 10),
y_lim=(0, 10),
)
axis_dialog = show_axis_dialog(qtbot, multi_waveform_widget)
assert axis_dialog is not None
assert axis_dialog.widget.ui.plot_title.text() == "Test Title"
assert axis_dialog.widget.ui.x_label.text() == "X Label"
assert axis_dialog.widget.ui.y_label.text() == "Y Label"
assert axis_dialog.widget.ui.x_scale.currentText() == "linear"
assert axis_dialog.widget.ui.y_scale.currentText() == "log"
assert axis_dialog.widget.ui.x_min.value() == 0
assert axis_dialog.widget.ui.x_max.value() == 10
assert axis_dialog.widget.ui.y_min.value() == 0
assert axis_dialog.widget.ui.y_max.value() == 10
def test_axis_dialog_set_properties(qtbot, multi_waveform_widget):
axis_dialog = show_axis_dialog(qtbot, multi_waveform_widget)
axis_dialog.widget.ui.plot_title.setText("New Title")
axis_dialog.widget.ui.x_label.setText("New X Label")
axis_dialog.widget.ui.y_label.setText("New Y Label")
axis_dialog.widget.ui.x_scale.setCurrentText("log")
axis_dialog.widget.ui.y_scale.setCurrentText("linear")
axis_dialog.widget.ui.x_min.setValue(5)
axis_dialog.widget.ui.x_max.setValue(15)
axis_dialog.widget.ui.y_min.setValue(5)
axis_dialog.widget.ui.y_max.setValue(15)
axis_dialog.accept()
assert multi_waveform_widget.waveform.config.axis.title == "New Title"
assert multi_waveform_widget.waveform.config.axis.x_label == "New X Label"
assert multi_waveform_widget.waveform.config.axis.y_label == "New Y Label"
assert multi_waveform_widget.waveform.config.axis.x_scale == "log"
assert multi_waveform_widget.waveform.config.axis.y_scale == "linear"
assert multi_waveform_widget.waveform.config.axis.x_lim == (5, 15)
assert multi_waveform_widget.waveform.config.axis.y_lim == (5, 15)
###################################
# Theme Update Test
###################################
def test_multi_waveform_widget_theme_update(qtbot, multi_waveform_widget):
"""Test theme update for multi waveform widget."""
qapp = QApplication.instance()
# Set the theme to dark
set_theme("dark")
palette = get_theme_palette()
waveform_color_dark = multi_waveform_widget.waveform.plot_item.getAxis("left").pen().color()
bg_color = multi_waveform_widget.fig.backgroundBrush().color()
assert bg_color == QColor("black")
assert waveform_color_dark == palette.text().color()
# Set the theme to light
set_theme("light")
palette = get_theme_palette()
waveform_color_light = multi_waveform_widget.waveform.plot_item.getAxis("left").pen().color()
bg_color = multi_waveform_widget.fig.backgroundBrush().color()
assert bg_color == QColor("white")
assert waveform_color_light == palette.text().color()
assert waveform_color_dark != waveform_color_light
# Set the theme to auto and simulate OS theme change
set_theme("auto")
qapp.theme_signal.theme_updated.emit("dark")
apply_theme("dark")
waveform_color = multi_waveform_widget.waveform.plot_item.getAxis("left").pen().color()
bg_color = multi_waveform_widget.fig.backgroundBrush().color()
assert bg_color == QColor("black")
assert waveform_color == waveform_color_dark