Skip to content

Commit 982822c

Browse files
committed
ENH: Add ReportCapableInterface mix-in/base class
1 parent 854afe6 commit 982822c

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

nipype/interfaces/mixins/reporting.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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, traits, BaseInterface, BaseInterfaceInputSpec, TraitedSpec)
14+
15+
iflogger = logging.getLogger('interface')
16+
17+
18+
class ReportCapableInputSpec(BaseInterfaceInputSpec):
19+
generate_report = traits.Bool(False, usedefault=True,
20+
desc="Enable report generation")
21+
out_report = File('report', usedefault=True, hash_files=False,
22+
desc='filename for the visual report')
23+
24+
25+
class ReportCapableOutputSpec(TraitedSpec):
26+
out_report = File(desc='filename for the visual report')
27+
28+
29+
class ReportCapableInterface(BaseInterface):
30+
"""Mixin to enable reporting for Nipype interfaces"""
31+
_out_report = None
32+
33+
def _post_run_hook(self, runtime):
34+
runtime = super(ReportCapableInterface, self)._post_run_hook(runtime)
35+
36+
# leave early if there's nothing to do
37+
if not self.inputs.generate_report:
38+
return runtime
39+
40+
self._out_report = os.path.abspath(self.inputs.out_report)
41+
self._generate_report()
42+
43+
return runtime
44+
45+
def _list_outputs(self):
46+
try:
47+
outputs = super(ReportCapableInterface, self)._list_outputs()
48+
except NotImplementedError:
49+
outputs = {}
50+
if self._out_report is not None:
51+
outputs['out_report'] = self._out_report
52+
return outputs
53+
54+
@abstractmethod
55+
def _generate_report(self):
56+
"""
57+
Saves report to file identified by _out_report instance variable
58+
"""
59+
raise NotImplementedError

0 commit comments

Comments
 (0)