本文整理汇总了Python中mne.io.concatenate_raws函数的典型用法代码示例。如果您正苦于以下问题:Python concatenate_raws函数的具体用法?Python concatenate_raws怎么用?Python concatenate_raws使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了concatenate_raws函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_basics
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])
assert_and_remove_boundary_annot(raw)
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))
示例2: test_annotations
def test_annotations():
"""Test annotation class."""
raw = Raw(fif_fname)
onset = np.array(range(10))
duration = np.ones(10) + raw.first_samp
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
annot = Annotations(onset, duration, description, dt)
sfreq = raw.info['sfreq']
raw2 = raw.copy()
raw2.annotations = annot
concatenate_raws([raw, raw2])
assert_array_equal(annot.onset, raw.annotations.onset)
assert_array_equal(annot.duration, raw.annotations.duration)
raw2.annotations = Annotations(onset, duration * 2, description, None)
last_samp = raw.last_samp - 1
concatenate_raws([raw, raw2])
onsets = np.concatenate([onset,
onset + (last_samp - raw.first_samp) / sfreq])
assert_array_equal(raw.annotations.onset, onsets)
assert_array_equal(raw.annotations.onset[:10], onset)
assert_array_equal(raw.annotations.duration[:10], duration)
assert_array_equal(raw.annotations.duration[10:], duration * 2)
assert_array_equal(raw.annotations.description, np.repeat('test', 20))
示例3: test_resample
def test_resample():
"""Test resample (with I/O and multiple files)
"""
tempdir = _TempDir()
raw = Raw(fif_fname).crop(0, 3, False)
raw.preload_data()
raw_resamp = raw.copy()
sfreq = raw.info['sfreq']
# test parallel on upsample
raw_resamp.resample(sfreq * 2, n_jobs=2)
assert_equal(raw_resamp.n_times, len(raw_resamp.times))
raw_resamp.save(op.join(tempdir, 'raw_resamp-raw.fif'))
raw_resamp = Raw(op.join(tempdir, 'raw_resamp-raw.fif'), preload=True)
assert_equal(sfreq, raw_resamp.info['sfreq'] / 2)
assert_equal(raw.n_times, raw_resamp.n_times / 2)
assert_equal(raw_resamp._data.shape[1], raw_resamp.n_times)
assert_equal(raw._data.shape[0], raw_resamp._data.shape[0])
# test non-parallel on downsample
raw_resamp.resample(sfreq, n_jobs=1)
assert_equal(raw_resamp.info['sfreq'], sfreq)
assert_equal(raw._data.shape, raw_resamp._data.shape)
assert_equal(raw.first_samp, raw_resamp.first_samp)
assert_equal(raw.last_samp, raw.last_samp)
# upsampling then downsampling doubles resampling error, but this still
# works (hooray). Note that the stim channels had to be sub-sampled
# without filtering to be accurately preserved
# note we have to treat MEG and EEG+STIM channels differently (tols)
assert_allclose(raw._data[:306, 200:-200],
raw_resamp._data[:306, 200:-200],
rtol=1e-2, atol=1e-12)
assert_allclose(raw._data[306:, 200:-200],
raw_resamp._data[306:, 200:-200],
rtol=1e-2, atol=1e-7)
# now check multiple file support w/resampling, as order of operations
# (concat, resample) should not affect our data
raw1 = raw.copy()
raw2 = raw.copy()
raw3 = raw.copy()
raw4 = raw.copy()
raw1 = concatenate_raws([raw1, raw2])
raw1.resample(10)
raw3.resample(10)
raw4.resample(10)
raw3 = concatenate_raws([raw3, raw4])
assert_array_equal(raw1._data, raw3._data)
assert_array_equal(raw1._first_samps, raw3._first_samps)
assert_array_equal(raw1._last_samps, raw3._last_samps)
assert_array_equal(raw1._raw_lengths, raw3._raw_lengths)
assert_equal(raw1.first_samp, raw3.first_samp)
assert_equal(raw1.last_samp, raw3.last_samp)
assert_equal(raw1.info['sfreq'], raw3.info['sfreq'])
示例4: test_crop
def test_crop():
"""Test cropping raw files
"""
# split a concatenated file to test a difficult case
raw = Raw([fif_fname, fif_fname], preload=False)
split_size = 10. # in seconds
sfreq = raw.info['sfreq']
nsamp = (raw.last_samp - raw.first_samp + 1)
# do an annoying case (off-by-one splitting)
tmins = np.r_[1., np.round(np.arange(0., nsamp - 1, split_size * sfreq))]
tmins = np.sort(tmins)
tmaxs = np.concatenate((tmins[1:] - 1, [nsamp - 1]))
tmaxs /= sfreq
tmins /= sfreq
raws = [None] * len(tmins)
for ri, (tmin, tmax) in enumerate(zip(tmins, tmaxs)):
raws[ri] = raw.crop(tmin, tmax, True)
all_raw_2 = concatenate_raws(raws, preload=False)
assert_equal(raw.first_samp, all_raw_2.first_samp)
assert_equal(raw.last_samp, all_raw_2.last_samp)
assert_array_equal(raw[:, :][0], all_raw_2[:, :][0])
tmins = np.round(np.arange(0., nsamp - 1, split_size * sfreq))
tmaxs = np.concatenate((tmins[1:] - 1, [nsamp - 1]))
tmaxs /= sfreq
tmins /= sfreq
# going in revere order so the last fname is the first file (need it later)
raws = [None] * len(tmins)
for ri, (tmin, tmax) in enumerate(zip(tmins, tmaxs)):
raws[ri] = raw.copy()
raws[ri].crop(tmin, tmax, False)
# test concatenation of split file
all_raw_1 = concatenate_raws(raws, preload=False)
all_raw_2 = raw.crop(0, None, True)
for ar in [all_raw_1, all_raw_2]:
assert_equal(raw.first_samp, ar.first_samp)
assert_equal(raw.last_samp, ar.last_samp)
assert_array_equal(raw[:, :][0], ar[:, :][0])
# test shape consistency of cropped raw
data = np.zeros((1, 1002001))
info = create_info(1, 1000)
raw = RawArray(data, info)
for tmin in range(0, 1001, 100):
raw1 = raw.crop(tmin=tmin, tmax=tmin + 2, copy=True)
assert_equal(raw1[:][0].shape, (1, 2001))
示例5: test_annotations
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'])
示例6: test_crop
def test_crop():
"""Test cropping raw files
"""
# split a concatenated file to test a difficult case
raw = Raw([fif_fname, fif_fname], preload=False)
split_size = 10.0 # in seconds
sfreq = raw.info["sfreq"]
nsamp = raw.last_samp - raw.first_samp + 1
# do an annoying case (off-by-one splitting)
tmins = np.r_[1.0, np.round(np.arange(0.0, nsamp - 1, split_size * sfreq))]
tmins = np.sort(tmins)
tmaxs = np.concatenate((tmins[1:] - 1, [nsamp - 1]))
tmaxs /= sfreq
tmins /= sfreq
raws = [None] * len(tmins)
for ri, (tmin, tmax) in enumerate(zip(tmins, tmaxs)):
raws[ri] = raw.crop(tmin, tmax, True)
all_raw_2 = concatenate_raws(raws, preload=False)
assert_equal(raw.first_samp, all_raw_2.first_samp)
assert_equal(raw.last_samp, all_raw_2.last_samp)
assert_array_equal(raw[:, :][0], all_raw_2[:, :][0])
tmins = np.round(np.arange(0.0, nsamp - 1, split_size * sfreq))
tmaxs = np.concatenate((tmins[1:] - 1, [nsamp - 1]))
tmaxs /= sfreq
tmins /= sfreq
# going in revere order so the last fname is the first file (need it later)
raws = [None] * len(tmins)
for ri, (tmin, tmax) in enumerate(zip(tmins, tmaxs)):
raws[ri] = raw.copy()
raws[ri].crop(tmin, tmax, False)
# test concatenation of split file
all_raw_1 = concatenate_raws(raws, preload=False)
all_raw_2 = raw.crop(0, None, True)
for ar in [all_raw_1, all_raw_2]:
assert_equal(raw.first_samp, ar.first_samp)
assert_equal(raw.last_samp, ar.last_samp)
assert_array_equal(raw[:, :][0], ar[:, :][0])
示例7: test_annotation_epoching
def test_annotation_epoching():
"""Test that annotations work properly with concatenated edges."""
# Create data with just a DC component
data = np.ones((1, 1000))
info = create_info(1, 1000., 'eeg')
raw = concatenate_raws([RawArray(data, info) for ii in range(3)])
events = np.array([[a, 0, 1] for a in [0, 500, 1000, 1500, 2000]])
epochs = Epochs(raw, events, tmin=0, tmax=0.999, baseline=None,
preload=True) # 1000 samples long
assert_equal(len(epochs.drop_log), len(events))
assert_equal(len(epochs), 3)
assert_equal([0, 2, 4], epochs.selection)
示例8: test_annotation_filtering
def test_annotation_filtering():
"""Test that annotations work properly with filtering."""
# Create data with just a DC component
data = np.ones((1, 1000))
info = create_info(1, 1000., 'eeg')
raws = [RawArray(data * (ii + 1), info) for ii in range(4)]
kwargs_pass = dict(l_freq=None, h_freq=50., fir_design='firwin')
kwargs_stop = dict(l_freq=50., h_freq=None, fir_design='firwin')
# lowpass filter, which should not modify the data
raws_pass = [raw.copy().filter(**kwargs_pass) for raw in raws]
# highpass filter, which should zero it out
raws_stop = [raw.copy().filter(**kwargs_stop) for raw in raws]
# concat the original and the filtered segments
raws_concat = concatenate_raws([raw.copy() for raw in raws])
raws_zero = raws_concat.copy().apply_function(lambda x: x * 0)
raws_pass_concat = concatenate_raws(raws_pass)
raws_stop_concat = concatenate_raws(raws_stop)
# make sure we did something reasonable with our individual-file filtering
assert_allclose(raws_concat[0][0], raws_pass_concat[0][0], atol=1e-14)
assert_allclose(raws_zero[0][0], raws_stop_concat[0][0], atol=1e-14)
# ensure that our Annotations cut up the filtering properly
raws_concat_pass = raws_concat.copy().filter(skip_by_annotation='edge',
**kwargs_pass)
assert_allclose(raws_concat[0][0], raws_concat_pass[0][0], atol=1e-14)
raws_concat_stop = raws_concat.copy().filter(skip_by_annotation='edge',
**kwargs_stop)
assert_allclose(raws_zero[0][0], raws_concat_stop[0][0], atol=1e-14)
# one last test: let's cut out a section entirely:
# here the 1-3 second window should be skipped
raw = raws_concat.copy()
raw.annotations.append(1., 2., 'foo')
raw.filter(l_freq=50., h_freq=None, fir_design='firwin',
skip_by_annotation='foo')
# our filter will zero out anything not skipped:
mask = np.concatenate((np.zeros(1000), np.ones(2000), np.zeros(1000)))
expected_data = raws_concat[0][0][0] * mask
assert_allclose(raw[0][0][0], expected_data, atol=1e-14)
示例9: test_raw_array_orig_times
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
示例10: test_resample
def test_resample():
"""Test resample (with I/O and multiple files)
"""
tempdir = _TempDir()
raw = Raw(fif_fname).crop(0, 3, False)
raw.load_data()
raw_resamp = raw.copy()
sfreq = raw.info['sfreq']
# test parallel on upsample
raw_resamp.resample(sfreq * 2, n_jobs=2, npad='auto')
assert_equal(raw_resamp.n_times, len(raw_resamp.times))
raw_resamp.save(op.join(tempdir, 'raw_resamp-raw.fif'))
raw_resamp = Raw(op.join(tempdir, 'raw_resamp-raw.fif'), preload=True)
assert_equal(sfreq, raw_resamp.info['sfreq'] / 2)
assert_equal(raw.n_times, raw_resamp.n_times / 2)
assert_equal(raw_resamp._data.shape[1], raw_resamp.n_times)
assert_equal(raw._data.shape[0], raw_resamp._data.shape[0])
# test non-parallel on downsample
raw_resamp.resample(sfreq, n_jobs=1, npad='auto')
assert_equal(raw_resamp.info['sfreq'], sfreq)
assert_equal(raw._data.shape, raw_resamp._data.shape)
assert_equal(raw.first_samp, raw_resamp.first_samp)
assert_equal(raw.last_samp, raw.last_samp)
# upsampling then downsampling doubles resampling error, but this still
# works (hooray). Note that the stim channels had to be sub-sampled
# without filtering to be accurately preserved
# note we have to treat MEG and EEG+STIM channels differently (tols)
assert_allclose(raw._data[:306, 200:-200],
raw_resamp._data[:306, 200:-200],
rtol=1e-2, atol=1e-12)
assert_allclose(raw._data[306:, 200:-200],
raw_resamp._data[306:, 200:-200],
rtol=1e-2, atol=1e-7)
# now check multiple file support w/resampling, as order of operations
# (concat, resample) should not affect our data
raw1 = raw.copy()
raw2 = raw.copy()
raw3 = raw.copy()
raw4 = raw.copy()
raw1 = concatenate_raws([raw1, raw2])
raw1.resample(10., npad='auto')
raw3.resample(10., npad='auto')
raw4.resample(10., npad='auto')
raw3 = concatenate_raws([raw3, raw4])
assert_array_equal(raw1._data, raw3._data)
assert_array_equal(raw1._first_samps, raw3._first_samps)
assert_array_equal(raw1._last_samps, raw3._last_samps)
assert_array_equal(raw1._raw_lengths, raw3._raw_lengths)
assert_equal(raw1.first_samp, raw3.first_samp)
assert_equal(raw1.last_samp, raw3.last_samp)
assert_equal(raw1.info['sfreq'], raw3.info['sfreq'])
# test resampling of stim channel
# basic decimation
stim = [1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0]
raw = RawArray([stim], create_info(1, len(stim), ['stim']))
assert_allclose(raw.resample(8., npad='auto')._data,
[[1, 1, 0, 0, 1, 1, 0, 0]])
# decimation of multiple stim channels
raw = RawArray(2 * [stim], create_info(2, len(stim), 2 * ['stim']))
assert_allclose(raw.resample(8., npad='auto')._data,
[[1, 1, 0, 0, 1, 1, 0, 0],
[1, 1, 0, 0, 1, 1, 0, 0]])
# decimation that could potentially drop events if the decimation is
# done naively
stim = [0, 0, 0, 1, 1, 0, 0, 0]
raw = RawArray([stim], create_info(1, len(stim), ['stim']))
assert_allclose(raw.resample(4., npad='auto')._data,
[[0, 1, 1, 0]])
# two events are merged in this case (warning)
stim = [0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0]
raw = RawArray([stim], create_info(1, len(stim), ['stim']))
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
raw.resample(8., npad='auto')
assert_true(len(w) == 1)
# events are dropped in this case (warning)
stim = [0, 1, 1, 0, 0, 1, 1, 0]
raw = RawArray([stim], create_info(1, len(stim), ['stim']))
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
raw.resample(4., npad='auto')
assert_true(len(w) == 1)
# test resampling events: this should no longer give a warning
stim = [0, 1, 1, 0, 0, 1, 1, 0]
raw = RawArray([stim], create_info(1, len(stim), ['stim']))
events = find_events(raw)
raw, events = raw.resample(4., events=events, npad='auto')
assert_equal(events, np.array([[0, 0, 1], [2, 0, 1]]))
# test copy flag
stim = [1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0]
raw = RawArray([stim], create_info(1, len(stim), ['stim']))
#.........这里部分代码省略.........
示例11: test_multiple_files
def test_multiple_files():
"""Test loading multiple files simultaneously
"""
# split file
tempdir = _TempDir()
raw = Raw(fif_fname).crop(0, 10, False)
raw.load_data()
raw.load_data() # test no operation
split_size = 3. # in seconds
sfreq = raw.info['sfreq']
nsamp = (raw.last_samp - raw.first_samp)
tmins = np.round(np.arange(0., nsamp, split_size * sfreq))
tmaxs = np.concatenate((tmins[1:] - 1, [nsamp]))
tmaxs /= sfreq
tmins /= sfreq
assert_equal(raw.n_times, len(raw.times))
# going in reverse order so the last fname is the first file (need later)
raws = [None] * len(tmins)
for ri in range(len(tmins) - 1, -1, -1):
fname = op.join(tempdir, 'test_raw_split-%d_raw.fif' % ri)
raw.save(fname, tmin=tmins[ri], tmax=tmaxs[ri])
raws[ri] = Raw(fname)
events = [find_events(r, stim_channel='STI 014') for r in raws]
last_samps = [r.last_samp for r in raws]
first_samps = [r.first_samp for r in raws]
# test concatenation of split file
assert_raises(ValueError, concatenate_raws, raws, True, events[1:])
all_raw_1, events1 = concatenate_raws(raws, preload=False,
events_list=events)
assert_equal(raw.first_samp, all_raw_1.first_samp)
assert_equal(raw.last_samp, all_raw_1.last_samp)
assert_allclose(raw[:, :][0], all_raw_1[:, :][0])
raws[0] = Raw(fname)
all_raw_2 = concatenate_raws(raws, preload=True)
assert_allclose(raw[:, :][0], all_raw_2[:, :][0])
# test proper event treatment for split files
events2 = concatenate_events(events, first_samps, last_samps)
events3 = find_events(all_raw_2, stim_channel='STI 014')
assert_array_equal(events1, events2)
assert_array_equal(events1, events3)
# test various methods of combining files
raw = Raw(fif_fname, preload=True)
n_times = raw.n_times
# make sure that all our data match
times = list(range(0, 2 * n_times, 999))
# add potentially problematic points
times.extend([n_times - 1, n_times, 2 * n_times - 1])
raw_combo0 = Raw([fif_fname, fif_fname], preload=True)
_compare_combo(raw, raw_combo0, times, n_times)
raw_combo = Raw([fif_fname, fif_fname], preload=False)
_compare_combo(raw, raw_combo, times, n_times)
raw_combo = Raw([fif_fname, fif_fname], preload='memmap8.dat')
_compare_combo(raw, raw_combo, times, n_times)
assert_raises(ValueError, Raw, [fif_fname, ctf_fname])
assert_raises(ValueError, Raw, [fif_fname, fif_bad_marked_fname])
assert_equal(raw[:, :][0].shape[1] * 2, raw_combo0[:, :][0].shape[1])
assert_equal(raw_combo0[:, :][0].shape[1], raw_combo0.n_times)
# with all data preloaded, result should be preloaded
raw_combo = Raw(fif_fname, preload=True)
raw_combo.append(Raw(fif_fname, preload=True))
assert_true(raw_combo.preload is True)
assert_equal(raw_combo.n_times, raw_combo._data.shape[1])
_compare_combo(raw, raw_combo, times, n_times)
# with any data not preloaded, don't set result as preloaded
raw_combo = concatenate_raws([Raw(fif_fname, preload=True),
Raw(fif_fname, preload=False)])
assert_true(raw_combo.preload is False)
assert_array_equal(find_events(raw_combo, stim_channel='STI 014'),
find_events(raw_combo0, stim_channel='STI 014'))
_compare_combo(raw, raw_combo, times, n_times)
# user should be able to force data to be preloaded upon concat
raw_combo = concatenate_raws([Raw(fif_fname, preload=False),
Raw(fif_fname, preload=True)],
preload=True)
assert_true(raw_combo.preload is True)
_compare_combo(raw, raw_combo, times, n_times)
raw_combo = concatenate_raws([Raw(fif_fname, preload=False),
Raw(fif_fname, preload=True)],
preload='memmap3.dat')
_compare_combo(raw, raw_combo, times, n_times)
raw_combo = concatenate_raws([Raw(fif_fname, preload=True),
Raw(fif_fname, preload=True)],
preload='memmap4.dat')
_compare_combo(raw, raw_combo, times, n_times)
raw_combo = concatenate_raws([Raw(fif_fname, preload=False),
Raw(fif_fname, preload=False)],
preload='memmap5.dat')
_compare_combo(raw, raw_combo, times, n_times)
#.........这里部分代码省略.........
示例12: print
from mne.decoding import CSP
print(__doc__)
# #############################################################################
# # Set parameters and read data
# avoid classification of evoked responses by using epochs that start 1s after
# cue onset.
tmin, tmax = -1., 4.
event_id = dict(hands=2, feet=3)
subject = 1
runs = [6, 10, 14] # motor imagery: hands vs feet
raw_fnames = eegbci.load_data(subject, runs)
raw = concatenate_raws([read_raw_edf(f, preload=True) for f in raw_fnames])
# strip channel names of "." characters
raw.rename_channels(lambda x: x.strip('.'))
# Apply band-pass filter
raw.filter(7., 30., fir_design='firwin', skip_by_annotation='edge')
events, _ = events_from_annotations(raw, event_id=dict(T1=2, T2=3))
picks = pick_types(raw.info, meg=False, eeg=True, stim=False, eog=False,
exclude='bads')
# Read epochs (train will be done only between 1 and 2s)
# Testing will be done with a running classifier
epochs = Epochs(raw, events, event_id, tmin, tmax, proj=True, picks=picks,
示例13: test_basics
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'])
示例14: test_annotation_filtering
def test_annotation_filtering():
"""Test that annotations work properly with filtering."""
# Create data with just a DC component
data = np.ones((1, 1000))
info = create_info(1, 1000., 'eeg')
raws = [RawArray(data * (ii + 1), info) for ii in range(4)]
kwargs_pass = dict(l_freq=None, h_freq=50., fir_design='firwin')
kwargs_stop = dict(l_freq=50., h_freq=None, fir_design='firwin')
# lowpass filter, which should not modify the data
raws_pass = [raw.copy().filter(**kwargs_pass) for raw in raws]
# highpass filter, which should zero it out
raws_stop = [raw.copy().filter(**kwargs_stop) for raw in raws]
# concat the original and the filtered segments
raws_concat = concatenate_raws([raw.copy() for raw in raws])
raws_zero = raws_concat.copy().apply_function(lambda x: x * 0)
raws_pass_concat = concatenate_raws(raws_pass)
raws_stop_concat = concatenate_raws(raws_stop)
# make sure we did something reasonable with our individual-file filtering
assert_allclose(raws_concat[0][0], raws_pass_concat[0][0], atol=1e-14)
assert_allclose(raws_zero[0][0], raws_stop_concat[0][0], atol=1e-14)
# ensure that our Annotations cut up the filtering properly
raws_concat_pass = raws_concat.copy().filter(skip_by_annotation='edge',
**kwargs_pass)
assert_allclose(raws_concat[0][0], raws_concat_pass[0][0], atol=1e-14)
raws_concat_stop = raws_concat.copy().filter(skip_by_annotation='edge',
**kwargs_stop)
assert_allclose(raws_zero[0][0], raws_concat_stop[0][0], atol=1e-14)
# one last test: let's cut out a section entirely:
# here the 1-3 second window should be skipped
raw = raws_concat.copy()
raw.annotations.append(1., 2., 'foo')
with catch_logging() as log:
raw.filter(l_freq=50., h_freq=None, fir_design='firwin',
skip_by_annotation='foo', verbose='info')
log = log.getvalue()
assert '2 contiguous segments' in log
raw.annotations.append(2., 1., 'foo') # shouldn't change anything
with catch_logging() as log:
raw.filter(l_freq=50., h_freq=None, fir_design='firwin',
skip_by_annotation='foo', verbose='info')
log = log.getvalue()
assert '2 contiguous segments' in log
# our filter will zero out anything not skipped:
mask = np.concatenate((np.zeros(1000), np.ones(2000), np.zeros(1000)))
expected_data = raws_concat[0][0][0] * mask
assert_allclose(raw[0][0][0], expected_data, atol=1e-14)
# Let's try another one
raw = raws[0].copy()
raw.set_annotations(Annotations([0.], [0.5], ['BAD_ACQ_SKIP']))
my_data, times = raw.get_data(reject_by_annotation='omit',
return_times=True)
assert_allclose(times, raw.times[500:])
assert my_data.shape == (1, 500)
raw_filt = raw.copy().filter(skip_by_annotation='bad_acq_skip',
**kwargs_stop)
expected = data.copy()
expected[:, 500:] = 0
assert_allclose(raw_filt[:][0], expected, atol=1e-14)
raw = raws[0].copy()
raw.set_annotations(Annotations([0.5], [0.5], ['BAD_ACQ_SKIP']))
my_data, times = raw.get_data(reject_by_annotation='omit',
return_times=True)
assert_allclose(times, raw.times[:500])
assert my_data.shape == (1, 500)
raw_filt = raw.copy().filter(skip_by_annotation='bad_acq_skip',
**kwargs_stop)
expected = data.copy()
expected[:, :500] = 0
assert_allclose(raw_filt[:][0], expected, atol=1e-14)
示例15: test_events_from_annot_in_raw_objects
def test_events_from_annot_in_raw_objects():
"""Test basic functionality of events_fron_annot for raw objects."""
raw = read_raw_fif(fif_fname)
events = mne.find_events(raw)
event_id = {
'Auditory/Left': 1,
'Auditory/Right': 2,
'Visual/Left': 3,
'Visual/Right': 4,
'Visual/Smiley': 32,
'Motor/Button': 5
}
event_map = {v: k for k, v in event_id.items()}
annot = Annotations(onset=raw.times[events[:, 0] - raw.first_samp],
duration=np.zeros(len(events)),
description=[event_map[vv] for vv in events[:, 2]],
orig_time=None)
raw.set_annotations(annot)
events2, event_id2 = \
events_from_annotations(raw, event_id=event_id, regexp=None)
assert_array_equal(events, events2)
assert_equal(event_id, event_id2)
events3, event_id3 = \
events_from_annotations(raw, event_id=None, regexp=None)
assert_array_equal(events[:, 0], events3[:, 0])
assert set(event_id.keys()) == set(event_id3.keys())
# ensure that these actually got sorted properly
expected_event_id = {
desc: idx + 1 for idx, desc in enumerate(sorted(event_id.keys()))}
assert event_id3 == expected_event_id
first = np.unique(events3[:, 2])
second = np.arange(1, len(event_id) + 1, 1).astype(first.dtype)
assert_array_equal(first, second)
first = np.unique(list(event_id3.values()))
second = np.arange(1, len(event_id) + 1, 1).astype(first.dtype)
assert_array_equal(first, second)
events4, event_id4 =\
events_from_annotations(raw, event_id=None, regexp='.*Left')
expected_event_id4 = {k: v for k, v in event_id.items() if 'Left' in k}
assert_equal(event_id4.keys(), expected_event_id4.keys())
expected_events4 = events[(events[:, 2] == 1) | (events[:, 2] == 3)]
assert_array_equal(expected_events4[:, 0], events4[:, 0])
events5, event_id5 = \
events_from_annotations(raw, event_id=event_id, regexp='.*Left')
expected_event_id5 = {k: v for k, v in event_id.items() if 'Left' in k}
assert_equal(event_id5, expected_event_id5)
expected_events5 = events[(events[:, 2] == 1) | (events[:, 2] == 3)]
assert_array_equal(expected_events5, events5)
with pytest.raises(ValueError, match='not find any of the events'):
events_from_annotations(raw, regexp='not_there')
with pytest.raises(ValueError, match='Invalid input event_id'):
events_from_annotations(raw, event_id='wrong')
# concat does not introduce BAD or EDGE
raw_concat = concatenate_raws([raw.copy(), raw.copy()])
_, event_id = events_from_annotations(raw_concat)
assert isinstance(event_id, dict)
assert len(event_id) > 0
for kind in ('BAD', 'EDGE'):
assert '%s boundary' % kind in raw_concat.annotations.description
for key in event_id.keys():
assert kind not in key
# remove all events
raw.set_annotations(None)
events7, _ = events_from_annotations(raw)
assert_array_equal(events7, np.empty((0, 3), dtype=int))