Skip to content

Commit d216b4a

Browse files
authored
Merge pull request #2294 from oesteban/fix/antsBrainExtraction-ANTSPATH
[FIX] Improve determination of ANTSPATH in BrainExtraction interface
2 parents 1b05497 + 80247fc commit d216b4a

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

nipype/interfaces/ants/segmentation.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -720,17 +720,19 @@ class BrainExtraction(ANTSCommand):
720720
def _run_interface(self, runtime, correct_return_codes=(0,)):
721721
# antsBrainExtraction.sh requires ANTSPATH to be defined
722722
out_environ = self._get_environ()
723-
if out_environ.get('ANTSPATH') is None:
724-
runtime.environ.update(out_environ)
725-
executable_name = self.cmd.split()[0]
726-
exist_val, cmd_path = _exists_in_path(executable_name, runtime.environ)
727-
if not exist_val:
728-
raise IOError("command '%s' could not be found on host %s" %
729-
(self.cmd.split()[0], runtime.hostname))
730-
731-
# Set the environment variable if found
732-
runtime.environ.update({'ANTSPATH': os.path.dirname(cmd_path)})
733-
723+
ants_path = out_environ.get('ANTSPATH', None) or os.getenv('ANTSPATH', None)
724+
if ants_path is None:
725+
# Check for antsRegistration, which is under bin/ (the $ANTSPATH) instead of
726+
# checking for antsBrainExtraction.sh which is under script/
727+
_, cmd_path = _exists_in_path('antsRegistration', runtime.environ)
728+
if not cmd_path:
729+
raise RuntimeError(
730+
'The environment variable $ANTSPATH is not defined in host "%s", '
731+
'and Nipype could not determine it automatically.' % runtime.hostname)
732+
ants_path = os.path.dirname(cmd_path)
733+
734+
self.inputs.environ.update({'ANTSPATH': ants_path})
735+
runtime.environ.update({'ANTSPATH': ants_path})
734736
runtime = super(BrainExtraction, self)._run_interface(runtime)
735737

736738
# Still, double-check if it didn't found N4
@@ -740,8 +742,8 @@ def _run_interface(self, runtime, correct_return_codes=(0,)):
740742
tool = line.strip().replace('we cant find the', '').split(' ')[0]
741743
break
742744

743-
errmsg = ('antsBrainExtraction.sh requires %s the environment variable '
744-
'ANTSPATH to be defined' % tool)
745+
errmsg = ('antsBrainExtraction.sh requires "%s" to be found in $ANTSPATH '
746+
'($ANTSPATH="%s").') % (tool, ants_path)
745747
if runtime.stderr is None:
746748
runtime.stderr = errmsg
747749
else:

0 commit comments

Comments
 (0)