Repository: napari/napari

Issue:
Crash/errors when deleting layers
## 🐛 Bug


## To Reproduce

Steps to reproduce the behavior:

1. Add a single channel (maybe multiple t/z) with viewer.add_image
2. Add a multichannel image, don't use channel axis
3. Delete the multichannel image

<!-- If you have a code sample, error messages, stack traces, please provide it here as well -->
```
Traceback (most recent call last):
  File "/Users/cjw/Code/napari/napari/_qt/qt_viewer_buttons.py", line 148, in <lambda>
    self.clicked.connect(lambda: self.viewer.layers.remove_selected())
  File "/Users/cjw/Code/napari/napari/components/layerlist.py", line 139, in remove_selected
    self.pop(i)
  File "/Users/cjw/Code/napari/napari/utils/list/_model.py", line 64, in pop
    self.events.removed(item=obj, index=key)
  File "/Users/cjw/Code/napari/napari/utils/event.py", line 508, in __call__
    self._invoke_callback(cb, event)
  File "/Users/cjw/Code/napari/napari/utils/event.py", line 529, in _invoke_callback
    cb_event=(cb, event),
  File "/Users/cjw/Code/napari/napari/utils/event.py", line 523, in _invoke_callback
    cb(event)
  File "/Users/cjw/Code/napari/napari/utils/event.py", line 508, in __call__
    self._invoke_callback(cb, event)
  File "/Users/cjw/Code/napari/napari/utils/event.py", line 529, in _invoke_callback
    cb_event=(cb, event),
  File "/Users/cjw/Code/napari/napari/utils/event.py", line 523, in _invoke_callback
    cb(event)
  File "/Users/cjw/Code/napari/napari/components/viewer_model.py", line 513, in _update_grid
    stride=self.grid_stride,
  File "/Users/cjw/Code/napari/napari/components/viewer_model.py", line 500, in grid_view
    self._subplot(layer, (i_row, i_column))
  File "/Users/cjw/Code/napari/napari/components/viewer_model.py", line 528, in _subplot
    scene_size, corner = self._scene_shape()
  File "/Users/cjw/Code/napari/napari/components/viewer_model.py", line 266, in _scene_shape
    size = [size[i] for i in self.dims.displayed]
  File "/Users/cjw/Code/napari/napari/components/viewer_model.py", line 266, in <listcomp>
    size = [size[i] for i in self.dims.displayed]
IndexError: index 4 is out of bounds for axis 0 with size 4
```

When napari is launched from the command line and the images are opened with drag/drop the application will crash with the above stack trace and `Abort trap: 6.`

## Expected behavior
When deleting a layer, napari should update the dimension information so activating the new layer does not raise errors or crash.

## Environment
```
 napari: 0.3.2+5.g9b13b57
Platform: Darwin-17.5.0-x86_64-i386-64bit
Python: 3.7.4 (default, Aug 13 2019, 15:17:50) [Clang 4.0.1 (tags/RELEASE_401/final)]
Qt: 5.13.0
PyQt5: 5.13.1
NumPy: 1.17.2
SciPy: 1.3.1
Dask: 2.14.0
VisPy: 0.6.4

GL version: 2.1 INTEL-10.32.48
MAX_TEXTURE_SIZE: 16384

Plugins:
- napari-plugin-engine: 0.1.5
- svg: 0.1.2
 - Any other relevant information:
```
## Additional context




Discussion:

Resolving patch:
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
@@ -84,9 +84,9 @@ def __init__(
         self.dims.events.ndisplay.connect(self._update_layers)
         self.dims.events.order.connect(self._update_layers)
         self.dims.events.axis.connect(self._update_layers)
-        self.layers.events.changed.connect(self._on_layers_change)
         self.layers.events.changed.connect(self._update_active_layer)
         self.layers.events.changed.connect(self._update_grid)
+        self.layers.events.changed.connect(self._on_layers_change)
 
         self.keymap_providers = [self]
 


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
@@ -374,3 +374,18 @@ def test_add_layer_from_data_raises():
             {'rgb': True},  # vectors do not have an 'rgb' kwarg
             layer_type='vectors',
         )
+
+
+def test_add_delete_layers():
+    """Test adding and deleting layers with different dims."""
+    viewer = ViewerModel()
+    np.random.seed(0)
+    viewer.add_image(np.random.random((5, 5, 10, 15)))
+    assert len(viewer.layers) == 1
+    assert viewer.dims.ndim == 4
+    viewer.add_image(np.random.random((5, 6, 5, 10, 15)))
+    assert len(viewer.layers) == 2
+    assert viewer.dims.ndim == 5
+    viewer.layers.remove_selected()
+    assert len(viewer.layers) == 1
+    assert viewer.dims.ndim == 4