Repository: mne-tools/mne-python

Issue:
Incorrect behavior of annotation rename method
### Description of the problem

The _rename_ method of [Annotation](https://github.com/mne-tools/mne-python/blob/maint/1.2/mne/annotations.py#L649-L677) object  is implemented in such a way that it does not replace the label name with a new one, but does replace on the string variable as many times as there are pairs in the dictionary.

```
for old, new in mapping.items():   
    self.description = [d.replace(old, new) for d in self.description]   
```

In addition to unnecessary iterations, the result is incorrect when the text of one label may be part of the text of another.

Expected code:

```
self.description = [mapping.get(d, d) for d in self.description]
```

Which replace label with a new one if _mapping_ contains that label as a key, otherwise we keep current label name

### Steps to reproduce

```Python
import numpy as np
import mne

test = np.array([
    [0, 0, 11],
    [1000, 0, 1],
    [1230, 0, 111]
])

test_annotation = mne.annotations_from_events(test, 256)
test_annotation.rename({'1': 'A', '11': 'B', '111': 'C'})
```


### Link to data

_No response_

### Expected results

<Annotations | 3 segments: A (1), B (1), C (1)>

### Actual results

<Annotations | 3 segments: A (1), AA (1), AAA (1)>

### Additional information

Python:           3.8.10 (default, Jun 22 2022, 20:18:18)  [GCC 9.4.0]
Executable:       /usr/bin/python3
CPU:              x86_64: 40 cores
Memory:           62.4 GB

mne:              1.1.1
numpy:            1.23.2 {}
scipy:            1.9.1
matplotlib:       3.5.3 {backend=nbAgg}

sklearn:          1.1.2
numba:            Not found
nibabel:          Not found
nilearn:          Not found
dipy:             Not found
cupy:             Not found
pandas:           1.4.4
pyvista:          Not found
pyvistaqt:        Not found
ipyvtklink:       Not found
vtk:              Not found
qtpy:             Not found
ipympl:           Not found
pyqtgraph:        Not found
pooch:            v1.6.0

mne_bids:         Not found
mne_nirs:         Not found
mne_features:     Not found
mne_qt_browser:   Not found
mne_connectivity: 0.4.0
mne_icalabel:     Not found


Discussion:
Hello! 👋 Thanks for opening your first issue here! ❤️ We will try to get back to you soon. 🚴🏽‍♂️

Hi @Qwinpin, thanks for noticing this and letting us know!
Indeed, your implementation is better and avoids the bug. Could you open a PR with your suggested change?

Resolving patch:
diff --git a/mne/annotations.py b/mne/annotations.py
--- a/mne/annotations.py
+++ b/mne/annotations.py
@@ -674,11 +674,8 @@ def rename(self, mapping, verbose=None):
         _validate_type(mapping, dict)
         _check_dict_keys(mapping, self.description, valid_key_source="data",
                          key_description="Annotation description(s)")
-
-        for old, new in mapping.items():
-            self.description = [d.replace(old, new) for d in self.description]
-
-        self.description = np.array(self.description)
+        self.description = np.array(
+            [str(mapping.get(d, d)) for d in self.description])
         return self
 
 


Test patch:
diff --git a/mne/tests/test_annotations.py b/mne/tests/test_annotations.py
--- a/mne/tests/test_annotations.py
+++ b/mne/tests/test_annotations.py
@@ -1416,6 +1416,11 @@ def test_annotation_rename():
     with pytest.raises(TypeError, match="dict, got <class 'set'> instead"):
         a.rename({"wrong"})
 
+    a = np.array([[0, 0, 11], [1000, 0, 1], [1230, 0, 111]])
+    a = mne.annotations_from_events(a, 256)
+    a.rename({'1': 'A', '11': 'B', '111': 'C'})
+    assert_array_equal(a.description, ['B', 'A', 'C'])
+
 
 def test_annotation_duration_setting():
     """Test annotation duration setting works."""