|
1 | 1 | import os
|
2 | 2 | from tempfile import mktemp
|
3 | 3 |
|
4 |
| -from subprocess import Popen, PIPE |
| 4 | +from subprocess import Popen, PIPE, check_output |
5 | 5 | import gzip
|
6 | 6 | import numpy as np
|
7 | 7 | import nibabel as nib
|
@@ -166,6 +166,18 @@ def project_volume_data(filepath, hemi, reg_file=None, subject_id=None,
|
166 | 166 | verbose : bool, str, int, or None
|
167 | 167 | If not None, override default verbose level (see surfer.verbose).
|
168 | 168 | """
|
| 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 | + |
169 | 181 | # Set the basic commands
|
170 | 182 | cmd_list = ["mri_vol2surf",
|
171 | 183 | "--mov", filepath,
|
@@ -203,7 +215,7 @@ def project_volume_data(filepath, hemi, reg_file=None, subject_id=None,
|
203 | 215 | out_file = mktemp(prefix="pysurfer-v2s", suffix='.mgz')
|
204 | 216 | cmd_list.extend(["--o", out_file])
|
205 | 217 | 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) |
207 | 219 | stdout, stderr = p.communicate()
|
208 | 220 | out = p.returncode
|
209 | 221 | if out:
|
|
0 commit comments