Skip to content

Commit 854afe6

Browse files
committed
ENH: Add pre- and post-run hooks to BaseInterface
1 parent 5a96ea5 commit 854afe6

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

nipype/interfaces/base/core.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,9 @@ def run(self, cwd=None, ignore_exception=None, **inputs):
517517
outputs = None
518518

519519
try:
520+
runtime = self._pre_run_hook(runtime)
520521
runtime = self._run_interface(runtime)
522+
runtime = self._post_run_hook(runtime)
521523
outputs = self.aggregate_outputs(runtime)
522524
except Exception as e:
523525
import traceback
@@ -653,6 +655,28 @@ def save_inputs_to_json(self, json_file):
653655
with open(json_file, 'w' if PY3 else 'wb') as fhandle:
654656
json.dump(inputs, fhandle, indent=4, ensure_ascii=False)
655657

658+
def _pre_run_hook(self, runtime):
659+
"""
660+
Perform any pre-_run_interface() processing
661+
662+
Subclasses may override this function to modify ``runtime`` object or
663+
interface state
664+
665+
MUST return runtime object
666+
"""
667+
return runtime
668+
669+
def _post_run_hook(self, runtime):
670+
"""
671+
Perform any post-_run_interface() processing
672+
673+
Subclasses may override this function to modify ``runtime`` object or
674+
interface state
675+
676+
MUST return runtime object
677+
"""
678+
return runtime
679+
656680

657681
class SimpleInterface(BaseInterface):
658682
""" An interface pattern that allows outputs to be set in a dictionary

0 commit comments

Comments
 (0)