本文整理汇总了Python中mne.io.RawArray.set_annotations方法的典型用法代码示例。如果您正苦于以下问题:Python RawArray.set_annotations方法的具体用法?Python RawArray.set_annotations怎么用?Python RawArray.set_annotations使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mne.io.RawArray
的用法示例。
在下文中一共展示了RawArray.set_annotations方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_chunk_duration
# 需要导入模块: from mne.io import RawArray [as 别名]
# 或者: from mne.io.RawArray import set_annotations [as 别名]
def test_chunk_duration():
"""Test chunk_duration."""
# create dummy raw
raw = RawArray(data=np.empty([10, 10], dtype=np.float64),
info=create_info(ch_names=10, sfreq=1.),
first_samp=0)
raw.info['meas_date'] = 0
raw.set_annotations(Annotations(description='foo', onset=[0],
duration=[10], orig_time=None))
# expected_events = [[0, 0, 1], [0, 0, 1], [1, 0, 1], [1, 0, 1], ..
# [9, 0, 1], [9, 0, 1]]
expected_events = np.atleast_2d(np.repeat(range(10), repeats=2)).T
expected_events = np.insert(expected_events, 1, 0, axis=1)
expected_events = np.insert(expected_events, 2, 1, axis=1)
events, events_id = events_from_annotations(raw, chunk_duration=.5,
use_rounding=False)
assert_array_equal(events, expected_events)
# test chunk durations that do not fit equally in annotation duration
expected_events = np.zeros((3, 3))
expected_events[:, -1] = 1
expected_events[:, 0] = np.arange(0, 9, step=3)
events, events_id = events_from_annotations(raw, chunk_duration=3.)
assert_array_equal(events, expected_events)
示例2: _raw_annot
# 需要导入模块: from mne.io import RawArray [as 别名]
# 或者: from mne.io.RawArray import set_annotations [as 别名]
def _raw_annot(meas_date, orig_time):
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
annot = Annotations([.5], [.2], ['dummy'], orig_time)
raw.set_annotations(annotations=annot)
return raw
示例3: raw_factory
# 需要导入模块: from mne.io import RawArray [as 别名]
# 或者: from mne.io.RawArray import set_annotations [as 别名]
def raw_factory(meas_date):
raw = RawArray(data=np.empty((10, 10)),
info=create_info(ch_names=10, sfreq=10., ),
first_samp=10)
raw.info['meas_date'] = meas_date
raw.set_annotations(annotations=Annotations(onset=[.5],
duration=[.2],
description='dummy',
orig_time=None))
return raw
示例4: test_raw_reject
# 需要导入模块: from mne.io import RawArray [as 别名]
# 或者: from mne.io.RawArray import set_annotations [as 别名]
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))
示例5: _create_annotation_based_on_descr
# 需要导入模块: from mne.io import RawArray [as 别名]
# 或者: from mne.io.RawArray import set_annotations [as 别名]
def _create_annotation_based_on_descr(description, annotation_start_sampl=0,
duration=0, orig_time=0):
"""Create a raw object with annotations from descriptions.
The returning raw object contains as many annotations as description given.
All starting at `annotation_start_sampl`.
"""
# create dummy raw
raw = RawArray(data=np.empty([10, 10], dtype=np.float64),
info=create_info(ch_names=10, sfreq=1000.),
first_samp=0)
raw.info['meas_date'] = 0
# create dummy annotations based on the descriptions
onset = raw.times[annotation_start_sampl]
onset_matching_desc = np.full_like(description, onset, dtype=type(onset))
duration_matching_desc = np.full_like(description, duration,
dtype=type(duration))
annot = Annotations(description=description,
onset=onset_matching_desc,
duration=duration_matching_desc,
orig_time=orig_time)
if duration != 0:
with pytest.warns(RuntimeWarning, match='Limited.*expanding outside'):
# duration 0.1s is larger than the raw data expand
raw.set_annotations(annot)
else:
raw.set_annotations(annot)
# Make sure that set_annotations(annot) works
assert all(raw.annotations.onset == onset)
if duration != 0:
expected_duration = (len(raw.times) / raw.info['sfreq']) - onset
else:
expected_duration = 0
_duration = raw.annotations.duration[0]
assert _duration == approx(expected_duration)
assert all(raw.annotations.duration == _duration)
assert all(raw.annotations.description == description)
return raw
示例6: test_get_data_reject
# 需要导入模块: from mne.io import RawArray [as 别名]
# 或者: from mne.io.RawArray import set_annotations [as 别名]
def test_get_data_reject():
"""Test if reject_by_annotation is working correctly."""
fs = 256
ch_names = ["C3", "Cz", "C4"]
info = create_info(ch_names, sfreq=fs)
raw = RawArray(np.zeros((len(ch_names), 10 * fs)), info)
raw.set_annotations(Annotations(onset=[2, 4], duration=[3, 2],
description="bad"))
with catch_logging() as log:
data = raw.get_data(reject_by_annotation="omit", verbose=True)
msg = ('Omitting 1024 of 2560 (40.00%) samples, retaining 1536' +
' (60.00%) samples.')
assert log.getvalue().strip() == msg
assert data.shape == (len(ch_names), 1536)
with catch_logging() as log:
data = raw.get_data(reject_by_annotation="nan", verbose=True)
msg = ('Setting 1024 of 2560 (40.00%) samples to NaN, retaining 1536' +
' (60.00%) samples.')
assert log.getvalue().strip() == msg
assert data.shape == (len(ch_names), 2560) # shape doesn't change
assert np.isnan(data).sum() == 3072 # but NaNs are introduced instead
示例7: test_annotation_omit
# 需要导入模块: from mne.io import RawArray [as 别名]
# 或者: from mne.io.RawArray import set_annotations [as 别名]
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.set_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)
pytest.raises(ValueError, raw.get_data, reject_by_annotation='foo')
示例8: test_raw_array_orig_times
# 需要导入模块: from mne.io import RawArray [as 别名]
# 或者: from mne.io.RawArray import set_annotations [as 别名]
def test_raw_array_orig_times():
"""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, 0)
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.set_annotations(ants)
raws.append(raw)
raw = RawArray(data.copy(), info)
raw.set_annotations(Annotations([1.], [.5], 'x', None))
raws.append(raw)
raw = concatenate_raws(raws, verbose='debug')
assert_and_remove_boundary_annot(raw, 3)
assert_array_equal(raw.annotations.onset, [124., 125., 134., 135.,
144., 145., 154.])
raw.annotations.delete(2)
assert_array_equal(raw.annotations.onset, [124., 125., 135., 144.,
145., 154.])
raw.annotations.append(5, 1.5, 'y')
assert_array_equal(raw.annotations.onset,
[5., 124., 125., 135., 144., 145., 154.])
assert_array_equal(raw.annotations.duration,
[1.5, .5, .5, .5, .5, .5, .5])
assert_array_equal(raw.annotations.description,
['y', 'x', 'x', 'x', 'x', 'x', 'x'])
# These three things should be equivalent
expected_orig_time = (raw.info['meas_date'][0] +
raw.info['meas_date'][1] / 1000000)
for empty_annot in (
Annotations([], [], [], expected_orig_time),
Annotations([], [], [], None),
None):
raw.set_annotations(empty_annot)
assert isinstance(raw.annotations, Annotations)
assert len(raw.annotations) == 0
assert raw.annotations.orig_time == expected_orig_time
示例9: test_basics
# 需要导入模块: from mne.io import RawArray [as 别名]
# 或者: from mne.io.RawArray import set_annotations [as 别名]
def test_basics():
"""Test annotation class."""
raw = read_raw_fif(fif_fname)
assert raw.annotations is not None # XXX to be fixed in #5416
assert len(raw.annotations.onset) == 0 # XXX to be fixed in #5416
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_time)
offset = orig_time - _handle_meas_date(raw2.info['meas_date'])
annot = Annotations(onset, duration, description, orig_time)
assert ' segments' in repr(annot)
raw2.set_annotations(annot)
assert_array_equal(raw2.annotations.onset, onset + offset)
assert id(raw2.annotations) != id(annot)
concatenate_raws([raw, raw2])
raw.annotations.delete(-1) # remove boundary annotations
raw.annotations.delete(-1)
assert_allclose(onset + offset + 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, 0)
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.set_annotations(ants)
raws.append(raw)
raw = RawArray(data.copy(), info)
raw.set_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, [124., 125., 134., 135.,
144., 145., 154.])
raw.annotations.delete(2)
assert_array_equal(raw.annotations.onset, [124., 125., 135., 144.,
145., 154.])
raw.annotations.append(5, 1.5, 'y')
assert_array_equal(raw.annotations.onset, [124., 125., 135., 144.,
145., 154., 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'])