diff --git a/mne/simulation/evoked.py b/mne/simulation/evoked.py index 0bbf8af4cdc..d984b01d32b 100644 --- a/mne/simulation/evoked.py +++ b/mne/simulation/evoked.py @@ -69,8 +69,8 @@ def simulate_evoked(fwd, stc, info, cov, snr=3., tmin=None, tmax=None, evoked = apply_forward(fwd, stc, info) if snr < np.inf: noise = simulate_noise_evoked(evoked, cov, iir_filter, random_state) - evoked_noise = add_noise_evoked(evoked, noise, snr, - tmin=tmin, tmax=tmax) + evoked_noise = add_noise_evoked(evoked, noise, snr, tmin=tmin, + tmax=tmax) else: evoked_noise = evoked return evoked_noise @@ -111,6 +111,12 @@ def _generate_noise(info, cov, iir_filter, random_state, n_samples, zi=None): """Helper to create spatially colored and temporally IIR-filtered noise""" from scipy.signal import lfilter noise_cov = pick_channels_cov(cov, include=info['ch_names'], exclude=[]) + if set(info['ch_names']) != set(noise_cov.ch_names): + raise ValueError('Evoked and covariance channel names are not ' + 'identical. Cannot generate the noise matrix. ' + 'Channels missing in covariance %s.' % + np.setdiff1d(info['ch_names'], noise_cov.ch_names)) + rng = check_random_state(random_state) c = np.diag(noise_cov.data) if noise_cov['diag'] else noise_cov.data mu_channels = np.zeros(len(c)) diff --git a/mne/simulation/tests/test_evoked.py b/mne/simulation/tests/test_evoked.py index 01be7388142..6f1727556ba 100644 --- a/mne/simulation/tests/test_evoked.py +++ b/mne/simulation/tests/test_evoked.py @@ -65,6 +65,7 @@ def test_simulate_evoked(): stc_bad = stc.copy() mv = np.max(fwd['src'][0]['vertno'][fwd['src'][0]['inuse']]) stc_bad.vertices[0][0] = mv + 1 + assert_raises(RuntimeError, simulate_evoked, fwd, stc_bad, evoked_template.info, cov, snr, tmin=0.0, tmax=0.2) evoked_1 = simulate_evoked(fwd, stc, evoked_template.info, cov, np.inf, @@ -82,9 +83,13 @@ def test_simulate_evoked(): iir_filter=None) noise = evoked_noise.data - evoked_clean.data - empirical_snr = 10 * np.log10(np.mean((evoked_clean.data**2).ravel()) / - np.mean((noise**2).ravel())) + empirical_snr = 10 * np.log10(np.mean((evoked_clean.data ** 2).ravel()) / + np.mean((noise ** 2).ravel())) assert_almost_equal(snr, empirical_snr, decimal=5) + cov['names'] = cov.ch_names[:-2] # Error channels are different. + assert_raises(ValueError, simulate_evoked, fwd, stc, evoked_template.info, + cov, snr=3., tmin=None, tmax=None, iir_filter=None) + run_tests_if_main()