Skip to content

ENH: Add SimpleInterface #2220

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
Oct 9, 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
44 changes: 44 additions & 0 deletions nipype/interfaces/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,50 @@ def save_inputs_to_json(self, json_file):
json.dump(inputs, fhandle, indent=4, ensure_ascii=False)


class SimpleInterface(BaseInterface):
""" An interface pattern that allows outputs to be set in a dictionary
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd suggest:

"""
An interface pattern that allows outputs to be set in a dictionary called ``_results``
that is automatically interpreted by ``_list_outputs()`` to find the outputs.

...
"""

called ``_results`` that is automatically interpreted by
``_list_outputs()`` to find the outputs.

When implementing ``_run_interface``, set outputs with::

self._results[out_name] = out_value

This can be a way to upgrade a ``Function`` interface to do type checking.

Examples
--------
>>> def double(x):
... return 2 * x
...
>>> class DoubleInputSpec(BaseInterfaceInputSpec):
... x = traits.Float(mandatory=True)
...
>>> class DoubleOutputSpec(TraitedSpec):
... doubled = traits.Float()
...
>>> class Double(SimpleInterface):
... input_spec = DoubleInputSpec
... output_spec = DoubleOutputSpec
...
... def _run_interface(self, runtime):
... self._results['doubled'] = double(self.inputs.x)
... return runtime

>>> dbl = Double()
>>> dbl.inputs.x = 2
>>> dbl.run().outputs.doubled
4.0
"""
def __init__(self, from_file=None, resource_monitor=None, **inputs):
super(SimpleInterface, self).__init__(
from_file=from_file, resource_monitor=resource_monitor, **inputs)
self._results = {}

def _list_outputs(self):
return self._results


class Stream(object):
"""Function to capture stdout and stderr streams with timestamps

Expand Down