本文整理汇总了Python中mne.source_space._compare_source_spaces函数的典型用法代码示例。如果您正苦于以下问题:Python _compare_source_spaces函数的具体用法?Python _compare_source_spaces怎么用?Python _compare_source_spaces使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了_compare_source_spaces函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_simulate_round_trip
def test_simulate_round_trip():
"""Test simulate_raw round trip calculations."""
# Check a diagonal round-trip
raw, src, stc, trans, sphere = _get_data()
raw.pick_types(meg=True, stim=True)
bem = read_bem_solution(bem_1_fname)
old_bem = bem.copy()
old_src = src.copy()
old_trans = trans.copy()
fwd = make_forward_solution(raw.info, trans, src, bem)
# no omissions
assert (sum(len(s['vertno']) for s in src) ==
sum(len(s['vertno']) for s in fwd['src']) ==
36)
# make sure things were not modified
assert (old_bem['surfs'][0]['coord_frame'] ==
bem['surfs'][0]['coord_frame'])
assert trans == old_trans
_compare_source_spaces(src, old_src)
data = np.eye(fwd['nsource'])
raw.crop(0, (len(data) - 1) / raw.info['sfreq'])
stc = SourceEstimate(data, [s['vertno'] for s in fwd['src']],
0, 1. / raw.info['sfreq'])
for use_cps in (False, True):
this_raw = simulate_raw(raw, stc, trans, src, bem, cov=None,
use_cps=use_cps)
this_raw.pick_types(meg=True, eeg=True)
assert (old_bem['surfs'][0]['coord_frame'] ==
bem['surfs'][0]['coord_frame'])
assert trans == old_trans
_compare_source_spaces(src, old_src)
this_fwd = convert_forward_solution(fwd, force_fixed=True,
use_cps=use_cps)
assert_allclose(this_raw[:][0], this_fwd['sol']['data'],
atol=1e-12, rtol=1e-6)
示例2: _compare_forwards
def _compare_forwards(fwd, fwd_py, n_sensors, n_src,
meg_rtol=1e-4, meg_atol=1e-9,
eeg_rtol=1e-3, eeg_atol=1e-3):
"""Helper to test forwards"""
# check source spaces
assert_equal(len(fwd['src']), len(fwd_py['src']))
_compare_source_spaces(fwd['src'], fwd_py['src'], mode='approx')
for surf_ori in [False, True]:
if surf_ori:
# use copy here to leave our originals unmodified
fwd = convert_forward_solution(fwd, surf_ori, copy=True)
fwd_py = convert_forward_solution(fwd, surf_ori, copy=True)
for key in ['nchan', 'source_nn', 'source_rr', 'source_ori',
'surf_ori', 'coord_frame', 'nsource']:
print(key)
assert_allclose(fwd_py[key], fwd[key], rtol=1e-4, atol=1e-7)
assert_allclose(fwd_py['mri_head_t']['trans'],
fwd['mri_head_t']['trans'], rtol=1e-5, atol=1e-8)
assert_equal(fwd_py['sol']['data'].shape, (n_sensors, n_src))
assert_equal(len(fwd['sol']['row_names']), n_sensors)
assert_equal(len(fwd_py['sol']['row_names']), n_sensors)
# check MEG
assert_allclose(fwd['sol']['data'][:306],
fwd_py['sol']['data'][:306],
rtol=meg_rtol, atol=meg_atol,
err_msg='MEG mismatch')
# check EEG
if fwd['sol']['data'].shape[0] > 306:
assert_allclose(fwd['sol']['data'][306:],
fwd_py['sol']['data'][306:],
rtol=eeg_rtol, atol=eeg_atol,
err_msg='EEG mismatch')
示例3: test_morph_source_spaces
def test_morph_source_spaces():
"""Test morphing of source spaces."""
src = read_source_spaces(fname_fs)
src_morph = read_source_spaces(fname_morph)
src_morph_py = morph_source_spaces(src, 'sample',
subjects_dir=subjects_dir)
_compare_source_spaces(src_morph, src_morph_py, mode='approx')
示例4: _compare_forwards
def _compare_forwards(fwd, fwd_py, n_sensors, n_src, meg_rtol=1e-4, meg_atol=1e-9, eeg_rtol=1e-3, eeg_atol=1e-3):
"""Helper to test forwards"""
# check source spaces
assert_equal(len(fwd["src"]), len(fwd_py["src"]))
_compare_source_spaces(fwd["src"], fwd_py["src"], mode="approx")
for surf_ori in [False, True]:
if surf_ori:
# use copy here to leave our originals unmodified
fwd = convert_forward_solution(fwd, surf_ori, copy=True)
fwd_py = convert_forward_solution(fwd, surf_ori, copy=True)
for key in ["nchan", "source_nn", "source_rr", "source_ori", "surf_ori", "coord_frame", "nsource"]:
print(key)
assert_allclose(fwd_py[key], fwd[key], rtol=1e-4, atol=1e-7)
assert_allclose(fwd_py["mri_head_t"]["trans"], fwd["mri_head_t"]["trans"], rtol=1e-5, atol=1e-8)
assert_equal(fwd_py["sol"]["data"].shape, (n_sensors, n_src))
assert_equal(len(fwd["sol"]["row_names"]), n_sensors)
assert_equal(len(fwd_py["sol"]["row_names"]), n_sensors)
# check MEG
assert_allclose(
fwd["sol"]["data"][:306], fwd_py["sol"]["data"][:306], rtol=meg_rtol, atol=meg_atol, err_msg="MEG mismatch"
)
# check EEG
if fwd["sol"]["data"].shape[0] > 306:
assert_allclose(
fwd["sol"]["data"][306:],
fwd_py["sol"]["data"][306:],
rtol=eeg_rtol,
atol=eeg_atol,
err_msg="EEG mismatch",
)
示例5: test_other_volume_source_spaces
def test_other_volume_source_spaces():
"""Test setting up other volume source spaces"""
# these are split off because they require the MNE tools, and
# Travis doesn't seem to like them
# let's try the spherical one (no bem or surf supplied)
tempdir = _TempDir()
temp_name = op.join(tempdir, 'temp-src.fif')
run_subprocess(['mne_volume_source_space',
'--grid', '7.0',
'--src', temp_name,
'--mri', fname_mri])
src = read_source_spaces(temp_name)
src_new = setup_volume_source_space('sample', temp_name, pos=7.0,
mri=fname_mri,
subjects_dir=subjects_dir)
_compare_source_spaces(src, src_new, mode='approx')
del src
del src_new
assert_raises(ValueError, setup_volume_source_space, 'sample', temp_name,
pos=7.0, sphere=[1., 1.], mri=fname_mri, # bad sphere
subjects_dir=subjects_dir)
# now without MRI argument, it should give an error when we try
# to read it
run_subprocess(['mne_volume_source_space',
'--grid', '7.0',
'--src', temp_name])
assert_raises(ValueError, read_source_spaces, temp_name)
示例6: test_source_space_from_label
def test_source_space_from_label():
"""Test generating a source space from volume label."""
tempdir = _TempDir()
aseg_fname = op.join(subjects_dir, 'sample', 'mri', 'aseg.mgz')
label_names = get_volume_labels_from_aseg(aseg_fname)
volume_label = label_names[int(np.random.rand() * len(label_names))]
# Test pos as dict
pos = dict()
pytest.raises(ValueError, setup_volume_source_space, 'sample', pos=pos,
volume_label=volume_label, mri=aseg_fname)
# Test no mri provided
pytest.raises(RuntimeError, setup_volume_source_space, 'sample', mri=None,
volume_label=volume_label)
# Test invalid volume label
pytest.raises(ValueError, setup_volume_source_space, 'sample',
volume_label='Hello World!', mri=aseg_fname)
src = setup_volume_source_space('sample', subjects_dir=subjects_dir,
volume_label=volume_label, mri=aseg_fname,
add_interpolator=False)
assert_equal(volume_label, src[0]['seg_name'])
# test reading and writing
out_name = op.join(tempdir, 'temp-src.fif')
write_source_spaces(out_name, src)
src_from_file = read_source_spaces(out_name)
_compare_source_spaces(src, src_from_file, mode='approx')
示例7: test_simulate_round_trip
def test_simulate_round_trip(raw_data):
"""Test simulate_raw round trip calculations."""
# Check a diagonal round-trip
raw, src, stc, trans, sphere = raw_data
raw.pick_types(meg=True, stim=True)
bem = read_bem_solution(bem_1_fname)
old_bem = bem.copy()
old_src = src.copy()
old_trans = trans.copy()
fwd = make_forward_solution(raw.info, trans, src, bem)
# no omissions
assert (sum(len(s['vertno']) for s in src) ==
sum(len(s['vertno']) for s in fwd['src']) ==
36)
# make sure things were not modified
assert (old_bem['surfs'][0]['coord_frame'] ==
bem['surfs'][0]['coord_frame'])
assert trans == old_trans
_compare_source_spaces(src, old_src)
data = np.eye(fwd['nsource'])
raw.crop(0, (len(data) - 1) / raw.info['sfreq'])
stc = SourceEstimate(data, [s['vertno'] for s in fwd['src']],
0, 1. / raw.info['sfreq'])
for use_fwd in (None, fwd):
if use_fwd is None:
use_trans, use_src, use_bem = trans, src, bem
else:
use_trans = use_src = use_bem = None
with pytest.deprecated_call():
this_raw = simulate_raw(raw, stc, use_trans, use_src, use_bem,
cov=None, forward=use_fwd)
this_raw.pick_types(meg=True, eeg=True)
assert (old_bem['surfs'][0]['coord_frame'] ==
bem['surfs'][0]['coord_frame'])
assert trans == old_trans
_compare_source_spaces(src, old_src)
this_fwd = convert_forward_solution(fwd, force_fixed=True)
assert_allclose(this_raw[:][0], this_fwd['sol']['data'],
atol=1e-12, rtol=1e-6)
with pytest.raises(ValueError, match='If forward is not None then'):
simulate_raw(raw.info, stc, trans, src, bem, forward=fwd)
# Not iterable
with pytest.raises(TypeError, match='SourceEstimate, tuple, or iterable'):
simulate_raw(raw.info, 0., trans, src, bem, None)
# STC with a source that `src` does not have
assert 0 not in src[0]['vertno']
vertices = [[0, fwd['src'][0]['vertno'][0]], []]
stc_bad = SourceEstimate(data[:2], vertices, 0, 1. / raw.info['sfreq'])
with pytest.warns(RuntimeWarning,
match='1 of 2 SourceEstimate vertices'):
simulate_raw(raw.info, stc_bad, trans, src, bem)
assert 0 not in fwd['src'][0]['vertno']
with pytest.warns(RuntimeWarning,
match='1 of 2 SourceEstimate vertices'):
simulate_raw(raw.info, stc_bad, None, None, None, forward=fwd)
# dev_head_t mismatch
fwd['info']['dev_head_t']['trans'][0, 0] = 1.
with pytest.raises(ValueError, match='dev_head_t.*does not match'):
simulate_raw(raw.info, stc, None, None, None, forward=fwd)
示例8: test_volume_source_space
def test_volume_source_space():
"""Test setting up volume source spaces
"""
tempdir = _TempDir()
src = read_source_spaces(fname_vol)
temp_name = op.join(tempdir, 'temp-src.fif')
# The one in the sample dataset (uses bem as bounds)
src_new = setup_volume_source_space('sample', temp_name, pos=7.0,
bem=fname_bem, mri=fname_mri,
subjects_dir=subjects_dir)
_compare_source_spaces(src, src_new, mode='approx')
del src_new
src_new = read_source_spaces(temp_name)
_compare_source_spaces(src, src_new, mode='approx')
示例9: test_write_source_space
def test_write_source_space():
"""Test reading and writing of source spaces."""
tempdir = _TempDir()
src0 = read_source_spaces(fname, patch_stats=False)
write_source_spaces(op.join(tempdir, 'tmp-src.fif'), src0)
src1 = read_source_spaces(op.join(tempdir, 'tmp-src.fif'),
patch_stats=False)
_compare_source_spaces(src0, src1)
# test warnings on bad filenames
src_badname = op.join(tempdir, 'test-bad-name.fif.gz')
with pytest.warns(RuntimeWarning, match='-src.fif'):
write_source_spaces(src_badname, src0)
with pytest.warns(RuntimeWarning, match='-src.fif'):
read_source_spaces(src_badname)
示例10: test_write_source_space
def test_write_source_space():
"""Test reading and writing of source spaces
"""
tempdir = _TempDir()
src0 = read_source_spaces(fname, add_geom=False)
write_source_spaces(op.join(tempdir, 'tmp-src.fif'), src0)
src1 = read_source_spaces(op.join(tempdir, 'tmp-src.fif'), add_geom=False)
_compare_source_spaces(src0, src1)
# test warnings on bad filenames
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter('always')
src_badname = op.join(tempdir, 'test-bad-name.fif.gz')
write_source_spaces(src_badname, src0)
read_source_spaces(src_badname)
assert_equal(len(w), 2)
示例11: test_setup_source_space_spacing
def test_setup_source_space_spacing(tmpdir, spacing):
"""Test setting up surface source spaces using a given spacing."""
tempdir = str(tmpdir)
copytree(op.join(subjects_dir, 'sample'), op.join(tempdir, 'sample'))
args = [] if spacing == 7 else ['--spacing', str(spacing)]
with modified_env(SUBJECTS_DIR=tempdir, SUBJECT='sample'):
run_subprocess(['mne_setup_source_space'] + args)
src = read_source_spaces(op.join(tempdir, 'sample', 'bem',
'sample-%d-src.fif' % spacing))
src_new = setup_source_space('sample', spacing=spacing, add_dist=False,
subjects_dir=subjects_dir)
_compare_source_spaces(src, src_new, mode='approx', nearest=True)
# Degenerate conditions
with pytest.raises(TypeError, match='spacing must be.*got.*float.*'):
setup_source_space('sample', 7., subjects_dir=subjects_dir)
with pytest.raises(ValueError, match='spacing must be >= 2, got 1'):
setup_source_space('sample', 1, subjects_dir=subjects_dir)
示例12: _compare_forwards
def _compare_forwards(fwd, fwd_py, n_sensors, n_src,
meg_rtol=1e-4, meg_atol=1e-9,
eeg_rtol=1e-3, eeg_atol=1e-3):
"""Test forwards."""
# check source spaces
assert_equal(len(fwd['src']), len(fwd_py['src']))
_compare_source_spaces(fwd['src'], fwd_py['src'], mode='approx')
for surf_ori, force_fixed in product([False, True], [False, True]):
# use copy here to leave our originals unmodified
fwd = convert_forward_solution(fwd, surf_ori, force_fixed, copy=True,
use_cps=True)
fwd_py = convert_forward_solution(fwd_py, surf_ori, force_fixed,
copy=True, use_cps=True)
check_src = n_src // 3 if force_fixed else n_src
for key in ('nchan', 'source_rr', 'source_ori',
'surf_ori', 'coord_frame', 'nsource'):
assert_allclose(fwd_py[key], fwd[key], rtol=1e-4, atol=1e-7,
err_msg=key)
# In surf_ori=True only Z matters for source_nn
if surf_ori and not force_fixed:
ori_sl = slice(2, None, 3)
else:
ori_sl = slice(None)
assert_allclose(fwd_py['source_nn'][ori_sl], fwd['source_nn'][ori_sl],
rtol=1e-4, atol=1e-6)
assert_allclose(fwd_py['mri_head_t']['trans'],
fwd['mri_head_t']['trans'], rtol=1e-5, atol=1e-8)
assert_equal(fwd_py['sol']['data'].shape, (n_sensors, check_src))
assert_equal(len(fwd['sol']['row_names']), n_sensors)
assert_equal(len(fwd_py['sol']['row_names']), n_sensors)
# check MEG
assert_allclose(fwd['sol']['data'][:306, ori_sl],
fwd_py['sol']['data'][:306, ori_sl],
rtol=meg_rtol, atol=meg_atol,
err_msg='MEG mismatch')
# check EEG
if fwd['sol']['data'].shape[0] > 306:
assert_allclose(fwd['sol']['data'][306:, ori_sl],
fwd_py['sol']['data'][306:, ori_sl],
rtol=eeg_rtol, atol=eeg_atol,
err_msg='EEG mismatch')
示例13: _compare_forwards
def _compare_forwards(fwd, fwd_py, n_sensors, n_src, meg_rtol=1e-4, meg_atol=1e-9, eeg_rtol=1e-3, eeg_atol=1e-3):
"""Helper to test forwards"""
# check source spaces
assert_equal(len(fwd["src"]), len(fwd_py["src"]))
_compare_source_spaces(fwd["src"], fwd_py["src"], mode="approx")
for surf_ori, force_fixed in product([False, True], [False, True]):
# use copy here to leave our originals unmodified
fwd = convert_forward_solution(fwd, surf_ori, force_fixed, copy=True)
fwd_py = convert_forward_solution(fwd_py, surf_ori, force_fixed, copy=True)
check_src = n_src // 3 if force_fixed else n_src
for key in ("nchan", "source_rr", "source_ori", "surf_ori", "coord_frame", "nsource"):
assert_allclose(fwd_py[key], fwd[key], rtol=1e-4, atol=1e-7, err_msg=key)
# In surf_ori=True only Z matters for source_nn
if surf_ori and not force_fixed:
ori_sl = slice(2, None, 3)
else:
ori_sl = slice(None)
assert_allclose(fwd_py["source_nn"][ori_sl], fwd["source_nn"][ori_sl], rtol=1e-4, atol=1e-6)
assert_allclose(fwd_py["mri_head_t"]["trans"], fwd["mri_head_t"]["trans"], rtol=1e-5, atol=1e-8)
assert_equal(fwd_py["sol"]["data"].shape, (n_sensors, check_src))
assert_equal(len(fwd["sol"]["row_names"]), n_sensors)
assert_equal(len(fwd_py["sol"]["row_names"]), n_sensors)
# check MEG
assert_allclose(
fwd["sol"]["data"][:306, ori_sl],
fwd_py["sol"]["data"][:306, ori_sl],
rtol=meg_rtol,
atol=meg_atol,
err_msg="MEG mismatch",
)
# check EEG
if fwd["sol"]["data"].shape[0] > 306:
assert_allclose(
fwd["sol"]["data"][306:, ori_sl],
fwd_py["sol"]["data"][306:, ori_sl],
rtol=eeg_rtol,
atol=eeg_atol,
err_msg="EEG mismatch",
)
示例14: test_other_volume_source_spaces
def test_other_volume_source_spaces():
"""Test setting up other volume source spaces."""
# these are split off because they require the MNE tools, and
# Travis doesn't seem to like them
# let's try the spherical one (no bem or surf supplied)
tempdir = _TempDir()
temp_name = op.join(tempdir, 'temp-src.fif')
run_subprocess(['mne_volume_source_space',
'--grid', '7.0',
'--src', temp_name,
'--mri', fname_mri])
src = read_source_spaces(temp_name)
src_new = setup_volume_source_space(None, pos=7.0, mri=fname_mri,
subjects_dir=subjects_dir)
# we use a more accurate elimination criteria, so let's fix the MNE-C
# source space
assert len(src_new[0]['vertno']) == 7497
assert len(src) == 1
assert len(src_new) == 1
good_mask = np.in1d(src[0]['vertno'], src_new[0]['vertno'])
src[0]['inuse'][src[0]['vertno'][~good_mask]] = 0
assert src[0]['inuse'].sum() == 7497
src[0]['vertno'] = src[0]['vertno'][good_mask]
assert len(src[0]['vertno']) == 7497
src[0]['nuse'] = len(src[0]['vertno'])
assert src[0]['nuse'] == 7497
_compare_source_spaces(src_new, src, mode='approx')
assert 'volume, shape' in repr(src)
del src
del src_new
pytest.raises(ValueError, setup_volume_source_space, 'sample', pos=7.0,
sphere=[1., 1.], mri=fname_mri, # bad sphere
subjects_dir=subjects_dir)
# now without MRI argument, it should give an error when we try
# to read it
run_subprocess(['mne_volume_source_space',
'--grid', '7.0',
'--src', temp_name])
pytest.raises(ValueError, read_source_spaces, temp_name)
示例15: test_volume_source_space
def test_volume_source_space():
"""Test setting up volume source spaces
"""
tempdir = _TempDir()
src = read_source_spaces(fname_vol)
temp_name = op.join(tempdir, 'temp-src.fif')
surf = read_bem_surfaces(fname_bem, s_id=FIFF.FIFFV_BEM_SURF_ID_BRAIN)
surf['rr'] *= 1e3 # convert to mm
# The one in the testing dataset (uses bem as bounds)
for bem, surf in zip((fname_bem, None), (None, surf)):
src_new = setup_volume_source_space('sample', temp_name, pos=7.0,
bem=bem, surface=surf,
mri=fname_mri,
subjects_dir=subjects_dir)
_compare_source_spaces(src, src_new, mode='approx')
del src_new
src_new = read_source_spaces(temp_name)
_compare_source_spaces(src, src_new, mode='approx')
assert_raises(IOError, setup_volume_source_space, 'sample', temp_name,
pos=7.0, bem=None, surface='foo', # bad surf
mri=fname_mri, subjects_dir=subjects_dir)