Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 37 additions & 9 deletions src/ndv/controllers/_array_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ def __init__(
self._gen_counter = count()
self._current_gen: int = 0
self._futures: dict[Future[DataResponse], int] = {}
# A superseded response may still be drawn if only the current index changed
# since it was requested (e.g. while dragging a slider), so the display keeps
# up instead of freezing until the slider stops. Responses older than
# `_min_drawable_gen` (requested before a data, shape or layout change) or
# older than the one already on screen (`_shown_gen`) are discarded.
self._min_drawable_gen: int = 0
self._shown_gen: int = -1

# mapping of channel keys to their respective controllers
# where None is the default channel
Expand Down Expand Up @@ -469,14 +476,13 @@ def _apply_changes(
if old.current_index != new.current_index:
self._view.set_current_index(self._display_model.current_index)

needs_data = (
layout_changed = (
old.visible_axes != new.visible_axes
or old.channel_axis != new.channel_axis
or old.channel_mode != new.channel_mode
or old.current_index != new.current_index
)
if needs_data:
self._request_data()
if layout_changed or old.current_index != new.current_index:
self._request_data(index_only=not layout_changed)

if old.visible_scales != new.visible_scales:
self._canvas.set_scales(new.visible_scales)
Expand Down Expand Up @@ -591,6 +597,10 @@ def _on_dims_changed(self) -> None:
"""Update sliders and info when dimension sizes change."""
if self._data_wrapper is None:
return
# responses requested for the old shape must not be drawn as superseded
# frames (they may have other channels or extents); the current request
# is still drawn if nothing newer is issued, as before
self._min_drawable_gen = self._current_gen + 1
with self._view.currentIndexChanged.blocked():
self._view.create_sliders(self._data_wrapper.coords)
self._re_resolve()
Expand Down Expand Up @@ -750,17 +760,23 @@ def _highlight_values(
# 4. each request is submitted as a future
# 5. when the future resolves, `_on_data_response_ready` draws the response.

def _request_data(self) -> None:
def _request_data(self, *, index_only: bool = False) -> None:
"""Fetch and update the displayed data.

This is called (frequently) when anything changes that requires a redraw.
It fetches the current data slice from the model and updates the image handle.

`index_only` means only the current index changed since the last request, so
a response still in flight from an earlier request may be drawn when it
arrives (it is a valid, just older, view of the same data).
"""
if not self._data_wrapper:
return # pragma: no cover

self._cancel_futures()
self._current_gen = gen = next(self._gen_counter)
if not index_only:
self._min_drawable_gen = gen

for req in build_slice_requests(self._resolved, self._data_wrapper):
future: Future[DataResponse]
Expand All @@ -785,11 +801,22 @@ def _join(self) -> None:
future.result()

def _cancel_futures(self) -> None:
while self._futures:
f, _ = self._futures.popitem()
f.cancel()
# Futures that already started cannot be cancelled. They stay tracked so
# their generation is known when they finish (see `_is_drawable`).
for f in list(self._futures):
if f.cancel():
# cancel() may already have run the done callback, which pops it
self._futures.pop(f, None)
self._viewer_model.show_progress_spinner = False

def _is_drawable(self, gen: int) -> bool:
"""Return True if a response from request generation `gen` may be drawn."""
if gen == self._current_gen:
return True
# superseded: draw only if nothing but the index changed since, and it is
# not older than what is already on screen
return 0 <= gen and self._min_drawable_gen <= gen and self._shown_gen <= gen
Comment on lines +817 to +818

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# not older than what is already on screen
return 0 <= gen and self._min_drawable_gen <= gen and self._shown_gen <= gen
# not older than what is already on screen
# and no newer response is already waiting to be drawn
if gen < max(self._min_drawable_gen, self._shown_gen):
return False
return not any(
g > gen and f.done() and not f.cancelled() for f, g in self._futures.items()
)

I think we also want to skip a superseded response if a newer one has already finished and is just waiting for its callback to run on the main thread. Otherwise, whenever several requests finish before the event loop gets to them (e.g. a burst of index updates while the main thread is busy), they all pass the _shown_gen <= gen check (since they arrive in order) and we replay the whole backlog.

note: this breaks the last assertion in test_index_change_keeps_earlier_responses_drawable (before isn't drawable once the newer request is done but not yet drawn). I'd just drop that line, the _min_drawable_gen check above it already covers the intent. And maybe add a test for this:

@no_type_check
@_patch_views
def test_superseded_response_skipped_if_newer_done() -> None:
    """A superseded response is skipped when a newer one is already waiting."""
    ctrl = ArrayViewer()
    ctrl._current_gen = 5
    ctrl._min_drawable_gen = 1
    newer = _response_future(ctrl, 4, value=4)  # done, callback not yet run

    ctrl._on_data_response_ready(_response_future(ctrl, 3, value=3))
    ctrl._canvas.refresh.assert_not_called()

    ctrl._on_data_response_ready(newer)
    assert ctrl._shown_gen == 4
    ctrl._canvas.refresh.assert_called_once()


@_app.ensure_main_thread
def _on_data_response_ready(self, future: Future[DataResponse]) -> None:
# NOTE: popping the future is important because it holds a reference to
Expand All @@ -798,14 +825,15 @@ def _on_data_response_ready(self, future: Future[DataResponse]) -> None:
if not self._futures:
self._viewer_model.show_progress_spinner = False

if future.cancelled() or gen != self._current_gen:
if future.cancelled() or not self._is_drawable(gen):
return

try:
response = future.result()
except Exception as e:
warnings.warn(f"Error fetching data: {e}", stacklevel=1)
return
self._shown_gen = max(self._shown_gen, gen)

for key, data in response.data.items():
if data.size == 0:
Expand Down
110 changes: 109 additions & 1 deletion tests/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,11 +545,13 @@ def test_stale_response_discard() -> None:
"""Test that responses from old request generations are discarded."""
ctrl = ArrayViewer()

# simulate a stale response from generation 1 when we're on generation 2
# simulate a stale response from generation 1 when we're on generation 2,
# and the request for generation 2 changed more than the index
old_response = DataResponse(n_visible_axes=2, data={None: np.zeros((8, 8))})
future = Future()
future.set_result(old_response)
ctrl._current_gen = 2
ctrl._min_drawable_gen = 2
ctrl._futures[future] = 1 # old generation

ctrl._on_data_response_ready(future)
Expand All @@ -559,6 +561,112 @@ def test_stale_response_discard() -> None:
ctrl._canvas.refresh.assert_not_called()


def _response_future(ctrl: ArrayViewer, gen: int, value: int) -> Future:
future: Future[DataResponse] = Future()
future.set_result(
DataResponse(n_visible_axes=2, data={None: np.full((8, 8), value)})
)
ctrl._futures[future] = gen
return future


@no_type_check
@_patch_views
def test_superseded_index_response_is_drawn() -> None:
"""While only the index changes (slider drag), a late response still draws."""
ctrl = ArrayViewer()
ctrl._current_gen = 5 # gens 1..5 were index-only changes after gen 1
ctrl._min_drawable_gen = 1

ctrl._on_data_response_ready(_response_future(ctrl, 3, value=3))

assert ctrl._shown_gen == 3
ctrl._canvas.refresh.assert_called_once()


@no_type_check
@_patch_views
def test_response_older_than_shown_is_discarded() -> None:
"""A superseded response never replaces a newer one already on screen."""
ctrl = ArrayViewer()
ctrl._current_gen = 5
ctrl._min_drawable_gen = 1

ctrl._on_data_response_ready(_response_future(ctrl, 4, value=4))
ctrl._canvas.refresh.reset_mock()
ctrl._on_data_response_ready(_response_future(ctrl, 2, value=2))

assert ctrl._shown_gen == 4
ctrl._canvas.refresh.assert_not_called()


@no_type_check
@_patch_views
def test_cancel_keeps_running_futures() -> None:
"""Queued requests are cancelled; started ones stay tracked with their gen."""
ctrl = ArrayViewer()
pending: Future[DataResponse] = Future()
running: Future[DataResponse] = Future()
running.set_running_or_notify_cancel() # a worker picked it up
ctrl._futures[pending] = 0
ctrl._futures[running] = 0

ctrl._cancel_futures()

assert pending.cancelled()
assert pending not in ctrl._futures
assert ctrl._futures == {running: 0}


@no_type_check
@_patch_views
def test_layout_change_discards_earlier_responses() -> None:
"""After a visible-axes change, responses requested before it never draw."""
ctrl = ArrayViewer(np.zeros((4, 4, 8, 8)))
ctrl._join()
before = ctrl._current_gen

ctrl.display_model.visible_axes = (1, 2, 3) # 2D -> 3D
ctrl._join()

assert ctrl._min_drawable_gen == ctrl._current_gen > before
assert not ctrl._is_drawable(before)


@no_type_check
@_patch_views
def test_index_change_keeps_earlier_responses_drawable() -> None:
"""A pure index change leaves the previous request drawable."""
ctrl = ArrayViewer(np.zeros((4, 8, 8)))
ctrl._join()
before = ctrl._current_gen
min_before = ctrl._min_drawable_gen

ctrl.display_model.current_index.update({0: 3})
ctrl._join()

assert ctrl._current_gen > before
assert ctrl._min_drawable_gen == min_before
assert ctrl._is_drawable(before) == (ctrl._shown_gen <= before)


@no_type_check
@_patch_views
def test_dims_change_discards_earlier_responses() -> None:
"""Responses requested for the old shape never draw after dims change."""
ctrl = ArrayViewer(np.zeros((4, 8, 8)))
ctrl._join()
ctrl._shown_gen = -1
before = ctrl._current_gen
ctrl._current_gen = before + 1 # a later index-only request is in flight
ctrl._min_drawable_gen = 0

ctrl._on_dims_changed() # no index change, so no new request

assert not ctrl._is_drawable(before)
assert ctrl._is_drawable(before + 1) # the current request still draws


@no_type_check
@_patch_views
def test_rgba_3d_fallback_warns() -> None:
Expand Down
Loading