Skip to content

[FIX] Define ANTSPATH for ants.BrainExtraction automatically #1986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 3, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion nipype/interfaces/ants/segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
import os
from ...external.due import BibTeX
from ...utils.filemanip import split_filename, copyfile
from ..base import TraitedSpec, File, traits, InputMultiPath, OutputMultiPath, isdefined
from ..base import (TraitedSpec, File, traits, InputMultiPath, OutputMultiPath, isdefined,
_exists_in_path)
from .base import ANTSCommand, ANTSCommandInputSpec


Expand Down Expand Up @@ -692,6 +693,40 @@ class BrainExtraction(ANTSCommand):
output_spec = BrainExtractionOutputSpec
_cmd = 'antsBrainExtraction.sh'

def _run_interface(self, runtime, correct_return_codes=(0,)):
# antsBrainExtraction.sh requires ANTSPATH to be defined
out_environ = self._get_environ()
if out_environ.get('ANTSPATH') is None:
runtime.environ.update(out_environ)
executable_name = self.cmd.split()[0]
exist_val, cmd_path = _exists_in_path(executable_name, runtime.environ)
if not exist_val:
raise IOError("command '%s' could not be found on host %s" %
(self.cmd.split()[0], runtime.hostname))

# Set the environment variable if found
runtime.environ.update({'ANTSPATH': os.path.dirname(cmd_path)})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i believe ANTSPATH requires a trailing /. could you confirm this and make sure we adjust for it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the scripts don't need the trailing /. Particularly, the antsBrainExtraction.sh doesn't need it.

But I can add it anyways if you want.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then not necessary.


runtime = super(BrainExtraction, self)._run_interface(runtime)

# Still, double-check if it didn't found N4
if 'we cant find' in runtime.stdout:
for line in runtime.stdout.split('\n'):
if line.strip().startswith('we cant find'):
tool = line.strip().replace('we cant find the', '').split(' ')[0]
break

errmsg = ('antsBrainExtraction.sh requires %s the environment variable '
'ANTSPATH to be defined' % tool)
if runtime.stderr is None:
runtime.stderr = errmsg
else:
runtime.stderr += '\n' + errmsg
runtime.returncode = 1
self.raise_exception(runtime)

return runtime

def _list_outputs(self):
outputs = self._outputs().get()
outputs['BrainExtractionMask'] = os.path.join(os.getcwd(),
Expand Down