Repository: napari/napari

Issue:
Slider is no longer in the middle of the volume when scale != 1
## 🐛 Bug

I suspect this is from #5522 but haven't confirmed. When launching a new viewer, if the layer scale in not 1, the slider for non-visible dimensions doesn't start in the middle of the volume anymore; rather, it seems to be the middle transformed by the scale.

## To Reproduce

```python
from skimage import data
import napari

viewer, layer = napari.imshow(data.cells3d()[:, 1], scale=[1.5, 1, 1])
napari.run()
```

<img width="1912" alt="Screenshot 2023-04-17 at 12 29 45 pm" src="https://user-images.githubusercontent.com/492549/232363932-0ea4838c-5924-4172-afe9-17859b9b1f14.png">

## Expected behavior

Slider should always start in the middle, regardless of scale.

## Environment

<details>

<summary>

```
napari: 0.4.17rc4.dev169+g57fa998e7.d20221219
```

</summary>

```
napari: 0.4.17rc4.dev169+g57fa998e7.d20221219
Platform: macOS-13.2.1-arm64-arm-64bit
System: MacOS 13.2.1
Python: 3.10.10 | packaged by conda-forge | (main, Mar 24 2023, 20:12:31) [Clang 14.0.6 ]
Qt: 5.15.4
PyQt5: 5.15.7
NumPy: 1.22.4
SciPy: 1.8.1
Dask: 2023.3.1+13.g4b1e3600.dirty
VisPy: 0.12.1
magicgui: 0.5.1
superqt: unknown
in-n-out: 0.1.5
app-model: 0.1.1
npe2: 0.6.1

OpenGL:
  - GL version:  2.1 Metal - 83
  - MAX_TEXTURE_SIZE: 16384

Screens:
  - screen 1: resolution 1800x1169, scale 2.0

Settings path:
  - /Users/jni/Library/Application Support/napari/all_f332943dd4a538ce2ff3134fd0ff74396d8d8b23/settings.yaml
Plugins:
Error importing contributions for first-generation napari plugin 'napari-midi': No module named 'rtmidi'
  - affinder: 0.2.3.dev12+g6d235f9 (2 contributions)
  - napari: 0.4.17rc4.dev169+g57fa998e7.d20221219 (77 contributions)
  - napari-animation: 0.0.6.dev1+g28d2258 (2 contributions)
  - napari-console: 0.0.6 (0 contributions)
  - napari-defdap: 0.1.dev1+g85d347f.d20230405 (10 contributions)
  - napari-matplotlib: 0.2.1 (8 contributions)
  - napari-midi: 0.1.dev8+gf25a356 (0 contributions)
  - napari-multiscale-rendering-prototype: 0.0.1 (6 contributions)
  - napari-ome-zarr: 0.5.3.dev23+g0da60a2 (2 contributions)
  - napari-pdf-reader: 0.0.1a3 (2 contributions)
  - napari-pymeshlab: 0.0.5 (17 contributions)
  - napari-skeleton-curator: 0.1.dev21+gf2de859 (4 contributions)
  - napari-svg: 0.1.6 (2 contributions)
  - napari-watershed: 0.0.0 (4 contributions)
  - ome-types: 0.3.4.dev3+gae84abf (4 contributions)
  - platelet-unet-watershed: 0.0.3 (4 contributions)
  - skan: 0.11.0.dev0 (2 contributions)
  - zarpaint: 0.1.1.dev23+g0734529 (14 contributions)
```

</details>



Discussion:
@brisvag sorry if I have tarnished your good name in vain, I'm preparing for a talk so I don't have time to git-bisect at the moment 😅 🙏 
My name was rightfully tarnished, according to `git bisect`! Will look into it.

Resolving patch:
diff --git a/napari/components/dims.py b/napari/components/dims.py
--- a/napari/components/dims.py
+++ b/napari/components/dims.py
@@ -430,6 +430,9 @@ def _roll(self):
         order[nsteps > 1] = np.roll(order[nsteps > 1], 1)
         self.order = order.tolist()
 
+    def _go_to_center_step(self):
+        self.current_step = [int((ns - 1) / 2) for ns in self.nsteps]
+
     def _sanitize_input(
         self, axis, value, value_is_sequence=False
     ) -> Tuple[List[int], List]:
diff --git a/napari/components/viewer_model.py b/napari/components/viewer_model.py
--- a/napari/components/viewer_model.py
+++ b/napari/components/viewer_model.py
@@ -446,6 +446,13 @@ def _on_active_layer(self, event):
 
     @staticmethod
     def rounded_division(min_val, max_val, precision):
+        warnings.warn(
+            trans._(
+                'Viewer.rounded_division is deprecated since v0.4.18 and will soon be removed.'
+            ),
+            DeprecationWarning,
+            stacklevel=2,
+        )
         return int(((min_val + max_val) / 2) / precision) * precision
 
     def _on_layers_change(self):
@@ -588,13 +595,9 @@ def _on_add_layer(self, event):
         self._update_layers(layers=[layer])
 
         if len(self.layers) == 1:
+            # set dims slider to the middle of all dimensions
             self.reset_view()
-            ranges = self.layers._ranges
-            midpoint = [
-                self.rounded_division(low, high, step)
-                for low, high, step in ranges
-            ]
-            self.dims.current_step = midpoint
+            self.dims._go_to_center_step()
 
     @staticmethod
     def _layer_help_from_mode(layer: Layer):


Test patch:
diff --git a/napari/components/_tests/test_viewer_model.py b/napari/components/_tests/test_viewer_model.py
--- a/napari/components/_tests/test_viewer_model.py
+++ b/napari/components/_tests/test_viewer_model.py
@@ -331,6 +331,19 @@ def test_view_centering_with_points_add():
     assert tuple(viewer.dims.point) == (0, 4, 4)
 
 
+def test_view_centering_with_scale():
+    """Regression test for issue #5735"""
+    image = np.zeros((5, 10, 10))
+
+    viewer = ViewerModel()
+    viewer.add_image(image, scale=(1, 1, 1))
+    assert tuple(viewer.dims.point) == (2, 4, 4)
+
+    viewer.layers.pop()
+    viewer.add_image(image, scale=(2, 1, 1))
+    assert tuple(viewer.dims.point) == (4, 4, 4)
+
+
 def test_new_shapes():
     """Test adding new shapes layer."""
     # Add labels to empty viewer