Skip to content

Grid mode using vispy ViewBox and linked cameras - #7870

Merged
TimMonko merged 178 commits into
napari:mainfrom
brisvag:feature/canvas-model
Jun 25, 2025
Merged

Grid mode using vispy ViewBox and linked cameras#7870
TimMonko merged 178 commits into
napari:mainfrom
brisvag:feature/canvas-model

Conversation

@brisvag

@brisvag brisvag commented Apr 29, 2025

Copy link
Copy Markdown
Contributor

References and relevant issues

Description

This PR replaces the current transform-based grid mode with a viewbox-based one using linked cameras.

Drag events will remain relative to the original viewbox, making annotation at the edges a breeze. Interacting with one viewbox, such as during annotation, will update the corresponding active layer, potentially in a different grid view.

Additionally, viewer canvas overlays (such as scale_bar) now have a gridded property which, when set to True, will make it so the overlay appears in every grid box instead that on the general canvas.

Grid mode spacing now works proportionally to the layer extensions (i.e. [0,1), as before this PR) or as a pixel value (1,1500]. If spacing amount exceeds a limit that will result in degenerate viewboxes, the spacing will automatically adjust and present a warning to the user. Stride continues to work as before. For discussion on changing the grid layout see #8044.

import napari
v = napari.Viewer(ndisplay=3)
l0, l1 = v.open_sample('napari', 'cells3d')
l1.bounding_box.visible = True

v.axes.visible = True
v.grid.enabled = True

v.scale_bar.visible = True
v.scale_bar.gridded = True

image

@brisvag
brisvag requested review from a team and melonora as code owners April 29, 2025 14:35
@github-actions github-actions Bot added the tests Something related to our tests label Apr 29, 2025
@brisvag
brisvag marked this pull request as draft April 29, 2025 14:35
Comment thread napari/_vispy/canvas.py Outdated
@github-actions github-actions Bot added the topic:qt Relates to qt label May 6, 2025
@brisvag
brisvag force-pushed the feature/canvas-model branch from 443e246 to da63dc6 Compare May 6, 2025 12:58
@brisvag
brisvag marked this pull request as ready for review May 6, 2025 13:56
@brisvag

brisvag commented May 6, 2025

Copy link
Copy Markdown
Contributor Author

Todo from pairing with @melonora:

  • fix camera getting stuck (and infinite event loops)
  • fix canvas overlays not being shown

Comment on lines -323 to -324
center = tuple(np.round(np.divide(screenshot.shape[:2], 2)).astype(int))
np.testing.assert_almost_equal(screenshot[center], [0, 255, 255, 255])

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.

maybe here and above, comment out the code and add a note, as you did with other tests above, so that we know to revive the test as soon as we can?

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.

Will do, good catch! Didn't realize Lorenzo commented these out too ... another lead on how this issue is happening. 😬

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.

or, why does it need commented out? the test passes?

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.

ok actually I'm just confused on what I'm looking at. Is this the most recent commit? It's not what I see locally and I think it's the most recent commit. But this does look like a diff that has this code removed.

@TimMonko TimMonko added ready to merge Last chance for comments! Will be merged in ~24h feature New feature or request labels Jun 25, 2025
@TimMonko

Copy link
Copy Markdown
Member

I am merging this PR because it has had many eyes from the team:

  1. Lorenzo and Wouter-Michiel worked on this initial implementation, with Lorenzo following up for weeks to address issues.
  2. Peter and I stress tested the PR, found many issues, which Lorenzo and I fixed over a period of days.
  3. Juan has given excitement over these changes

We will open follow-up PRs to address a few things, and others revealed during the pre-release including

  1. Figuring out why screenshot-related tests required some changes, despite non-test behavior seems similar.
  2. Adding highlights to the active layer viewbox
  3. Potentially adding borders to the viewboxes

@TimMonko
TimMonko merged commit 123d6b5 into napari:main Jun 25, 2025
@github-actions github-actions Bot removed the ready to merge Last chance for comments! Will be merged in ~24h label Jun 25, 2025
@imagesc-bot

Copy link
Copy Markdown

This pull request has been mentioned on Image.sc Forum. There might be relevant details there:

https://forum.image.sc/t/test-out-the-upcoming-napari-0-6-2-release/114029/1

@jni jni mentioned this pull request Jun 26, 2025
jni pushed a commit that referenced this pull request Jun 27, 2025
# References and and Description

Follow up to #7870. After that PR, on main, stride results in layers
stacking up in the wrong order, where lower indexed layers are on top of
the stack.

See #8053 and #8044 for original discussion

```python
import napari
from skimage import data

viewer, layer = napari.imshow(
        data.lily(),
        name='lily',
        channel_axis=2,
        colormap=['red', 'green', 'blue', 'gray'],
        blending='translucent',
        opacity=1,
        )

viewer.grid.enabled = True
viewer.grid.stride = 2

napari.run()
```

main:


![screenshot_XtB3XXSr@2x](https://github.com/user-attachments/assets/62725a9b-5409-405c-b0e4-b6cea548174e)

this PR:


![screenshot_tsNdp7E1@2x](https://github.com/user-attachments/assets/1671a4a3-9955-4653-af41-74b6c6d2617c)

---------

Co-authored-by: Peter Sobolewski <76622105+psobolewskiPhD@users.noreply.github.com>
@TimMonko

TimMonko commented Jun 29, 2025

Copy link
Copy Markdown
Member

for release notes. this includes #8053

2025-06-29.16-52-45.mp4

python_5sYeBZbLSZ

TimMonko added a commit to napari/docs that referenced this pull request Jun 30, 2025
# References and relevant issues
- depends on napari/napari#7870

# Description
Updated the descripion of grid mode to better reflect the new version,
and added a small video to showcase it.

---------

Co-authored-by: Peter Sobolewski <76622105+psobolewskiPhD@users.noreply.github.com>
Co-authored-by: TimMonko <47310455+TimMonko@users.noreply.github.com>
TimMonko added a commit that referenced this pull request Jun 30, 2025
Alternative to #8044
Depends on #8057 
More context
#8044 (comment) and
#7870 (comment)
Test with this script:

```python
import napari
from skimage import data

viewer, layer = napari.imshow(
        data.lily(),
        name='lily',
        channel_axis=2,
        colormap=['red', 'green', 'blue', 'gray'],
        blending='translucent',
        opacity=1,
        )

viewer.grid.enabled = True
viewer.grid.stride = 2

napari.run()
```

This PR reverses the default order of the panels with a default stride,
so that later layers end up to the bottom-right of earlier layers.

To enable the earlier behaviour, set a stride of -1, which can be set as
default in the viewer preferences.


![screenshot_4nFePQ5C@2x](https://github.com/user-attachments/assets/528aebca-d623-4f9a-97f4-691329d2a2a7)

## Script output:

main:


![screenshot_ikFxA97W@2x](https://github.com/user-attachments/assets/6d961f78-17ac-4ef7-aa8c-5fd66bc91dca)

this PR:


![screenshot_dlFFazIh@2x](https://github.com/user-attachments/assets/4516932d-0b2b-4e61-b378-6ff221d8c9be)

this PR with negative grid stride:


![screenshot_yQwsLxv6@2x](https://github.com/user-attachments/assets/4f791a10-9f00-4309-b029-50b6842f728d)

Co-authored-by: TimMonko <47310455+TimMonko@users.noreply.github.com>
TimMonko added a commit that referenced this pull request Jun 30, 2025
# References and relevant issues

Discussed, in part, in #8053 and at napari community meeting
Follow-up to #7870 

# Description


Improves tooltips for Grid stride to clarify how stride works.
Improves layout of strings in the GUI
TimMonko pushed a commit that referenced this pull request Jul 25, 2025
# References and relevant issues
- #7870

# Description
We use grid mode in a few examples, but there's no explicit example just
to showcase it, especially now that we have extra related features such
as the `Overlay.gridded` attributes.
TimMonko added a commit that referenced this pull request Jul 28, 2025
# References and relevant issues
- closes #7956
- closes (kinda?) #6055

# Description

1. This issue has been bothering me for a long time and I finally
figured it out. On main, if you have the scalebar enabled and zoom out
beyond 1000, it will suddenly collapse to a single pixel.

Turns out, when the scalebar is dimensionless we need to ensure to keep
the world pixel length to the *real* lenght, because we don't have unit
multiples. Funny thing is, we already had this check in place, but for
some reason only applied to high zooms, and not low zooms. Anyways,
AFAICT this works.

2. The second issue is ultimately caused by our camera reporting a zoom
of `1` when it reaches a low enough scale factor. This code points to
#2875, but I can't figure out
why.

EDIT: it seems to be #2958. I tried
to remove this guard completely and I don't actually see that issue
anymore... I think it might be solved, either by the fact that we moved
a lot of camera updates to `on_draw` in
#7870 specifically because the
viewbox was degenerate, or for some other reason. Maybe we can just
remove this?

---------

Co-authored-by: Tim Monko <timmonko@gmail.com>
@TimMonko TimMonko added topic:preferences Issues relating to the creation of new preference fields/panels and removed preferences labels Jun 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request highlight PR that should be mentioned in next release notes tests Something related to our tests topic:preferences Issues relating to the creation of new preference fields/panels topic:qt Relates to qt UI/UX

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Grid is not correctly calculated when dim order is not default (in 3D only?) multicanvas grid display for layers in Napari

8 participants