|
72 | 72 | from .base import EngineBase |
73 | 73 |
|
74 | 74 |
|
75 | | -def _write_inputs(node): |
76 | | - lines = [] |
77 | | - nodename = node.fullname.replace('.', '_') |
78 | | - for key, _ in node.inputs.items(): |
79 | | - val = getattr(node.inputs, key) |
80 | | - if isdefined(val): |
81 | | - if type(val) == str: |
82 | | - try: |
83 | | - func = create_function_from_source(val) |
84 | | - except RuntimeError: |
85 | | - lines.append("%s.inputs.%s = '%s'" % (nodename, key, val)) |
86 | | - else: |
87 | | - funcname = [name for name in func.func_globals |
88 | | - if name != '__builtins__'][0] |
89 | | - lines.append(cPickle.loads(val)) |
90 | | - if funcname == nodename: |
91 | | - lines[-1] = lines[-1].replace(' %s(' % funcname, |
92 | | - ' %s_1(' % funcname) |
93 | | - funcname = '%s_1' % funcname |
94 | | - lines.append('from nipype.utils.misc import getsource') |
95 | | - lines.append("%s.inputs.%s = getsource(%s)" % (nodename, |
96 | | - key, |
97 | | - funcname)) |
98 | | - else: |
99 | | - lines.append('%s.inputs.%s = %s' % (nodename, key, val)) |
100 | | - return lines |
101 | | - |
102 | | - |
103 | | -def format_node(node, format='python', include_config=False): |
104 | | - """Format a node in a given output syntax.""" |
105 | | - lines = [] |
106 | | - name = node.fullname.replace('.', '_') |
107 | | - if format == 'python': |
108 | | - klass = node._interface |
109 | | - importline = 'from %s import %s' % (klass.__module__, |
110 | | - klass.__class__.__name__) |
111 | | - comment = '# Node: %s' % node.fullname |
112 | | - spec = inspect.getargspec(node._interface.__init__) |
113 | | - args = spec.args[1:] |
114 | | - if args: |
115 | | - filled_args = [] |
116 | | - for arg in args: |
117 | | - if hasattr(node._interface, '_%s' % arg): |
118 | | - filled_args.append('%s=%s' % (arg, getattr(node._interface, |
119 | | - '_%s' % arg))) |
120 | | - args = ', '.join(filled_args) |
121 | | - else: |
122 | | - args = '' |
123 | | - klass_name = klass.__class__.__name__ |
124 | | - if isinstance(node, MapNode): |
125 | | - nodedef = '%s = MapNode(%s(%s), iterfield=%s, name="%s")' \ |
126 | | - % (name, klass_name, args, node.iterfield, name) |
127 | | - else: |
128 | | - nodedef = '%s = Node(%s(%s), name="%s")' \ |
129 | | - % (name, klass_name, args, name) |
130 | | - lines = [importline, comment, nodedef] |
131 | | - |
132 | | - if include_config: |
133 | | - lines = [importline, "from collections import OrderedDict", |
134 | | - comment, nodedef] |
135 | | - lines.append('%s.config = %s' % (name, node.config)) |
136 | | - |
137 | | - if node.iterables is not None: |
138 | | - lines.append('%s.iterables = %s' % (name, node.iterables)) |
139 | | - lines.extend(_write_inputs(node)) |
140 | | - |
141 | | - return lines |
142 | | - |
143 | | - |
144 | 75 | class WorkflowBase(object): |
145 | 76 | """Defines common attributes and functions for workflows and nodes.""" |
146 | 77 |
|
|
0 commit comments