napari / napari

napari: a fast, interactive, multi-dimensional image viewer for python
https://napari.org
BSD 3-Clause "New" or "Revised" License
2.13k stars 419 forks source link

Bounding box has wrong extent for non-base level of multi-scale images #7036

Open andy-sweet opened 3 weeks ago

andy-sweet commented 3 weeks ago

🐛 Bug Report

When showing the bounding box for a multi-scale image at some resolution other than its highest (i.e. other than level 0), it is too large.

Screenshot 2024-06-27 at 9 35 19 AM

💡 Steps to Reproduce

One way to reproduce this issue is to show a multi-scale image in 3D since that always uses the lowest resolution image.

import napari
import numpy as np
from skimage.transform import pyramid_gaussian

image = np.random.rand(8, 8, 8)
ms_image = list(pyramid_gaussian(image, downscale=2, max_layer=3))
scale = 3 * (2,)

viewer = napari.Viewer(ndisplay=3)
layer = viewer.add_image(ms_image, scale=scale)
layer.bounding_box.visible = True

napari.run()

There are ways to write tests with non-public APIs to force similar behavior for 2D. Or you can force the multi-scale image slicing code to use a specific level to see the effect in 2D.

💡 Expected Behavior

I expect the bounding-box to always show the correct extent of the image, regardless of the scale being viewed.

🌎 Environment

napari: 0.4.17rc4.dev804+g1bcbbf885.d20240625 Platform: macOS-14.5-arm64-arm-64bit System: MacOS 14.5 Python: 3.10.14 | packaged by conda-forge | (main, Mar 20 2024, 12:51:49) [Clang 16.0.6 ] Qt: 5.15.11 PyQt5: 5.15.10 NumPy: 1.26.4 SciPy: 1.13.1 Dask: 2024.5.1 VisPy: 0.14.2 magicgui: 0.8.2 superqt: 0.6.6 in-n-out: 0.2.1 app-model: 0.2.7 npe2: 0.7.5

OpenGL:

Screens:

Optional:

Settings path:

💡 Additional Context

This issue comes from a similar fix: https://github.com/napari/napari/pull/6319#issuecomment-2192432547

I think this can be fixed by modifying the child transform so that it also factors in the scale component of tile2data, not just the offsets.

andy-sweet commented 3 weeks ago

Also note that toggling the visibility of the node or taking several other actions seems to fix this, so this may not be directly related to tile2data after all, and instead may be related to the bounding box transform being updated at the right time.

psobolewskiPhD commented 3 weeks ago

Thanks for opening this, I've run into it before "live" while doing something and then forgotten to track it down because it wasn't consistent.