0
0
mirror of https://github.com/bec-project/bec_widgets.git synced 2025-07-14 11:41:49 +02:00

feat(image): image widget can take data from monitor_1d endpoint

This commit is contained in:
2024-10-04 11:12:17 +02:00
committed by wyzula_j
parent b23695167a
commit 9ef1d1c9ac
10 changed files with 288 additions and 65 deletions

View File

@ -139,6 +139,7 @@ DEVICES = [
FakeDevice("bpm3a"),
FakeDevice("bpm3i"),
FakeDevice("eiger"),
FakeDevice("waveform1d"),
FakeDevice("async_device", readout_priority=ReadoutPriority.ASYNC),
Positioner("test", limits=[-10, 10], read_value=2.0),
Device("test_device"),

View File

@ -12,11 +12,6 @@ from .client_mocks import mocked_client
from .conftest import create_widget
@pytest.fixture
def bec_image_show(bec_figure):
yield bec_figure.image("eiger")
def test_on_image_update(qtbot, mocked_client):
bec_image_show = create_widget(qtbot, BECFigure, client=mocked_client).image("eiger")
data = np.random.rand(100, 100)
@ -61,3 +56,45 @@ def test_autorange_on_image_update(qtbot, mocked_client):
vmin = max(np.mean(data) - 2 * np.std(data), 0)
vmax = np.mean(data) + 2 * np.std(data)
assert np.isclose(img.color_bar.getLevels(), (vmin, vmax), rtol=(1e-5, 1e-5)).all()
def test_on_image_update_variable_length(qtbot, mocked_client):
"""
Test the on_image_update slot with data arrays of varying lengths for 'device_monitor_1d' image type.
"""
# Create the widget and set image_type to 'device_monitor_1d'
bec_image_show = create_widget(qtbot, BECFigure, client=mocked_client).image("waveform1d", "1d")
# Generate data arrays of varying lengths
data_lengths = [10, 15, 12, 20, 5, 8, 1, 21]
data_arrays = [np.random.rand(length) for length in data_lengths]
# Simulate sending messages with these data arrays
device = "waveform1d"
for data in data_arrays:
msg = messages.DeviceMonitor1DMessage(
device=device, data=data, metadata={"scan_id": "12345"}
)
bec_image_show.on_image_update(msg.content, msg.metadata)
# After processing all data, retrieve the image and its data
img = bec_image_show.images[0]
image_buffer = img.get_data()
# The image_buffer should be a 2D array with number of rows equal to number of data arrays
# and number of columns equal to the maximum data length
expected_num_rows = len(data_arrays)
expected_num_cols = max(data_lengths)
assert image_buffer.shape == (
expected_num_rows,
expected_num_cols,
), f"Expected image buffer shape {(expected_num_rows, expected_num_cols)}, got {image_buffer.shape}"
# Check that each row in image_buffer corresponds to the padded data arrays
for i, data in enumerate(data_arrays):
padded_data = np.pad(
data, (0, expected_num_cols - len(data)), mode="constant", constant_values=0
)
assert np.array_equal(
image_buffer[i], padded_data
), f"Row {i} in image buffer does not match expected padded data"

View File

@ -47,14 +47,19 @@ def test_image_widget_init(image_widget):
# Toolbar Actions
###################################
def test_toolbar_connect_action(image_widget, mock_image, qtbot):
combo = image_widget.toolbar.widgets["monitor"].device_combobox
combo.setCurrentText("eiger")
combo_device = image_widget.toolbar.widgets["monitor"].device_combobox
combo_device.setCurrentText("eiger")
qtbot.wait(200)
assert combo.currentText() == "eiger"
assert combo_device.currentText() == "eiger"
combo_dim = image_widget.toolbar.widgets["monitor_type"].widget
combo_dim.setCurrentText("2d")
qtbot.wait(200)
assert combo_dim.currentText() == "2d"
action = image_widget.toolbar.widgets["connect"].action
action.trigger()
image_widget._image.image.assert_called_once_with(
monitor="eiger",
monitor_type="2d",
color_map="magma",
color_bar="full",
downsample=True,
@ -146,9 +151,10 @@ def test_image_toolbar_rotation(image_widget, mock_image):
def test_image_set_image(image_widget, mock_image):
image_widget.image(monitor="image")
image_widget.image(monitor="image", monitor_type="2d")
image_widget._image.image.assert_called_once_with(
monitor="image",
monitor_type="2d",
color_map="magma",
color_bar="full",
downsample=True,

View File

@ -64,6 +64,7 @@ def test_device_input_combobox_init(device_input_combobox):
"bpm3a",
"bpm3i",
"eiger",
"waveform1d",
"async_device",
"test",
"test_device",
@ -150,6 +151,7 @@ def test_device_input_line_edit_init(device_input_line_edit):
"bpm3a",
"bpm3i",
"eiger",
"waveform1d",
"async_device",
"test",
"test_device",