mirror of
https://github.com/bec-project/bec_widgets.git
synced 2026-07-27 13:42:58 +02:00
fix(image): guard shape/dtype edge cases in the data path
This commit is contained in:
@@ -959,6 +959,8 @@ class Image(ImageBase):
|
||||
Returns:
|
||||
np.ndarray: The updated image buffer with adjusted shapes.
|
||||
"""
|
||||
# Guard for wrong data shapes
|
||||
new_data = np.atleast_1d(np.asarray(new_data))
|
||||
new_len = new_data.shape[0]
|
||||
if not hasattr(image, "buffer"):
|
||||
image.buffer = []
|
||||
|
||||
@@ -1439,4 +1439,21 @@ def test_image_processor_log_is_finite_for_non_positive():
|
||||
# maps to 0.
|
||||
assert out[0, 0] == pytest.approx(-1.0)
|
||||
assert out[1, 0] == pytest.approx(-1.0)
|
||||
assert proc.log(np.array([[1.0]]))[0, 0] == pytest.approx(0.0)
|
||||
assert proc.log(np.array([[1.0]]))[0, 0] == pytest.approx(0.0)
|
||||
|
||||
|
||||
##############################################
|
||||
# Shape / dtype edge cases in the data path
|
||||
##############################################
|
||||
|
||||
|
||||
def test_adjust_image_buffer_coerces_list_and_scalar(qtbot, mocked_client):
|
||||
"""Non-ndarray payloads (python list, 0-d scalar) must not crash the
|
||||
1D buffer accumulation."""
|
||||
view = create_widget(qtbot, Image, client=mocked_client)
|
||||
image = view.main_image
|
||||
|
||||
buf = view.adjust_image_buffer(image, [1, 2, 3]) # python list
|
||||
assert buf.shape == (1, 3)
|
||||
buf = view.adjust_image_buffer(image, np.array(42.0)) # 0-d scalar
|
||||
assert buf.shape[0] == 2
|
||||
Reference in New Issue
Block a user