Repository: napari/napari

Issue:
Stop automatically highlighting "added" points on set_data
## 🐛 Bug

Currently when we set the data of a points layer, if the new data has more points than the previous data, then the additional points get selected automatically and highlighted. This can be very harmful to a visualisation because the highlight is very distracting and totally unrelated to the data. In many situations, the points are actually a completely different set of points, so highlighting the last few is completely nonsensical — they are not the "new" points, all the points were new.

## To Reproduce

```python
import numpy as np
import napari
from magicgui import magicgui


viewer = napari.Viewer(ndisplay=3)

@magicgui(
    auto_call=True,
    x={'widget_type': 'Slider', 'min': 0, 'max': 5000}
)
def get_pts(x=2500) -> napari.types.LayerDataTuple:
    npoints = np.random.randint(900, 1100)
    data_raw = np.random.random((npoints, 3))
    data = data_raw[np.lexsort(data_raw.T)]
    return (
        data * 255,
        {'size': 2, 'name': 'pts', 'shading': 'spherical'},
        'points',
    )

w = viewer.window.add_dock_widget(get_pts, area='bottom')
_ = get_pts()  # call once to create the layer

napari.run()
```

https://github.com/napari/napari/assets/492549/dd49c2c3-ca94-41e2-a559-85dfb6a0fd04

## Expected behavior

Points should not be selected on setting data.

CC @francescalted


Discussion:
Agreed the behavior of the `data` setter is surprising and undesirable in this case.

Note that the same logic in the `data` setter described above applies to much of the state in `Points` (e.g. `features`), where we just append new values but keep the old ones: https://github.com/napari/napari/blob/929e30c39be1df00cf7b87526974770b973ccb66/napari/layers/points/points.py#L552

I think this general behavior has been around for a long time and was likely driven by desired behavior when interactively/manually adding points (e.g. the last one added should be selected, should get the "current" feature value). But seeing the layer as a data model, it's generally surprising behavior. I think there are similar general behaviors in other layers (e.g `Shapes`), so there's probably a very wide discussion to be had that goes outside the scope of this issue.

To fix this specific issue, a few ideas come to mind.

1. Move selection change to `Points.add`.
2. Remove the selection change behavior entirely.

I don't have strong opinions here. Implementing (2) should be as simple as removing the following line: https://github.com/napari/napari/blob/929e30c39be1df00cf7b87526974770b973ccb66/napari/layers/points/points.py#L587

and maybe updating some tests.
Thanks @andy-sweet! I think indeed 1 is the desired behaviour. Maybe we can fix this one this week. 😃 
I agree, approach 1 is the way to go!
Assigned to me, but delegating to @akuten1298

Resolving patch:
diff --git a/napari/layers/points/points.py b/napari/layers/points/points.py
--- a/napari/layers/points/points.py
+++ b/napari/layers/points/points.py
@@ -584,7 +584,6 @@ def data(self, data: Optional[np.ndarray]):
                     (self._edge_width, edge_width), axis=0
                 )
                 self.symbol = np.concatenate((self._symbol, symbol), axis=0)
-                self.selected_data = set(np.arange(cur_npoints, len(data)))
 
         self._update_dims()
         self._reset_editable()
@@ -1914,6 +1913,7 @@ def add(self, coords):
         coords : array
             Point or points to add to the layer data.
         """
+        cur_points = len(self.data)
         self.data = np.append(self.data, np.atleast_2d(coords), axis=0)
         self.events.data(
             value=self.data,
@@ -1921,6 +1921,7 @@ def add(self, coords):
             data_indices=(-1,),
             vertex_indices=((),),
         )
+        self.selected_data = set(np.arange(cur_points, len(self.data)))
 
     def remove_selected(self):
         """Removes selected points if any."""


Test patch:
diff --git a/napari/layers/points/_tests/test_points.py b/napari/layers/points/_tests/test_points.py
--- a/napari/layers/points/_tests/test_points.py
+++ b/napari/layers/points/_tests/test_points.py
@@ -370,12 +370,25 @@ def test_adding_points():
     layer.add(coords)
     assert len(layer.data) == 13
     assert np.all(layer.data[11:, :] == coords)
+    assert layer.selected_data == {11, 12}
 
     # test that the last added points can be deleted
     layer.remove_selected()
     np.testing.assert_equal(layer.data, np.vstack((data, coord)))
 
 
+def test_points_selection_with_setter():
+    shape = (10, 2)
+    np.random.seed(0)
+    data = 20 * np.random.random(shape)
+    layer = Points(data)
+
+    coords = [[10, 10], [15, 15]]
+    layer.data = np.append(layer.data, np.atleast_2d(coords), axis=0)
+    assert len(layer.data) == 12
+    assert layer.selected_data == set()
+
+
 def test_adding_points_to_empty():
     """Test adding Points data to empty."""
     shape = (0, 2)
@@ -387,6 +400,7 @@ def test_adding_points_to_empty():
     layer.add(coord)
     assert len(layer.data) == 1
     assert np.all(layer.data[0] == coord)
+    assert layer.selected_data == {0}
 
 
 def test_removing_selected_points():