forked from nipy/nipype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodes.py
1395 lines (1204 loc) · 50.9 KB
/
nodes.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""Defines functionality for pipelined execution of interfaces
The `Node` class provides core functionality for batch processing.
"""
from collections import OrderedDict, defaultdict
import os
import os.path as op
from pathlib import Path
import shutil
import socket
from copy import deepcopy
from glob import glob
from logging import INFO
from tempfile import mkdtemp
from ... import config, logging
from ...utils.misc import flatten, unflatten, str2bool, dict_diff
from ...utils.filemanip import (
md5,
ensure_list,
simplify_list,
copyfiles,
fnames_presuffix,
loadpkl,
split_filename,
load_json,
emptydirs,
savepkl,
silentrm,
)
from ...interfaces.base import (
traits,
InputMultiPath,
CommandLine,
Undefined,
DynamicTraitedSpec,
Bunch,
InterfaceResult,
Interface,
isdefined,
)
from ...interfaces.base.specs import get_filecopy_info
from .utils import (
_parameterization_dir,
save_hashfile as _save_hashfile,
load_resultfile as _load_resultfile,
save_resultfile as _save_resultfile,
nodelist_runner as _node_runner,
strip_temp as _strip_temp,
write_node_report,
clean_working_directory,
merge_dict,
evaluate_connect_function,
)
from .base import EngineBase
logger = logging.getLogger("nipype.workflow")
class NodeExecutionError(RuntimeError):
"""A nipype-specific name for exceptions when executing a Node."""
class Node(EngineBase):
"""
Wraps interface objects for use in pipeline
A Node creates a sandbox-like directory for executing the underlying
interface. It will copy or link inputs into this directory to ensure that
input data are not overwritten. A hash of the input state is used to
determine if the Node inputs have changed and whether the node needs to be
re-executed.
Examples
--------
>>> from nipype import Node
>>> from nipype.interfaces import spm
>>> realign = Node(spm.Realign(), 'realign')
>>> realign.inputs.in_files = 'functional.nii'
>>> realign.inputs.register_to_mean = True
>>> realign.run() # doctest: +SKIP
"""
def __init__(
self,
interface,
name,
iterables=None,
itersource=None,
synchronize=False,
overwrite=None,
needed_outputs=None,
run_without_submitting=False,
n_procs=None,
mem_gb=0.20,
**kwargs,
):
"""
Parameters
----------
interface : interface object
node specific interface (fsl.Bet(), spm.Coregister())
name : alphanumeric string
node specific name
iterables : generator
Input field and list to iterate using the pipeline engine
for example to iterate over different frac values in fsl.Bet()
for a single field the input can be a tuple, otherwise a list
of tuples ::
node.iterables = ('frac',[0.5,0.6,0.7])
node.iterables = [('fwhm',[2,4]),('fieldx',[0.5,0.6,0.7])]
If this node has an itersource, then the iterables values
is a dictionary which maps an iterable source field value
to the target iterables field values, e.g.: ::
inputspec.iterables = ('images',['img1.nii', 'img2.nii']])
node.itersource = ('inputspec', ['frac'])
node.iterables = ('frac', {'img1.nii': [0.5, 0.6],
'img2.nii': [0.6, 0.7]})
If this node's synchronize flag is set, then an alternate
form of the iterables is a [fields, values] list, where
fields is the list of iterated fields and values is the
list of value tuples for the given fields, e.g.: ::
node.synchronize = True
node.iterables = [('frac', 'threshold'),
[(0.5, True),
(0.6, False)]]
itersource: tuple
The (name, fields) iterables source which specifies the name
of the predecessor iterable node and the input fields to use
from that source node. The output field values comprise the
key to the iterables parameter value mapping dictionary.
synchronize: boolean
Flag indicating whether iterables are synchronized.
If the iterables are synchronized, then this iterable
node is expanded once per iteration over all of the
iterables values.
Otherwise, this iterable node is expanded once per
each permutation of the iterables values.
overwrite : Boolean
Whether to overwrite contents of output directory if it already
exists. If directory exists and hash matches it
assumes that process has been executed
needed_outputs : list of output_names
Force the node to keep only specific outputs. By default all
outputs are kept. Setting this attribute will delete any output
files and directories from the node's working directory that are
not part of the `needed_outputs`.
run_without_submitting : boolean
Run the node without submitting to a job engine or to a
multiprocessing pool
"""
# Make sure an interface is set, and that it is an Interface
if interface is None:
raise IOError("Interface must be provided")
if not isinstance(interface, Interface):
raise IOError("interface must be an instance of an Interface")
super(Node, self).__init__(name, kwargs.get("base_dir"))
self._interface = interface
self._hierarchy = None
self._got_inputs = False
self._originputs = None
self._output_dir = None
self.iterables = iterables
self.synchronize = synchronize
self.itersource = itersource
self.overwrite = overwrite
self.parameterization = []
self.input_source = {}
self.plugin_args = {}
self.run_without_submitting = run_without_submitting
self._mem_gb = mem_gb
self._n_procs = n_procs
# Downstream n_procs
if hasattr(self._interface.inputs, "num_threads") and self._n_procs is not None:
self._interface.inputs.num_threads = self._n_procs
# Initialize needed_outputs and hashes
self._hashvalue = None
self._hashed_inputs = None
self._needed_outputs = []
self.needed_outputs = needed_outputs
self.config = None
if hasattr(self._interface, "write_cmdline"):
self._interface.write_cmdline = True
@property
def interface(self):
"""Return the underlying interface object"""
return self._interface
@property
def result(self):
"""Get result from result file (do not hold it in memory)"""
return _load_resultfile(
op.join(self.output_dir(), "result_%s.pklz" % self.name)
)
@property
def inputs(self):
"""Return the inputs of the underlying interface"""
return self._interface.inputs
@property
def outputs(self):
"""Return the output fields of the underlying interface"""
return self._interface._outputs()
@property
def needed_outputs(self):
return self._needed_outputs
@needed_outputs.setter
def needed_outputs(self, new_outputs):
"""Needed outputs changes the hash, refresh if changed"""
new_outputs = sorted(list(set(new_outputs or [])))
if new_outputs != self._needed_outputs:
# Reset hash
self._hashvalue = None
self._hashed_inputs = None
self._needed_outputs = new_outputs
@property
def mem_gb(self):
"""Get estimated memory (GB)"""
if hasattr(self._interface, "estimated_memory_gb"):
self._mem_gb = self._interface.estimated_memory_gb
logger.warning(
'Setting "estimated_memory_gb" on Interfaces has been '
"deprecated as of nipype 1.0, please use Node.mem_gb."
)
return self._mem_gb
@property
def n_procs(self):
"""Get the estimated number of processes/threads"""
if self._n_procs is not None:
return self._n_procs
if hasattr(self._interface.inputs, "num_threads") and isdefined(
self._interface.inputs.num_threads
):
return self._interface.inputs.num_threads
return 1
@n_procs.setter
def n_procs(self, value):
"""Set an estimated number of processes/threads"""
self._n_procs = value
# Overwrite interface's dynamic input of num_threads
if hasattr(self._interface.inputs, "num_threads"):
self._interface.inputs.num_threads = self._n_procs
def output_dir(self):
"""Return the location of the output directory for the node"""
# Output dir is cached
if self._output_dir:
return self._output_dir
# Calculate & cache otherwise
if self.base_dir is None:
self.base_dir = mkdtemp()
outputdir = self.base_dir
if self._hierarchy:
outputdir = op.join(outputdir, *self._hierarchy.split("."))
if self.parameterization:
params_str = ["{}".format(p) for p in self.parameterization]
if not str2bool(self.config["execution"]["parameterize_dirs"]):
params_str = [_parameterization_dir(p,32) for p in params_str]
params_str = [_parameterization_dir(p,252) for p in params_str]
outputdir = op.join(outputdir, *params_str)
self._output_dir = op.realpath(op.join(outputdir, self.name))
return self._output_dir
def set_input(self, parameter, val):
"""Set interface input value"""
logger.debug(
"[Node] %s - setting input %s = %s", self.name, parameter, str(val)
)
setattr(self.inputs, parameter, deepcopy(val))
def get_output(self, parameter):
"""Retrieve a particular output of the node"""
return getattr(self.result.outputs, parameter, None)
def help(self):
"""Print interface help"""
self._interface.help()
def is_cached(self, rm_outdated=False):
"""
Check if the interface has been run previously, and whether
cached results are up-to-date.
"""
outdir = self.output_dir()
# The output folder does not exist: not cached
if not op.exists(outdir) or not op.exists(
op.join(outdir, "result_%s.pklz" % self.name)
):
logger.debug('[Node] Not cached "%s".', outdir)
return False, False
# Check if there are hashfiles
globhashes = glob(op.join(outdir, "_0x*.json"))
unfinished = [path for path in globhashes if path.endswith("_unfinished.json")]
hashfiles = list(set(globhashes) - set(unfinished))
# Update hash
hashed_inputs, hashvalue = self._get_hashval()
hashfile = op.join(outdir, "_0x%s.json" % hashvalue)
logger.debug(
"[Node] Hashes: %s, %s, %s, %s",
hashed_inputs,
hashvalue,
hashfile,
hashfiles,
)
cached = hashfile in hashfiles
# No previous hashfiles found, we're all set.
if cached and len(hashfiles) == 1:
assert hashfile == hashfiles[0]
logger.debug('[Node] Up-to-date cache found for "%s".', self.fullname)
return True, True # Cached and updated
if len(hashfiles) > 1:
if cached:
hashfiles.remove(hashfile) # Do not clean up the node, if cached
logger.warning(
"[Node] Found %d previous hashfiles indicating that the working "
'directory of node "%s" is stale, deleting old hashfiles.',
len(hashfiles),
self.fullname,
)
for rmfile in hashfiles:
os.remove(rmfile)
hashfiles = [hashfile] if cached else []
if not hashfiles:
logger.debug('[Node] No hashfiles found in "%s".', outdir)
assert not cached
return False, False
# At this point only one hashfile is in the folder
# and we directly check whether it is updated
updated = hashfile == hashfiles[0]
if not updated: # Report differences depending on log verbosity
cached = True
logger.info('[Node] Outdated cache found for "%s".', self.fullname)
# If logging is more verbose than INFO (20), print diff between hashes
loglevel = logger.getEffectiveLevel()
if loglevel < INFO: # Lazy logging: only < INFO
exp_hash_file_base = split_filename(hashfiles[0])[1]
exp_hash = exp_hash_file_base[len("_0x") :]
logger.log(
loglevel, "[Node] Old/new hashes = %s/%s", exp_hash, hashvalue
)
try:
prev_inputs = load_json(hashfiles[0])
except Exception:
pass
else:
logger.log(loglevel, dict_diff(prev_inputs, hashed_inputs, 10))
if rm_outdated:
os.remove(hashfiles[0])
assert cached # At this point, node is cached (may not be up-to-date)
return cached, updated
def hash_exists(self, updatehash=False):
"""
Decorate the new `is_cached` method with hash updating
to maintain backwards compatibility.
"""
# Get a dictionary with hashed filenames and a hashvalue
# of the dictionary itself.
cached, updated = self.is_cached(rm_outdated=True)
outdir = self.output_dir()
hashfile = op.join(outdir, "_0x%s.json" % self._hashvalue)
if updated:
return True, self._hashvalue, hashfile, self._hashed_inputs
# Update only possible if it exists
if cached and updatehash:
logger.debug("[Node] Updating hash: %s", self._hashvalue)
_save_hashfile(hashfile, self._hashed_inputs)
return cached, self._hashvalue, hashfile, self._hashed_inputs
def run(self, updatehash=False):
"""
Execute the node in its directory.
Parameters
----------
updatehash: boolean
When the hash stored in the output directory as a result of a previous run
does not match that calculated for this execution, updatehash=True only
updates the hash without re-running.
"""
if self.config is None:
self.config = {}
self.config = merge_dict(deepcopy(config._sections), self.config)
outdir = self.output_dir()
force_run = self.overwrite or (
self.overwrite is None and self._interface.always_run
)
# Check hash, check whether run should be enforced
if not isinstance(self, MapNode):
logger.info(f'[Node] Setting-up "{self.fullname}" in "{outdir}".')
cached, updated = self.is_cached()
# If the node is cached, check on pklz files and finish
if not force_run and (updated or (not updated and updatehash)):
logger.debug("Only updating node hashes or skipping execution")
inputs_file = op.join(outdir, "_inputs.pklz")
if not op.exists(inputs_file):
logger.debug("Creating inputs file %s", inputs_file)
savepkl(inputs_file, self.inputs.get_traitsfree())
node_file = op.join(outdir, "_node.pklz")
if not op.exists(node_file):
logger.debug("Creating node file %s", node_file)
savepkl(node_file, self)
result = self._run_interface(
execute=False, updatehash=updatehash and not updated
)
logger.info(
'[Node] "%s" found cached%s.',
self.fullname,
" (and hash updated)" * (updatehash and not updated),
)
return result
if cached and updated and not isinstance(self, MapNode):
logger.debug('[Node] Rerunning cached, up-to-date node "%s"', self.fullname)
if not force_run and str2bool(
self.config["execution"]["stop_on_first_rerun"]
):
raise Exception(
'Cannot rerun when "stop_on_first_rerun" is set to True'
)
# Remove any hashfile that exists at this point (re)running.
if cached:
for outdatedhash in glob(op.join(self.output_dir(), "_0x*.json")):
os.remove(outdatedhash)
# _get_hashval needs to be called before running. When there is a valid (or seemingly
# valid cache), the is_cached() member updates the hashval via _get_hashval.
# However, if this node's folder doesn't exist or the result file is not found, then
# the hashval needs to be generated here. See #3026 for a larger context.
self._get_hashval()
# Hashfile while running
hashfile_unfinished = op.join(outdir, "_0x%s_unfinished.json" % self._hashvalue)
# Delete directory contents if this is not a MapNode or can't resume
can_resume = not (self._interface.can_resume and op.isfile(hashfile_unfinished))
if can_resume and not isinstance(self, MapNode):
emptydirs(outdir, noexist_ok=True)
else:
logger.debug(
"[%sNode] Resume - hashfile=%s",
"Map" * int(isinstance(self, MapNode)),
hashfile_unfinished,
)
if isinstance(self, MapNode):
# remove old json files
for filename in glob(op.join(outdir, "_0x*.json")):
os.remove(filename)
# Make sure outdir is created
os.makedirs(outdir, exist_ok=True)
# Store runtime-hashfile, pre-execution report, the node and the inputs set.
_save_hashfile(hashfile_unfinished, self._hashed_inputs)
write_node_report(self, is_mapnode=isinstance(self, MapNode))
savepkl(op.join(outdir, "_node.pklz"), self)
savepkl(op.join(outdir, "_inputs.pklz"), self.inputs.get_traitsfree())
try:
result = self._run_interface(execute=True)
except Exception:
logger.warning('[Node] Error on "%s" (%s)', self.fullname, outdir)
# Tear-up after error
if not silentrm(hashfile_unfinished):
logger.warning(
"""\
Interface finished unexpectedly and the corresponding unfinished hashfile %s \
does not exist. Another nipype instance may be running against the same work \
directory. Please ensure no other concurrent workflows are racing""",
hashfile_unfinished,
)
raise
# Tear-up after success
shutil.move(hashfile_unfinished, hashfile_unfinished.replace("_unfinished", ""))
write_node_report(self, result=result, is_mapnode=isinstance(self, MapNode))
return result
def _get_hashval(self):
"""Return a hash of the input state"""
self._get_inputs()
if self._hashvalue is None and self._hashed_inputs is None:
self._hashed_inputs, self._hashvalue = self.inputs.get_hashval(
hash_method=self.config["execution"]["hash_method"]
)
rm_extra = self.config["execution"]["remove_unnecessary_outputs"]
if str2bool(rm_extra) and self.needed_outputs:
hashobject = md5()
hashobject.update(self._hashvalue.encode())
hashobject.update(str(self.needed_outputs).encode())
self._hashvalue = hashobject.hexdigest()
self._hashed_inputs.append(("needed_outputs", self.needed_outputs))
return self._hashed_inputs, self._hashvalue
def _get_inputs(self):
"""
Retrieve inputs from pointers to results files.
This mechanism can be easily extended/replaced to retrieve data from
other data sources (e.g., XNAT, HTTP, etc.,.)
"""
if self._got_inputs: # Inputs cached
return
if not self.input_source: # No previous nodes
self._got_inputs = True
return
prev_results = defaultdict(list)
for key, info in list(self.input_source.items()):
prev_results[info[0]].append((key, info[1]))
logger.debug(
'[Node] Setting %d connected inputs of node "%s" from %d previous nodes.',
len(self.input_source),
self.name,
len(prev_results),
)
for results_fname, connections in list(prev_results.items()):
outputs = None
try:
outputs = _load_resultfile(results_fname).outputs
except AttributeError as e:
logger.critical("%s", e)
if outputs is None:
raise NodeExecutionError(
"""\
Error populating the inputs of node "%s": the results file of the source node \
(%s) does not contain any outputs."""
% (self.name, results_fname)
)
for key, conn in connections:
output_value = Undefined
if isinstance(conn, tuple):
value = getattr(outputs, conn[0])
if isdefined(value):
output_value = evaluate_connect_function(
conn[1], conn[2], value
)
else:
output_name = conn
try:
output_value = outputs.trait_get()[output_name]
except AttributeError:
output_value = outputs.dictcopy()[output_name]
logger.debug("output: %s", output_name)
try:
self.set_input(key, deepcopy(output_value))
except traits.TraitError as e:
msg = (
e.args[0],
"",
"Error setting node input:",
"Node: %s" % self.name,
"input: %s" % key,
"results_file: %s" % results_fname,
"value: %s" % str(output_value),
)
e.args = ("\n".join(msg),)
raise
# Successfully set inputs
self._got_inputs = True
def _update_hash(self):
for outdatedhash in glob(op.join(self.output_dir(), "_0x*.json")):
os.remove(outdatedhash)
_save_hashfile(self._hashvalue, self._hashed_inputs)
def _run_interface(self, execute=True, updatehash=False):
if updatehash:
self._update_hash()
return self._load_results()
return self._run_command(execute)
def _load_results(self):
cwd = self.output_dir()
try:
result = _load_resultfile(op.join(cwd, "result_%s.pklz" % self.name))
except (traits.TraitError, EOFError):
logger.debug("Error populating inputs/outputs, (re)aggregating results...")
except (AttributeError, ImportError) as err:
logger.debug(
"attribute error: %s probably using " "different trait pickled file",
str(err),
)
old_inputs = loadpkl(op.join(cwd, "_inputs.pklz"))
self.inputs.trait_set(**old_inputs)
else:
return result
# try aggregating first
if not isinstance(self, MapNode):
self._copyfiles_to_wd(linksonly=True)
aggouts = self._interface.aggregate_outputs(
needed_outputs=self.needed_outputs
)
runtime = Bunch(
cwd=cwd,
returncode=0,
environ=dict(os.environ),
hostname=socket.gethostname(),
)
result = InterfaceResult(
interface=self._interface.__class__,
runtime=runtime,
inputs=self._interface.inputs.get_traitsfree(),
outputs=aggouts,
)
_save_resultfile(
result,
cwd,
self.name,
rebase=str2bool(self.config["execution"]["use_relative_paths"]),
)
else:
logger.debug("aggregating mapnode results")
result = self._run_interface()
return result
def _run_command(self, execute, copyfiles=True):
if not execute:
try:
result = self._load_results()
except (FileNotFoundError, AttributeError):
# if aggregation does not work, rerun the node
logger.info(
"[Node] Some of the outputs were not found: " "rerunning node."
)
copyfiles = False # OE: this was like this before,
execute = True # I'll keep them for safety
else:
logger.info(
'[Node] Cached "%s" - collecting precomputed outputs', self.fullname
)
return result
outdir = Path(self.output_dir())
if copyfiles:
self._originputs = deepcopy(self._interface.inputs)
self._copyfiles_to_wd(execute=execute)
# Run command: either execute is true or load_results failed.
logger.info(
f'[Node] Executing "{self.name}" <{self._interface.__module__}'
f".{self._interface.__class__.__name__}>"
)
# Invoke core run method of the interface ignoring exceptions
result = self._interface.run(cwd=outdir, ignore_exception=True)
logger.info(
f'[Node] Finished "{self.name}", elapsed time {result.runtime.duration}s.'
)
exc_tb = getattr(result.runtime, "traceback", None)
if not exc_tb:
# Clean working directory if no errors
dirs2keep = None
if isinstance(self, MapNode):
dirs2keep = [op.join(outdir, "mapflow")]
result.outputs = clean_working_directory(
result.outputs,
outdir,
self._interface.inputs,
self.needed_outputs,
self.config,
dirs2keep=dirs2keep,
)
# Store results file under all circumstances
_save_resultfile(
result,
outdir,
self.name,
rebase=str2bool(self.config["execution"]["use_relative_paths"]),
)
if exc_tb:
raise NodeExecutionError(
f"Exception raised while executing Node {self.name}.\n\n{result.runtime.traceback}"
)
return result
def _copyfiles_to_wd(self, execute=True, linksonly=False):
"""copy files over and change the inputs"""
filecopy_info = get_filecopy_info(self.interface)
if not filecopy_info:
# Nothing to be done
return
logger.debug(
"copying files to wd [execute=%s, linksonly=%s]", execute, linksonly
)
outdir = self.output_dir()
if execute and linksonly:
olddir = outdir
outdir = op.join(outdir, "_tempinput")
os.makedirs(outdir, exist_ok=True)
for info in filecopy_info:
files = self.inputs.trait_get().get(info["key"])
if not isdefined(files) or not files:
continue
infiles = ensure_list(files)
if execute:
if linksonly:
if not info["copy"]:
newfiles = copyfiles(
infiles, [outdir], copy=info["copy"], create_new=True
)
else:
newfiles = fnames_presuffix(infiles, newpath=outdir)
newfiles = _strip_temp(
newfiles, op.abspath(olddir).split(op.sep)[-1]
)
else:
newfiles = copyfiles(
infiles, [outdir], copy=info["copy"], create_new=True
)
else:
newfiles = fnames_presuffix(infiles, newpath=outdir)
if not isinstance(files, list):
newfiles = simplify_list(newfiles)
setattr(self.inputs, info["key"], newfiles)
if execute and linksonly:
emptydirs(outdir, noexist_ok=True)
def update(self, **opts):
"""Update inputs"""
self.inputs.update(**opts)
class JoinNode(Node):
"""Wraps interface objects that join inputs into a list.
Examples
--------
>>> import nipype.pipeline.engine as pe
>>> from nipype import Node, JoinNode, Workflow
>>> from nipype.interfaces.utility import IdentityInterface
>>> from nipype.interfaces import (ants, dcm2nii, fsl)
>>> wf = Workflow(name='preprocess')
>>> inputspec = Node(IdentityInterface(fields=['image']),
... name='inputspec')
>>> inputspec.iterables = [('image',
... ['img1.nii', 'img2.nii', 'img3.nii'])]
>>> img2flt = Node(fsl.ImageMaths(out_data_type='float'),
... name='img2flt')
>>> wf.connect(inputspec, 'image', img2flt, 'in_file')
>>> average = JoinNode(ants.AverageImages(), joinsource='inputspec',
... joinfield='images', name='average')
>>> wf.connect(img2flt, 'out_file', average, 'images')
>>> realign = Node(fsl.FLIRT(), name='realign')
>>> wf.connect(img2flt, 'out_file', realign, 'in_file')
>>> wf.connect(average, 'output_average_image', realign, 'reference')
>>> strip = Node(fsl.BET(), name='strip')
>>> wf.connect(realign, 'out_file', strip, 'in_file')
"""
def __init__(
self, interface, name, joinsource, joinfield=None, unique=False, **kwargs
):
"""
Parameters
----------
interface : interface object
node specific interface (fsl.Bet(), spm.Coregister())
name : alphanumeric string
node specific name
joinsource : node name
name of the join predecessor iterable node
joinfield : string or list of strings
name(s) of list input fields that will be aggregated.
The default is all of the join node input fields.
unique : flag indicating whether to ignore duplicate input values
See Node docstring for additional keyword arguments.
"""
super(JoinNode, self).__init__(interface, name, **kwargs)
self._joinsource = None # The member should be defined
self.joinsource = joinsource # Let the setter do the job
"""the join predecessor iterable node"""
if not joinfield:
# default is the interface fields
joinfield = self._interface.inputs.copyable_trait_names()
elif isinstance(joinfield, (str, bytes)):
joinfield = [joinfield]
self.joinfield = joinfield
"""the fields to join"""
self._inputs = self._override_join_traits(
self._interface.inputs, self.joinfield
)
"""the override inputs"""
self._unique = unique
"""flag indicating whether to ignore duplicate input values"""
self._next_slot_index = 0
"""the joinfield index assigned to an iterated input"""
@property
def joinsource(self):
return self._joinsource
@joinsource.setter
def joinsource(self, value):
"""Set the joinsource property. If the given value is a Node,
then the joinsource is set to the node name.
"""
if isinstance(value, Node):
value = value.name
self._joinsource = value
@property
def inputs(self):
"""The JoinNode inputs include the join field overrides."""
return self._inputs
def _add_join_item_fields(self):
"""Add new join item fields assigned to the next iterated
input
This method is intended solely for workflow graph expansion.
Examples
--------
>>> from nipype.interfaces.utility import IdentityInterface
>>> import nipype.pipeline.engine as pe
>>> from nipype import Node, JoinNode, Workflow
>>> inputspec = Node(IdentityInterface(fields=['image']),
... name='inputspec'),
>>> join = JoinNode(IdentityInterface(fields=['images', 'mask']),
... joinsource='inputspec', joinfield='images', name='join')
>>> join._add_join_item_fields()
{'images': 'imagesJ1'}
Return the {base field: slot field} dictionary
"""
# create the new join item fields
idx = self._next_slot_index
newfields = dict(
[(field, self._add_join_item_field(field, idx)) for field in self.joinfield]
)
# increment the join slot index
logger.debug("Added the %s join item fields %s.", self, newfields)
self._next_slot_index += 1
return newfields
def _add_join_item_field(self, field, index):
"""Add new join item fields qualified by the given index
Return the new field name
"""
# the new field name
name = "%sJ%d" % (field, index + 1)
# make a copy of the join trait
trait = self._inputs.trait(field, False, True)
# add the join item trait to the override traits
self._inputs.add_trait(name, trait)
return name
def _override_join_traits(self, basetraits, fields):
"""Convert the given join fields to accept an input that
is a list item rather than a list. Non-join fields
delegate to the interface traits.
Return the override DynamicTraitedSpec
"""
dyntraits = DynamicTraitedSpec()
if fields is None:
fields = basetraits.copyable_trait_names()
else:
# validate the fields
for field in fields:
if not basetraits.trait(field):
raise ValueError(
"The JoinNode %s does not have a field"
" named %s" % (self.name, field)
)
for name, trait in list(basetraits.items()):
# if a join field has a single inner trait, then the item
# trait is that inner trait. Otherwise, the item trait is
# a new Any trait.
if name in fields and len(trait.inner_traits) == 1:
item_trait = trait.inner_traits[0]
dyntraits.add_trait(name, item_trait)
setattr(dyntraits, name, Undefined)
logger.debug(
"Converted the join node %s field %s trait type from %s to %s",
self,
name,
trait.trait_type.info(),
item_trait.info(),
)
else:
dyntraits.add_trait(name, traits.Any)
setattr(dyntraits, name, Undefined)
return dyntraits
def _run_command(self, execute, copyfiles=True):
"""Collates the join inputs prior to delegating to the superclass."""
self._collate_join_field_inputs()
return super(JoinNode, self)._run_command(execute, copyfiles)
def _collate_join_field_inputs(self):
"""
Collects each override join item field into the interface join
field input."""
for field in self.inputs.copyable_trait_names():
if field in self.joinfield:
# collate the join field
val = self._collate_input_value(field)
try:
setattr(self._interface.inputs, field, val)
except Exception as e:
raise ValueError(
">>JN %s %s %s %s %s: %s"