Repository: mne-tools/mne-python

Issue:
Cannot save forward solution after calling restrict_forward_to_*
Steps to reproduce:

```python
import mne
path = mne.datasets.sample.data_path()
fwd = mne.read_forward_solution(path + '/MEG/sample/sample_audvis-meg-oct-6-fwd.fif')
stc = mne.read_source_estimate(path + '/MEG/sample/sample_audvis-meg')
fwd_restricted = mne.forward.restrict_forward_to_stc(fwd, stc)
mne.write_forward_solution('restricted-fwd.fif', fwd_restricted, overwrite=True)
```

Gives the following error:

```
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-300-d1587ecf7da4> in <module>()
      4 stc = mne.read_source_estimate(path + '/MEG/sample/sample_audvis-meg')
      5 fwd_restricted = mne.forward.restrict_forward_to_stc(fwd, stc)
----> 6 mne.write_forward_solution('restricted-fwd.fif', fwd_r, overwrite=True)

/data/marijn/mne-python/mne/forward/forward.pyc in write_forward_solution(fname, fwd, overwrite, verbose)

/data/marijn/mne-python/mne/utils.pyc in verbose(function, *args, **kwargs)
    724         with use_log_level(verbose_level):
    725             return function(*args, **kwargs)
--> 726     return function(*args, **kwargs)
    727 
    728 

/data/marijn/mne-python/mne/forward/forward.pyc in write_forward_solution(fname, fwd, overwrite, verbose)
    732     # Write the source spaces (again)
    733     #
--> 734     _write_source_spaces_to_fid(fid, src)
    735     n_vert = sum([ss['nuse'] for ss in src])
    736     n_col = fwd['sol']['data'].shape[1]

/data/marijn/mne-python/mne/source_space.pyc in _write_source_spaces_to_fid(fid, src, verbose)

/data/marijn/mne-python/mne/utils.pyc in verbose(function, *args, **kwargs)
    724         with use_log_level(verbose_level):
    725             return function(*args, **kwargs)
--> 726     return function(*args, **kwargs)
    727 
    728 

/data/marijn/mne-python/mne/source_space.pyc in _write_source_spaces_to_fid(fid, src, verbose)
    926         logger.info('    Write a source space...')
    927         start_block(fid, FIFF.FIFFB_MNE_SOURCE_SPACE)
--> 928         _write_one_source_space(fid, s, verbose)
    929         end_block(fid, FIFF.FIFFB_MNE_SOURCE_SPACE)
    930         logger.info('    [done]')

/data/marijn/mne-python/mne/source_space.pyc in _write_one_source_space(fid, this, verbose)
   1016         write_int(fid, FIFF.FIFF_MNE_SOURCE_SPACE_NUSE_TRI, this['nuse_tri'])
   1017         write_int_matrix(fid, FIFF.FIFF_MNE_SOURCE_SPACE_USE_TRIANGLES,
-> 1018                          this['use_tris'] + 1)
   1019 
   1020     if this['type'] == 'vol':

/data/marijn/mne-python/mne/io/write.pyc in write_int_matrix(fid, kind, mat)
    172 
    173     dims = np.empty(3, dtype=np.int32)
--> 174     dims[0] = mat.shape[1]
    175     dims[1] = mat.shape[0]
    176     dims[2] = 2

IndexError: tuple index out of range
```


Discussion:
looks like restrict_forward_to_stc forgets some stuff to do :)

Yup, I'll fix and make a PR

Resolving patch:
diff --git a/mne/forward/forward.py b/mne/forward/forward.py
--- a/mne/forward/forward.py
+++ b/mne/forward/forward.py
@@ -1247,7 +1247,7 @@ def restrict_forward_to_stc(fwd, stc):
         fwd_out['src'][i]['inuse'] = fwd['src'][i]['inuse'].copy()
         fwd_out['src'][i]['inuse'].fill(0)
         fwd_out['src'][i]['inuse'][stc.vertices[i]] = 1
-        fwd_out['src'][i]['use_tris'] = np.array([], int)
+        fwd_out['src'][i]['use_tris'] = np.array([[]], int)
         fwd_out['src'][i]['nuse_tri'] = np.array([0])
 
     return fwd_out
@@ -1300,7 +1300,7 @@ def restrict_forward_to_label(fwd, labels):
         fwd_out['src'][i]['nuse'] = 0
         fwd_out['src'][i]['inuse'] = fwd['src'][i]['inuse'].copy()
         fwd_out['src'][i]['inuse'].fill(0)
-        fwd_out['src'][i]['use_tris'] = np.array([], int)
+        fwd_out['src'][i]['use_tris'] = np.array([[]], int)
         fwd_out['src'][i]['nuse_tri'] = np.array([0])
 
         # src_sel is idx to cols in fwd that are in any label per hemi


Test patch:
diff --git a/mne/forward/tests/test_forward.py b/mne/forward/tests/test_forward.py
--- a/mne/forward/tests/test_forward.py
+++ b/mne/forward/tests/test_forward.py
@@ -235,6 +235,14 @@ def test_restrict_forward_to_stc():
     assert_equal(fwd_out['src'][0]['vertno'], fwd['src'][0]['vertno'][0:15])
     assert_equal(fwd_out['src'][1]['vertno'], fwd['src'][1]['vertno'][0:5])
 
+    # Test saving the restricted forward object. This only works if all fields
+    # are properly accounted for.
+    temp_dir = _TempDir()
+    fname_copy = op.join(temp_dir, 'copy-fwd.fif')
+    write_forward_solution(fname_copy, fwd_out, overwrite=True)
+    fwd_out_read = read_forward_solution(fname_copy)
+    compare_forwards(fwd_out, fwd_out_read)
+
 
 @testing.requires_testing_data
 def test_restrict_forward_to_label():
@@ -293,6 +301,14 @@ def test_restrict_forward_to_label():
     assert_equal(fwd_out['src'][0]['vertno'], vertno_lh)
     assert_equal(fwd_out['src'][1]['vertno'], vertno_rh)
 
+    # Test saving the restricted forward object. This only works if all fields
+    # are properly accounted for.
+    temp_dir = _TempDir()
+    fname_copy = op.join(temp_dir, 'copy-fwd.fif')
+    write_forward_solution(fname_copy, fwd_out, overwrite=True)
+    fwd_out_read = read_forward_solution(fname_copy)
+    compare_forwards(fwd_out, fwd_out_read)
+
 
 @testing.requires_testing_data
 @requires_mne