From 26f00d68009408f027e0d57843a80886cea3c4b0 Mon Sep 17 00:00:00 2001 From: jaeilepp Date: Fri, 1 Jul 2016 09:52:47 +0200 Subject: [PATCH 1/3] Error message for simulate evoked. --- mne/simulation/evoked.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/mne/simulation/evoked.py b/mne/simulation/evoked.py index 0bbf8af4cdc..9b87bae3073 100644 --- a/mne/simulation/evoked.py +++ b/mne/simulation/evoked.py @@ -69,8 +69,14 @@ 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) + if evoked.data.shape != noise.data.shape: + raise ValueError('Evoked and noise data shapes are different. Got ' + '%s and %s. Perhaps covariance has a different ' + 'set of channels?' % (evoked.data.shape, + noise.data.shape)) + + evoked_noise = add_noise_evoked(evoked, noise, snr, tmin=tmin, + tmax=tmax) else: evoked_noise = evoked return evoked_noise From b13be1d72c089da3d1c8b98633b6fe77a10b979e Mon Sep 17 00:00:00 2001 From: jaeilepp Date: Mon, 4 Jul 2016 10:31:41 +0200 Subject: [PATCH 2/3] Check channel names. --- mne/simulation/evoked.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/mne/simulation/evoked.py b/mne/simulation/evoked.py index 9b87bae3073..d984b01d32b 100644 --- a/mne/simulation/evoked.py +++ b/mne/simulation/evoked.py @@ -69,12 +69,6 @@ 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) - if evoked.data.shape != noise.data.shape: - raise ValueError('Evoked and noise data shapes are different. Got ' - '%s and %s. Perhaps covariance has a different ' - 'set of channels?' % (evoked.data.shape, - noise.data.shape)) - evoked_noise = add_noise_evoked(evoked, noise, snr, tmin=tmin, tmax=tmax) else: @@ -117,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)) From 0981a0a6ed426d8d1baf7f364f3422da63d0f323 Mon Sep 17 00:00:00 2001 From: jaeilepp Date: Mon, 4 Jul 2016 13:54:46 +0200 Subject: [PATCH 3/3] Added test. --- mne/simulation/tests/test_evoked.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) 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()