def test_evoked_resample(): """Test for resampling of evoked data """ # upsample, write it out, read it in ave = read_evokeds(fname, 0) sfreq_normal = ave.info['sfreq'] ave.resample(2 * sfreq_normal) write_evokeds(op.join(tempdir, 'evoked-ave.fif'), ave) ave_up = read_evokeds(op.join(tempdir, 'evoked-ave.fif'), 0) # compare it to the original ave_normal = read_evokeds(fname, 0) # and compare the original to the downsampled upsampled version ave_new = read_evokeds(op.join(tempdir, 'evoked-ave.fif'), 0) ave_new.resample(sfreq_normal) assert_array_almost_equal(ave_normal.data, ave_new.data, 2) assert_array_almost_equal(ave_normal.times, ave_new.times) assert_equal(ave_normal.nave, ave_new.nave) assert_equal(ave_normal._aspect_kind, ave_new._aspect_kind) assert_equal(ave_normal.kind, ave_new.kind) assert_equal(ave_normal.last, ave_new.last) assert_equal(ave_normal.first, ave_new.first) # for the above to work, the upsampling just about had to, but # we'll add a couple extra checks anyway assert_true(len(ave_up.times) == 2 * len(ave_normal.times)) assert_true(ave_up.data.shape[1] == 2 * ave_normal.data.shape[1])
def test_evoked_standard_error(): """Test calculation and read/write of standard error """ epochs = Epochs(raw, events[:4], event_id, tmin, tmax, picks=picks, baseline=(None, 0)) evoked = [epochs.average(), epochs.standard_error()] io.write_evokeds(op.join(tempdir, 'evoked.fif'), evoked) evoked2 = read_evokeds(op.join(tempdir, 'evoked.fif'), [0, 1]) evoked3 = [read_evokeds(op.join(tempdir, 'evoked.fif'), 'Unknown'), read_evokeds(op.join(tempdir, 'evoked.fif'), 'Unknown', kind='standard_error')] for evoked_new in [evoked2, evoked3]: assert_true(evoked_new[0]._aspect_kind == FIFF.FIFFV_ASPECT_AVERAGE) assert_true(evoked_new[0].kind == 'average') assert_true(evoked_new[1]._aspect_kind == FIFF.FIFFV_ASPECT_STD_ERR) assert_true(evoked_new[1].kind == 'standard_error') for ave, ave2 in zip(evoked, evoked_new): assert_array_almost_equal(ave.data, ave2.data) assert_array_almost_equal(ave.times, ave2.times) assert_equal(ave.nave, ave2.nave) assert_equal(ave._aspect_kind, ave2._aspect_kind) assert_equal(ave.kind, ave2.kind) assert_equal(ave.last, ave2.last) assert_equal(ave.first, ave2.first)
def test_io_evoked(): """Test IO for evoked data (fif + gz) with integer and str args """ ave = read_evokeds(fname, 0) write_evokeds(op.join(tempdir, 'evoked.fif'), ave) ave2 = read_evokeds(op.join(tempdir, 'evoked.fif'))[0] # This not being assert_array_equal due to windows rounding assert_true(np.allclose(ave.data, ave2.data, atol=1e-16, rtol=1e-3)) assert_array_almost_equal(ave.times, ave2.times) assert_equal(ave.nave, ave2.nave) assert_equal(ave._aspect_kind, ave2._aspect_kind) assert_equal(ave.kind, ave2.kind) assert_equal(ave.last, ave2.last) assert_equal(ave.first, ave2.first) # test compressed i/o ave2 = read_evokeds(fname_gz, 0) assert_true(np.allclose(ave.data, ave2.data, atol=1e-16, rtol=1e-8)) # test str access condition = 'Left Auditory' assert_raises(ValueError, read_evokeds, fname, condition, kind='stderr') assert_raises(ValueError, read_evokeds, fname, condition, kind='standard_error') ave3 = read_evokeds(fname, condition) assert_array_almost_equal(ave.data, ave3.data, 19) # test deprecation warning for read_evoked and write_evoked # XXX should be deleted for 0.9 release with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') ave = read_evoked(fname, setno=0) assert_true(w[0].category == DeprecationWarning) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') write_evoked(op.join(tempdir, 'evoked.fif'), ave) assert_true(w[0].category == DeprecationWarning) # test read_evokeds and write_evokeds types = ['Left Auditory', 'Right Auditory', 'Left visual', 'Right visual'] aves1 = read_evokeds(fname) aves2 = read_evokeds(fname, [0, 1, 2, 3]) aves3 = read_evokeds(fname, types) write_evokeds(op.join(tempdir, 'evoked.fif'), aves1) aves4 = read_evokeds(op.join(tempdir, 'evoked.fif')) for aves in [aves2, aves3, aves4]: for [av1, av2] in zip(aves1, aves): assert_array_almost_equal(av1.data, av2.data) assert_array_almost_equal(av1.times, av2.times) assert_equal(av1.nave, av2.nave) assert_equal(av1.kind, av2.kind) assert_equal(av1._aspect_kind, av2._aspect_kind) assert_equal(av1.last, av2.last) assert_equal(av1.first, av2.first) assert_equal(av1.comment, av2.comment)
def test_shift_time_evoked(): """ Test for shifting of time scale """ # Shift backward ave = read_evokeds(fname, 0) ave.shift_time(-0.1, relative=True) write_evokeds(op.join(tempdir, 'evoked-ave.fif'), ave) # Shift forward twice the amount ave_bshift = read_evokeds(op.join(tempdir, 'evoked-ave.fif'), 0) ave_bshift.shift_time(0.2, relative=True) write_evokeds(op.join(tempdir, 'evoked-ave.fif'), ave_bshift) # Shift backward again ave_fshift = read_evokeds(op.join(tempdir, 'evoked-ave.fif'), 0) ave_fshift.shift_time(-0.1, relative=True) write_evokeds(op.join(tempdir, 'evoked-ave.fif'), ave_fshift) ave_normal = read_evokeds(fname, 0) ave_relative = read_evokeds(op.join(tempdir, 'evoked-ave.fif'), 0) assert_true(np.allclose(ave_normal.data, ave_relative.data, atol=1e-16, rtol=1e-3)) assert_array_almost_equal(ave_normal.times, ave_relative.times, 10) assert_equal(ave_normal.last, ave_relative.last) assert_equal(ave_normal.first, ave_relative.first) # Absolute time shift ave = read_evokeds(fname, 0) ave.shift_time(-0.3, relative=False) write_evokeds(op.join(tempdir, 'evoked-ave.fif'), ave) ave_absolute = read_evokeds(op.join(tempdir, 'evoked-ave.fif'), 0) assert_true(np.allclose(ave_normal.data, ave_absolute.data, atol=1e-16, rtol=1e-3)) assert_equal(ave_absolute.first, int(-0.3 * ave.info['sfreq']))
def test_shift_time_evoked(): """ Test for shifting of time scale """ # Shift backward ave = read_evokeds(fname, 0) ave.shift_time(-0.1, relative=True) write_evokeds(op.join(tempdir, 'evoked-ave.fif'), ave) # Shift forward twice the amount ave_bshift = read_evokeds(op.join(tempdir, 'evoked-ave.fif'), 0) ave_bshift.shift_time(0.2, relative=True) write_evokeds(op.join(tempdir, 'evoked-ave.fif'), ave_bshift) # Shift backward again ave_fshift = read_evokeds(op.join(tempdir, 'evoked-ave.fif'), 0) ave_fshift.shift_time(-0.1, relative=True) write_evokeds(op.join(tempdir, 'evoked-ave.fif'), ave_fshift) ave_normal = read_evokeds(fname, 0) ave_relative = read_evokeds(op.join(tempdir, 'evoked-ave.fif'), 0) assert_true( np.allclose(ave_normal.data, ave_relative.data, atol=1e-16, rtol=1e-3)) assert_array_almost_equal(ave_normal.times, ave_relative.times, 10) assert_equal(ave_normal.last, ave_relative.last) assert_equal(ave_normal.first, ave_relative.first) # Absolute time shift ave = read_evokeds(fname, 0) ave.shift_time(-0.3, relative=False) write_evokeds(op.join(tempdir, 'evoked-ave.fif'), ave) ave_absolute = read_evokeds(op.join(tempdir, 'evoked-ave.fif'), 0) assert_true( np.allclose(ave_normal.data, ave_absolute.data, atol=1e-16, rtol=1e-3)) assert_equal(ave_absolute.first, int(-0.3 * ave.info['sfreq']))
def test_io_evoked(): """Test IO for evoked data (fif + gz) with integer and str args """ ave = read_evokeds(fname, 0) write_evokeds(op.join(tempdir, 'evoked-ave.fif'), ave) ave2 = read_evokeds(op.join(tempdir, 'evoked-ave.fif'))[0] # This not being assert_array_equal due to windows rounding assert_true(np.allclose(ave.data, ave2.data, atol=1e-16, rtol=1e-3)) assert_array_almost_equal(ave.times, ave2.times) assert_equal(ave.nave, ave2.nave) assert_equal(ave._aspect_kind, ave2._aspect_kind) assert_equal(ave.kind, ave2.kind) assert_equal(ave.last, ave2.last) assert_equal(ave.first, ave2.first) # test compressed i/o ave2 = read_evokeds(fname_gz, 0) assert_true(np.allclose(ave.data, ave2.data, atol=1e-16, rtol=1e-8)) # test str access condition = 'Left Auditory' assert_raises(ValueError, read_evokeds, fname, condition, kind='stderr') assert_raises(ValueError, read_evokeds, fname, condition, kind='standard_error') ave3 = read_evokeds(fname, condition) assert_array_almost_equal(ave.data, ave3.data, 19) # test deprecation warning for read_evoked and write_evoked # XXX should be deleted for 0.9 release with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') ave = read_evoked(fname, setno=0) assert_true(w[0].category == DeprecationWarning) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') write_evoked(op.join(tempdir, 'evoked-ave.fif'), ave) assert_true(w[0].category == DeprecationWarning) # test read_evokeds and write_evokeds types = ['Left Auditory', 'Right Auditory', 'Left visual', 'Right visual'] aves1 = read_evokeds(fname) aves2 = read_evokeds(fname, [0, 1, 2, 3]) aves3 = read_evokeds(fname, types) write_evokeds(op.join(tempdir, 'evoked-ave.fif'), aves1) aves4 = read_evokeds(op.join(tempdir, 'evoked-ave.fif')) for aves in [aves2, aves3, aves4]: for [av1, av2] in zip(aves1, aves): assert_array_almost_equal(av1.data, av2.data) assert_array_almost_equal(av1.times, av2.times) assert_equal(av1.nave, av2.nave) assert_equal(av1.kind, av2.kind) assert_equal(av1._aspect_kind, av2._aspect_kind) assert_equal(av1.last, av2.last) assert_equal(av1.first, av2.first) assert_equal(av1.comment, av2.comment) # test warnings on bad filenames with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') fname2 = op.join(tempdir, 'test-bad-name.fif') write_evokeds(fname2, ave) read_evokeds(fname2) assert_true(len(w) == 2)
picks = mne.pick_types(raw.info, meg=False, eeg=True, stim=False, eog=True, include=include, exclude='bads') # Read epochs epochs = mne.Epochs(raw, events, event_ids, tmin, tmax, picks=picks, baseline=(None, 0), reject=dict(eeg=80e-6, eog=150e-6)) # Let's equalize the trial counts in each condition epochs.equalize_event_counts(['AudL', 'AudR', 'VisL', 'VisR'], copy=False) # Now let's combine some conditions combine_event_ids(epochs, ['AudL', 'AudR'], {'Auditory': 12}, copy=False) combine_event_ids(epochs, ['VisL', 'VisR'], {'Visual': 34}, copy=False) # average epochs and get Evoked datasets evokeds = [epochs[cond].average() for cond in ['Auditory', 'Visual']] # save evoked data to disk io.write_evokeds('sample_auditory_and_visual_eeg-ave.fif', evokeds) ############################################################################### # View evoked response import matplotlib.pyplot as plt plt.clf() ax = plt.subplot(2, 1, 1) evokeds[0].plot(axes=ax) plt.title('EEG evoked potential, auditory trials') plt.ylabel('Potential (uV)') ax = plt.subplot(2, 1, 2) evokeds[1].plot(axes=ax) plt.title('EEG evoked potential, visual trials') plt.ylabel('Potential (uV)') plt.show()
tmin, tmax, picks=picks, baseline=(None, 0), reject=dict(eeg=80e-6, eog=150e-6)) # Let's equalize the trial counts in each condition epochs.equalize_event_counts(['AudL', 'AudR', 'VisL', 'VisR'], copy=False) # Now let's combine some conditions combine_event_ids(epochs, ['AudL', 'AudR'], {'Auditory': 12}, copy=False) combine_event_ids(epochs, ['VisL', 'VisR'], {'Visual': 34}, copy=False) # average epochs and get Evoked datasets evokeds = [epochs[cond].average() for cond in ['Auditory', 'Visual']] # save evoked data to disk io.write_evokeds('sample_auditory_and_visual_eeg-ave.fif', evokeds) ############################################################################### # View evoked response import matplotlib.pyplot as plt plt.clf() ax = plt.subplot(2, 1, 1) evokeds[0].plot(axes=ax) plt.title('EEG evoked potential, auditory trials') plt.ylabel('Potential (uV)') ax = plt.subplot(2, 1, 2) evokeds[1].plot(axes=ax) plt.title('EEG evoked potential, visual trials') plt.ylabel('Potential (uV)') plt.show()