本文整理汇总了Python中mne.read_source_spaces函数的典型用法代码示例。如果您正苦于以下问题:Python read_source_spaces函数的具体用法?Python read_source_spaces怎么用?Python read_source_spaces使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了read_source_spaces函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_volume_source_space
def test_volume_source_space():
"""Test setting up volume source spaces
"""
fname_vol = op.join(data_path, 'subjects', 'sample', 'bem',
'volume-7mm-src.fif')
src = read_source_spaces(fname_vol)
temp_name = op.join(tempdir, 'temp-src.fif')
try:
# 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')
src_new = read_source_spaces(temp_name)
_compare_source_spaces(src, src_new, mode='approx')
# let's try the spherical one (no bem or surf supplied)
run_subprocess(['mne_volume_source_space',
'--grid', '15.0',
'--src', temp_name,
'--mri', fname_mri])
src = read_source_spaces(temp_name)
src_new = setup_volume_source_space('sample', temp_name, pos=15.0,
mri=fname_mri,
subjects_dir=subjects_dir)
_compare_source_spaces(src, src_new, mode='approx')
finally:
if op.isfile(temp_name):
os.remove(temp_name)
示例2: test_write_source_space
def test_write_source_space():
"""Test writing and reading of source spaces
"""
src0 = read_source_spaces(fname, add_geom=False)
src0_old = read_source_spaces(fname, add_geom=False)
write_source_spaces(op.join(tempdir, 'tmp.fif'), src0)
src1 = read_source_spaces(op.join(tempdir, 'tmp.fif'), add_geom=False)
for orig in [src0, src0_old]:
for s0, s1 in zip(src0, src1):
for name in ['nuse', 'dist_limit', 'ntri', 'np', 'type', 'id',
'subject_his_id']:
assert_true(s0[name] == s1[name])
for name in ['nn', 'rr', 'inuse', 'vertno', 'nuse_tri',
'coord_frame', 'use_tris', 'tris', 'nearest',
'nearest_dist']:
assert_array_equal(s0[name], s1[name])
for name in ['dist']:
if s0[name] is not None:
assert_true(s1[name].shape == s0[name].shape)
assert_true(len((s0['dist'] - s1['dist']).data) == 0)
for name in ['pinfo']:
if s0[name] is not None:
assert_true(len(s0[name]) == len(s1[name]))
for p1, p2 in zip(s0[name], s1[name]):
assert_true(all(p1 == p2))
# The above "if s0[name] is not None" can be removed once the sample
# dataset is updated to have a source space with distance info
for name in ['working_dir', 'command_line']:
assert_true(src0.info[name] == src1.info[name])
示例3: test_setup_source_space
def test_setup_source_space():
"""Test setting up ico, oct, and all source spaces
"""
fname_all = op.join(data_path, "subjects", "sample", "bem", "sample-all-src.fif")
fname_ico = op.join(data_path, "subjects", "fsaverage", "bem", "fsaverage-ico-5-src.fif")
# first lets test some input params
assert_raises(ValueError, setup_source_space, "sample", spacing="oct")
assert_raises(ValueError, setup_source_space, "sample", spacing="octo")
assert_raises(ValueError, setup_source_space, "sample", spacing="oct6e")
assert_raises(ValueError, setup_source_space, "sample", spacing="7emm")
assert_raises(ValueError, setup_source_space, "sample", spacing="alls")
assert_raises(IOError, setup_source_space, "sample", spacing="oct6", subjects_dir=subjects_dir)
# ico 5 (fsaverage) - write to temp file
src = read_source_spaces(fname_ico)
temp_name = op.join(tempdir, "temp-src.fif")
with warnings.catch_warnings(record=True): # sklearn equiv neighbors
src_new = setup_source_space("fsaverage", temp_name, spacing="ico5", subjects_dir=subjects_dir)
_compare_source_spaces(src, src_new, mode="approx")
# oct-6 (sample) - auto filename + IO
src = read_source_spaces(fname)
temp_name = op.join(tempdir, "temp-src.fif")
with warnings.catch_warnings(record=True): # sklearn equiv neighbors
src_new = setup_source_space("sample", temp_name, spacing="oct6", subjects_dir=subjects_dir, overwrite=True)
_compare_source_spaces(src, src_new, mode="approx")
src_new = read_source_spaces(temp_name)
_compare_source_spaces(src, src_new, mode="approx")
# all source points - no file writing
src = read_source_spaces(fname_all)
src_new = setup_source_space("sample", None, spacing="all", subjects_dir=subjects_dir)
_compare_source_spaces(src, src_new, mode="approx")
示例4: test_add_source_space_distances_limited
def test_add_source_space_distances_limited():
"""Test adding distances to source space with a dist_limit."""
tempdir = _TempDir()
src = read_source_spaces(fname)
src_new = read_source_spaces(fname)
del src_new[0]['dist']
del src_new[1]['dist']
n_do = 200 # limit this for speed
src_new[0]['vertno'] = src_new[0]['vertno'][:n_do].copy()
src_new[1]['vertno'] = src_new[1]['vertno'][:n_do].copy()
out_name = op.join(tempdir, 'temp-src.fif')
try:
add_source_space_distances(src_new, dist_limit=0.007)
except RuntimeError: # what we throw when scipy version is wrong
raise SkipTest('dist_limit requires scipy > 0.13')
write_source_spaces(out_name, src_new)
src_new = read_source_spaces(out_name)
for so, sn in zip(src, src_new):
assert_array_equal(so['dist_limit'], np.array([-0.007], np.float32))
assert_array_equal(sn['dist_limit'], np.array([0.007], np.float32))
do = so['dist']
dn = sn['dist']
# clean out distances > 0.007 in C code
do.data[do.data > 0.007] = 0
do.eliminate_zeros()
# make sure we have some comparable distances
assert np.sum(do.data < 0.007) > 400
# do comparison over the region computed
d = (do - dn)[:sn['vertno'][n_do - 1]][:, :sn['vertno'][n_do - 1]]
assert_allclose(np.zeros_like(d.data), d.data, rtol=0, atol=1e-6)
示例5: test_discrete_source_space
def test_discrete_source_space():
"""Test setting up (and reading/writing) discrete source spaces
"""
src = read_source_spaces(fname)
v = src[0]["vertno"]
# let's make a discrete version with the C code, and with ours
temp_name = op.join(tempdir, "temp-src.fif")
try:
# save
temp_pos = op.join(tempdir, "temp-pos.txt")
np.savetxt(temp_pos, np.c_[src[0]["rr"][v], src[0]["nn"][v]])
# let's try the spherical one (no bem or surf supplied)
run_subprocess(["mne_volume_source_space", "--meters", "--pos", temp_pos, "--src", temp_name])
src_c = read_source_spaces(temp_name)
pos_dict = dict(rr=src[0]["rr"][v], nn=src[0]["nn"][v])
src_new = setup_volume_source_space("sample", None, pos=pos_dict, subjects_dir=subjects_dir)
_compare_source_spaces(src_c, src_new, mode="approx")
assert_allclose(src[0]["rr"][v], src_new[0]["rr"], rtol=1e-3, atol=1e-6)
assert_allclose(src[0]["nn"][v], src_new[0]["nn"], rtol=1e-3, atol=1e-6)
# now do writing
write_source_spaces(temp_name, src_c)
src_c2 = read_source_spaces(temp_name)
_compare_source_spaces(src_c, src_c2)
# now do MRI
assert_raises(ValueError, setup_volume_source_space, "sample", pos=pos_dict, mri=fname_mri)
finally:
if op.isfile(temp_name):
os.remove(temp_name)
示例6: test_discrete_source_space
def test_discrete_source_space():
"""Test setting up (and reading/writing) discrete source spaces
"""
src = read_source_spaces(fname)
v = src[0]['vertno']
# let's make a discrete version with the C code, and with ours
temp_name = op.join(tempdir, 'temp-src.fif')
try:
# save
temp_pos = op.join(tempdir, 'temp-pos.txt')
np.savetxt(temp_pos, np.c_[src[0]['rr'][v], src[0]['nn'][v]])
# let's try the spherical one (no bem or surf supplied)
run_subprocess(['mne_volume_source_space', '--meters',
'--pos', temp_pos, '--src', temp_name])
src_c = read_source_spaces(temp_name)
src_new = setup_volume_source_space('sample', None,
pos=dict(rr=src[0]['rr'][v],
nn=src[0]['nn'][v]),
subjects_dir=subjects_dir)
_compare_source_spaces(src_c, src_new, mode='approx')
assert_allclose(src[0]['rr'][v], src_new[0]['rr'],
rtol=1e-3, atol=1e-6)
assert_allclose(src[0]['nn'][v], src_new[0]['nn'],
rtol=1e-3, atol=1e-6)
# now do writing
write_source_spaces(temp_name, src_c)
src_c2 = read_source_spaces(temp_name)
_compare_source_spaces(src_c, src_c2)
finally:
if op.isfile(temp_name):
os.remove(temp_name)
示例7: create_src_space
def create_src_space(sbj_dir, sbj_id, spacing, is_blind):
import os.path as op
import mne
bem_dir = op.join(sbj_dir, sbj_id, 'bem')
# check if source space exists, if not it creates using mne-python fun
# we have to create the cortical surface source space even when aseg is
# True
if is_blind:
# if is_blind we have to precomputed the source space sincw we had
# to remove some labels
src_fname = op.join(bem_dir, '%s-blind-%s-src.fif' % (sbj_id, spacing))
if not op.isfile(src_fname):
raise '\n *** you have to compute the source space blind!!! ***\n'
else:
print '\n*** source space file %s exists!!!\n' % src_fname
src = mne.read_source_spaces(src_fname)
else:
src_fname = op.join(bem_dir, '%s-%s-src.fif' % (sbj_id, spacing))
if not op.isfile(src_fname):
src = mne.setup_source_space(sbj_id, subjects_dir=sbj_dir,
fname=True,
spacing=spacing.replace('-', ''),
add_dist=False, overwrite=True,
n_jobs=2)
print '\n*** source space file %s written ***\n' % src_fname
else:
print '\n*** source space file %s exists!!!\n' % src_fname
src = mne.read_source_spaces(src_fname)
return src
示例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')
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', pos=7.0, bem=bem, surface=surf, mri='T1.mgz',
subjects_dir=subjects_dir)
write_source_spaces(temp_name, src_new, overwrite=True)
src[0]['subject_his_id'] = 'sample' # XXX: to make comparison pass
_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')
pytest.raises(IOError, setup_volume_source_space, 'sample',
pos=7.0, bem=None, surface='foo', # bad surf
mri=fname_mri, subjects_dir=subjects_dir)
assert repr(src) == repr(src_new)
assert src.kind == 'volume'
# Spheres
sphere = make_sphere_model(r0=(0., 0., 0.), head_radius=0.1,
relative_radii=(0.9, 1.0), sigmas=(0.33, 1.0))
src = setup_volume_source_space(pos=10)
src_new = setup_volume_source_space(pos=10, sphere=sphere)
_compare_source_spaces(src, src_new, mode='exact')
pytest.raises(ValueError, setup_volume_source_space, sphere='foo')
# Need a radius
sphere = make_sphere_model(head_radius=None)
pytest.raises(ValueError, setup_volume_source_space, sphere=sphere)
示例9: test_volume_source_space
def test_volume_source_space():
"""Test setting up volume source spaces
"""
fname_vol = op.join(data_path, "subjects", "sample", "bem", "volume-7mm-src.fif")
src = read_source_spaces(fname_vol)
temp_name = op.join(tempdir, "temp-src.fif")
try:
# 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")
src_new = read_source_spaces(temp_name)
_compare_source_spaces(src, src_new, mode="approx")
# let's try the spherical one (no bem or surf supplied)
run_subprocess(["mne_volume_source_space", "--grid", "15.0", "--src", temp_name, "--mri", fname_mri])
src = read_source_spaces(temp_name)
src_new = setup_volume_source_space("sample", temp_name, pos=15.0, mri=fname_mri, subjects_dir=subjects_dir)
_compare_source_spaces(src, src_new, mode="approx")
# now without MRI argument, it should give an error when we try
# to read it
run_subprocess(["mne_volume_source_space", "--grid", "15.0", "--src", temp_name])
assert_raises(ValueError, read_source_spaces, temp_name)
finally:
if op.isfile(temp_name):
os.remove(temp_name)
示例10: test_add_patch_info
def test_add_patch_info():
"""Test adding patch info to source space."""
# let's setup a small source space
src = read_source_spaces(fname_small)
src_new = read_source_spaces(fname_small)
for s in src_new:
s['nearest'] = None
s['nearest_dist'] = None
s['pinfo'] = None
# test that no patch info is added for small dist_limit
try:
add_source_space_distances(src_new, dist_limit=0.00001)
except RuntimeError: # what we throw when scipy version is wrong
pass
else:
assert all(s['nearest'] is None for s in src_new)
assert all(s['nearest_dist'] is None for s in src_new)
assert all(s['pinfo'] is None for s in src_new)
# now let's use one that works
add_source_space_distances(src_new)
for s1, s2 in zip(src, src_new):
assert_array_equal(s1['nearest'], s2['nearest'])
assert_allclose(s1['nearest_dist'], s2['nearest_dist'], atol=1e-7)
assert_equal(len(s1['pinfo']), len(s2['pinfo']))
for p1, p2 in zip(s1['pinfo'], s2['pinfo']):
assert_array_equal(p1, p2)
示例11: test_add_patch_info
def test_add_patch_info(monkeypatch):
"""Test adding patch info to source space."""
# let's setup a small source space
src = read_source_spaces(fname_small)
src_new = read_source_spaces(fname_small)
for s in src_new:
s['nearest'] = None
s['nearest_dist'] = None
s['pinfo'] = None
# test that no patch info is added for small dist_limit
add_source_space_distances(src_new, dist_limit=0.00001)
assert all(s['nearest'] is None for s in src_new)
assert all(s['nearest_dist'] is None for s in src_new)
assert all(s['pinfo'] is None for s in src_new)
# now let's use one that works (and test our warning-throwing)
monkeypatch.setattr(mne.source_space, '_DIST_WARN_LIMIT', 1)
with pytest.warns(RuntimeWarning, match='Computing distances for 258'):
add_source_space_distances(src_new)
for s1, s2 in zip(src, src_new):
assert_array_equal(s1['nearest'], s2['nearest'])
assert_allclose(s1['nearest_dist'], s2['nearest_dist'], atol=1e-7)
assert_equal(len(s1['pinfo']), len(s2['pinfo']))
for p1, p2 in zip(s1['pinfo'], s2['pinfo']):
assert_array_equal(p1, p2)
示例12: 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')
示例13: test_stc_to_label
def test_stc_to_label():
"""Test stc_to_label
"""
src = read_source_spaces(src_fname)
src_bad = read_source_spaces(src_bad_fname)
stc = read_source_estimate(stc_fname, 'sample')
os.environ['SUBJECTS_DIR'] = op.join(data_path, 'subjects')
labels1 = stc_to_label(stc, src='sample', smooth=3)
with warnings.catch_warnings(record=True) as w: # connectedness warning
warnings.simplefilter('always')
labels2 = stc_to_label(stc, src=src, smooth=3)
assert_true(len(w) == 1)
assert_true(len(labels1) == len(labels2))
for l1, l2 in zip(labels1, labels2):
assert_labels_equal(l1, l2, decimal=4)
with warnings.catch_warnings(record=True) as w: # connectedness warning
warnings.simplefilter('always')
labels_lh, labels_rh = stc_to_label(stc, src=src, smooth=3,
connected=True)
assert_true(len(w) == 1)
assert_raises(ValueError, stc_to_label, stc, 'sample', smooth=3,
connected=True)
assert_raises(RuntimeError, stc_to_label, stc, src=src_bad, connected=True)
assert_true(len(labels_lh) == 1)
assert_true(len(labels_rh) == 1)
示例14: test_write_source_space
def test_write_source_space():
"""Test writing and reading of source spaces
"""
src0 = read_source_spaces(fname, add_geom=False)
write_source_spaces(op.join(tempdir, 'tmp.fif'), src0)
src1 = read_source_spaces(op.join(tempdir, 'tmp.fif'), add_geom=False)
_compare_source_spaces(src0, src1)
示例15: test_morphed_source_space_return
def test_morphed_source_space_return():
"""Test returning a morphed source space to the original subject"""
# let's create some random data on fsaverage
data = rng.randn(20484, 1)
tmin, tstep = 0, 1.
src_fs = read_source_spaces(fname_fs)
stc_fs = SourceEstimate(data, [s['vertno'] for s in src_fs],
tmin, tstep, 'fsaverage')
# Create our morph source space
src_morph = morph_source_spaces(src_fs, 'sample',
subjects_dir=subjects_dir)
# Morph the data over using standard methods
stc_morph = stc_fs.morph('sample', [s['vertno'] for s in src_morph],
smooth=1, subjects_dir=subjects_dir)
# We can now pretend like this was real data we got e.g. from an inverse.
# To be complete, let's remove some vertices
keeps = [np.sort(rng.permutation(np.arange(len(v)))[:len(v) - 10])
for v in stc_morph.vertices]
stc_morph = SourceEstimate(
np.concatenate([stc_morph.lh_data[keeps[0]],
stc_morph.rh_data[keeps[1]]]),
[v[k] for v, k in zip(stc_morph.vertices, keeps)], tmin, tstep,
'sample')
# Return it to the original subject
stc_morph_return = stc_morph.to_original_src(
src_fs, subjects_dir=subjects_dir)
# Compare to the original data
stc_morph_morph = stc_morph.morph('fsaverage', stc_morph_return.vertices,
smooth=1,
subjects_dir=subjects_dir)
assert_equal(stc_morph_return.subject, stc_morph_morph.subject)
for ii in range(2):
assert_array_equal(stc_morph_return.vertices[ii],
stc_morph_morph.vertices[ii])
# These will not match perfectly because morphing pushes data around
corr = np.corrcoef(stc_morph_return.data[:, 0],
stc_morph_morph.data[:, 0])[0, 1]
assert_true(corr > 0.99, corr)
# Degenerate cases
stc_morph.subject = None # no .subject provided
assert_raises(ValueError, stc_morph.to_original_src,
src_fs, subject_orig='fsaverage', subjects_dir=subjects_dir)
stc_morph.subject = 'sample'
del src_fs[0]['subject_his_id'] # no name in src_fsaverage
assert_raises(ValueError, stc_morph.to_original_src,
src_fs, subjects_dir=subjects_dir)
src_fs[0]['subject_his_id'] = 'fsaverage' # name mismatch
assert_raises(ValueError, stc_morph.to_original_src,
src_fs, subject_orig='foo', subjects_dir=subjects_dir)
src_fs[0]['subject_his_id'] = 'sample'
src = read_source_spaces(fname) # wrong source space
assert_raises(RuntimeError, stc_morph.to_original_src,
src, subjects_dir=subjects_dir)