def test_auto_scale(): """Test auto-scaling of channels for quick plotting.""" raw = read_raw_fif(raw_fname, preload=False, add_eeg_ref=False) ev = read_events(ev_fname) epochs = Epochs(raw, ev, add_eeg_ref=False) rand_data = np.random.randn(10, 100) for inst in [raw, epochs]: scale_grad = 1e10 scalings_def = dict([('eeg', 'auto'), ('grad', scale_grad), ('stim', 'auto')]) # Test for wrong inputs assert_raises(ValueError, inst.plot, scalings='foo') assert_raises(ValueError, _compute_scalings, 'foo', inst) # Make sure compute_scalings doesn't change anything not auto scalings_new = _compute_scalings(scalings_def, inst) assert_true(scale_grad == scalings_new['grad']) assert_true(scalings_new['eeg'] != 'auto') assert_raises(ValueError, _compute_scalings, scalings_def, rand_data) epochs = epochs[0].load_data() epochs.pick_types(eeg=True, meg=False) assert_raises(ValueError, _compute_scalings, dict(grad='auto'), epochs)
def test_apply_mne_inverse_epochs(): """Test MNE with precomputed inverse operator on Epochs """ event_id, tmin, tmax = 1, -0.2, 0.5 picks = fiff.pick_types(raw.info, meg=True, eeg=False, stim=True, ecg=True, eog=True, include=['STI 014']) reject = dict(grad=4000e-13, mag=4e-12, eog=150e-6) flat = dict(grad=1e-15, mag=1e-15) events = read_events(fname_event)[:15] epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), reject=reject, flat=flat) stcs = apply_inverse_epochs(epochs, inverse_operator, lambda2, "dSPM", label=label, pick_normal=True) assert_true(len(stcs) == 4) assert_true(3 < stcs[0].data.max() < 10) data = sum(stc.data for stc in stcs) / len(stcs) flip = label_sign_flip(label, inverse_operator['src']) label_mean = np.mean(data, axis=0) label_mean_flip = np.mean(flip[:, np.newaxis] * data, axis=0) assert_true(label_mean.max() < label_mean_flip.max())
def test_auto_scale(): """Test auto-scaling of channels for quick plotting.""" raw = read_raw_fif(raw_fname) epochs = Epochs(raw, read_events(ev_fname)) rand_data = np.random.randn(10, 100) for inst in [raw, epochs]: scale_grad = 1e10 scalings_def = dict([('eeg', 'auto'), ('grad', scale_grad), ('stim', 'auto')]) # Test for wrong inputs pytest.raises(ValueError, inst.plot, scalings='foo') pytest.raises(ValueError, _compute_scalings, 'foo', inst) # Make sure compute_scalings doesn't change anything not auto scalings_new = _compute_scalings(scalings_def, inst) assert (scale_grad == scalings_new['grad']) assert (scalings_new['eeg'] != 'auto') pytest.raises(ValueError, _compute_scalings, scalings_def, rand_data) epochs = epochs[0].load_data() epochs.pick_types(eeg=True, meg=False) pytest.raises(ValueError, _compute_scalings, dict(grad='auto'), epochs)
def test_stim_elim(): """Test eliminate stim artifact""" raw = Raw(raw_fname, preload=True) events = read_events(event_fname) event_idx = np.where(events[:, 2] == 1)[0][0] tidx = events[event_idx, 0] - raw.first_samp raw = eliminate_stim_artifact(raw, events, event_id=1, tmin=-0.005, tmax=0.01, mode='linear') data, times = raw[:, tidx - 3:tidx + 5] diff_data0 = np.diff(data[0]) diff_data0 -= np.mean(diff_data0) assert_array_almost_equal(diff_data0, np.zeros(len(diff_data0))) raw = eliminate_stim_artifact(raw, events, event_id=1, tmin=-0.005, tmax=0.01, mode='window') data, times = raw[:, tidx:tidx + 1] assert_true(np.all(data) == 0.)
def test_apply_mne_inverse_epochs(): """Test MNE with precomputed inverse operator on Epochs """ event_id, tmin, tmax = 1, -0.2, 0.5 picks = fiff.pick_types(raw.info, meg=True, eeg=False, stim=True, ecg=True, eog=True, include=['STI 014']) reject = dict(grad=4000e-13, mag=4e-12, eog=150e-6) flat = dict(grad=1e-15, mag=1e-15) events = read_events(fname_event)[:15] epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), reject=reject, flat=flat) stcs = apply_inverse_epochs(epochs, inverse_operator, lambda2, "dSPM", label=label_lh, pick_normal=True) assert_true(len(stcs) == 4) assert_true(3 < stcs[0].data.max() < 10) data = sum(stc.data for stc in stcs) / len(stcs) flip = label_sign_flip(label_lh, inverse_operator['src']) label_mean = np.mean(data, axis=0) label_mean_flip = np.mean(flip[:, np.newaxis] * data, axis=0) assert_true(label_mean.max() < label_mean_flip.max()) # test extracting a BiHemiLabel stcs_rh = apply_inverse_epochs(epochs, inverse_operator, lambda2, "dSPM", label=label_rh, pick_normal=True) stcs_bh = apply_inverse_epochs(epochs, inverse_operator, lambda2, "dSPM", label=label_lh + label_rh, pick_normal=True) n_lh = len(stcs[0].data) assert_array_almost_equal(stcs[0].data, stcs_bh[0].data[:n_lh]) assert_array_almost_equal(stcs_rh[0].data, stcs_bh[0].data[n_lh:])
def test_stim_elim(): """Test eliminate stim artifact""" raw = Raw(raw_fname, preload=True) events = read_events(event_fname) event_idx = np.where(events[:, 2] == 1)[0][0] tidx = int(events[event_idx, 0] - raw.first_samp) # use window around stimulus tmin = -0.02 tmax = 0.02 test_tminidx = int(-0.01 * raw.info['sfreq']) test_tmaxidx = int(0.01 * raw.info['sfreq']) raw = eliminate_stim_artifact(raw, events, event_id=1, tmin=tmin, tmax=tmax, mode='linear') data, times = raw[:, (tidx + test_tminidx):(tidx + test_tmaxidx)] diff_data0 = np.diff(data[0]) diff_data0 -= np.mean(diff_data0) assert_array_almost_equal(diff_data0, np.zeros(len(diff_data0))) raw = eliminate_stim_artifact(raw, events, event_id=1, tmin=tmin, tmax=tmax, mode='window') data, times = raw[:, (tidx + test_tminidx):(tidx + test_tmaxidx)] assert_true(np.all(data) == 0.) # use window before stimulus tmin = -0.045 tmax = 0.015 test_tminidx = int(-0.035 * raw.info['sfreq']) test_tmaxidx = int(-0.015 * raw.info['sfreq']) raw = eliminate_stim_artifact(raw, events, event_id=1, tmin=tmin, tmax=tmax, mode='linear') data, times = raw[:, (tidx + test_tminidx):(tidx + test_tmaxidx)] diff_data0 = np.diff(data[0]) diff_data0 -= np.mean(diff_data0) assert_array_almost_equal(diff_data0, np.zeros(len(diff_data0))) raw = eliminate_stim_artifact(raw, events, event_id=1, tmin=tmin, tmax=tmax, mode='window') data, times = raw[:, (tidx + test_tminidx):(tidx + test_tmaxidx)] assert_true(np.all(data) == 0.) # use window after stimulus tmin = 0.005 tmax = 0.045 test_tminidx = int(0.015 * raw.info['sfreq']) test_tmaxidx = int(0.035 * raw.info['sfreq']) raw = eliminate_stim_artifact(raw, events, event_id=1, tmin=tmin, tmax=tmax, mode='linear') data, times = raw[:, (tidx + test_tminidx):(tidx + test_tmaxidx)] diff_data0 = np.diff(data[0]) diff_data0 -= np.mean(diff_data0) assert_array_almost_equal(diff_data0, np.zeros(len(diff_data0))) raw = eliminate_stim_artifact(raw, events, event_id=1, tmin=tmin, tmax=tmax, mode='window') data, times = raw[:, (tidx + test_tminidx):(tidx + test_tmaxidx)] assert_true(np.all(data) == 0.)
def test_apply_mne_inverse_epochs(): """Test MNE with precomputed inverse operator on Epochs """ inverse_operator = read_inverse_operator(fname_inv) label_lh = read_label(fname_label % 'Aud-lh') label_rh = read_label(fname_label % 'Aud-rh') event_id, tmin, tmax = 1, -0.2, 0.5 raw = fiff.Raw(fname_raw) picks = fiff.pick_types(raw.info, meg=True, eeg=False, stim=True, ecg=True, eog=True, include=['STI 014'], exclude='bads') reject = dict(grad=4000e-13, mag=4e-12, eog=150e-6) flat = dict(grad=1e-15, mag=1e-15) events = read_events(fname_event)[:15] epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), reject=reject, flat=flat) stcs = apply_inverse_epochs(epochs, inverse_operator, lambda2, "dSPM", label=label_lh, pick_ori="normal") assert_true(len(stcs) == 4) assert_true(3 < stcs[0].data.max() < 10) assert_true(stcs[0].subject == 'sample') data = sum(stc.data for stc in stcs) / len(stcs) flip = label_sign_flip(label_lh, inverse_operator['src']) label_mean = np.mean(data, axis=0) label_mean_flip = np.mean(flip[:, np.newaxis] * data, axis=0) assert_true(label_mean.max() < label_mean_flip.max()) # test extracting a BiHemiLabel stcs_rh = apply_inverse_epochs(epochs, inverse_operator, lambda2, "dSPM", label=label_rh, pick_ori="normal") stcs_bh = apply_inverse_epochs(epochs, inverse_operator, lambda2, "dSPM", label=label_lh + label_rh, pick_ori="normal") n_lh = len(stcs[0].data) assert_array_almost_equal(stcs[0].data, stcs_bh[0].data[:n_lh]) assert_array_almost_equal(stcs_rh[0].data, stcs_bh[0].data[n_lh:]) # test without using a label (so delayed computation is used) stcs = apply_inverse_epochs(epochs, inverse_operator, lambda2, "dSPM", pick_ori="normal") assert_true(stcs[0].subject == 'sample') label_stc = stcs[0].in_label(label_rh) assert_true(label_stc.subject == 'sample') assert_array_almost_equal(stcs_rh[0].data, label_stc.data)
def test_apply_mne_inverse_epochs(self): """Test MNE with precomputed inverse operator on Epochs """ event_id, tmin, tmax = 1, -0.2, 0.5 picks = fiff.pick_types(raw.info, meg=True, eeg=False, stim=True, ecg=True, eog=True, include=['STI 014'], exclude='bads') reject = dict(grad=4000e-13, mag=4e-12, eog=150e-6) flat = dict(grad=1e-15, mag=1e-15) events = read_events(fname_event)[:15] epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), reject=reject, flat=flat) stcs = apply_inverse_epochs(epochs, self.inv_op, lambda2, "dSPM", label=label_lh, pick_normal=True) assert_true(len(stcs) == 4) assert_true(3 < stcs[0].data.max() < 10) data = sum(stc.data for stc in stcs) / len(stcs) flip = label_sign_flip(label_lh, self.inv_op['src']) label_mean = np.mean(data, axis=0) label_mean_flip = np.mean(flip[:, np.newaxis] * data, axis=0) assert_true(label_mean.max() < label_mean_flip.max()) # test extracting a BiHemiLabel stcs_rh = apply_inverse_epochs(epochs, self.inv_op, lambda2, "dSPM", label=label_rh, pick_normal=True) stcs_bh = apply_inverse_epochs(epochs, self.inv_op, lambda2, "dSPM", label=label_lh + label_rh, pick_normal=True) n_lh = len(stcs[0].data) assert_array_almost_equal(stcs[0].data, stcs_bh[0].data[:n_lh]) assert_array_almost_equal(stcs_rh[0].data, stcs_bh[0].data[n_lh:])
def test_auto_scale(): """Test auto-scaling of channels for quick plotting.""" raw = read_raw_fif(raw_fname) epochs = Epochs(raw, read_events(ev_fname)) rand_data = np.random.randn(10, 100) for inst in [raw, epochs]: scale_grad = 1e10 scalings_def = dict([('eeg', 'auto'), ('grad', scale_grad), ('stim', 'auto')]) # Test for wrong inputs with pytest.raises(ValueError, match=r".*scalings.*'foo'.*"): inst.plot(scalings='foo') # Make sure compute_scalings doesn't change anything not auto scalings_new = _compute_scalings(scalings_def, inst) assert (scale_grad == scalings_new['grad']) assert (scalings_new['eeg'] != 'auto') with pytest.raises(ValueError, match='Must supply either Raw or Epochs'): _compute_scalings(scalings_def, rand_data) epochs = epochs[0].load_data() epochs.pick_types(eeg=True, meg=False)
def test_auto_scale(): """Test auto-scaling of channels for quick plotting.""" raw = read_raw_fif(raw_fname, preload=False) ev = read_events(ev_fname) epochs = Epochs(raw, ev) rand_data = np.random.randn(10, 100) for inst in [raw, epochs]: scale_grad = 1e10 scalings_def = dict([("eeg", "auto"), ("grad", scale_grad), ("stim", "auto")]) # Test for wrong inputs assert_raises(ValueError, inst.plot, scalings="foo") assert_raises(ValueError, _compute_scalings, "foo", inst) # Make sure compute_scalings doesn't change anything not auto scalings_new = _compute_scalings(scalings_def, inst) assert_true(scale_grad == scalings_new["grad"]) assert_true(scalings_new["eeg"] != "auto") assert_raises(ValueError, _compute_scalings, scalings_def, rand_data) epochs = epochs[0].load_data() epochs.pick_types(eeg=True, meg=False) assert_raises(ValueError, _compute_scalings, dict(grad="auto"), epochs)
def test_apply_mne_inverse_epochs(): """Test MNE with precomputed inverse operator on Epochs.""" inverse_operator = read_inverse_operator(fname_full) label_lh = read_label(fname_label % 'Aud-lh') label_rh = read_label(fname_label % 'Aud-rh') event_id, tmin, tmax = 1, -0.2, 0.5 raw = read_raw_fif(fname_raw) picks = pick_types(raw.info, meg=True, eeg=False, stim=True, ecg=True, eog=True, include=['STI 014'], exclude='bads') reject = dict(grad=4000e-13, mag=4e-12, eog=150e-6) flat = dict(grad=1e-15, mag=1e-15) events = read_events(fname_event)[:15] epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, baseline=(None, 0), reject=reject, flat=flat) inverse_operator = prepare_inverse_operator(inverse_operator, nave=1, lambda2=lambda2, method="dSPM") for pick_ori in [None, "normal", "vector"]: stcs = apply_inverse_epochs(epochs, inverse_operator, lambda2, "dSPM", label=label_lh, pick_ori=pick_ori) stcs2 = apply_inverse_epochs(epochs, inverse_operator, lambda2, "dSPM", label=label_lh, pick_ori=pick_ori, prepared=True) # test if using prepared and not prepared inverse operator give the # same result assert_array_almost_equal(stcs[0].data, stcs2[0].data) assert_array_almost_equal(stcs[0].times, stcs2[0].times) assert (len(stcs) == 2) assert (3 < stcs[0].data.max() < 10) assert (stcs[0].subject == 'sample') inverse_operator = read_inverse_operator(fname_full) stcs = apply_inverse_epochs(epochs, inverse_operator, lambda2, "dSPM", label=label_lh, pick_ori='normal') data = sum(stc.data for stc in stcs) / len(stcs) flip = label_sign_flip(label_lh, inverse_operator['src']) label_mean = np.mean(data, axis=0) label_mean_flip = np.mean(flip[:, np.newaxis] * data, axis=0) assert (label_mean.max() < label_mean_flip.max()) # test extracting a BiHemiLabel inverse_operator = prepare_inverse_operator(inverse_operator, nave=1, lambda2=lambda2, method="dSPM") stcs_rh = apply_inverse_epochs(epochs, inverse_operator, lambda2, "dSPM", label=label_rh, pick_ori="normal", prepared=True) stcs_bh = apply_inverse_epochs(epochs, inverse_operator, lambda2, "dSPM", label=label_lh + label_rh, pick_ori="normal", prepared=True) n_lh = len(stcs[0].data) assert_array_almost_equal(stcs[0].data, stcs_bh[0].data[:n_lh]) assert_array_almost_equal(stcs_rh[0].data, stcs_bh[0].data[n_lh:]) # test without using a label (so delayed computation is used) stcs = apply_inverse_epochs(epochs, inverse_operator, lambda2, "dSPM", pick_ori="normal", prepared=True) assert (stcs[0].subject == 'sample') label_stc = stcs[0].in_label(label_rh) assert (label_stc.subject == 'sample') assert_array_almost_equal(stcs_rh[0].data, label_stc.data)
def test_fix_stim_artifact(): """Test fix stim artifact.""" events = read_events(event_fname) raw = read_raw_fif(raw_fname) pytest.raises(RuntimeError, fix_stim_artifact, raw) raw = read_raw_fif(raw_fname, preload=True) # use window before stimulus in epochs tmin, tmax, event_id = -0.2, 0.5, 1 picks = pick_types(raw.info, meg=True, eeg=True, eog=True, stim=False, exclude='bads') epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, preload=True, reject=None) e_start = int(np.ceil(epochs.info['sfreq'] * epochs.tmin)) tmin, tmax = -0.045, -0.015 tmin_samp = int(-0.035 * epochs.info['sfreq']) - e_start tmax_samp = int(-0.015 * epochs.info['sfreq']) - e_start epochs = fix_stim_artifact(epochs, tmin=tmin, tmax=tmax, mode='linear') data = epochs.get_data()[:, :, tmin_samp:tmax_samp] diff_data0 = np.diff(data[0][0]) diff_data0 -= np.mean(diff_data0) assert_array_almost_equal(diff_data0, np.zeros(len(diff_data0))) epochs = fix_stim_artifact(epochs, tmin=tmin, tmax=tmax, mode='window') data_from_epochs_fix = epochs.get_data()[:, :, tmin_samp:tmax_samp] # XXX This is a very weird check... assert np.all(data_from_epochs_fix) == 0. # use window before stimulus in raw event_idx = np.where(events[:, 2] == 1)[0][0] tmin, tmax = -0.045, -0.015 tmin_samp = int(-0.035 * raw.info['sfreq']) tmax_samp = int(-0.015 * raw.info['sfreq']) tidx = int(events[event_idx, 0] - raw.first_samp) pytest.raises(ValueError, fix_stim_artifact, raw, events=np.array([])) raw = fix_stim_artifact(raw, events=None, event_id=1, tmin=tmin, tmax=tmax, mode='linear', stim_channel='STI 014') data, times = raw[:, (tidx + tmin_samp):(tidx + tmax_samp)] diff_data0 = np.diff(data[0]) diff_data0 -= np.mean(diff_data0) assert_array_almost_equal(diff_data0, np.zeros(len(diff_data0))) raw = fix_stim_artifact(raw, events, event_id=1, tmin=tmin, tmax=tmax, mode='window') data, times = raw[:, (tidx + tmin_samp):(tidx + tmax_samp)] assert np.all(data) == 0. # get epochs from raw with fixed data tmin, tmax, event_id = -0.2, 0.5, 1 epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, preload=True, reject=None, baseline=None) e_start = int(np.ceil(epochs.info['sfreq'] * epochs.tmin)) tmin_samp = int(-0.035 * epochs.info['sfreq']) - e_start tmax_samp = int(-0.015 * epochs.info['sfreq']) - e_start data_from_raw_fix = epochs.get_data()[:, :, tmin_samp:tmax_samp] assert np.all(data_from_raw_fix) == 0. # use window after stimulus evoked = epochs.average() tmin, tmax = 0.005, 0.045 tmin_samp = int(0.015 * evoked.info['sfreq']) - evoked.first tmax_samp = int(0.035 * evoked.info['sfreq']) - evoked.first evoked = fix_stim_artifact(evoked, tmin=tmin, tmax=tmax, mode='linear') data = evoked.data[:, tmin_samp:tmax_samp] diff_data0 = np.diff(data[0]) diff_data0 -= np.mean(diff_data0) assert_array_almost_equal(diff_data0, np.zeros(len(diff_data0))) evoked = fix_stim_artifact(evoked, tmin=tmin, tmax=tmax, mode='window') data = evoked.data[:, tmin_samp:tmax_samp] assert np.all(data) == 0.
def test_fix_stim_artifact(): """Test fix stim artifact.""" events = read_events(event_fname) raw = read_raw_fif(raw_fname) assert_raises(RuntimeError, fix_stim_artifact, raw) raw = read_raw_fif(raw_fname, preload=True) # use window before stimulus in epochs tmin, tmax, event_id = -0.2, 0.5, 1 picks = pick_types(raw.info, meg=True, eeg=True, eog=True, stim=False, exclude='bads') epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, preload=True, reject=None) e_start = int(np.ceil(epochs.info['sfreq'] * epochs.tmin)) tmin, tmax = -0.045, -0.015 tmin_samp = int(-0.035 * epochs.info['sfreq']) - e_start tmax_samp = int(-0.015 * epochs.info['sfreq']) - e_start epochs = fix_stim_artifact(epochs, tmin=tmin, tmax=tmax, mode='linear') data = epochs.get_data()[:, :, tmin_samp:tmax_samp] diff_data0 = np.diff(data[0][0]) diff_data0 -= np.mean(diff_data0) assert_array_almost_equal(diff_data0, np.zeros(len(diff_data0))) epochs = fix_stim_artifact(epochs, tmin=tmin, tmax=tmax, mode='window') data_from_epochs_fix = epochs.get_data()[:, :, tmin_samp:tmax_samp] assert_true(np.all(data_from_epochs_fix) == 0.) # use window before stimulus in raw event_idx = np.where(events[:, 2] == 1)[0][0] tmin, tmax = -0.045, -0.015 tmin_samp = int(-0.035 * raw.info['sfreq']) tmax_samp = int(-0.015 * raw.info['sfreq']) tidx = int(events[event_idx, 0] - raw.first_samp) assert_raises(ValueError, fix_stim_artifact, raw, events=np.array([])) raw = fix_stim_artifact(raw, events=None, event_id=1, tmin=tmin, tmax=tmax, mode='linear', stim_channel='STI 014') data, times = raw[:, (tidx + tmin_samp):(tidx + tmax_samp)] diff_data0 = np.diff(data[0]) diff_data0 -= np.mean(diff_data0) assert_array_almost_equal(diff_data0, np.zeros(len(diff_data0))) raw = fix_stim_artifact(raw, events, event_id=1, tmin=tmin, tmax=tmax, mode='window') data, times = raw[:, (tidx + tmin_samp):(tidx + tmax_samp)] assert_true(np.all(data) == 0.) # get epochs from raw with fixed data tmin, tmax, event_id = -0.2, 0.5, 1 epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks, preload=True, reject=None, baseline=None) e_start = int(np.ceil(epochs.info['sfreq'] * epochs.tmin)) tmin_samp = int(-0.035 * epochs.info['sfreq']) - e_start tmax_samp = int(-0.015 * epochs.info['sfreq']) - e_start data_from_raw_fix = epochs.get_data()[:, :, tmin_samp:tmax_samp] assert_true(np.all(data_from_raw_fix) == 0.) # use window after stimulus evoked = epochs.average() tmin, tmax = 0.005, 0.045 tmin_samp = int(0.015 * evoked.info['sfreq']) - evoked.first tmax_samp = int(0.035 * evoked.info['sfreq']) - evoked.first evoked = fix_stim_artifact(evoked, tmin=tmin, tmax=tmax, mode='linear') data = evoked.data[:, tmin_samp:tmax_samp] diff_data0 = np.diff(data[0]) diff_data0 -= np.mean(diff_data0) assert_array_almost_equal(diff_data0, np.zeros(len(diff_data0))) evoked = fix_stim_artifact(evoked, tmin=tmin, tmax=tmax, mode='window') data = evoked.data[:, tmin_samp:tmax_samp] assert_true(np.all(data) == 0.)