napari / napari

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

`MouseEvent` position varying according to layers scale #3758

Open JoOkuma opened 2 years ago

JoOkuma commented 2 years ago

🐛 Bug

The MouseEvent position doesn't seem to be mapping to the correct position in the canvas when the layers have different scales.

I'm reproducing this bug by setting the current step of the viewer to the position where the mouse double click was captured. I expected the viewer to stay at the same step, but it moves, hence event.position is not at the current_step. Is this expected?

transform_bug

The layer's world_to_data transform also doesn't take this into account, you can see with the print that no transformation is done. My guess is that the scaling of the layers results in a shift (rounding/interpolation) in the data and this is not taken into account.

To Reproduce

Steps to reproduce the behavior:

  1. Execute the following code:
import napari
import numpy as np

def double_click(layer, event):
    print('world', event.position, 'data', layer.world_to_data(event.position))                                                                                                                                                             
    viewer.dims.set_current_step(range(4), event.position)

x = np.zeros((5, 25, 50, 50))
y = np.zeros((100, 50, 50, 50))

viewer = napari.Viewer()

x_layer = viewer.add_image(x, scale=(20, 2, 1, 1)) 
y_layer = viewer.add_image(y)

x_layer.mouse_double_click_callbacks.append(double_click)
y_layer.mouse_double_click_callbacks.append(double_click)

viewer.dims.set_current_step(0, 100)
viewer.dims.set_current_step(1, 41) 

napari.run()
  1. Double click anywhere in the canvas and notice that the planes are changing.

Expected behavior

I didn't expect it to change the plane since I'm setting the step to the position where the click occurred.

Environment

napari: 0.4.11.dev364+ge7ff53a0
Platform: Linux-5.11.0-40-generic-x86_64-with-glibc2.17
System: Ubuntu 20.04.3 LTS
Python: 3.8.10 (default, Jun 4 2021, 15:09:15) [GCC 7.5.0]
Qt: 5.15.2
PyQt5: 5.15.4
NumPy: 1.20.3
SciPy: 1.7.1
Dask: 2021.07.1
VisPy: 0.9.4

OpenGL:
- GL version: 4.6.0 NVIDIA 460.73.01
- MAX_TEXTURE_SIZE: 32768

Screens:
- screen 1: resolution 3440x1440, scale 1.0

Additional context

It should be noted as well, that the 0-axis displays a length longer than the length of the largest data (100)

image

This behavior exists for a while, it never bothered me, but now I think it's related to the interpolation of the data after the scaling. So it could be related to the bug.

psobolewskiPhD commented 8 months ago

@JoOkuma I tried the code snippet in main c95f4b68a57154ddb0b65354a1be9d191dca2014 and I don't get any strange behavior, just the expected (?) print:

world (99.0, 41.0, 21.498198859064274, 39.00870470341276) data [99.         41.         21.49819886 39.0087047 ]

Is this still an issue for you?