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_add_noise(): """Test noise addition.""" if check_version('numpy', '1.17'): rng = np.random.default_rng(0) else: rng = np.random.RandomState(0) raw = read_raw_fif(raw_fname) raw.del_proj() picks = pick_types(raw.info, meg=True, eeg=True, exclude=()) cov = compute_raw_covariance(raw, picks=picks) with pytest.raises(RuntimeError, match='to be loaded'): add_noise(raw, cov) raw.crop(0, 1).load_data() with pytest.raises(TypeError, match='Raw, Epochs, or Evoked'): add_noise(0., cov) with pytest.raises(TypeError, match='Covariance'): add_noise(raw, 0.) # test a no-op (data preserved) orig_data = raw[:][0] zero_cov = cov.copy() zero_cov['data'].fill(0) add_noise(raw, zero_cov) new_data = raw[:][0] assert_allclose(orig_data, new_data, atol=1e-30) # set to zero to make comparisons easier raw._data[:] = 0. epochs = EpochsArray(np.zeros((1, len(raw.ch_names), 100)), raw.info.copy()) epochs.info['bads'] = [] evoked = epochs.average(picks=np.arange(len(raw.ch_names))) for inst in (raw, epochs, evoked): with catch_logging() as log: add_noise(inst, cov, random_state=rng, verbose=True) log = log.getvalue() want = ('to {0}/{1} channels ({0}' .format(len(cov['names']), len(raw.ch_names))) assert want in log if inst is evoked: inst = EpochsArray(inst.data[np.newaxis], inst.info) if inst is raw: cov_new = compute_raw_covariance(inst, picks=picks) else: cov_new = compute_covariance(inst) assert cov['names'] == cov_new['names'] r = np.corrcoef(cov['data'].ravel(), cov_new['data'].ravel())[0, 1] assert r > 0.99
def test_add_noise(): """Test noise addition.""" rng = np.random.RandomState(0) data_path = testing.data_path() raw = read_raw_fif(data_path + '/MEG/sample/sample_audvis_trunc_raw.fif') raw.del_proj() picks = pick_types(raw.info, eeg=True, exclude=()) cov = compute_raw_covariance(raw, picks=picks) with pytest.raises(RuntimeError, match='to be loaded'): add_noise(raw, cov) raw.crop(0, 1).load_data() with pytest.raises(TypeError, match='Raw, Epochs, or Evoked'): add_noise(0., cov) with pytest.raises(TypeError, match='Covariance'): add_noise(raw, 0.) # test a no-op (data preserved) orig_data = raw[:][0] zero_cov = cov.copy() zero_cov['data'].fill(0) add_noise(raw, zero_cov) new_data = raw[:][0] assert_allclose(orig_data, new_data, atol=1e-30) # set to zero to make comparisons easier raw._data[:] = 0. epochs = EpochsArray(np.zeros((1, len(raw.ch_names), 100)), raw.info.copy()) epochs.info['bads'] = [] evoked = epochs.average(picks=np.arange(len(raw.ch_names))) for inst in (raw, epochs, evoked): with catch_logging() as log: add_noise(inst, cov, random_state=rng, verbose=True) log = log.getvalue() want = ('to {0}/{1} channels ({0}' .format(len(cov['names']), len(raw.ch_names))) assert want in log if inst is evoked: inst = EpochsArray(inst.data[np.newaxis], inst.info) if inst is raw: cov_new = compute_raw_covariance(inst, picks=picks, verbose='error') # samples else: cov_new = compute_covariance(inst, verbose='error') # avg ref assert cov['names'] == cov_new['names'] r = np.corrcoef(cov['data'].ravel(), cov_new['data'].ravel())[0, 1] assert r > 0.99
def test_run_GLM_order(): raw = simulate_nirs_raw(sig_dur=200, stim_dur=5., sfreq=3) design_matrix = make_first_level_design_matrix(raw, stim_dur=5., drift_order=1, drift_model='polynomial') # Default should be first order AR glm_estimates = run_glm(raw, design_matrix) assert glm_estimates.pick("Simulated").model()[0].order == 1 # Default should be first order AR glm_estimates = run_glm(raw, design_matrix, noise_model='ar2') assert glm_estimates.pick("Simulated").model()[0].order == 2 glm_estimates = run_glm(raw, design_matrix, noise_model='ar7') assert glm_estimates.pick("Simulated").model()[0].order == 7 # Auto should be 4 times sample rate cov = Covariance(np.ones(1) * 1e-11, raw.ch_names, raw.info['bads'], raw.info['projs'], nfree=0) raw = add_noise(raw, cov, iir_filter=iir_filter) glm_estimates = run_glm(raw, design_matrix, noise_model='auto') assert glm_estimates.pick("Simulated").model()[0].order == 3 * 4 raw = simulate_nirs_raw(sig_dur=10, stim_dur=5., sfreq=2) cov = Covariance(np.ones(1) * 1e-11, raw.ch_names, raw.info['bads'], raw.info['projs'], nfree=0) raw = add_noise(raw, cov, iir_filter=iir_filter) design_matrix = make_first_level_design_matrix(raw, stim_dur=5., drift_order=1, drift_model='polynomial') # Auto should be 4 times sample rate glm_estimates = run_glm(raw, design_matrix, noise_model='auto') assert glm_estimates.pick("Simulated").model()[0].order == 2 * 4
def test_rank_deficiency(): """Test adding noise from M/EEG float32 (I/O) cov with projectors.""" # See gh-5940 evoked = read_evokeds(ave_fname, 0, baseline=(None, 0)) evoked.info['bads'] = ['MEG 2443'] evoked.info['lowpass'] = 20 # fake for decim picks = pick_types(evoked.info, meg=True, eeg=False) picks = picks[::16] evoked.pick_channels([evoked.ch_names[pick] for pick in picks]) evoked.info.normalize_proj() cov = read_cov(cov_fname) cov['projs'] = [] cov = regularize(cov, evoked.info, rank=None) cov = pick_channels_cov(cov, evoked.ch_names) evoked.data[:] = 0 add_noise(evoked, cov) cov_new = compute_covariance( EpochsArray(evoked.data[np.newaxis], evoked.info), verbose='error') assert cov['names'] == cov_new['names'] r = np.corrcoef(cov['data'].ravel(), cov_new['data'].ravel())[0, 1] assert r > 0.98
def test_rank_deficiency(): """Test adding noise from M/EEG float32 (I/O) cov with projectors.""" # See gh-5940 evoked = read_evokeds(ave_fname, 0, baseline=(None, 0)) evoked.info['bads'] = ['MEG 2443'] evoked.info['lowpass'] = 20 # fake for decim picks = pick_types(evoked.info, meg=True, eeg=False) picks = picks[::16] evoked.pick_channels([evoked.ch_names[pick] for pick in picks]) evoked.info.normalize_proj() cov = read_cov(cov_fname) cov['projs'] = [] cov = regularize(cov, evoked.info, rank=None) cov = pick_channels_cov(cov, evoked.ch_names) evoked.data[:] = 0 add_noise(evoked, cov) cov_new = compute_covariance( EpochsArray(evoked.data[np.newaxis], evoked.info), verbose='error') assert cov['names'] == cov_new['names'] r = np.corrcoef(cov['data'].ravel(), cov_new['data'].ravel())[0, 1] assert r > 0.98
# Only use gradiometers picks = mne.pick_types(info, meg='grad', stim=True, exclude=()) mne.pick_info(info, picks, copy=False) # Define a covariance matrix for the simulated noise. In this tutorial, we use # a simple diagonal matrix. cov = mne.cov.make_ad_hoc_cov(info) cov['data'] *= (20. / snr)**2 # Scale the noise to achieve the desired SNR # Simulate the raw data, with a lowpass filter on the noise stcs = [(stc_signal, unit_impulse(n_samp, dtype=int) * 1), (stc_noise, unit_impulse(n_samp, dtype=int) * 2)] # stacked in time duration = (len(stc_signal.times) * 2) / sfreq raw = simulate_raw(info, stcs, forward=fwd) add_noise(raw, cov, iir_filter=[4, -4, 0.8], random_state=rand) ############################################################################### # We create an :class:`mne.Epochs` object containing two trials: one with # both noise and signal and one with just noise events = mne.find_events(raw, initial_event=True) tmax = (len(stc_signal.times) - 1) / sfreq epochs = mne.Epochs(raw, events, event_id=dict(signal=1, noise=2), tmin=0, tmax=tmax, baseline=None, preload=True) assert len(epochs) == 2 # ensure that we got the two expected events
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')
# Only use gradiometers picks = mne.pick_types(info, meg='grad', stim=True, exclude=()) mne.pick_info(info, picks, copy=False) # Define a covariance matrix for the simulated noise. In this tutorial, we use # a simple diagonal matrix. cov = mne.cov.make_ad_hoc_cov(info) cov['data'] *= (20. / snr) ** 2 # Scale the noise to achieve the desired SNR # Simulate the raw data, with a lowpass filter on the noise stcs = [(stc_signal, unit_impulse(n_samp, dtype=int) * 1), (stc_noise, unit_impulse(n_samp, dtype=int) * 2)] # stacked in time duration = (len(stc_signal.times) * 2) / sfreq raw = simulate_raw(info, stcs, forward=fwd) add_noise(raw, cov, iir_filter=[4, -4, 0.8], random_state=rand) ############################################################################### # We create an :class:`mne.Epochs` object containing two trials: one with # both noise and signal and one with just noise events = mne.find_events(raw, initial_event=True) tmax = (len(stc_signal.times) - 1) / sfreq epochs = mne.Epochs(raw, events, event_id=dict(signal=1, noise=2), tmin=0, tmax=tmax, baseline=None, preload=True) assert len(epochs) == 2 # ensure that we got the two expected events # Plot some of the channels of the simulated data that are situated above one # of our simulated sources. picks = mne.pick_channels(epochs.ch_names, mne.read_selection('Left-frontal'))