From 87c1c27b65706c99b5c3533c94d149d26ddb5c73 Mon Sep 17 00:00:00 2001 From: "Christopher J. Markiewicz" Date: Mon, 9 Oct 2017 11:27:25 -0400 Subject: [PATCH 1/3] ENH: Add SimpleInterface --- nipype/interfaces/base.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/nipype/interfaces/base.py b/nipype/interfaces/base.py index c63d64a7f1..2078b0783f 100644 --- a/nipype/interfaces/base.py +++ b/nipype/interfaces/base.py @@ -1212,6 +1212,41 @@ 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 + + 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: + + >>> 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 + """ + 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 From 95110ee0cf4f81ec75fab79828c0ec0dbbd1171f Mon Sep 17 00:00:00 2001 From: "Christopher J. Markiewicz" Date: Mon, 9 Oct 2017 14:38:00 -0400 Subject: [PATCH 2/3] DOC: Update docstring --- nipype/interfaces/base.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/nipype/interfaces/base.py b/nipype/interfaces/base.py index 2078b0783f..7394c6bd9c 100644 --- a/nipype/interfaces/base.py +++ b/nipype/interfaces/base.py @@ -1214,22 +1214,26 @@ def save_inputs_to_json(self, json_file): class SimpleInterface(BaseInterface): """ 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. - When implementing `_run_interface`, set outputs with:: + 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: + 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 From cba28e465e0541a6da6c4fa009acb2afa1633fce Mon Sep 17 00:00:00 2001 From: "Christopher J. Markiewicz" Date: Mon, 9 Oct 2017 14:40:49 -0400 Subject: [PATCH 3/3] DOCTEST: Exercise __init__ and _list_outputs --- nipype/interfaces/base.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/nipype/interfaces/base.py b/nipype/interfaces/base.py index 7394c6bd9c..79812d4b19 100644 --- a/nipype/interfaces/base.py +++ b/nipype/interfaces/base.py @@ -1241,6 +1241,11 @@ class SimpleInterface(BaseInterface): ... 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__(