|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- |
| 3 | +# vi: set ft=python sts=4 ts=4 sw=4 et: |
| 4 | +""" class mixin and utilities for enabling reports for nipype interfaces """ |
| 5 | +from __future__ import (print_function, division, unicode_literals, |
| 6 | + absolute_import) |
| 7 | + |
| 8 | +import os |
| 9 | +from abc import abstractmethod |
| 10 | + |
| 11 | +from ... import logging |
| 12 | +from ..base import ( |
| 13 | + File, BaseInterface, BaseInterfaceInputSpec, TraitedSpec) |
| 14 | + |
| 15 | +iflogger = logging.getLogger('interface') |
| 16 | + |
| 17 | + |
| 18 | +class ReportCapableInputSpec(BaseInterfaceInputSpec): |
| 19 | + out_report = File('report', usedefault=True, hash_files=False, |
| 20 | + desc='filename for the visual report') |
| 21 | + |
| 22 | + |
| 23 | +class ReportCapableOutputSpec(TraitedSpec): |
| 24 | + out_report = File(desc='filename for the visual report') |
| 25 | + |
| 26 | + |
| 27 | +class ReportCapableInterface(BaseInterface): |
| 28 | + """Mixin to enable reporting for Nipype interfaces""" |
| 29 | + _out_report = None |
| 30 | + |
| 31 | + def __init__(self, generate_report=False, **kwargs): |
| 32 | + super(ReportCapableInterface, self).__init__(**kwargs) |
| 33 | + self.generate_report = generate_report |
| 34 | + |
| 35 | + def _post_run_hook(self, runtime): |
| 36 | + runtime = super(ReportCapableInterface, self)._post_run_hook(runtime) |
| 37 | + |
| 38 | + # leave early if there's nothing to do |
| 39 | + if not self.generate_report: |
| 40 | + return runtime |
| 41 | + |
| 42 | + self._out_report = self.inputs.out_report |
| 43 | + if not os.path.isabs(self._out_report): |
| 44 | + self._out_report = os.path.abspath(os.path.join(runtime.cwd, |
| 45 | + self._out_report)) |
| 46 | + |
| 47 | + self._generate_report() |
| 48 | + |
| 49 | + return runtime |
| 50 | + |
| 51 | + def _list_outputs(self): |
| 52 | + try: |
| 53 | + outputs = super(ReportCapableInterface, self)._list_outputs() |
| 54 | + except NotImplementedError: |
| 55 | + outputs = {} |
| 56 | + if self._out_report is not None: |
| 57 | + outputs['out_report'] = self._out_report |
| 58 | + return outputs |
| 59 | + |
| 60 | + @abstractmethod |
| 61 | + def _generate_report(self): |
| 62 | + """ |
| 63 | + Saves report to file identified by _out_report instance variable |
| 64 | + """ |
| 65 | + raise NotImplementedError |
0 commit comments