Skip to content

Commit e48c9c8

Browse files
authored
Merge pull request #1986 from oesteban/fix/antsBrainExtraction-1901
[FIX] Define ANTSPATH for ants.BrainExtraction automatically
2 parents e5e5edd + 3b78740 commit e48c9c8

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

nipype/interfaces/ants/segmentation.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
import os
1515
from ...external.due import BibTeX
1616
from ...utils.filemanip import split_filename, copyfile
17-
from ..base import TraitedSpec, File, traits, InputMultiPath, OutputMultiPath, isdefined
17+
from ..base import (TraitedSpec, File, traits, InputMultiPath, OutputMultiPath, isdefined,
18+
_exists_in_path)
1819
from .base import ANTSCommand, ANTSCommandInputSpec
1920

2021

@@ -692,6 +693,40 @@ class BrainExtraction(ANTSCommand):
692693
output_spec = BrainExtractionOutputSpec
693694
_cmd = 'antsBrainExtraction.sh'
694695

696+
def _run_interface(self, runtime, correct_return_codes=(0,)):
697+
# antsBrainExtraction.sh requires ANTSPATH to be defined
698+
out_environ = self._get_environ()
699+
if out_environ.get('ANTSPATH') is None:
700+
runtime.environ.update(out_environ)
701+
executable_name = self.cmd.split()[0]
702+
exist_val, cmd_path = _exists_in_path(executable_name, runtime.environ)
703+
if not exist_val:
704+
raise IOError("command '%s' could not be found on host %s" %
705+
(self.cmd.split()[0], runtime.hostname))
706+
707+
# Set the environment variable if found
708+
runtime.environ.update({'ANTSPATH': os.path.dirname(cmd_path)})
709+
710+
runtime = super(BrainExtraction, self)._run_interface(runtime)
711+
712+
# Still, double-check if it didn't found N4
713+
if 'we cant find' in runtime.stdout:
714+
for line in runtime.stdout.split('\n'):
715+
if line.strip().startswith('we cant find'):
716+
tool = line.strip().replace('we cant find the', '').split(' ')[0]
717+
break
718+
719+
errmsg = ('antsBrainExtraction.sh requires %s the environment variable '
720+
'ANTSPATH to be defined' % tool)
721+
if runtime.stderr is None:
722+
runtime.stderr = errmsg
723+
else:
724+
runtime.stderr += '\n' + errmsg
725+
runtime.returncode = 1
726+
self.raise_exception(runtime)
727+
728+
return runtime
729+
695730
def _list_outputs(self):
696731
outputs = self._outputs().get()
697732
outputs['BrainExtractionMask'] = os.path.join(os.getcwd(),

0 commit comments

Comments
 (0)