Skip to content

ENH: ReportCapableInterface mix-in/base interface #2560

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 7 commits into from
May 18, 2018
Merged
Show file tree
Hide file tree
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
24 changes: 24 additions & 0 deletions nipype/interfaces/base/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,9 @@ def run(self, cwd=None, ignore_exception=None, **inputs):
outputs = None

try:
runtime = self._pre_run_hook(runtime)
runtime = self._run_interface(runtime)
runtime = self._post_run_hook(runtime)
Copy link
Member Author

Choose a reason for hiding this comment

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

@satra Are you comfortable with this change to BaseInterface?

Copy link
Member

Choose a reason for hiding this comment

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

for now yes - we may have to add some prov stuff when i refactor that piece.

outputs = self.aggregate_outputs(runtime)
except Exception as e:
import traceback
Expand Down Expand Up @@ -653,6 +655,28 @@ def save_inputs_to_json(self, json_file):
with open(json_file, 'w' if PY3 else 'wb') as fhandle:
json.dump(inputs, fhandle, indent=4, ensure_ascii=False)

def _pre_run_hook(self, runtime):
"""
Perform any pre-_run_interface() processing

Subclasses may override this function to modify ``runtime`` object or
interface state

MUST return runtime object
"""
return runtime

def _post_run_hook(self, runtime):
"""
Perform any post-_run_interface() processing

Subclasses may override this function to modify ``runtime`` object or
interface state

MUST return runtime object
"""
return runtime


class SimpleInterface(BaseInterface):
""" An interface pattern that allows outputs to be set in a dictionary
Expand Down
2 changes: 2 additions & 0 deletions nipype/interfaces/mixins/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .reporting import (
ReportCapableInterface, ReportCapableInputSpec, ReportCapableOutputSpec)
65 changes: 65 additions & 0 deletions nipype/interfaces/mixins/reporting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# -*- coding: utf-8 -*-
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
""" class mixin and utilities for enabling reports for nipype interfaces """
from __future__ import (print_function, division, unicode_literals,
absolute_import)

import os
from abc import abstractmethod

from ... import logging
from ..base import (
File, BaseInterface, BaseInterfaceInputSpec, TraitedSpec)

iflogger = logging.getLogger('interface')


class ReportCapableInputSpec(BaseInterfaceInputSpec):
out_report = File('report', usedefault=True, hash_files=False,
desc='filename for the visual report')


class ReportCapableOutputSpec(TraitedSpec):
out_report = File(desc='filename for the visual report')


class ReportCapableInterface(BaseInterface):
"""Mixin to enable reporting for Nipype interfaces"""
_out_report = None

def __init__(self, generate_report=False, **kwargs):
super(ReportCapableInterface, self).__init__(**kwargs)
self.generate_report = generate_report

def _post_run_hook(self, runtime):
runtime = super(ReportCapableInterface, self)._post_run_hook(runtime)

# leave early if there's nothing to do
if not self.generate_report:
return runtime

self._out_report = self.inputs.out_report
if not os.path.isabs(self._out_report):
self._out_report = os.path.abspath(os.path.join(runtime.cwd,
self._out_report))

self._generate_report()

return runtime

def _list_outputs(self):
try:
outputs = super(ReportCapableInterface, self)._list_outputs()
except NotImplementedError:
outputs = {}
if self._out_report is not None:
outputs['out_report'] = self._out_report
return outputs

@abstractmethod
def _generate_report(self):
"""
Saves report to file identified by _out_report instance variable
"""
raise NotImplementedError