Skip to content

Commit

Permalink
test to reproduce #302 (#303)
Browse files Browse the repository at this point in the history
* test to reproduce #302

* fix single scale images

---------

Co-authored-by: Grzegorz Bokota <[email protected]>
  • Loading branch information
LucaMarconato and Czaki authored Aug 26, 2024
1 parent 14a93d7 commit 885b5fa
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
16 changes: 10 additions & 6 deletions src/napari_spatialdata/_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,16 @@ def _channel_changed(self, event: Event) -> None:

current_point = list(event.value)
displayed = self._viewer.dims.displayed
for i, (lo_size, hi_size, cord) in enumerate(zip(layer.data[-1].shape, layer.data[0].shape, current_point)):
if i in displayed:
current_point[i] = slice(None)
else:
current_point[i] = int(cord * lo_size / hi_size)

if layer.multiscale:
for i, (lo_size, hi_size, cord) in enumerate(zip(layer.data[-1].shape, layer.data[0].shape, current_point)):
if i in displayed:
current_point[i] = slice(None)
else:
current_point[i] = int(cord * lo_size / hi_size)
else:
for i in range(len(current_point)):
if i in displayed:
current_point[i] = slice(None)
# TODO remove once contrast limits in napari are fixed
if isinstance(layer.data, MultiScaleData):
# just compute lowest resolution
Expand Down
40 changes: 39 additions & 1 deletion tests/test_view.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
from typing import Any

import numpy as np
import pytest
from napari.utils.events import EventedList
from napari_spatialdata._sdata_widgets import SdataWidget
from napari_spatialdata._view import QtAdataViewWidget
from napari_spatialdata.utils._test_utils import click_list_widget_item, get_center_pos_listitem
from spatialdata.datasets import blobs
from spatialdata.transformations import Affine, set_transformation


@pytest.mark.parametrize("widget", [QtAdataViewWidget])
@pytest.mark.parametrize("n_channels", [3, 5])
def test_channel_slider_images(qtbot, make_napari_viewer: any, widget: Any, n_channels: int):
def test_channel_slider_images_bug_282(qtbot, make_napari_viewer: any, widget: Any, n_channels: int):
# https://github.com/scverse/napari-spatialdata/issues/282
channels = [f"channel_{i}" for i in range(n_channels)]
sdata_blobs = blobs(c_coords=channels)
viewer = make_napari_viewer()
Expand Down Expand Up @@ -63,3 +66,38 @@ def test_plot_dataframe_annotation_on_points(qtbot, make_napari_viewer: any, wid
)
widget.dataframe_columns_widget._onAction([column])
viewer.close()


@pytest.mark.parametrize("multiscale", [True, False])
@pytest.mark.parametrize("widget", [QtAdataViewWidget])
def test_channel_slider_images_bug_302(qtbot, make_napari_viewer: any, widget: Any, multiscale: bool):
# https://github.com/scverse/napari-spatialdata/issues/302
channels = [f"channel_{i}" for i in range(3)]
sdata_blobs = blobs(c_coords=channels)

# set a transformation to the single-scale and multi-scale images
element_name = "blobs_image" if not multiscale else "blobs_multiscale_image"
affine = Affine(np.array([[10, 20, 30], [40, 50, 60], [0, 0, 1]]), input_axes=("x", "y"), output_axes=("x", "y"))
set_transformation(sdata_blobs[element_name], transformation=affine, to_coordinate_system="global")

viewer = make_napari_viewer()
sdata_widget = SdataWidget(viewer, EventedList([sdata_blobs]))

viewer.window.add_dock_widget(sdata_widget, name="SpatialData")
sdata_widget.viewer_model.add_sdata_image(sdata_blobs, element_name, "global", False)

# this connects the slider to the viewer (done in __init__)
_ = widget(viewer)

# check if the slider is present
start, stop, step = viewer.dims.range[0]
assert start == 0
assert step == 1

# simulate position change of the slider
viewer.dims.set_current_step(0, 0)
qtbot.wait(50) # wait for a short time to simulate user interaction
viewer.dims.set_current_step(0, 1)
qtbot.wait(50) # wait for a short time to simulate user interaction

viewer.close()

0 comments on commit 885b5fa

Please sign in to comment.