Repository: mne-tools/mne-python

Issue:
simulate_sparse_stc throws assertion error when n_dipoles > len(labels)
#### Describe the bug
 ```simulate_sparse_stc ```  throws an assertion error when   ```n_dipoles > len(labels) ```.
It should generate   ```min(n_dipoles, len(labels))  ``` dipoles.


#### Steps and/or code to reproduce
If you slightly modified the code of the following MNE example: 
https://www.martinos.org/mne/stable/auto_examples/simulation/plot_simulate_evoked_data.html#sphx-glr-auto-examples-simulation-plot-simulate-evoked-data-py
by remplacing ```n_dipoles=2``` by ```n_dipoles=4``` ligne 58, whiwh I did here:
https://gist.github.com/QB3/6ac76962c9ab7b9f1b44c1026021cafe
an assertion error is thrown.

#### Expected results
As  ```len(labels) = 3  ``` I expect 3 dipoles.


#### Actual results
assertion error from this line https://github.com/mne-tools/mne-python/blob/8b00fc96ec8574c364007ea6b9655eb288d464bb/mne/simulation/source.py#L210





Discussion:
I would suggest to raise an informative exception message rather than
silently using min(n_dipoles, len(labels))

>

OK then we will have to change also the current warning msg:
https://github.com/mne-tools/mne-python/blob/8b00fc96ec8574c364007ea6b9655eb288d464bb/mne/simulation/source.py#L180
hum so now you get a warning but it will always crash afterwards? Basically warn should be ValueError or the `min(n_dipoles, len(labels))` should actually be implemented?
yes, current warning is  ```The number of labels is different from the number of dipoles. 2 dipole(s) will be generated```, but then an assertion error is thrown
ok then I would make it a ValueError and not a warn

>

OK I will do this then:
-  ```n_dipoles > len(labels)```  --->>> ValueError
- ```n_dipoles < len(labels)```  ---->>> keep the same generate ```n_dipoles``` dipoles
go for it @QB3


Resolving patch:
diff --git a/mne/simulation/source.py b/mne/simulation/source.py
--- a/mne/simulation/source.py
+++ b/mne/simulation/source.py
@@ -175,6 +175,9 @@ def simulate_sparse_stc(src, n_dipoles, times,
         vs = [s['vertno'][np.sort(rng.permutation(np.arange(s['nuse']))[:n])]
               for n, s in zip(n_dipoles_ss, src)]
         datas = data
+    elif n_dipoles > len(labels):
+        raise ValueError('Number of labels (%d) smaller than n_dipoles (%d) '
+                         'is not allowed.' % (len(labels), n_dipoles))
     else:
         if n_dipoles != len(labels):
             warn('The number of labels is different from the number of '


Test patch:
diff --git a/mne/simulation/tests/test_source.py b/mne/simulation/tests/test_source.py
--- a/mne/simulation/tests/test_source.py
+++ b/mne/simulation/tests/test_source.py
@@ -146,6 +146,12 @@ def test_simulate_sparse_stc():
                   subjects_dir=subjects_dir)  # no subject
     pytest.raises(ValueError, simulate_sparse_stc, fwd['src'], len(labels),
                   times, labels=labels, location='foo')  # bad location
+    err_str = 'Number of labels'
+    with pytest.raises(ValueError, match=err_str):
+        simulate_sparse_stc(
+            fwd['src'], len(labels) + 1, times, labels=labels,
+            random_state=random_state, location=location,
+            subjects_dir=subjects_dir)
 
 
 @testing.requires_testing_data