本文整理汇总了Python中mne.utils.run_subprocess函数的典型用法代码示例。如果您正苦于以下问题:Python run_subprocess函数的具体用法?Python run_subprocess怎么用?Python run_subprocess使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了run_subprocess函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_scale_mri
def test_scale_mri():
"""Test creating fsaverage and scaling it"""
# create fsaverage
create_default_subject(subjects_dir=tempdir)
is_mri = _is_mri_subject('fsaverage', tempdir)
assert_true(is_mri, "Creating fsaverage failed")
fid_path = os.path.join(tempdir, 'fsaverage', 'bem',
'fsaverage-fiducials.fif')
os.remove(fid_path)
create_default_subject(update=True, subjects_dir=tempdir)
assert_true(os.path.exists(fid_path), "Updating fsaverage")
# create source space
path = os.path.join(tempdir, 'fsaverage', 'bem', 'fsaverage-ico-6-src.fif')
if not os.path.exists(path):
cmd = ['mne_setup_source_space', '--subject', 'fsaverage', '--ico',
'6']
env = os.environ.copy()
env['SUBJECTS_DIR'] = tempdir
run_subprocess(cmd, env=env)
# scale fsaverage
scale_mri('fsaverage', 'flachkopf', [1, .2, .8], True, subjects_dir=tempdir)
is_mri = _is_mri_subject('flachkopf', tempdir)
assert_true(is_mri, "Scaling fsaverage failed")
src_path = os.path.join(tempdir, 'flachkopf', 'bem',
'flachkopf-ico-6-src.fif')
assert_true(os.path.exists(src_path), "Source space was not scaled")
scale_labels('flachkopf', subjects_dir=tempdir)
# scale source space separately
os.remove(src_path)
scale_source_space('flachkopf', 'ico-6', subjects_dir=tempdir)
assert_true(os.path.exists(src_path), "Source space was not scaled")
示例2: 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)
示例3: test_make_forward_solution_sphere
def test_make_forward_solution_sphere():
"""Test making a forward solution with a sphere model"""
temp_dir = _TempDir()
fname_src_small = op.join(temp_dir, "sample-oct-2-src.fif")
src = setup_source_space("sample", fname_src_small, "oct2", subjects_dir=subjects_dir, add_dist=False)
out_name = op.join(temp_dir, "tmp-fwd.fif")
run_subprocess(
[
"mne_forward_solution",
"--meg",
"--eeg",
"--meas",
fname_raw,
"--src",
fname_src_small,
"--mri",
fname_trans,
"--fwd",
out_name,
]
)
fwd = read_forward_solution(out_name)
sphere = make_sphere_model(verbose=True)
fwd_py = make_forward_solution(fname_raw, fname_trans, src, sphere, meg=True, eeg=True, verbose=True)
_compare_forwards(fwd, fwd_py, 366, 108, meg_rtol=5e-1, meg_atol=1e-6, eeg_rtol=5e-1, eeg_atol=5e-1)
# Since the above is pretty lax, let's check a different way
for meg, eeg in zip([True, False], [False, True]):
fwd_ = pick_types_forward(fwd, meg=meg, eeg=eeg)
fwd_py_ = pick_types_forward(fwd, meg=meg, eeg=eeg)
assert_allclose(np.corrcoef(fwd_["sol"]["data"].ravel(), fwd_py_["sol"]["data"].ravel())[0, 1], 1.0, rtol=1e-3)
示例4: 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)
示例5: compensate_mne
def compensate_mne(fname, comp):
"""Compensate using MNE-C."""
tmp_fname = '%s-%d-ave.fif' % (fname[:-4], comp)
cmd = ['mne_compensate_data', '--in', fname,
'--out', tmp_fname, '--grad', str(comp)]
run_subprocess(cmd)
return read_evokeds(tmp_fname)[0]
示例6: 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)
示例7: test_make_forward_solution_sphere
def test_make_forward_solution_sphere():
"""Test making a forward solution with a sphere model."""
temp_dir = _TempDir()
fname_src_small = op.join(temp_dir, 'sample-oct-2-src.fif')
src = setup_source_space('sample', 'oct2', subjects_dir=subjects_dir,
add_dist=False)
write_source_spaces(fname_src_small, src) # to enable working with MNE-C
out_name = op.join(temp_dir, 'tmp-fwd.fif')
run_subprocess(['mne_forward_solution', '--meg', '--eeg',
'--meas', fname_raw, '--src', fname_src_small,
'--mri', fname_trans, '--fwd', out_name])
fwd = read_forward_solution(out_name)
sphere = make_sphere_model(verbose=True)
fwd_py = make_forward_solution(fname_raw, fname_trans, src, sphere,
meg=True, eeg=True, verbose=True)
_compare_forwards(fwd, fwd_py, 366, 108,
meg_rtol=5e-1, meg_atol=1e-6,
eeg_rtol=5e-1, eeg_atol=5e-1)
# Since the above is pretty lax, let's check a different way
for meg, eeg in zip([True, False], [False, True]):
fwd_ = pick_types_forward(fwd, meg=meg, eeg=eeg)
fwd_py_ = pick_types_forward(fwd, meg=meg, eeg=eeg)
assert_allclose(np.corrcoef(fwd_['sol']['data'].ravel(),
fwd_py_['sol']['data'].ravel())[0, 1],
1.0, rtol=1e-3)
示例8: 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)
示例9: 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)
示例10: test_average_forward_solution
def test_average_forward_solution():
"""Test averaging forward solutions
"""
fwd = read_forward_solution(fname)
# input not a list
assert_raises(TypeError, average_forward_solutions, 1)
# list is too short
assert_raises(ValueError, average_forward_solutions, [])
# negative weights
assert_raises(ValueError, average_forward_solutions, [fwd, fwd], [-1, 0])
# all zero weights
assert_raises(ValueError, average_forward_solutions, [fwd, fwd], [0, 0])
# weights not same length
assert_raises(ValueError, average_forward_solutions, [fwd, fwd], [0, 0, 0])
# list does not only have all dict()
assert_raises(TypeError, average_forward_solutions, [1, fwd])
# try an easy case
fwd_copy = average_forward_solutions([fwd])
assert_array_equal(fwd['sol']['data'], fwd_copy['sol']['data'])
# modify a fwd solution, save it, use MNE to average with old one
fwd_copy['sol']['data'] *= 0.5
fname_copy = op.join(temp_dir, 'fwd.fif')
write_forward_solution(fname_copy, fwd_copy, overwrite=True)
cmd = ('mne_average_forward_solutions', '--fwd', fname, '--fwd',
fname_copy, '--out', fname_copy)
run_subprocess(cmd)
# now let's actually do it, with one filename and one fwd
fwd_ave = average_forward_solutions([fwd, fwd_copy])
assert_array_equal(0.75 * fwd['sol']['data'], fwd_ave['sol']['data'])
示例11: run_freesurfer_command
def run_freesurfer_command(command, subjects_dir):
"Run a FreeSurfer command"
env = os.environ.copy()
env['SUBJECTS_DIR'] = subjects_dir
# find FREESURFER_HOME
fs_home = mne.get_config('FREESURFER_HOME')
save_fs_home = False
while True:
problem = fs_home_problem(fs_home)
if problem:
save_fs_home = True
message = "Please select the directory where FreeSurfer is installed"
print problem
print message
fs_home = ui.ask_dir("Select FreeSurfer Directory", message)
if fs_home is False:
raise RuntimeError("Could not find FreeSurfer")
else:
break
if save_fs_home:
mne.set_config('FREESURFER_HOME', fs_home)
# adjust environment
env['FREESURFER_HOME'] = fs_home
bin_path = os.path.join(fs_home, 'bin')
if bin_path not in env['PATH']:
env['PATH'] = ':'.join((bin_path, env['PATH']))
# run command
run_subprocess(command, env=env)
示例12: freeview_bem_surfaces
def freeview_bem_surfaces(subject, subjects_dir, method):
"""View 3-Layers BEM model with Freeview.
Parameters
----------
subject : string
Subject name
subjects_dir : string
Directory containing subjects data (Freesurfer SUBJECTS_DIR)
method : string
Can be 'flash' or 'watershed'.
"""
subjects_dir = get_subjects_dir(subjects_dir, raise_error=True)
if subject is None:
raise ValueError("subject argument is None.")
subject_dir = op.join(subjects_dir, subject)
if not op.isdir(subject_dir):
raise ValueError("Wrong path: '{}'. Check subjects-dir or"
"subject argument.".format(subject_dir))
env = os.environ.copy()
env['SUBJECT'] = subject
env['SUBJECTS_DIR'] = subjects_dir
if 'FREESURFER_HOME' not in env:
raise RuntimeError('The FreeSurfer environment needs to be set up.')
mri_dir = op.join(subject_dir, 'mri')
bem_dir = op.join(subject_dir, 'bem')
mri = op.join(mri_dir, 'T1.mgz')
if method == 'watershed':
bem_dir = op.join(bem_dir, 'watershed')
outer_skin = op.join(bem_dir, '%s_outer_skin_surface' % subject)
outer_skull = op.join(bem_dir, '%s_outer_skull_surface' % subject)
inner_skull = op.join(bem_dir, '%s_inner_skull_surface' % subject)
else:
if method == 'flash':
bem_dir = op.join(bem_dir, 'flash')
outer_skin = op.join(bem_dir, 'outer_skin.surf')
outer_skull = op.join(bem_dir, 'outer_skull.surf')
inner_skull = op.join(bem_dir, 'inner_skull.surf')
# put together the command
cmd = ['freeview']
cmd += ["--volume", mri]
cmd += ["--surface", "%s:color=red:edgecolor=red" % inner_skull]
cmd += ["--surface", "%s:color=yellow:edgecolor=yellow" % outer_skull]
cmd += ["--surface",
"%s:color=255,170,127:edgecolor=255,170,127" % outer_skin]
run_subprocess(cmd, env=env, stdout=sys.stdout)
print("[done]")
示例13: run_freesurfer_command
def run_freesurfer_command(command, subjects_dir):
"Run a FreeSurfer command"
env = os.environ.copy()
env['SUBJECTS_DIR'] = subjects_dir
env['FREESURFER_HOME'] = fs_home = get_fs_home()
bin_path = os.path.join(fs_home, 'bin')
if bin_path not in env['PATH']:
env['PATH'] = ':'.join((bin_path, env['PATH']))
# run command
run_subprocess(command, env=env)
示例14: generate_commands_rst
def generate_commands_rst(app):
from sphinx_gallery import sphinx_compatibility
out_dir = op.abspath(op.join(op.dirname(__file__), '..', 'generated'))
if not op.isdir(out_dir):
os.mkdir(out_dir)
out_fname = op.join(out_dir, 'commands.rst')
command_path = op.abspath(
op.join(os.path.dirname(__file__), '..', '..', 'mne', 'commands'))
fnames = [op.basename(fname)
for fname in glob.glob(op.join(command_path, 'mne_*.py'))]
iterator = sphinx_compatibility.status_iterator(
fnames, 'generating MNE command help ... ', length=len(fnames))
with open(out_fname, 'w') as f:
f.write(header)
for fname in iterator:
cmd_name = fname[:-3]
run_name = op.join(command_path, fname)
output, _ = run_subprocess([sys.executable, run_name, '--help'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE, verbose=False)
f.write(command_rst % (cmd_name,
cmd_name.replace('mne_', 'mne '),
'-' * len(cmd_name),
output))
print('[Done]')
示例15: 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)