-
Notifications
You must be signed in to change notification settings - Fork 532
/
Copy pathexperimental.py
348 lines (283 loc) · 11.2 KB
/
experimental.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
"""Experimental Nipype 1.99 interfaces."""
import os
import sys
import platform
import json
from io import StringIO
from string import Formatter
from contextlib import AbstractContextManager
from copy import deepcopy
from datetime import datetime as dt
from dateutil.parser import parse as parseutc
from ... import config, logging
from ...utils.misc import str2bool, rgetcwd
from ...utils.provenance import write_provenance
from .support import Bunch, InterfaceResult
from .traits_extension import isdefined
iflogger = logging.getLogger("nipype.interface")
class Interface:
"""An abstract definition for interfaces."""
_input_spec = None
"""A traited input specification"""
_output_spec = None
"""A traited output specification"""
_redirect_x = False
@classmethod
def exec(cls, **inputs):
"""Instantiate the interface and run it."""
return cls(**inputs).run()
def __init__(self, from_file=None, resource_monitor=None, **inputs):
"""Initialize an interface."""
if self._input_spec is None:
raise TypeError(
"Input specification type not set for interface "
'"%s"' % self.__class__.__name__)
if self._output_spec is None:
raise TypeError(
"Output specification type not set for interface "
'"%s"' % self.__class__.__name__)
self._resource_monitor = config.resource_monitor
if resource_monitor is not None:
self._resource_monitor = self._resource_monitor and bool(resource_monitor)
# Initialize input object
self.inputs = self._input_spec(**inputs)
# Initialize inputs from JSON file
if from_file is not None:
self.from_json(from_file, overwrite=True)
for name, value in list(inputs.items()):
setattr(self.inputs, name, value)
def run(self, cwd=None):
"""
Execute this interface.
This interface will not raise an exception if runtime.returncode is
non-zero.
Parameters
----------
cwd : specify a folder where the interface should be run
inputs : allows the interface settings to be updated
Returns
-------
results : :obj:`nipype.interfaces.base.support.InterfaceResult`
A copy of the instance that was executed, provenance information and,
if successful, results
"""
from ...utils.profiler import ResourceMonitor
# Tear-up: get current and prev directories
syscwd = rgetcwd(error=False) # Recover when wd does not exist
if cwd is None:
cwd = syscwd
os.chdir(cwd) # Change to the interface wd
env = deepcopy(dict(os.environ))
if self._redirect_x:
env["DISPLAY"] = config.get_display()
runtime = Bunch(
cwd=cwd,
prevcwd=syscwd,
returncode=None,
duration=None,
environ=env,
startTime=dt.isoformat(dt.utcnow()),
endTime=None,
platform=platform.platform(),
hostname=platform.node(),
)
runtime_attrs = set(runtime.dictcopy())
runtime = self._pre_run_hook(runtime)
mon_sp = None
if self._resource_monitor:
mon_freq = float(config.get("execution", "resource_monitor_frequency", 1))
proc_pid = os.getpid()
iflogger.debug(
"Creating a ResourceMonitor on a %s interface, PID=%d.",
self.__class__.__name__,
proc_pid,
)
mon_sp = ResourceMonitor(proc_pid, freq=mon_freq)
mon_sp.start()
# Grab inputs now, as they should not change during execution
inputs = self.inputs.get_traitsfree()
stdout = StringIO()
stderr = StringIO()
try:
with RedirectStandardStreams(stdout, stderr=stderr):
runtime = self._run_interface(runtime)
except Exception as e:
import traceback
# Retrieve the maximum info fast
runtime.traceback = traceback.format_exc()
# Gather up the exception arguments and append nipype info.
exc_args = e.args if getattr(e, "args") else tuple()
exc_args += (
"An exception of type %s occurred while running interface %s."
% (type(e).__name__, self.__class__.__name__),
)
if config.get("logging", "interface_level", "info").lower() == "debug":
exc_args += ("Inputs: %s" % str(self.inputs),)
runtime.traceback_args = ("\n".join(["%s" % arg for arg in exc_args]),)
stderr.write("Nipype captured error:\n\n%s" % runtime.traceback)
raise
else:
# Execute post-hook only if successful
runtime = self._post_run_hook(runtime)
finally:
if runtime is None or runtime_attrs - set(runtime.dictcopy()):
raise RuntimeError(
"{} interface failed to return valid "
"runtime object".format(self.__class__.__name__)
)
# This needs to be done always
runtime.endTime = dt.isoformat(dt.utcnow())
timediff = parseutc(runtime.endTime) - parseutc(runtime.startTime)
runtime.duration = (
timediff.days * 86400 + timediff.seconds + timediff.microseconds / 1e6
)
results = InterfaceResult(
self.__class__, runtime, inputs=inputs, outputs=None, provenance=None
)
# Add provenance (if required)
if str2bool(config.get("execution", "write_provenance", "false")):
# Provenance will only throw a warning if something went wrong
results.provenance = write_provenance(results)
# Make sure runtime profiler is shut down
if self._resource_monitor:
import numpy as np
mon_sp.stop()
runtime.mem_peak_gb = None
runtime.cpu_percent = None
# Read .prof file in and set runtime values
vals = np.loadtxt(mon_sp.fname, delimiter=",")
if vals.size:
vals = np.atleast_2d(vals)
runtime.mem_peak_gb = vals[:, 2].max() / 1024
runtime.cpu_percent = vals[:, 1].max()
runtime.prof_dict = {
"time": vals[:, 0].tolist(),
"cpus": vals[:, 1].tolist(),
"rss_GiB": (vals[:, 2] / 1024).tolist(),
"vms_GiB": (vals[:, 3] / 1024).tolist(),
}
results.runtime = runtime
# Store captured outputs
runtime.stdout = stdout.getvalue()
runtime.stderr = stderr.getvalue()
del stdout
del stderr
results.outputs = self._find_outputs(runtime)
os.chdir(syscwd)
return results
def from_json(self, json_file, overwrite=True):
"""Import inputs from a JSON file."""
with open(json_file) as fhandle:
inputs_dict = json.load(fhandle)
def_inputs = set()
if not overwrite:
def_inputs = {i for i in self.inputs.get_traitsfree().keys()}
new_inputs = set(inputs_dict.keys()) - def_inputs
for key in new_inputs:
if hasattr(self.inputs, key):
setattr(self.inputs, key, inputs_dict[key])
def _run_interface(self, runtime):
"""Execute the body of this interface."""
raise NotImplementedError
def _find_outputs(self, runtime):
"""
Automagically fill in output fields.
Returns
-------
outputs : :obj:`traits.HasTraits`
Collected outputs
"""
outputs = self._output_spec()
inputs = self.inputs.get_traitsfree()
for name, spec in list(outputs.traits(transient=None).items()):
if spec.stdout is True:
setattr(outputs, name, runtime.stdout)
continue
elif callable(spec.stdout):
setattr(outputs, name, spec.stdout(runtime.stdout))
continue
if spec.stderr is True:
setattr(outputs, name, runtime.stderr)
continue
elif callable(spec.stderr):
setattr(outputs, name, spec.stderr(runtime.stderr))
continue
out_template = getattr(outputs, name)
if not isdefined(out_template):
continue
template_fields = {pat[1] for pat in Formatter().parse(out_template)
if pat[1] and not pat[1].isdigit()}
if template_fields.intersection(inputs.keys()):
fields = {}
for field in template_fields:
fname = os.path.basename(inputs[field])
fname, ext = os.path.splitext(fname)
if ext == '.gz':
fname, ext0 = os.path.splitext(fname)
ext = ''.join((ext0, ext))
fields[field] = fname
# Only the last extension is kept, if several template
# names
setattr(outputs, name,
''.join((out_template.format(**fields), ext)))
return outputs
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 RedirectStandardStreams(AbstractContextManager):
"""
Context that redirects standard out/err.
Examples
--------
>>> f = StringIO()
>>> with RedirectStandardStreams(f):
... print("1")
... print("2", file=sys.stderr)
>>> captured = f.getvalue()
>>> "1" in captured
True
>>> "2" in captured
True
>>> out = StringIO()
>>> err = StringIO()
>>> with RedirectStandardStreams(out, err):
... print("1")
... print("2", file=sys.stderr)
>>> captured_out = out.getvalue()
>>> "1" in captured_out
True
>>> "2" in captured_out
False
>>> captured_err = err.getvalue()
>>> "1" in captured_err
False
>>> "2" in captured_err
True
"""
_defaults = (sys.stdout, sys.stderr)
def __init__(self, stdout, stderr=None):
"""Redirect standard streams."""
self._out_target = stdout
self._err_target = stderr
def __enter__(self):
sys.stdout = self._out_target
sys.stderr = self._err_target
if self._err_target is None:
sys.stderr = self._out_target
return self._out_target
return self._out_target, self._err_target
def __exit__(self, exctype, excinst, exctb):
sys.stdout, sys.stderr = self._defaults