def test_get_peak(): """Test peak getter.""" evoked = read_evokeds(fname, condition=0, proj=True) pytest.raises(ValueError, evoked.get_peak, ch_type='mag', tmin=1) pytest.raises(ValueError, evoked.get_peak, ch_type='mag', tmax=0.9) pytest.raises(ValueError, evoked.get_peak, ch_type='mag', tmin=0.02, tmax=0.01) pytest.raises(ValueError, evoked.get_peak, ch_type='mag', mode='foo') pytest.raises(RuntimeError, evoked.get_peak, ch_type=None, mode='foo') pytest.raises(ValueError, evoked.get_peak, ch_type='misc', mode='foo') ch_name, time_idx = evoked.get_peak(ch_type='mag') assert (ch_name in evoked.ch_names) assert (time_idx in evoked.times) ch_name, time_idx, max_amp = evoked.get_peak(ch_type='mag', time_as_index=True, return_amplitude=True) assert (time_idx < len(evoked.times)) assert_equal(ch_name, 'MEG 1421') assert_allclose(max_amp, 7.17057e-13, rtol=1e-5) pytest.raises(ValueError, evoked.get_peak, ch_type='mag', merge_grads=True) ch_name, time_idx = evoked.get_peak(ch_type='grad', merge_grads=True) assert_equal(ch_name, 'MEG 244X') data = np.array([[0., 1., 2.], [0., -3., 0]]) times = np.array([.1, .2, .3]) ch_idx, time_idx, max_amp = _get_peak(data, times, mode='abs') assert_equal(ch_idx, 1) assert_equal(time_idx, 1) assert_allclose(max_amp, -3.) ch_idx, time_idx, max_amp = _get_peak(data * -1, times, mode='neg') assert_equal(ch_idx, 0) assert_equal(time_idx, 2) assert_allclose(max_amp, -2.) ch_idx, time_idx, max_amp = _get_peak(data, times, mode='pos') assert_equal(ch_idx, 0) assert_equal(time_idx, 2) assert_allclose(max_amp, 2.) pytest.raises(ValueError, _get_peak, data + 1e3, times, mode='neg') pytest.raises(ValueError, _get_peak, data - 1e3, times, mode='pos')
def test_get_peak(): """Test peak getter.""" evoked = read_evokeds(fname, condition=0, proj=True) assert_raises(ValueError, evoked.get_peak, ch_type='mag', tmin=1) assert_raises(ValueError, evoked.get_peak, ch_type='mag', tmax=0.9) assert_raises(ValueError, evoked.get_peak, ch_type='mag', tmin=0.02, tmax=0.01) assert_raises(ValueError, evoked.get_peak, ch_type='mag', mode='foo') assert_raises(RuntimeError, evoked.get_peak, ch_type=None, mode='foo') assert_raises(ValueError, evoked.get_peak, ch_type='misc', mode='foo') ch_name, time_idx = evoked.get_peak(ch_type='mag') assert_true(ch_name in evoked.ch_names) assert_true(time_idx in evoked.times) ch_name, time_idx, max_amp = evoked.get_peak(ch_type='mag', time_as_index=True, return_amplitude=True) assert_true(time_idx < len(evoked.times)) assert_equal(ch_name, 'MEG 1421') assert_allclose(max_amp, 7.17057e-13, rtol=1e-5) assert_raises(ValueError, evoked.get_peak, ch_type='mag', merge_grads=True) ch_name, time_idx = evoked.get_peak(ch_type='grad', merge_grads=True) assert_equal(ch_name, 'MEG 244X') data = np.array([[0., 1., 2.], [0., -3., 0]]) times = np.array([.1, .2, .3]) ch_idx, time_idx, max_amp = _get_peak(data, times, mode='abs') assert_equal(ch_idx, 1) assert_equal(time_idx, 1) assert_allclose(max_amp, -3.) ch_idx, time_idx, max_amp = _get_peak(data * -1, times, mode='neg') assert_equal(ch_idx, 0) assert_equal(time_idx, 2) assert_allclose(max_amp, -2.) ch_idx, time_idx, max_amp = _get_peak(data, times, mode='pos') assert_equal(ch_idx, 0) assert_equal(time_idx, 2) assert_allclose(max_amp, 2.) assert_raises(ValueError, _get_peak, data + 1e3, times, mode='neg') assert_raises(ValueError, _get_peak, data - 1e3, times, mode='pos')
def test_get_peak(): """Test peak getter.""" evoked = read_evokeds(fname, condition=0, proj=True) assert_raises(ValueError, evoked.get_peak, ch_type='mag', tmin=1) assert_raises(ValueError, evoked.get_peak, ch_type='mag', tmax=0.9) assert_raises(ValueError, evoked.get_peak, ch_type='mag', tmin=0.02, tmax=0.01) assert_raises(ValueError, evoked.get_peak, ch_type='mag', mode='foo') assert_raises(RuntimeError, evoked.get_peak, ch_type=None, mode='foo') assert_raises(ValueError, evoked.get_peak, ch_type='misc', mode='foo') ch_name, time_idx = evoked.get_peak(ch_type='mag') assert_true(ch_name in evoked.ch_names) assert_true(time_idx in evoked.times) ch_name, time_idx = evoked.get_peak(ch_type='mag', time_as_index=True) assert_true(time_idx < len(evoked.times)) assert_equal(ch_name, 'MEG 1421') assert_raises(ValueError, evoked.get_peak, ch_type='mag', merge_grads=True) ch_name, time_idx = evoked.get_peak(ch_type='grad', merge_grads=True) assert_equal(ch_name, 'MEG 244X') data = np.array([[0., 1., 2.], [0., -3., 0]]) times = np.array([.1, .2, .3]) ch_idx, time_idx = _get_peak(data, times, mode='abs') assert_equal(ch_idx, 1) assert_equal(time_idx, 1) ch_idx, time_idx = _get_peak(data * -1, times, mode='neg') assert_equal(ch_idx, 0) assert_equal(time_idx, 2) ch_idx, time_idx = _get_peak(data, times, mode='pos') assert_equal(ch_idx, 0) assert_equal(time_idx, 2) assert_raises(ValueError, _get_peak, data + 1e3, times, mode='neg') assert_raises(ValueError, _get_peak, data - 1e3, times, mode='pos')
def test_get_peak(): """Test peak getter """ evoked = read_evokeds(fname, condition=0, proj=True) assert_raises(ValueError, evoked.get_peak, ch_type='mag', tmin=1) assert_raises(ValueError, evoked.get_peak, ch_type='mag', tmax=0.9) assert_raises(ValueError, evoked.get_peak, ch_type='mag', tmin=0.02, tmax=0.01) assert_raises(ValueError, evoked.get_peak, ch_type='mag', mode='foo') assert_raises(RuntimeError, evoked.get_peak, ch_type=None, mode='foo') assert_raises(ValueError, evoked.get_peak, ch_type='misc', mode='foo') ch_name, time_idx = evoked.get_peak(ch_type='mag') assert_true(ch_name in evoked.ch_names) assert_true(time_idx in evoked.times) ch_name, time_idx = evoked.get_peak(ch_type='mag', time_as_index=True) assert_true(time_idx < len(evoked.times)) assert_equal(ch_name, 'MEG 1421') data = np.array([[0., 1., 2.], [0., -3., 0]]) times = np.array([.1, .2, .3]) ch_idx, time_idx = _get_peak(data, times, mode='abs') assert_equal(ch_idx, 1) assert_equal(time_idx, 1) ch_idx, time_idx = _get_peak(data * -1, times, mode='neg') assert_equal(ch_idx, 0) assert_equal(time_idx, 2) ch_idx, time_idx = _get_peak(data, times, mode='pos') assert_equal(ch_idx, 0) assert_equal(time_idx, 2) assert_raises(ValueError, _get_peak, data + 1e3, times, mode='neg') assert_raises(ValueError, _get_peak, data - 1e3, times, mode='pos')
def test_get_peak(): """Test peak getter.""" evoked = read_evokeds(fname, condition=0, proj=True) with pytest.raises(ValueError, match='tmin.*must be <= tmax'): evoked.get_peak(ch_type='mag', tmin=1) with pytest.raises(ValueError, match='tmax.*is out of bounds'): evoked.get_peak(ch_type='mag', tmax=0.9) with pytest.raises(ValueError, match='tmin.*must be <= tmax'): evoked.get_peak(ch_type='mag', tmin=0.02, tmax=0.01) with pytest.raises(ValueError, match="Invalid.*'mode' parameter"): evoked.get_peak(ch_type='mag', mode='foo') with pytest.raises(RuntimeError, match='Multiple data channel types'): evoked.get_peak(ch_type=None, mode='foo') with pytest.raises(ValueError, match='Channel type.*not found'): evoked.get_peak(ch_type='misc', mode='foo') ch_name, time_idx = evoked.get_peak(ch_type='mag') assert (ch_name in evoked.ch_names) assert (time_idx in evoked.times) ch_name, time_idx, max_amp = evoked.get_peak(ch_type='mag', time_as_index=True, return_amplitude=True) assert (time_idx < len(evoked.times)) assert_equal(ch_name, 'MEG 1421') assert_allclose(max_amp, 7.17057e-13, rtol=1e-5) with pytest.raises(ValueError, match='must be "grad" for merge_grads'): evoked.get_peak(ch_type='mag', merge_grads=True) with pytest.raises(ValueError, match='Negative mode.*does not make sense'): evoked.get_peak(ch_type='grad', merge_grads=True, mode='neg') ch_name, time_idx = evoked.get_peak(ch_type='grad', merge_grads=True) assert_equal(ch_name, 'MEG 244X') data = np.array([[0., 1., 2.], [0., -3., 0]]) times = np.array([.1, .2, .3]) ch_idx, time_idx, max_amp = _get_peak(data, times, mode='abs') assert_equal(ch_idx, 1) assert_equal(time_idx, 1) assert_allclose(max_amp, -3.) ch_idx, time_idx, max_amp = _get_peak(data * -1, times, mode='neg') assert_equal(ch_idx, 0) assert_equal(time_idx, 2) assert_allclose(max_amp, -2.) ch_idx, time_idx, max_amp = _get_peak(data, times, mode='pos') assert_equal(ch_idx, 0) assert_equal(time_idx, 2) assert_allclose(max_amp, 2.) # Check behavior if `mode` doesn't match the available data evoked_all_pos = evoked.copy().crop(0, 0.1).pick('EEG 001') evoked_all_neg = evoked.copy().crop(0, 0.1).pick('EEG 001') evoked_all_pos.data = np.abs(evoked_all_pos.data) # all values positive evoked_all_neg.data = -np.abs(evoked_all_neg.data) # all negative with pytest.raises(ValueError, match='No negative values'): evoked_all_pos.get_peak(mode='neg') with pytest.raises(ValueError, match='No positive values'): evoked_all_neg.get_peak(mode='pos') # Test interaction between `mode` and `tmin` / `tmax` # For the test, create an Evoked where half of the values are negative # and the rest is positive evoked_neg_and_pos = evoked_all_neg.copy() time_sep_neg_and_pos = 0.05 idx_time_sep_neg_and_pos = evoked_neg_and_pos.time_as_index( time_sep_neg_and_pos)[0] evoked_neg_and_pos.data[:, idx_time_sep_neg_and_pos:] *= -1 with pytest.raises(ValueError, match='No positive values'): evoked_neg_and_pos.get_peak( mode='pos', # subtract 1 time instant, otherwise were off-by-one tmax=time_sep_neg_and_pos - 1 / evoked_neg_and_pos.info['sfreq']) with pytest.raises(ValueError, match='No negative values'): evoked_neg_and_pos.get_peak(mode='neg', tmin=time_sep_neg_and_pos)
selection = ['Gradiometers'] this_data = zscore_data(this_data, times) # Get from evokeds dependent measures (peak latency, AUC) as ndarrays for ii, _ in enumerate(groups): for ci, _ in enumerate(conditions): if n_sel > 1: for jj in np.arange(n_sel): for kk in np.arange(n_samples): if ii == 0 and ci == 0 and jj == 0 and kk == 0: # grp x cond x sel x subj latencies = np.zeros(data.shape[:chs_dim]) locs = np.zeros_like(latencies) amps = np.zeros_like(latencies) # AUC -100 ms to peak out = _get_peak(data[ii, ci, jj, kk], times, tmin=.15, tmax=.55) locs[ii, ci, jj, kk] = out[0] latencies[ii, ci, jj, kk] = out[1] # milliseconds amps[ii, ci, jj, kk] = out[2] b = np.where(np.isclose(times, out[1] * 1e-3, atol=1e-3))[0][0] a = np.where( np.isclose(times, (out[1] - 100) * 1e-3, atol=1e-3))[0][0] auc = (np.sum(np.abs(data[ii, ci, jj, kk, :, a:b])) * (len(times) / sfreq)) amps[ii, ci, jj, kk] = auc # plot dependents box_kwargs = {'showmeans': True, 'meanline': False}
condition='standard', baseline=(None, 0)) standards.append(standard) assert sfreq == standard.info['sfreq'] if group == groups[0] and subj == subjects[0]: amp_data = np.zeros((len(groups), N)) lat_data = np.zeros((len(groups), N)) standard_data = np.zeros((len(groups), N)) # area under curve tmin, tmax = (timing[ii] - .05, timing[ii]) mask = _time_mask(deviant.times, tmin=tmin, tmax=tmax, sfreq=sfreq) dummy = deviant.copy().pick_channels(pairs[ii]) amp_data[ii, si] = np.sum(np.abs(dummy.data[:, mask])) * \ len(dummy.data) * (1. / sfreq) lat_data[ii, si] = times[_get_peak(dummy.data, times, tmin=tmin, tmax=tmax)[1]] print(" Peak latency for %s at %.3f sec \n \n" % (subj, lat_data[ii, si])) del dummy # for the standard stimulus dummy = standard.copy().pick_channels(pairs[ii]) standard_data[ii, si] = np.sum(np.abs(deviant.data[:, mask])) * \ len(deviant.data) * (1. / deviant.info['sfreq']) del dummy for cond, evs, f in zip(conditions, [standards, deviants], f_out): if not op.isfile(f): grand_avr = grand_average(evs) grand_avr.save( op.join( studydir, '%s_%s_%d_n%d_grand-ave.fif' %