Skip to content

MAINT: Refactor aggregate_outputs for readability #2969

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 4 commits into from
Jul 29, 2019
Merged
Show file tree
Hide file tree
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
59 changes: 30 additions & 29 deletions nipype/interfaces/base/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ def _check_requires(self, spec, name, value):
]
if any(values) and isdefined(value):
if len(values) > 1:
fmt = ("%s requires values for inputs %s because '%s' is set. "
fmt = ("%s requires values for inputs %s because '%s' is set. "
"For a list of required inputs, see %s.help()")
else:
fmt = ("%s requires a value for input %s because '%s' is set. "
fmt = ("%s requires a value for input %s because '%s' is set. "
"For a list of required inputs, see %s.help()")
msg = fmt % (self.__class__.__name__,
', '.join("'%s'" % req for req in spec.requires),
Expand Down Expand Up @@ -450,34 +450,35 @@ def _list_outputs(self):
return None

def aggregate_outputs(self, runtime=None, needed_outputs=None):
""" Collate expected outputs and check for existence
"""

predicted_outputs = self._list_outputs()
outputs = self._outputs()
if predicted_outputs:
_unavailable_outputs = []
if outputs:
_unavailable_outputs = \
self._check_version_requirements(self._outputs())
for key, val in list(predicted_outputs.items()):
if needed_outputs and key not in needed_outputs:
continue
if key in _unavailable_outputs:
raise KeyError(('Output trait %s not available in version '
'%s of interface %s. Please inform '
'developers.') % (key, self.version,
self.__class__.__name__))
try:
setattr(outputs, key, val)
except TraitError as error:
if getattr(error, 'info',
'default').startswith('an existing'):
msg = ("File/Directory '%s' not found for %s output "
"'%s'." % (val, self.__class__.__name__, key))
raise FileNotFoundError(msg)
raise error
"""Collate expected outputs and apply output traits validation."""
outputs = self._outputs() # Generate an empty output spec object
predicted_outputs = self._list_outputs() # Predictions from _list_outputs
if not predicted_outputs:
return outputs

# Precalculate the list of output trait names that should be aggregated
aggregate_names = set(predicted_outputs.keys())
if needed_outputs is not None:
aggregate_names = set(needed_outputs).intersection(aggregate_names)

if aggregate_names: # Make sure outputs are compatible
_na_outputs = self._check_version_requirements(outputs)
na_names = aggregate_names.intersection(set(_na_outputs))
if na_names:
raise TypeError("""\
Output trait(s) %s not available in version %s of interface %s.\
""" % (', '.join(na_names), self.version, self.__class__.__name__))

for key in aggregate_names: # Final aggregation
val = predicted_outputs[key]
try:
setattr(outputs, key, val)
except TraitError as error:
if 'an existing' in getattr(error, 'info', 'default'):
msg = "No such file or directory for output '%s' of a %s interface" % \
(key, self.__class__.__name__)
raise FileNotFoundError(val, message=msg)
raise error
return outputs

@property
Expand Down
2 changes: 1 addition & 1 deletion nipype/interfaces/base/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def _list_outputs(self):
return {'foo': 1}

obj = DerivedInterface1()
with pytest.raises(KeyError):
with pytest.raises(TypeError):
obj.run()


Expand Down