Test fix stim artifact.
()
| 19 | |
| 20 | |
| 21 | def test_fix_stim_artifact(): |
| 22 | """Test fix stim artifact.""" |
| 23 | events = read_events(event_fname) |
| 24 | |
| 25 | raw = read_raw_fif(raw_fname) |
| 26 | pytest.raises(RuntimeError, fix_stim_artifact, raw) |
| 27 | |
| 28 | raw = read_raw_fif(raw_fname, preload=True) |
| 29 | |
| 30 | # use window before stimulus in epochs |
| 31 | tmin, tmax, event_id = -0.2, 0.5, 1 |
| 32 | picks = ("meg", "eeg", "eog") |
| 33 | epochs = Epochs( |
| 34 | raw, events, event_id, tmin, tmax, picks=picks, preload=True, reject=None |
| 35 | ) |
| 36 | e_start = int(np.ceil(epochs.info["sfreq"] * epochs.tmin)) |
| 37 | tmin, tmax = -0.045, -0.015 |
| 38 | tmin_samp = int(-0.035 * epochs.info["sfreq"]) - e_start |
| 39 | tmax_samp = int(-0.015 * epochs.info["sfreq"]) - e_start |
| 40 | |
| 41 | epochs = fix_stim_artifact( |
| 42 | epochs, tmin=tmin, tmax=tmax, mode="linear", picks=("eeg", "eog") |
| 43 | ) |
| 44 | data = epochs.get_data(("eeg", "eog"))[:, :, tmin_samp:tmax_samp] |
| 45 | diff_data0 = np.diff(data[0][0]) |
| 46 | diff_data0 -= np.mean(diff_data0) |
| 47 | assert_array_almost_equal(diff_data0, np.zeros(len(diff_data0))) |
| 48 | |
| 49 | data = epochs.get_data("meg")[:, :, tmin_samp:tmax_samp] |
| 50 | diff_data0 = np.diff(data[0][0]) |
| 51 | diff_data0 -= np.mean(diff_data0) |
| 52 | assert np.all(diff_data0 != 0) |
| 53 | |
| 54 | epochs = fix_stim_artifact(epochs, tmin=tmin, tmax=tmax, mode="window") |
| 55 | data_from_epochs_fix = epochs.get_data(copy=False)[:, :, tmin_samp:tmax_samp] |
| 56 | assert not np.all(data_from_epochs_fix != 0) |
| 57 | |
| 58 | baseline = (-0.1, -0.05) |
| 59 | epochs = fix_stim_artifact( |
| 60 | epochs, tmin=tmin, tmax=tmax, baseline=baseline, mode="constant" |
| 61 | ) |
| 62 | b_start = int(np.ceil(epochs.info["sfreq"] * baseline[0])) |
| 63 | b_end = int(np.ceil(epochs.info["sfreq"] * baseline[1])) |
| 64 | base_t1 = b_start - e_start |
| 65 | base_t2 = b_end - e_start |
| 66 | baseline_mean = epochs.get_data()[:, :, base_t1:base_t2].mean(axis=2)[0][0] |
| 67 | data = epochs.get_data()[:, :, tmin_samp:tmax_samp] |
| 68 | assert data[0][0][0] == baseline_mean |
| 69 | |
| 70 | # use window before stimulus in raw |
| 71 | event_idx = np.where(events[:, 2] == 1)[0][0] |
| 72 | tmin, tmax = -0.045, -0.015 |
| 73 | tmin_samp = int(-0.035 * raw.info["sfreq"]) |
| 74 | tmax_samp = int(-0.015 * raw.info["sfreq"]) |
| 75 | tidx = int(events[event_idx, 0] - raw.first_samp) |
| 76 | |
| 77 | pytest.raises(ValueError, fix_stim_artifact, raw, events=np.array([])) |
| 78 | raw = fix_stim_artifact( |
nothing calls this directly
no test coverage detected