def test_degenerate(raw_data): """Test degenerate conditions.""" raw, src, stc, trans, sphere = raw_data info = raw.info # Make impossible transform (translate up into helmet) and ensure failure hp_err = _get_head_pos_sim(raw) hp_err[1.][2, 3] -= 0.1 # z trans upward 10cm with pytest.raises(RuntimeError, match='collided with inner skull'): simulate_raw(info, stc, trans, src, sphere, head_pos=hp_err) # other degenerate conditions with pytest.raises(TypeError, match='info must be an instance of'): simulate_raw('foo', stc, trans, src, sphere) with pytest.raises(TypeError, match='stc must be an instance of'): simulate_raw(info, 'foo', trans, src, sphere) with pytest.raises(ValueError, match='stc must have at least three time'): simulate_raw(info, stc.copy().crop(0, 0), trans, src, sphere) with pytest.raises(TypeError, match='must be an instance of Info'): simulate_raw(0, stc, trans, src, sphere) stc_bad = stc.copy() stc_bad.tstep += 0.1 with pytest.raises(ValueError, match='same sample rate'): simulate_raw(info, stc_bad, trans, src, sphere) with pytest.raises(ValueError, match='interp must be one of'): simulate_raw(info, stc, trans, src, sphere, interp='foo') with pytest.raises(TypeError, match='unknown head_pos type'): simulate_raw(info, stc, trans, src, sphere, head_pos=1.) head_pos_sim_err = _get_head_pos_sim(raw) head_pos_sim_err[-1.] = head_pos_sim_err[1.] # negative time with pytest.raises(RuntimeError, match='All position times'): simulate_raw(info, stc, trans, src, sphere, head_pos=head_pos_sim_err) raw_bad = raw.copy() raw_bad.info['dig'] = None with pytest.raises(RuntimeError, match='Cannot fit headshape'): add_eog(raw_bad)
def test_simulation_cascade(): """Test that cascading operations do not overwrite data.""" # Create 10 second raw dataset with zeros in the data matrix raw_null = read_raw_fif(raw_chpi_fname, allow_maxshield='yes') raw_null.crop(0, 1).pick_types(meg=True).load_data() raw_null.apply_function(lambda x: np.zeros_like(x)) assert_array_equal(raw_null.get_data(), 0.) # Calculate independent signal additions raw_eog = raw_null.copy() add_eog(raw_eog, random_state=0) raw_ecg = raw_null.copy() add_ecg(raw_ecg, random_state=0) raw_noise = raw_null.copy() cov = make_ad_hoc_cov(raw_null.info) add_noise(raw_noise, cov, random_state=0) raw_chpi = raw_null.copy() add_chpi(raw_chpi) # Calculate Cascading signal additions raw_cascade = raw_null.copy() add_eog(raw_cascade, random_state=0) add_ecg(raw_cascade, random_state=0) add_chpi(raw_cascade) add_noise(raw_cascade, cov, random_state=0) cascade_data = raw_cascade.get_data() serial_data = 0. for raw_other in (raw_eog, raw_ecg, raw_noise, raw_chpi): serial_data += raw_other.get_data() assert_allclose(cascade_data, serial_data, atol=1e-20)
def test_degenerate(raw_data): """Test degenerate conditions.""" raw, src, stc, trans, sphere = raw_data info = raw.info # Make impossible transform (translate up into helmet) and ensure failure hp_err = _get_head_pos_sim(raw) hp_err[1.][2, 3] -= 0.1 # z trans upward 10cm with pytest.raises(RuntimeError, match='collided with inner skull'): simulate_raw(info, stc, trans, src, sphere, cov=None, head_pos=hp_err) # other degenerate conditions with pytest.raises(TypeError, match='info must be an instance of'): simulate_raw('foo', stc, trans, src, sphere) with pytest.raises(TypeError, match='stc must be an instance of'): simulate_raw(info, 'foo', trans, src, sphere) with pytest.raises(ValueError, match='stc must have at least three time'): simulate_raw(info, stc.copy().crop(0, 0), trans, src, sphere) with pytest.raises(TypeError, match='must be an instance of Raw or Info'): simulate_raw(0, stc, trans, src, sphere) stc_bad = stc.copy() stc_bad.tstep += 0.1 with pytest.raises(ValueError, match='same sample rate'): simulate_raw(info, stc_bad, trans, src, sphere) with pytest.raises(TypeError, match='Covariance matrix type'): with pytest.deprecated_call(): simulate_raw(info, stc, trans, src, sphere, cov=0) with pytest.raises(RuntimeError, match='cHPI information not found'): with pytest.deprecated_call(): simulate_raw(info, stc, trans, src, sphere, chpi=True) with pytest.raises(ValueError, match='interp must be one of'): simulate_raw(info, stc, trans, src, sphere, interp='foo') with pytest.raises(TypeError, match='unknown head_pos type'): simulate_raw(info, stc, trans, src, sphere, head_pos=1.) with pytest.raises(RuntimeError, match='All position times'): with pytest.deprecated_call(): simulate_raw(raw, stc, trans, src, sphere, head_pos=pos_fname) head_pos_sim_err = _get_head_pos_sim(raw) head_pos_sim_err[-1.] = head_pos_sim_err[1.] # negative time with pytest.raises(RuntimeError, match='All position times'): simulate_raw(info, stc, trans, src, sphere, head_pos=head_pos_sim_err) raw_bad = raw.copy() raw_bad.info['dig'] = None with pytest.raises(RuntimeError, match='Cannot fit headshape'): with pytest.deprecated_call(): simulate_raw(raw_bad, stc, trans, src, sphere, blink=True) with pytest.raises(RuntimeError, match='Cannot fit headshape'): add_eog(raw_bad)
def test_simulate_raw_sphere(raw_data, tmpdir): """Test simulation of raw data with sphere model.""" seed = 42 raw, src, stc, trans, sphere = raw_data assert len(pick_types(raw.info, meg=False, ecg=True)) == 1 tempdir = str(tmpdir) # head pos head_pos_sim = _get_head_pos_sim(raw) # # Test raw simulation with basic parameters # raw.info.normalize_proj() cov = read_cov(cov_fname) cov['projs'] = raw.info['projs'] raw.info['bads'] = raw.ch_names[:1] with pytest.deprecated_call(match='cov is deprecated'): raw_sim = simulate_raw(raw, stc, trans, src, sphere, cov, head_pos=head_pos_sim, blink=True, ecg=True, random_state=seed, verbose=True) with pytest.warns(RuntimeWarning, match='applying projector with'): raw_sim_2 = simulate_raw(raw, stc, trans_fname, src_fname, sphere, cov_fname, head_pos=head_pos_sim, blink=True, ecg=True, random_state=seed) with pytest.raises(RuntimeError, match='Maximum number of STC iterations'): simulate_raw(raw.info, [stc] * 5, trans_fname, src_fname, sphere, cov=None, max_iter=1) assert_array_equal(raw_sim_2[:][0], raw_sim[:][0]) std = dict(grad=2e-13, mag=10e-15, eeg=0.1e-6) with pytest.deprecated_call(): raw_sim = simulate_raw(raw, stc, trans, src, sphere, make_ad_hoc_cov(raw.info, std=std), head_pos=head_pos_sim, blink=True, ecg=True, random_state=seed) with pytest.deprecated_call(): raw_sim_2 = simulate_raw(raw, stc, trans_fname, src_fname, sphere, cov=std, head_pos=head_pos_sim, blink=True, ecg=True, random_state=seed) assert_array_equal(raw_sim_2[:][0], raw_sim[:][0]) sphere_norad = make_sphere_model('auto', None, raw.info) raw_meg = raw.copy().pick_types() with pytest.deprecated_call(): raw_sim = simulate_raw(raw_meg, stc, trans, src, sphere_norad, cov=None, head_pos=head_pos_sim, blink=True, ecg=True, random_state=seed) with pytest.deprecated_call(): raw_sim_2 = simulate_raw(raw_meg, stc, trans_fname, src_fname, sphere_norad, cov=None, head_pos=head_pos_sim, blink=True, ecg=True, random_state=seed) assert_array_equal(raw_sim_2[:][0], raw_sim[:][0]) # Test IO on processed data test_outname = op.join(tempdir, 'sim_test_raw.fif') raw_sim.save(test_outname) raw_sim_loaded = read_raw_fif(test_outname, preload=True) assert_allclose(raw_sim_loaded[:][0], raw_sim[:][0], rtol=1e-6, atol=1e-20) del raw_sim, raw_sim_2 # with no cov (no noise) but with artifacts, most time periods should match # but the EOG/ECG channels should not for ecg, eog in ((True, False), (False, True), (True, True)): with pytest.deprecated_call(): raw_sim_3 = simulate_raw(raw, stc, trans, src, sphere, cov=None, head_pos=head_pos_sim, blink=eog, ecg=ecg, random_state=seed) with pytest.deprecated_call(): raw_sim_4 = simulate_raw(raw, stc, trans, src, sphere, cov=None, head_pos=head_pos_sim, blink=False, ecg=False, random_state=seed) picks = np.arange(len(raw.ch_names)) diff_picks = pick_types(raw.info, meg=False, ecg=ecg, eog=eog) these_picks = np.setdiff1d(picks, diff_picks) close = np.isclose(raw_sim_3[these_picks][0], raw_sim_4[these_picks][0], atol=1e-20) assert np.mean(close) > 0.7 far = ~np.isclose( raw_sim_3[diff_picks][0], raw_sim_4[diff_picks][0], atol=1e-20) assert np.mean(far) > 0.99 del raw_sim_3, raw_sim_4 # make sure it works with EEG-only and MEG-only with pytest.deprecated_call(): raw_sim_meg = simulate_raw(raw.copy().pick_types(meg=True, eeg=False), stc, trans, src, sphere, cov=None) raw_sim_eeg = simulate_raw(raw.copy().pick_types(meg=False, eeg=True), stc, trans, src, sphere, cov=None) raw_sim_meeg = simulate_raw(raw.copy().pick_types(meg=True, eeg=True), stc, trans, src, sphere, cov=None) for this_raw in (raw_sim_meg, raw_sim_eeg, raw_sim_meeg): add_eog(this_raw, random_state=seed) for this_raw in (raw_sim_meg, raw_sim_meeg): add_ecg(this_raw, random_state=seed) with pytest.raises(RuntimeError, match='only add ECG artifacts if MEG'): add_ecg(raw_sim_eeg) assert_allclose(np.concatenate((raw_sim_meg[:][0], raw_sim_eeg[:][0])), raw_sim_meeg[:][0], rtol=1e-7, atol=1e-20) del raw_sim_meg, raw_sim_eeg, raw_sim_meeg # check that raw-as-info is supported n_samp = len(stc.times) raw_crop = raw.copy().crop(0., (n_samp - 1.) / raw.info['sfreq']) assert len(raw_crop.times) == len(stc.times) with pytest.deprecated_call(): raw_sim = simulate_raw(raw_crop, stc, trans, src, sphere, cov=None) with catch_logging() as log: raw_sim_2 = simulate_raw(raw_crop.info, stc, trans, src, sphere, cov=None, verbose=True) log = log.getvalue() assert '1 STC iteration provided' in log assert len(raw_sim_2.times) == n_samp assert_allclose(raw_sim[:, :n_samp][0], raw_sim_2[:, :n_samp][0], rtol=1e-5, atol=1e-30) del raw_sim, raw_sim_2 # check that different interpolations are similar given small movements with pytest.deprecated_call(): raw_sim = simulate_raw(raw, stc, trans, src, sphere, cov=None, head_pos=head_pos_sim, interp='linear') with pytest.deprecated_call(): raw_sim_hann = simulate_raw(raw, stc, trans, src, sphere, cov=None, head_pos=head_pos_sim, interp='hann') assert_allclose(raw_sim[:][0], raw_sim_hann[:][0], rtol=1e-1, atol=1e-14) del raw_sim_hann # check that new Generator objects can be used if check_version('numpy', '1.17'): random_state = np.random.default_rng(seed) add_ecg(raw_sim, random_state=random_state) add_eog(raw_sim, random_state=random_state)
def test_simulate_raw_sphere(raw_data, tmpdir): """Test simulation of raw data with sphere model.""" seed = 42 raw, src, stc, trans, sphere = raw_data assert len(pick_types(raw.info, meg=False, ecg=True)) == 1 tempdir = str(tmpdir) # head pos head_pos_sim = _get_head_pos_sim(raw) # # Test raw simulation with basic parameters # raw.info.normalize_proj() cov = read_cov(cov_fname) cov['projs'] = raw.info['projs'] raw.info['bads'] = raw.ch_names[:1] sphere_norad = make_sphere_model('auto', None, raw.info) raw_meg = raw.copy().pick_types() raw_sim = simulate_raw(raw_meg.info, stc, trans, src, sphere_norad, head_pos=head_pos_sim) # Test IO on processed data test_outname = op.join(tempdir, 'sim_test_raw.fif') raw_sim.save(test_outname) raw_sim_loaded = read_raw_fif(test_outname, preload=True) assert_allclose(raw_sim_loaded[:][0], raw_sim[:][0], rtol=1e-6, atol=1e-20) del raw_sim # make sure it works with EEG-only and MEG-only raw_sim_meg = simulate_raw( raw.copy().pick_types(meg=True, eeg=False).info, stc, trans, src, sphere) raw_sim_eeg = simulate_raw( raw.copy().pick_types(meg=False, eeg=True).info, stc, trans, src, sphere) raw_sim_meeg = simulate_raw( raw.copy().pick_types(meg=True, eeg=True).info, stc, trans, src, sphere) for this_raw in (raw_sim_meg, raw_sim_eeg, raw_sim_meeg): add_eog(this_raw, random_state=seed) for this_raw in (raw_sim_meg, raw_sim_meeg): add_ecg(this_raw, random_state=seed) with pytest.raises(RuntimeError, match='only add ECG artifacts if MEG'): add_ecg(raw_sim_eeg) assert_allclose(np.concatenate((raw_sim_meg[:][0], raw_sim_eeg[:][0])), raw_sim_meeg[:][0], rtol=1e-7, atol=1e-20) del raw_sim_meg, raw_sim_eeg, raw_sim_meeg # check that raw-as-info is supported n_samp = len(stc.times) raw_crop = raw.copy().crop(0., (n_samp - 1.) / raw.info['sfreq']) assert len(raw_crop.times) == len(stc.times) raw_sim = simulate_raw(raw_crop.info, stc, trans, src, sphere) with catch_logging() as log: raw_sim_2 = simulate_raw(raw_crop.info, stc, trans, src, sphere, verbose=True) log = log.getvalue() assert '1 STC iteration provided' in log assert len(raw_sim_2.times) == n_samp assert_allclose(raw_sim[:, :n_samp][0], raw_sim_2[:, :n_samp][0], rtol=1e-5, atol=1e-30) del raw_sim, raw_sim_2 # check that different interpolations are similar given small movements raw_sim = simulate_raw(raw.info, stc, trans, src, sphere, head_pos=head_pos_sim, interp='linear') raw_sim_hann = simulate_raw(raw.info, stc, trans, src, sphere, head_pos=head_pos_sim, interp='hann') assert_allclose(raw_sim[:][0], raw_sim_hann[:][0], rtol=1e-1, atol=1e-14) del raw_sim_hann # check that new Generator objects can be used if check_version('numpy', '1.17'): random_state = np.random.default_rng(seed) add_ecg(raw_sim, random_state=random_state) add_eog(raw_sim, random_state=random_state)
times = raw.times[:int(raw.info['sfreq'] * epoch_duration)] fwd = mne.read_forward_solution(fwd_fname) src = fwd['src'] stc = simulate_sparse_stc(src, n_dipoles=n_dipoles, times=times, data_fun=data_fun, random_state=rng) # look at our source data fig, ax = plt.subplots(1) ax.plot(times, 1e9 * stc.data.T) ax.set(ylabel='Amplitude (nAm)', xlabel='Time (sec)') mne.viz.utils.plt_show() ############################################################################## # Simulate raw data raw_sim = simulate_raw(raw.info, [stc] * 10, forward=fwd, verbose=True) cov = make_ad_hoc_cov(raw_sim.info) add_noise(raw_sim, cov, iir_filter=[0.2, -0.2, 0.04], random_state=rng) add_ecg(raw_sim, random_state=rng) add_eog(raw_sim, random_state=rng) raw_sim.plot() ############################################################################## # Plot evoked data events = find_events(raw_sim) # only 1 pos, so event number == 1 epochs = Epochs(raw_sim, events, 1, tmin=-0.2, tmax=epoch_duration) cov = compute_covariance(epochs, tmax=0., method='empirical', verbose='error') # quick calc evoked = epochs.average() evoked.plot_white(cov, time_unit='s')
def test_simulate_raw_sphere(raw_data, tmpdir): """Test simulation of raw data with sphere model.""" seed = 42 raw, src, stc, trans, sphere = raw_data assert len(pick_types(raw.info, meg=False, ecg=True)) == 1 tempdir = str(tmpdir) # head pos head_pos_sim = _get_head_pos_sim(raw) # # Test raw simulation with basic parameters # raw.info.normalize_proj() cov = read_cov(cov_fname) cov['projs'] = raw.info['projs'] raw.info['bads'] = raw.ch_names[:1] with pytest.deprecated_call(match='cov is deprecated'): raw_sim = simulate_raw(raw, stc, trans, src, sphere, cov, head_pos=head_pos_sim, blink=True, ecg=True, random_state=seed, verbose=True) with pytest.warns(RuntimeWarning, match='applying projector with'): raw_sim_2 = simulate_raw(raw, stc, trans_fname, src_fname, sphere, cov_fname, head_pos=head_pos_sim, blink=True, ecg=True, random_state=seed) with pytest.raises(RuntimeError, match='Maximum number of STC iterations'): simulate_raw(raw.info, [stc] * 5, trans_fname, src_fname, sphere, cov=None, max_iter=1) assert_array_equal(raw_sim_2[:][0], raw_sim[:][0]) std = dict(grad=2e-13, mag=10e-15, eeg=0.1e-6) with pytest.deprecated_call(): raw_sim = simulate_raw(raw, stc, trans, src, sphere, make_ad_hoc_cov(raw.info, std=std), head_pos=head_pos_sim, blink=True, ecg=True, random_state=seed) with pytest.deprecated_call(): raw_sim_2 = simulate_raw(raw, stc, trans_fname, src_fname, sphere, cov=std, head_pos=head_pos_sim, blink=True, ecg=True, random_state=seed) assert_array_equal(raw_sim_2[:][0], raw_sim[:][0]) sphere_norad = make_sphere_model('auto', None, raw.info) raw_meg = raw.copy().pick_types() with pytest.deprecated_call(): raw_sim = simulate_raw(raw_meg, stc, trans, src, sphere_norad, cov=None, head_pos=head_pos_sim, blink=True, ecg=True, random_state=seed) with pytest.deprecated_call(): raw_sim_2 = simulate_raw(raw_meg, stc, trans_fname, src_fname, sphere_norad, cov=None, head_pos=head_pos_sim, blink=True, ecg=True, random_state=seed) assert_array_equal(raw_sim_2[:][0], raw_sim[:][0]) # Test IO on processed data test_outname = op.join(tempdir, 'sim_test_raw.fif') raw_sim.save(test_outname) raw_sim_loaded = read_raw_fif(test_outname, preload=True) assert_allclose(raw_sim_loaded[:][0], raw_sim[:][0], rtol=1e-6, atol=1e-20) del raw_sim, raw_sim_2 # with no cov (no noise) but with artifacts, most time periods should match # but the EOG/ECG channels should not for ecg, eog in ((True, False), (False, True), (True, True)): with pytest.deprecated_call(): raw_sim_3 = simulate_raw(raw, stc, trans, src, sphere, cov=None, head_pos=head_pos_sim, blink=eog, ecg=ecg, random_state=seed) with pytest.deprecated_call(): raw_sim_4 = simulate_raw(raw, stc, trans, src, sphere, cov=None, head_pos=head_pos_sim, blink=False, ecg=False, random_state=seed) picks = np.arange(len(raw.ch_names)) diff_picks = pick_types(raw.info, meg=False, ecg=ecg, eog=eog) these_picks = np.setdiff1d(picks, diff_picks) close = np.isclose(raw_sim_3[these_picks][0], raw_sim_4[these_picks][0], atol=1e-20) assert np.mean(close) > 0.7 far = ~np.isclose(raw_sim_3[diff_picks][0], raw_sim_4[diff_picks][0], atol=1e-20) assert np.mean(far) > 0.99 del raw_sim_3, raw_sim_4 # make sure it works with EEG-only and MEG-only with pytest.deprecated_call(): raw_sim_meg = simulate_raw(raw.copy().pick_types(meg=True, eeg=False), stc, trans, src, sphere, cov=None) raw_sim_eeg = simulate_raw(raw.copy().pick_types(meg=False, eeg=True), stc, trans, src, sphere, cov=None) raw_sim_meeg = simulate_raw(raw.copy().pick_types(meg=True, eeg=True), stc, trans, src, sphere, cov=None) for this_raw in (raw_sim_meg, raw_sim_eeg, raw_sim_meeg): add_eog(this_raw, random_state=seed) for this_raw in (raw_sim_meg, raw_sim_meeg): add_ecg(this_raw, random_state=seed) with pytest.raises(RuntimeError, match='only add ECG artifacts if MEG'): add_ecg(raw_sim_eeg) assert_allclose(np.concatenate((raw_sim_meg[:][0], raw_sim_eeg[:][0])), raw_sim_meeg[:][0], rtol=1e-7, atol=1e-20) del raw_sim_meg, raw_sim_eeg, raw_sim_meeg # check that raw-as-info is supported n_samp = len(stc.times) raw_crop = raw.copy().crop(0., (n_samp - 1.) / raw.info['sfreq']) assert len(raw_crop.times) == len(stc.times) with pytest.deprecated_call(): raw_sim = simulate_raw(raw_crop, stc, trans, src, sphere, cov=None) with catch_logging() as log: raw_sim_2 = simulate_raw(raw_crop.info, stc, trans, src, sphere, cov=None, verbose=True) log = log.getvalue() assert '1 STC iteration provided' in log assert len(raw_sim_2.times) == n_samp assert_allclose(raw_sim[:, :n_samp][0], raw_sim_2[:, :n_samp][0], rtol=1e-5, atol=1e-30) del raw_sim, raw_sim_2 # check that different interpolations are similar given small movements with pytest.deprecated_call(): raw_sim = simulate_raw(raw, stc, trans, src, sphere, cov=None, head_pos=head_pos_sim, interp='linear') with pytest.deprecated_call(): raw_sim_hann = simulate_raw(raw, stc, trans, src, sphere, cov=None, head_pos=head_pos_sim, interp='hann') assert_allclose(raw_sim[:][0], raw_sim_hann[:][0], rtol=1e-1, atol=1e-14) del raw_sim, raw_sim_hann