Repository: mne-tools/mne-python

Issue:
brain.add_annotation from a file fails when hemi='both'
### Description of the problem

When adding a parcellation to a brain render, you can specify a filename to read it from. Since there's two files, one for each hemisphere, there is some code that reads the file for the other hemisphere automatically. Only, this is currently broken.

### Steps to reproduce

```Python
import mne
subjects_dir = mne.datasets.sample.data_path() / 'subjects'
brain = mne.viz.Brain('fsaverage', subjects_dir=subjects_dir, hemi='both')
brain.add_annotation(str(subjects_dir / "fsaverage" / "label" / "lh.PALS_B12_Lobes.annot"))
```


### Link to data

_No response_

### Expected results

Parcellation is shown correctly.

### Actual results

`RuntimeError: To add both hemispheres simultaneously, filename must begin with "lh." or "rh."`

### Additional information

There is a unit-test verifying the above actually fails. This seems odd. Isn't this supposed to work?
https://github.com/mne-tools/mne-python/blob/99acd07823dc7018a09ccc8f1fa58a9e7ab884c2/mne/viz/_brain/tests/test_brain.py#L527


Discussion:
Maybe it should say "must **not** begin with"? But that would be weird. I would expect it just to add both so agree this is probably a bug.

While we (you?) are in there fixing things, you also shouldn't need to use a `str` cast as we nowadays try to accept `Path` everywhere. A `fname = _check_fname(fname, overwrite="read", must_exist=False)` inside that function should probably do it (including nice things like type checks)

Resolving patch:
diff --git a/mne/viz/_brain/_brain.py b/mne/viz/_brain/_brain.py
--- a/mne/viz/_brain/_brain.py
+++ b/mne/viz/_brain/_brain.py
@@ -84,6 +84,7 @@
     _to_rgb,
     _ensure_int,
     _auto_weakref,
+    _path_like,
 )
 
 
@@ -3055,20 +3056,18 @@ def add_annotation(
         hemis = self._check_hemis(hemi)
 
         # Figure out where the data is coming from
-        if isinstance(annot, str):
+        if _path_like(annot):
             if os.path.isfile(annot):
-                filepath = annot
-                path = os.path.split(filepath)[0]
-                file_hemi, annot = os.path.basename(filepath).split(".")[:2]
+                filepath = _check_fname(annot, overwrite="read")
+                file_hemi, annot = filepath.name.split(".", 1)
                 if len(hemis) > 1:
-                    if annot[:2] == "lh.":
-                        filepaths = [filepath, op.join(path, "rh" + annot[2:])]
-                    elif annot[:2] == "rh.":
-                        filepaths = [op.join(path, "lh" + annot[2:], filepath)]
+                    if file_hemi == "lh":
+                        filepaths = [filepath, filepath.parent / ("rh." + annot)]
+                    elif file_hemi == "rh":
+                        filepaths = [filepath.parent / ("lh." + annot), filepath]
                     else:
                         raise RuntimeError(
-                            "To add both hemispheres "
-                            "simultaneously, filename must "
+                            "To add both hemispheres simultaneously, filename must "
                             'begin with "lh." or "rh."'
                         )
                 else:


Test patch:
diff --git a/mne/viz/_brain/tests/test_brain.py b/mne/viz/_brain/tests/test_brain.py
--- a/mne/viz/_brain/tests/test_brain.py
+++ b/mne/viz/_brain/tests/test_brain.py
@@ -524,10 +524,9 @@ def __init__(self):
         surf="inflated",
         subjects_dir=subjects_dir,
     )
-    with pytest.raises(RuntimeError, match="both hemispheres"):
-        brain.add_annotation(str(annots[-1]))
     with pytest.raises(ValueError, match="does not exist"):
         brain.add_annotation("foo")
+    brain.add_annotation(annots[1])
     brain.close()
     brain = Brain(
         subject="fsaverage",