Skip to content

Commit 56b7c81

Browse files
authored
Merge pull request #2220 from effigies/enh/simple_interface
ENH: Add SimpleInterface
2 parents 8e1a86a + cba28e4 commit 56b7c81

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

nipype/interfaces/base.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,50 @@ def save_inputs_to_json(self, json_file):
12121212
json.dump(inputs, fhandle, indent=4, ensure_ascii=False)
12131213

12141214

1215+
class SimpleInterface(BaseInterface):
1216+
""" An interface pattern that allows outputs to be set in a dictionary
1217+
called ``_results`` that is automatically interpreted by
1218+
``_list_outputs()`` to find the outputs.
1219+
1220+
When implementing ``_run_interface``, set outputs with::
1221+
1222+
self._results[out_name] = out_value
1223+
1224+
This can be a way to upgrade a ``Function`` interface to do type checking.
1225+
1226+
Examples
1227+
--------
1228+
>>> def double(x):
1229+
... return 2 * x
1230+
...
1231+
>>> class DoubleInputSpec(BaseInterfaceInputSpec):
1232+
... x = traits.Float(mandatory=True)
1233+
...
1234+
>>> class DoubleOutputSpec(TraitedSpec):
1235+
... doubled = traits.Float()
1236+
...
1237+
>>> class Double(SimpleInterface):
1238+
... input_spec = DoubleInputSpec
1239+
... output_spec = DoubleOutputSpec
1240+
...
1241+
... def _run_interface(self, runtime):
1242+
... self._results['doubled'] = double(self.inputs.x)
1243+
... return runtime
1244+
1245+
>>> dbl = Double()
1246+
>>> dbl.inputs.x = 2
1247+
>>> dbl.run().outputs.doubled
1248+
4.0
1249+
"""
1250+
def __init__(self, from_file=None, resource_monitor=None, **inputs):
1251+
super(SimpleInterface, self).__init__(
1252+
from_file=from_file, resource_monitor=resource_monitor, **inputs)
1253+
self._results = {}
1254+
1255+
def _list_outputs(self):
1256+
return self._results
1257+
1258+
12151259
class Stream(object):
12161260
"""Function to capture stdout and stderr streams with timestamps
12171261

0 commit comments

Comments
 (0)