Skip to content

Commit b70372d

Browse files
committed
Check environment and run FreeSurferEnv.sh if necessary
1 parent bcdd7a0 commit b70372d

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

surfer/io.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
from tempfile import mktemp
33

4-
from subprocess import Popen, PIPE
4+
from subprocess import Popen, PIPE, check_output
55
import gzip
66
import numpy as np
77
import nibabel as nib
@@ -166,6 +166,18 @@ def project_volume_data(filepath, hemi, reg_file=None, subject_id=None,
166166
verbose : bool, str, int, or None
167167
If not None, override default verbose level (see surfer.verbose).
168168
"""
169+
170+
env = os.environ
171+
if 'FREESURFER_HOME' not in env:
172+
raise RuntimeError('FreeSurfer environment not defined. Define the '
173+
'FREESURFER_HOME environment variable.')
174+
# Run FreeSurferEnv.sh if not most recent script to set PATH
175+
if not env['PATH'].startswith(os.path.join(env['FREESURFER_HOME'], 'bin')):
176+
cmd = ['bash', '-c', 'source {} && env'.format(
177+
os.path.join(env['FREESURFER_HOME'], 'FreeSurferEnv.sh'))]
178+
envout = check_output(cmd)
179+
env = dict(line.split('=', 1) for line in envout.split('\n') if line)
180+
169181
# Set the basic commands
170182
cmd_list = ["mri_vol2surf",
171183
"--mov", filepath,
@@ -203,7 +215,7 @@ def project_volume_data(filepath, hemi, reg_file=None, subject_id=None,
203215
out_file = mktemp(prefix="pysurfer-v2s", suffix='.mgz')
204216
cmd_list.extend(["--o", out_file])
205217
logger.info(" ".join(cmd_list))
206-
p = Popen(cmd_list, stdout=PIPE, stderr=PIPE)
218+
p = Popen(cmd_list, stdout=PIPE, stderr=PIPE, env=env)
207219
stdout, stderr = p.communicate()
208220
out = p.returncode
209221
if out:

0 commit comments

Comments
 (0)