def test_raw_reject(): """Test raw data getter with annotation reject.""" info = create_info(['a', 'b', 'c', 'd', 'e'], 100, ch_types='eeg') raw = RawArray(np.ones((5, 15000)), info) with warnings.catch_warnings(record=True): # one outside range raw.annotations = Annotations([2, 100, 105, 148], [2, 8, 5, 8], 'BAD') data = raw.get_data([0, 1, 3, 4], 100, 11200, 'omit') assert_array_equal(data.shape, (4, 9900)) # with orig_time and complete overlap raw = read_raw_fif(fif_fname) raw.annotations = Annotations([44, 47, 48], [1, 3, 1], 'BAD', raw.info['meas_date']) data, times = raw.get_data(range(10), 0, 6000, 'omit', True) assert_array_equal(data.shape, (10, 4799)) assert_equal(times[-1], raw.times[5999]) assert_array_equal(data[:, -100:], raw[:10, 5900:6000][0]) data, times = raw.get_data(range(10), 0, 6000, 'NaN', True) assert_array_equal(data.shape, (10, 6000)) assert_equal(times[-1], raw.times[5999]) assert_true(np.isnan(data[:, 313:613]).all()) # 1s -2s assert_true(not np.isnan(data[:, 614].any())) assert_array_equal(data[:, -100:], raw[:10, 5900:6000][0]) assert_array_equal(raw.get_data(), raw[:][0]) # Test _sync_onset times = [10, -88, 190] onsets = _sync_onset(raw, times) assert_array_almost_equal(onsets, times - raw.first_samp / raw.info['sfreq']) assert_array_almost_equal(times, _sync_onset(raw, onsets, True))
def test_raw_reject(): """Test raw data getter with annotation reject.""" sfreq = 100. info = create_info(['a', 'b', 'c', 'd', 'e'], sfreq, ch_types='eeg') raw = RawArray(np.ones((5, 15000)), info) with warnings.catch_warnings(record=True): # one outside range raw.annotations = Annotations([2, 100, 105, 148], [2, 8, 5, 8], 'BAD') data, times = raw.get_data( [0, 1, 3, 4], 100, 11200, # 1-112 sec 'omit', return_times=True) bad_times = np.concatenate([ np.arange(200, 400), np.arange(10000, 10800), np.arange(10500, 11000) ]) expected_times = np.setdiff1d(np.arange(100, 11200), bad_times) / sfreq assert_allclose(times, expected_times) # with orig_time and complete overlap raw = read_raw_fif(fif_fname) t_0 = raw.first_samp / raw.info['sfreq'] raw.annotations = Annotations([t_0 + 1, t_0 + 4, t_0 + 5], [1, 3, 1], 'BAD', raw.info['meas_date']) t_stop = 18. assert raw.times[-1] > t_stop n_stop = int(round(t_stop * raw.info['sfreq'])) n_drop = int(round(4 * raw.info['sfreq'])) assert len(raw.times) >= n_stop data, times = raw.get_data(range(10), 0, n_stop, 'omit', True) assert data.shape == (10, n_stop - n_drop) assert times[-1] == raw.times[n_stop - 1] assert_array_equal(data[:, -100:], raw[:10, n_stop - 100:n_stop][0]) data, times = raw.get_data(range(10), 0, n_stop, 'NaN', True) assert_array_equal(data.shape, (10, n_stop)) assert times[-1] == raw.times[n_stop - 1] t_1, t_2 = raw.time_as_index([1, 2], use_rounding=True) assert np.isnan(data[:, t_1:t_2]).all() # 1s -2s assert not np.isnan(data[:, :t_1].any()) assert not np.isnan(data[:, t_2:].any()) assert_array_equal(data[:, -100:], raw[:10, n_stop - 100:n_stop][0]) assert_array_equal(raw.get_data(), raw[:][0]) # Test _sync_onset times = [10, -88, 190] onsets = _sync_onset(raw, times) assert_array_almost_equal(onsets, times - raw.first_samp / raw.info['sfreq']) assert_array_almost_equal(times, _sync_onset(raw, onsets, True))
def test_annotation_omit(): """Test raw.get_data with annotations.""" data = np.concatenate([np.ones((1, 1000)), 2 * np.ones((1, 1000))], -1) info = create_info(1, 1000., 'eeg') raw = RawArray(data, info) raw.annotations = Annotations([0.5], [1], ['bad']) expected = raw[0][0] assert_allclose(raw.get_data(reject_by_annotation=None), expected) # nan expected[0, 500:1500] = np.nan assert_allclose(raw.get_data(reject_by_annotation='nan'), expected) got = np.concatenate([ raw.get_data(start=start, stop=stop, reject_by_annotation='nan') for start, stop in ((0, 1000), (1000, 2000)) ], -1) assert_allclose(got, expected) # omit expected = expected[:, np.isfinite(expected[0])] assert_allclose(raw.get_data(reject_by_annotation='omit'), expected) got = np.concatenate([ raw.get_data(start=start, stop=stop, reject_by_annotation='omit') for start, stop in ((0, 1000), (1000, 2000)) ], -1) assert_allclose(got, expected) assert_raises(ValueError, raw.get_data, reject_by_annotation='foo')
def test_annotation_property_deprecation_warning(): """Test that assigning annotations warns and nowhere else.""" with pytest.warns(None) as w: raw = RawArray(np.random.rand(1, 1), create_info(1, 1)) assert len(w) is 0 with pytest.warns(DeprecationWarning, match='by assignment is deprecated'): raw.annotations = None
def test_raw_reject(): """Test raw data getter with annotation reject.""" sfreq = 100. info = create_info(['a', 'b', 'c', 'd', 'e'], sfreq, ch_types='eeg') raw = RawArray(np.ones((5, 15000)), info) with warnings.catch_warnings(record=True): # one outside range raw.annotations = Annotations([2, 100, 105, 148], [2, 8, 5, 8], 'BAD') data, times = raw.get_data([0, 1, 3, 4], 100, 11200, # 1-112 sec 'omit', return_times=True) bad_times = np.concatenate([np.arange(200, 400), np.arange(10000, 10800), np.arange(10500, 11000)]) expected_times = np.setdiff1d(np.arange(100, 11200), bad_times) / sfreq assert_allclose(times, expected_times) # with orig_time and complete overlap raw = read_raw_fif(fif_fname) t_0 = raw.first_samp / raw.info['sfreq'] raw.annotations = Annotations([t_0 + 1, t_0 + 4, t_0 + 5], [1, 3, 1], 'BAD', raw.info['meas_date']) t_stop = 18. assert raw.times[-1] > t_stop n_stop = int(round(t_stop * raw.info['sfreq'])) n_drop = int(round(4 * raw.info['sfreq'])) assert len(raw.times) >= n_stop data, times = raw.get_data(range(10), 0, n_stop, 'omit', True) assert data.shape == (10, n_stop - n_drop) assert times[-1] == raw.times[n_stop - 1] assert_array_equal(data[:, -100:], raw[:10, n_stop - 100:n_stop][0]) data, times = raw.get_data(range(10), 0, n_stop, 'NaN', True) assert_array_equal(data.shape, (10, n_stop)) assert times[-1] == raw.times[n_stop - 1] t_1, t_2 = raw.time_as_index([1, 2], use_rounding=True) assert np.isnan(data[:, t_1:t_2]).all() # 1s -2s assert not np.isnan(data[:, :t_1].any()) assert not np.isnan(data[:, t_2:].any()) assert_array_equal(data[:, -100:], raw[:10, n_stop - 100:n_stop][0]) assert_array_equal(raw.get_data(), raw[:][0]) # Test _sync_onset times = [10, -88, 190] onsets = _sync_onset(raw, times) assert_array_almost_equal(onsets, times - raw.first_samp / raw.info['sfreq']) assert_array_almost_equal(times, _sync_onset(raw, onsets, True))
def test_annotations(): """Test annotation class.""" raw = read_raw_fif(fif_fname) onset = np.array(range(10)) duration = np.ones(10) description = np.repeat('test', 10) dt = datetime.utcnow() meas_date = raw.info['meas_date'] # Test time shifts. for orig_time in [None, dt, meas_date[0], meas_date]: annot = Annotations(onset, duration, description, orig_time) assert_raises(ValueError, Annotations, onset, duration, description[:9]) assert_raises(ValueError, Annotations, [onset, 1], duration, description) assert_raises(ValueError, Annotations, onset, [duration, 1], description) # Test combining annotations with concatenate_raws raw2 = raw.copy() orig_time = (meas_date[0] + meas_date[1] * 0.000001 + raw2.first_samp / raw2.info['sfreq']) annot = Annotations(onset, duration, description, orig_time) raw2.annotations = annot assert_array_equal(raw2.annotations.onset, onset) concatenate_raws([raw, raw2]) assert_array_almost_equal(onset + 20., raw.annotations.onset, decimal=2) assert_array_equal(annot.duration, raw.annotations.duration) assert_array_equal(raw.annotations.description, np.repeat('test', 10)) # Test combining with RawArray and orig_times data = np.random.randn(2, 1000) * 10e-12 sfreq = 100. info = create_info(ch_names=['MEG1', 'MEG2'], ch_types=['grad'] * 2, sfreq=sfreq) info['meas_date'] = 0 raws = [] for i, fs in enumerate([1000, 100, 12]): raw = RawArray(data.copy(), info, first_samp=fs) ants = Annotations([1., 2.], [.5, .5], 'x', fs / sfreq) raw.annotations = ants raws.append(raw) raw = concatenate_raws(raws) assert_array_equal(raw.annotations.onset, [1., 2., 11., 12., 21., 22.]) raw.annotations.delete(2) assert_array_equal(raw.annotations.onset, [1., 2., 12., 21., 22.]) raw.annotations.append(5, 1.5, 'y') assert_array_equal(raw.annotations.onset, [1., 2., 12., 21., 22., 5]) assert_array_equal(raw.annotations.duration, [.5, .5, .5, .5, .5, 1.5]) assert_array_equal(raw.annotations.description, ['x', 'x', 'x', 'x', 'x', 'y'])
def test_raw_reject(): """Test raw data getter with annotation reject.""" info = create_info(['a', 'b', 'c', 'd', 'e'], 100, ch_types='eeg') raw = RawArray(np.ones((5, 15000)), info) raw.annotations = Annotations([2, 100, 105, 148], [2, 8, 5, 8], 'BAD') data = raw.get_data([0, 1, 3, 4], 100, 11200, 'omit') assert_array_equal(data.shape, (4, 9900)) # with orig_time and complete overlap raw = read_raw_fif(fif_fname) raw.annotations = Annotations([44, 47, 48], [1, 3, 1], 'BAD', raw.info['meas_date']) data, times = raw.get_data(range(10), 0, 6000, 'omit', True) assert_array_equal(data.shape, (10, 4799)) assert_equal(times[-1], raw.times[5999]) assert_array_equal(data[:, -100:], raw[:10, 5900:6000][0]) data, times = raw.get_data(range(10), 0, 6000, 'NaN', True) assert_array_equal(data.shape, (10, 6000)) assert_equal(times[-1], raw.times[5999]) assert_true(np.isnan(data[:, 313:613]).all()) # 1s -2s assert_true(not np.isnan(data[:, 614].any())) assert_array_equal(data[:, -100:], raw[:10, 5900:6000][0]) assert_array_equal(raw.get_data(), raw[:][0])
def test_annotation_omit(): """Test raw.get_data with annotations.""" data = np.concatenate([np.ones((1, 1000)), 2 * np.ones((1, 1000))], -1) info = create_info(1, 1000., 'eeg') raw = RawArray(data, info) raw.annotations = Annotations([0.5], [1], ['bad']) expected = raw[0][0] assert_allclose(raw.get_data(reject_by_annotation=None), expected) # nan expected[0, 500:1500] = np.nan assert_allclose(raw.get_data(reject_by_annotation='nan'), expected) got = np.concatenate([raw.get_data(start=start, stop=stop, reject_by_annotation='nan') for start, stop in ((0, 1000), (1000, 2000))], -1) assert_allclose(got, expected) # omit expected = expected[:, np.isfinite(expected[0])] assert_allclose(raw.get_data(reject_by_annotation='omit'), expected) got = np.concatenate([raw.get_data(start=start, stop=stop, reject_by_annotation='omit') for start, stop in ((0, 1000), (1000, 2000))], -1) assert_allclose(got, expected) assert_raises(ValueError, raw.get_data, reject_by_annotation='foo')
def test_basics(): """Test annotation class.""" raw = read_raw_fif(fif_fname) assert raw.annotations is None pytest.raises(IOError, read_annotations, fif_fname) onset = np.array(range(10)) duration = np.ones(10) description = np.repeat('test', 10) dt = datetime.utcnow() meas_date = raw.info['meas_date'] # Test time shifts. for orig_time in [None, dt, meas_date[0], meas_date]: annot = Annotations(onset, duration, description, orig_time) pytest.raises(ValueError, Annotations, onset, duration, description[:9]) pytest.raises(ValueError, Annotations, [onset, 1], duration, description) pytest.raises(ValueError, Annotations, onset, [duration, 1], description) # Test combining annotations with concatenate_raws raw2 = raw.copy() delta = raw.times[-1] + 1. / raw.info['sfreq'] orig_time = (meas_date[0] + meas_date[1] * 1e-6 + raw2.first_samp / raw2.info['sfreq']) annot = Annotations(onset, duration, description, orig_time) assert ' segments' in repr(annot) raw2.annotations = annot assert_array_equal(raw2.annotations.onset, onset) concatenate_raws([raw, raw2]) raw.annotations.delete(-1) # remove boundary annotations raw.annotations.delete(-1) assert_allclose(onset + delta, raw.annotations.onset, rtol=1e-5) assert_array_equal(annot.duration, raw.annotations.duration) assert_array_equal(raw.annotations.description, np.repeat('test', 10)) # Test combining with RawArray and orig_times data = np.random.randn(2, 1000) * 10e-12 sfreq = 100. info = create_info(ch_names=['MEG1', 'MEG2'], ch_types=['grad'] * 2, sfreq=sfreq) info['meas_date'] = np.pi raws = [] for first_samp in [12300, 100, 12]: raw = RawArray(data.copy(), info, first_samp=first_samp) ants = Annotations([1., 2.], [.5, .5], 'x', np.pi + first_samp / sfreq) raw.annotations = ants raws.append(raw) raw = RawArray(data.copy(), info) raw.annotations = Annotations([1.], [.5], 'x', None) raws.append(raw) raw = concatenate_raws(raws, verbose='debug') boundary_idx = np.where(raw.annotations.description == 'BAD boundary')[0] assert len(boundary_idx) == 3 raw.annotations.delete(boundary_idx) boundary_idx = np.where(raw.annotations.description == 'EDGE boundary')[0] assert len(boundary_idx) == 3 raw.annotations.delete(boundary_idx) assert_array_equal(raw.annotations.onset, [1., 2., 11., 12., 21., 22., 31.]) raw.annotations.delete(2) assert_array_equal(raw.annotations.onset, [1., 2., 12., 21., 22., 31.]) raw.annotations.append(5, 1.5, 'y') assert_array_equal(raw.annotations.onset, [1., 2., 12., 21., 22., 31., 5]) assert_array_equal(raw.annotations.duration, [.5, .5, .5, .5, .5, .5, 1.5]) assert_array_equal(raw.annotations.description, ['x', 'x', 'x', 'x', 'x', 'x', 'y'])
def bv2fif(dataf, corf, ch_order=None, eogs=('VEOG', 'HEOG'), ecg='ECG', emg='EMG', preload='default', ref_ch='Fp1', dbs=False, new_sfreq=1000.0): montage = read_dig_montage(bvct=corf) if preload == 'default': preload = os.path.dirname(dataf) + '/workfile' raw = read_raw_brainvision(dataf, preload=preload) if dbs: event_ch = get_events(raw) # save downsampled raw for multitaper spectrogram raw_data = np.zeros( (raw._data.shape[0], int(raw._data.shape[1] / raw.info['sfreq'] * new_sfreq))) raw_info = raw.info.copy() raw_info['sfreq'] = new_sfreq for i in tqdm(range(len(raw._data))): ch = raw._data[i, ::int(raw.info['sfreq'] / new_sfreq)] raw_data[i] = ch del ch raw_resampled = RawArray(raw_data, raw_info) raw_resampled.annotations = raw.annotations if dbs: old_event_ch = [ch for ch in raw.info['ch_names'] if 'STI' in ch] if old_event_ch: raw_resampled.drop_channels([old_event_ch[0]]) event_ch._data = event_ch._data[:, ::int(raw.info['sfreq'] / new_sfreq)] event_ch.info['sfreq'] = new_sfreq event_ch.__len__ = len(event_ch._data[0]) event_ch.info['lowpass'] = raw_resampled.info['lowpass'] raw_resampled.add_channels([event_ch]) prepInst(raw_resampled, dataf, 'raw', montage, ref_ch, eogs, ecg, emg) events, event_ids = events_from_annotations(raw) if len(np.unique(events[:, 2])) > 1: events = events[np.where(events[:, 2] == events[1, 2])[0]] #skip new segment epochs = Epochs(raw, events, tmin=-2, tmax=2, proj=False, preload=op.dirname(dataf) + '/workfile-epo', baseline=(-0.5, -0.1), verbose=False, detrend=1) events = events[ epochs. selection] #in case any epochs don't have data and get thrown out (poorly placed at beginning or end) epo_data = np.zeros( (epochs._data.shape[0], epochs._data.shape[1], int(np.ceil(epochs._data.shape[2] / epochs.info['sfreq'] * new_sfreq)))) for i in tqdm(range(epochs._data.shape[0])): for j in range(epochs._data.shape[1]): epo_curr = epochs._data[i, j, ::int(epochs.info['sfreq'] / new_sfreq)] epo_data[i, j] = epo_curr del epo_curr events[:, 0] = np.array(events[:, 0] * new_sfreq / raw.info['sfreq'], dtype=int) epo_resampled = EpochsArray(epo_data, epochs.info.copy(), events, tmin=-2) epo_resampled.info['sfreq'] = new_sfreq epo_resampled.events[:, 2] = np.arange(len(events)) epo_resampled.event_id = {str(i): i for i in range(len(events))} prepInst(epo_resampled, dataf, 'epo', montage, ref_ch, eogs, ecg, emg)
def test_annotations(): """Test annotation class.""" raw = read_raw_fif(fif_fname) assert raw.annotations is None assert_raises(ValueError, read_annotations, fif_fname) onset = np.array(range(10)) duration = np.ones(10) description = np.repeat('test', 10) dt = datetime.utcnow() meas_date = raw.info['meas_date'] # Test time shifts. for orig_time in [None, dt, meas_date[0], meas_date]: annot = Annotations(onset, duration, description, orig_time) assert_raises(ValueError, Annotations, onset, duration, description[:9]) assert_raises(ValueError, Annotations, [onset, 1], duration, description) assert_raises(ValueError, Annotations, onset, [duration, 1], description) # Test combining annotations with concatenate_raws raw2 = raw.copy() orig_time = (meas_date[0] + meas_date[1] * 0.000001 + raw2.first_samp / raw2.info['sfreq']) annot = Annotations(onset, duration, description, orig_time) assert_true(' segments' in repr(annot)) raw2.annotations = annot assert_array_equal(raw2.annotations.onset, onset) concatenate_raws([raw, raw2]) raw.annotations.delete(-1) # remove boundary annotations raw.annotations.delete(-1) assert_array_almost_equal(onset + 20., raw.annotations.onset, decimal=2) assert_array_equal(annot.duration, raw.annotations.duration) assert_array_equal(raw.annotations.description, np.repeat('test', 10)) # Test combining with RawArray and orig_times data = np.random.randn(2, 1000) * 10e-12 sfreq = 100. info = create_info(ch_names=['MEG1', 'MEG2'], ch_types=['grad'] * 2, sfreq=sfreq) info['meas_date'] = 0 raws = [] for i, fs in enumerate([12300, 100, 12]): raw = RawArray(data.copy(), info, first_samp=fs) ants = Annotations([1., 2.], [.5, .5], 'x', fs / sfreq) raw.annotations = ants raws.append(raw) raw = RawArray(data.copy(), info) raw.annotations = Annotations([1.], [.5], 'x', None) raws.append(raw) raw = concatenate_raws(raws) boundary_idx = np.where(raw.annotations.description == 'BAD boundary')[0] assert_equal(len(boundary_idx), 3) raw.annotations.delete(boundary_idx) boundary_idx = np.where(raw.annotations.description == 'EDGE boundary')[0] assert_equal(len(boundary_idx), 3) raw.annotations.delete(boundary_idx) assert_array_equal(raw.annotations.onset, [1., 2., 11., 12., 21., 22., 31.]) raw.annotations.delete(2) assert_array_equal(raw.annotations.onset, [1., 2., 12., 21., 22., 31.]) raw.annotations.append(5, 1.5, 'y') assert_array_equal(raw.annotations.onset, [1., 2., 12., 21., 22., 31., 5]) assert_array_equal(raw.annotations.duration, [.5, .5, .5, .5, .5, .5, 1.5]) assert_array_equal(raw.annotations.description, ['x', 'x', 'x', 'x', 'x', 'x', 'y']) # Test concatenating annotations with and without orig_time. raw = read_raw_fif(fif_fname) last_time = raw.last_samp / raw.info['sfreq'] raw2 = raw.copy() raw.annotations = Annotations([45.], [3], 'test', raw.info['meas_date']) raw2.annotations = Annotations([2.], [3], 'BAD', None) raw = concatenate_raws([raw, raw2]) raw.annotations.delete(-1) # remove boundary annotations raw.annotations.delete(-1) assert_array_almost_equal(raw.annotations.onset, [45., 2. + last_time], decimal=2) # Test IO tempdir = _TempDir() fname = op.join(tempdir, 'test-annot.fif') raw.annotations.save(fname) annot_read = read_annotations(fname) for attr in ('onset', 'duration', 'orig_time'): assert_allclose(getattr(annot_read, attr), getattr(raw.annotations, attr)) assert_array_equal(annot_read.description, raw.annotations.description)