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 pytest.warns(RuntimeWarning, match='outside the data range'): raw.set_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) raw.set_annotations( Annotations(onset=[1, 4, 5] + raw._first_time, duration=[1, 3, 1], description='BAD', orig_time=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_time_as_index_ref(offset, origin): """Test indexing of raw times.""" info = create_info(ch_names=10, sfreq=10.) raw = RawArray(data=np.empty((10, 10)), info=info, first_samp=10) raw.set_meas_date(1) relative_times = raw.times inds = raw.time_as_index(relative_times + offset, use_rounding=True, origin=origin) assert_array_equal(inds, np.arange(raw.n_times))
def test_time_as_index_ref(offset, origin): """Test indexing of raw times.""" meas_date = 1 info = create_info(ch_names=10, sfreq=10.) raw = RawArray(data=np.empty((10, 10)), info=info, first_samp=10) raw.info['meas_date'] = meas_date relative_times = raw.times inds = raw.time_as_index(relative_times + offset, use_rounding=True, origin=origin) assert_array_equal(inds, np.arange(raw.n_times))
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 pytest.warns(RuntimeWarning, match='outside the data range'): raw.set_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) raw.set_annotations(Annotations(onset=[1, 4, 5] + raw._first_time, duration=[1, 3, 1], description='BAD', orig_time=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))