Skip to content

Commit 87c1c27

Browse files
committed
ENH: Add SimpleInterface
1 parent 8e1a86a commit 87c1c27

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

nipype/interfaces/base.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,41 @@ 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+
1218+
When implementing `_run_interface`, set outputs with::
1219+
1220+
self._results[out_name] = out_value
1221+
1222+
This can be a way to upgrade a ``Function`` interface to do type checking:
1223+
1224+
>>> def double(x):
1225+
... return 2 * x
1226+
1227+
>>> class DoubleInputSpec(BaseInterfaceInputSpec):
1228+
... x = traits.Float(mandatory=True)
1229+
1230+
>>> class DoubleOutputSpec(TraitedSpec):
1231+
... doubled = traits.Float()
1232+
1233+
>>> class Double(SimpleInterface):
1234+
... input_spec = DoubleInputSpec
1235+
... output_spec = DoubleOutputSpec
1236+
...
1237+
... def _run_interface(self, runtime):
1238+
... self._results['doubled'] = double(self.inputs.x)
1239+
... return runtime
1240+
"""
1241+
def __init__(self, from_file=None, resource_monitor=None, **inputs):
1242+
super(SimpleInterface, self).__init__(
1243+
from_file=from_file, resource_monitor=resource_monitor, **inputs)
1244+
self._results = {}
1245+
1246+
def _list_outputs(self):
1247+
return self._results
1248+
1249+
12151250
class Stream(object):
12161251
"""Function to capture stdout and stderr streams with timestamps
12171252

0 commit comments

Comments
 (0)