Repository: nipy/nipype

Issue:
[BUG] antsBrainExtraction interface fails when $ANTSPATH is not defined, but exit code is 0
### Summary
 antsBrainExtraction interface fails when $ANTSPATH is not defined, but exit code is 0

### How to replicate the behavior
The problem is the following
```
$ antsBrainExtraction.sh -h
we cant find the N4 program -- does not seem to exist. please (re)define $ANTSPATH in your environment.
$ echo $?
0
```
when nipype gets to check the output, since the exit code is zero, it breaks. We could check, at least, for this output message and reraise the message.

cc @rwblair .


Discussion:
is this stdout or stderr?

On Tue, Mar 21, 2017 at 4:56 PM, Oscar Esteban <notifications@github.com>
wrote:

> Summary
>
> antsBrainExtraction interface fails when $ANTSPATH is not defined, but
> exit code is 0
> How to replicate the behavior
>
> The problem is the following
>
> $ antsBrainExtraction.sh -h
> we cant find the N4 program -- does not seem to exist. please (re)define $ANTSPATH in your environment.
> $ echo $?
> 0
>
> when nipype gets to check the output, since the exit code is zero, it
> breaks. We could check, at least, for this output message and reraise the
> message.
>
> cc @rwblair <https://github.com/rwblair> .
>
> —
> You are receiving this because you are subscribed to this thread.
> Reply to this email directly, view it on GitHub
> <https://github.com/nipy/nipype/issues/1901>, or mute the thread
> <https://github.com/notifications/unsubscribe-auth/AAOkp3svhDYl8vw07ed4tJoMm-lDyYaJks5roGOngaJpZM4Mkjxu>
> .
>

stdout:
```
$ antsBrainExtraction.sh > out 2> error
$ cat error 
$ cat out 
we cant find the N4 program -- does not seem to exist. please (re)define $ANTSPATH in your environment.
```

Resolving patch:
diff --git a/nipype/interfaces/ants/segmentation.py b/nipype/interfaces/ants/segmentation.py
--- a/nipype/interfaces/ants/segmentation.py
+++ b/nipype/interfaces/ants/segmentation.py
@@ -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
 
 
@@ -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)})
+
+        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(),


Test patch: