From a28c7a27e559eaa16df734fa5592d74a5f4e711a Mon Sep 17 00:00:00 2001 From: oesteban Date: Sun, 30 Apr 2017 19:20:34 -0700 Subject: [PATCH 1/3] [FIX] Define ANTSPATH for ants.BrainExtraction automatically When it is not already defined. Also check if the interface failed for that reason nonetheless. Fixes #1901 --- nipype/interfaces/ants/segmentation.py | 27 ++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/nipype/interfaces/ants/segmentation.py b/nipype/interfaces/ants/segmentation.py index 0a13d93bc5..b48d62c0be 100644 --- a/nipype/interfaces/ants/segmentation.py +++ b/nipype/interfaces/ants/segmentation.py @@ -692,6 +692,33 @@ class BrainExtraction(ANTSCommand): output_spec = BrainExtractionOutputSpec _cmd = 'antsBrainExtraction.sh' + def _run_interface(self, runtime, correct_return_codes=(0,)): + 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)) + + runtime.environ.update({'ANTSPATH': cmd_path}) + + runtime = super(BrainExtraction, self)._run_interface(runtime) + + if 'we cant find the N4 program' in runtime.stdout: + 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(), From 76cbf0f6d04a45e7ed4af3f6280cb239eaafbfaf Mon Sep 17 00:00:00 2001 From: oesteban Date: Sun, 30 Apr 2017 19:34:54 -0700 Subject: [PATCH 2/3] automatically set ANTSPATH when possible --- nipype/interfaces/ants/segmentation.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/nipype/interfaces/ants/segmentation.py b/nipype/interfaces/ants/segmentation.py index b48d62c0be..5af36a97d9 100644 --- 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 @@ -693,20 +694,22 @@ class BrainExtraction(ANTSCommand): _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) + 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)) - runtime.environ.update({'ANTSPATH': cmd_path}) + # 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 the N4 program' in runtime.stdout: errmsg = ('antsBrainExtraction.sh requires the environment variable ' 'ANTSPATH to be defined') From 3b787404fad063680468d6d01100c36b0c0afff0 Mon Sep 17 00:00:00 2001 From: oesteban Date: Tue, 2 May 2017 17:03:20 -0700 Subject: [PATCH 3/3] update check --- nipype/interfaces/ants/segmentation.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/nipype/interfaces/ants/segmentation.py b/nipype/interfaces/ants/segmentation.py index 5af36a97d9..bb012c868f 100644 --- a/nipype/interfaces/ants/segmentation.py +++ b/nipype/interfaces/ants/segmentation.py @@ -710,9 +710,14 @@ def _run_interface(self, runtime, correct_return_codes=(0,)): 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: - errmsg = ('antsBrainExtraction.sh requires the environment variable ' - 'ANTSPATH to be defined') + 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: