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 2 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
32 changes: 31 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,35 @@ 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 the N4 program' in runtime.stdout:
Copy link
Member

Choose a reason for hiding this comment

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

since the script can be in a different location than the N4 executable, can we not do this check before running in this particular case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry I'm lost here. Which check would you remove?

Copy link
Member

Choose a reason for hiding this comment

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

i meant to put the N4 check before calling _run_interface instead of checking the output.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Judging from this: https://github.com/stnava/ANTs/blob/646459bf297a50cdf608ea7d39cb79b44906ad22/Scripts/antsBrainExtraction.sh#L5-L20, I think we just parse the output for "we cant find" and raise the message for the missing tool.

For next release we can decouple or, as @blakedewey suggested, replicate antsBrainExtraction in pure nipype.

errmsg = ('antsBrainExtraction.sh requires the environment variable '
'ANTSPATH to be defined')
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