def test_evoked_baseline(tmp_path): """Test evoked baseline.""" evoked = read_evokeds(fname, condition=0, baseline=None) # Here we create a data_set with constant data. evoked = EvokedArray(np.ones_like(evoked.data), evoked.info, evoked.times[0]) assert evoked.baseline is None evoked_baselined = EvokedArray(np.ones_like(evoked.data), evoked.info, evoked.times[0], baseline=(None, 0)) assert_allclose(evoked_baselined.baseline, (evoked_baselined.tmin, 0)) del evoked_baselined # Mean baseline correction is applied, since the data is equal to its mean # the resulting data should be a matrix of zeroes. baseline = (None, None) evoked.apply_baseline(baseline) assert_allclose(evoked.baseline, (evoked.tmin, evoked.tmax)) assert_allclose(evoked.data, np.zeros_like(evoked.data)) # Test that the .baseline attribute changes if we apply a different # baseline now. baseline = (None, 0) evoked.apply_baseline(baseline) assert_allclose(evoked.baseline, (evoked.tmin, 0)) # By default for our test file, no baseline should be set upon reading evoked = read_evokeds(fname, condition=0) assert evoked.baseline is None # Test that the .baseline attribute is set when we call read_evokeds() # with a `baseline` parameter. baseline = (-0.2, -0.1) evoked = read_evokeds(fname, condition=0, baseline=baseline) assert_allclose(evoked.baseline, baseline) # Test that the .baseline attribute survives an I/O roundtrip. evoked = read_evokeds(fname, condition=0) baseline = (-0.2, -0.1) evoked.apply_baseline(baseline) assert_allclose(evoked.baseline, baseline) tmp_fname = tmp_path / 'test-ave.fif' evoked.save(tmp_fname) evoked_read = read_evokeds(tmp_fname, condition=0) assert_allclose(evoked_read.baseline, evoked.baseline) # We shouldn't be able to remove a baseline correction after it has been # applied. evoked = read_evokeds(fname, condition=0) baseline = (-0.2, -0.1) evoked.apply_baseline(baseline) with pytest.raises(ValueError, match='already been baseline-corrected'): evoked.apply_baseline(None)
def test_array_epochs(): """Test creating evoked from array.""" tempdir = _TempDir() # creating rng = np.random.RandomState(42) data1 = rng.randn(20, 60) sfreq = 1e3 ch_names = ['EEG %03d' % (i + 1) for i in range(20)] types = ['eeg'] * 20 info = create_info(ch_names, sfreq, types) evoked1 = EvokedArray(data1, info, tmin=-0.01) # save, read, and compare evokeds tmp_fname = op.join(tempdir, 'evkdary-ave.fif') evoked1.save(tmp_fname) evoked2 = read_evokeds(tmp_fname)[0] data2 = evoked2.data assert_allclose(data1, data2) assert_array_almost_equal(evoked1.times, evoked2.times, 8) assert_equal(evoked1.first, evoked2.first) assert_equal(evoked1.last, evoked2.last) assert_equal(evoked1.kind, evoked2.kind) assert_equal(evoked1.nave, evoked2.nave) # now compare with EpochsArray (with single epoch) data3 = data1[np.newaxis, :, :] events = np.c_[10, 0, 1] evoked3 = EpochsArray(data3, info, events=events, tmin=-0.01).average() assert_allclose(evoked1.data, evoked3.data) assert_allclose(evoked1.times, evoked3.times) assert_equal(evoked1.first, evoked3.first) assert_equal(evoked1.last, evoked3.last) assert_equal(evoked1.kind, evoked3.kind) assert_equal(evoked1.nave, evoked3.nave) # test kind check with pytest.raises(ValueError, match='Invalid value'): EvokedArray(data1, info, tmin=0, kind=1) with pytest.raises(ValueError, match='Invalid value'): EvokedArray(data1, info, kind='mean') # test match between channels info and data ch_names = ['EEG %03d' % (i + 1) for i in range(19)] types = ['eeg'] * 19 info = create_info(ch_names, sfreq, types) pytest.raises(ValueError, EvokedArray, data1, info, tmin=-0.01)
def test_array_epochs(): """Test creating evoked from array.""" tempdir = _TempDir() # creating rng = np.random.RandomState(42) data1 = rng.randn(20, 60) sfreq = 1e3 ch_names = ['EEG %03d' % (i + 1) for i in range(20)] types = ['eeg'] * 20 info = create_info(ch_names, sfreq, types) evoked1 = EvokedArray(data1, info, tmin=-0.01) # save, read, and compare evokeds tmp_fname = op.join(tempdir, 'evkdary-ave.fif') evoked1.save(tmp_fname) evoked2 = read_evokeds(tmp_fname)[0] data2 = evoked2.data assert_allclose(data1, data2) assert_allclose(evoked1.times, evoked2.times) assert_equal(evoked1.first, evoked2.first) assert_equal(evoked1.last, evoked2.last) assert_equal(evoked1.kind, evoked2.kind) assert_equal(evoked1.nave, evoked2.nave) # now compare with EpochsArray (with single epoch) data3 = data1[np.newaxis, :, :] events = np.c_[10, 0, 1] evoked3 = EpochsArray(data3, info, events=events, tmin=-0.01).average() assert_allclose(evoked1.data, evoked3.data) assert_allclose(evoked1.times, evoked3.times) assert_equal(evoked1.first, evoked3.first) assert_equal(evoked1.last, evoked3.last) assert_equal(evoked1.kind, evoked3.kind) assert_equal(evoked1.nave, evoked3.nave) # test kind check with pytest.raises(ValueError, match='Invalid value'): EvokedArray(data1, info, tmin=0, kind=1) with pytest.raises(ValueError, match='Invalid value'): EvokedArray(data1, info, kind='mean') # test match between channels info and data ch_names = ['EEG %03d' % (i + 1) for i in range(19)] types = ['eeg'] * 19 info = create_info(ch_names, sfreq, types) pytest.raises(ValueError, EvokedArray, data1, info, tmin=-0.01)