|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- |
| 4 | +# vi: set ft=python sts=4 ts=4 sw=4 et: |
| 5 | +"""Defines functionality for pipelined execution of interfaces |
| 6 | +
|
| 7 | +The `EngineBase` class implements the more general view of a task. |
| 8 | +
|
| 9 | + .. testsetup:: |
| 10 | + # Change directory to provide relative paths for doctests |
| 11 | + import os |
| 12 | + filepath = os.path.dirname(os.path.realpath( __file__ )) |
| 13 | + datadir = os.path.realpath(os.path.join(filepath, '../../testing/data')) |
| 14 | + os.chdir(datadir) |
| 15 | +
|
| 16 | +""" |
| 17 | + |
| 18 | +from future import standard_library |
| 19 | +standard_library.install_aliases() |
| 20 | +from builtins import object |
| 21 | + |
| 22 | +try: |
| 23 | + from collections import OrderedDict |
| 24 | +except ImportError: |
| 25 | + from ordereddict import OrderedDict |
| 26 | + |
| 27 | +from copy import deepcopy |
| 28 | +import re |
| 29 | +import numpy as np |
| 30 | +from ...interfaces.traits_extension import traits, Undefined |
| 31 | +from ...interfaces.base import DynamicTraitedSpec |
| 32 | +from ...utils.filemanip import loadpkl, savepkl |
| 33 | + |
| 34 | +from ... import logging |
| 35 | +logger = logging.getLogger('workflow') |
| 36 | + |
| 37 | + |
| 38 | +class EngineBase(object): |
| 39 | + """Defines common attributes and functions for workflows and nodes.""" |
| 40 | + |
| 41 | + def __init__(self, name=None, base_dir=None): |
| 42 | + """ Initialize base parameters of a workflow or node |
| 43 | +
|
| 44 | + Parameters |
| 45 | + ---------- |
| 46 | + name : string (mandatory) |
| 47 | + Name of this node. Name must be alphanumeric and not contain any |
| 48 | + special characters (e.g., '.', '@'). |
| 49 | + base_dir : string |
| 50 | + base output directory (will be hashed before creations) |
| 51 | + default=None, which results in the use of mkdtemp |
| 52 | +
|
| 53 | + """ |
| 54 | + self.base_dir = base_dir |
| 55 | + self.config = None |
| 56 | + self._verify_name(name) |
| 57 | + self.name = name |
| 58 | + # for compatibility with node expansion using iterables |
| 59 | + self._id = self.name |
| 60 | + self._hierarchy = None |
| 61 | + |
| 62 | + @property |
| 63 | + def inputs(self): |
| 64 | + raise NotImplementedError |
| 65 | + |
| 66 | + @property |
| 67 | + def outputs(self): |
| 68 | + raise NotImplementedError |
| 69 | + |
| 70 | + @property |
| 71 | + def fullname(self): |
| 72 | + fullname = self.name |
| 73 | + if self._hierarchy: |
| 74 | + fullname = self._hierarchy + '.' + self.name |
| 75 | + return fullname |
| 76 | + |
| 77 | + def clone(self, name): |
| 78 | + """Clone an EngineBase object |
| 79 | +
|
| 80 | + Parameters |
| 81 | + ---------- |
| 82 | +
|
| 83 | + name : string (mandatory) |
| 84 | + A clone of node or workflow must have a new name |
| 85 | + """ |
| 86 | + if (name is None) or (name == self.name): |
| 87 | + raise Exception('Cloning requires a new name') |
| 88 | + self._verify_name(name) |
| 89 | + clone = deepcopy(self) |
| 90 | + clone.name = name |
| 91 | + clone._id = name |
| 92 | + clone._hierarchy = None |
| 93 | + return clone |
| 94 | + |
| 95 | + def _check_outputs(self, parameter): |
| 96 | + return hasattr(self.outputs, parameter) |
| 97 | + |
| 98 | + def _check_inputs(self, parameter): |
| 99 | + if isinstance(self.inputs, DynamicTraitedSpec): |
| 100 | + return True |
| 101 | + return hasattr(self.inputs, parameter) |
| 102 | + |
| 103 | + def _verify_name(self, name): |
| 104 | + valid_name = bool(re.match('^[\w-]+$', name)) |
| 105 | + if not valid_name: |
| 106 | + raise ValueError('[Workflow|Node] name \'%s\' contains' |
| 107 | + ' special characters' % name) |
| 108 | + |
| 109 | + def __repr__(self): |
| 110 | + if self._hierarchy: |
| 111 | + return '.'.join((self._hierarchy, self._id)) |
| 112 | + else: |
| 113 | + return self._id |
| 114 | + |
| 115 | + def save(self, filename=None): |
| 116 | + if filename is None: |
| 117 | + filename = 'temp.pklz' |
| 118 | + savepkl(filename, self) |
| 119 | + |
| 120 | + def load(self, filename): |
| 121 | + if '.npz' in filename: |
| 122 | + DeprecationWarning(('npz files will be deprecated in the next ' |
| 123 | + 'release. you can use numpy to open them.')) |
| 124 | + return np.load(filename) |
| 125 | + return loadpkl(filename) |
0 commit comments