Repository: mne-tools/mne-python

Issue:
FIX: Make make_watershed_bem write correct surfaces inside bem directory
#### Describe the bug
`mne.bem.make_watershed_bem` incorrectly save surfaces to the current directory

#### Steps to reproduce
```python
import mne

subjects_dir = '../mne_data/MNE-testing-data/subjects'
subject = 'sample'
mne.bem.make_watershed_bem('sample', subjects_dir=subjects_dir, overwrite=False, show=False, verbose=True)

```

#### Expected results
```
bem
└── watershed
    ├── sample_brain_surface
    ├── sample_inner_skull_surface
    ├── sample_outer_skin_surface
    └── sample_outer_skull_surface
```


#### Actual results
```
# surfaces with updated volume info
.
├── brain
├── inner_skull
├── outer_skin
└── outer_skull
```

```
# surfaces without updated volume info
bem
└── watershed
    ├── sample_brain_surface
    ├── sample_inner_skull_surface
    ├── sample_outer_skin_surface
    └── sample_outer_skull_surface
```
#### Problem

Since `mne.bem.make_watershed_bem` incorrectly write surfaces to the current directory, it cannot be executed unless the created surfaces were deleted.

Line 1163 in `bem.py` writes surfaces with updated volume info into `brain`, `inner_skull`, `outer_skull`, `outer_skin` in the current directory. The symbolic links thus point to the original surfaces computed by FreeSurfer.
```python
        surfs = ['brain', 'inner_skull', 'outer_skull', 'outer_skin']
        for s in surfs:
            surf_ws_out = op.join(ws_dir, '%s_%s_surface' % (subject, s))

            rr, tris, volume_info = read_surface(surf_ws_out,
                                                 read_metadata=True)
            volume_info.update(new_info)  # replace volume info, 'head' stays

            write_surface(s, rr, tris, volume_info=volume_info,
                          overwrite=overwrite)
            # Create symbolic links
            surf_out = op.join(bem_dir, '%s.surf' % s)
            if not overwrite and op.exists(surf_out):
                skip_symlink = True
            else:
                if op.exists(surf_out):
                    os.remove(surf_out)
                _symlink(surf_ws_out, surf_out, copy)
                skip_symlink = False
```

After running `mne.bem.make_watershed_bem`, since `brain`, `inner_skull`, `outer_skull`, `outer_skin` present in the current directory, `mne.bem.make_watershed_bem` will complain about existing files in the next run.
Even when setting `overwrite=True`, the volume info still won't be updated and `brain`, `inner_skull`, `outer_skull`, `outer_skin` will be repeatedly written and remain in the currently directory to cause confusion.

#### Possible solution

I tried to make some quick modifications. Surfaces with updated volume info will be saved and pointed by the symbolic links. If `overwrite=True`, original surfaces will be overwritten, otherwise it will be renamed.

If this makes sense, I would try to make a PR for this.

```python
        for s in surfs:
            surf_ws_out = op.join(ws_dir, '%s_%s_surface' % (subject, s))

            rr, tris, volume_info = read_surface(surf_ws_out,
                                                 read_metadata=True)

            if overwrite:
                # replace volume info, 'head' stays
                volume_info.update(new_info)
                write_surface(surf_ws_out, rr, tris,
                            volume_info=volume_info, overwrite=True)
            else:
                orig_surf_ws_out = op.join(ws_dir, '%s_%s_original_surface' % (subject, s))
                # rename surfaces with the original volume info
                os.rename(surf_ws_out, orig_surf_ws_out)
                volume_info.update(new_info)
                write_surface(surf_ws_out, rr, tris, volume_info=volume_info,
                              overwrite=False)

```


#### Additional information
Platform:      Linux-5.3.14-200.fc30.x86_64-x86_64-with-fedora-30-Thirty
Python:        3.7.5 (default, Oct 17 2019, 12:09:47)  [GCC 9.2.1 20190827 (Red Hat 9.2.1-1)]
Executable:    /home/yuhan/env/mne/bin/python
CPU:           x86_64: 4 cores
Memory:        15.6 GB

mne:           0.19.1
numpy:         1.17.3 {blas=openblas, lapack=openblas}
scipy:         1.3.2
matplotlib:    3.1.1 {backend=Qt5Agg}

sklearn:       0.21.3
numba:         0.46.0
nibabel:       2.5.1
cupy:          Not found
pandas:        0.25.3
dipy:          1.0.0
mayavi:        4.7.1 {qt_api=pyqt5, PyQt5=5.13.2}
pyvista:       0.22.4
vtk:           8.1.2


Discussion:
@yh-luo thanks for the detailed but report. Can you open a PR to fix this? thanks again

Resolving patch:
diff --git a/mne/bem.py b/mne/bem.py
--- a/mne/bem.py
+++ b/mne/bem.py
@@ -1146,6 +1146,7 @@ def make_watershed_bem(subject, subjects_dir=None, overwrite=False,
                 % (ws_dir, ' '.join(cmd)))
     os.makedirs(op.join(ws_dir))
     run_subprocess_env(cmd)
+    del tempdir  # clean up directory
     if op.isfile(T1_mgz):
         new_info = _extract_volume_info(T1_mgz)
         if new_info is None:
@@ -1158,10 +1159,11 @@ def make_watershed_bem(subject, subjects_dir=None, overwrite=False,
 
             rr, tris, volume_info = read_surface(surf_ws_out,
                                                  read_metadata=True)
-            volume_info.update(new_info)  # replace volume info, 'head' stays
+            # replace volume info, 'head' stays
+            volume_info.update(new_info)
+            write_surface(surf_ws_out, rr, tris, volume_info=volume_info,
+                          overwrite=True)
 
-            write_surface(s, rr, tris, volume_info=volume_info,
-                          overwrite=overwrite)
             # Create symbolic links
             surf_out = op.join(bem_dir, '%s.surf' % s)
             if not overwrite and op.exists(surf_out):


Test patch: