4
4
"""Utility routines for workflow graphs
5
5
"""
6
6
from __future__ import print_function , division , unicode_literals , absolute_import
7
- from builtins import str , open , map , next , zip , range
7
+ from builtins import str , open , next , zip , range
8
8
9
+ import os
9
10
import sys
10
- from future import standard_library
11
- standard_library .install_aliases ()
11
+ import pickle
12
12
from collections import defaultdict
13
-
13
+ import re
14
14
from copy import deepcopy
15
15
from glob import glob
16
+ from distutils .version import LooseVersion
17
+
16
18
try :
17
19
from inspect import signature
18
20
except ImportError :
19
21
from funcsigs import signature
20
22
21
- import os
22
- import re
23
- import pickle
24
23
from functools import reduce
25
24
import numpy as np
26
- from distutils .version import LooseVersion
27
-
28
25
import networkx as nx
29
26
30
27
from ...utils .filemanip import (fname_presuffix , FileNotFoundError , to_str ,
37
34
from ...utils .provenance import ProvStore , pm , nipype_ns , get_id
38
35
39
36
from ... import logging , config
37
+ from future import standard_library
38
+
39
+ standard_library .install_aliases ()
40
40
logger = logging .getLogger ('workflow' )
41
41
PY3 = sys .version_info [0 ] > 2
42
42
@@ -262,8 +262,6 @@ def _write_detailed_dot(graph, dotfilename):
262
262
text = ['digraph structs {' , 'node [shape=record];' ]
263
263
# write nodes
264
264
edges = []
265
- replacefunk = lambda x : x .replace ('_' , '' ).replace ('.' , '' ). \
266
- replace ('@' , '' ).replace ('-' , '' )
267
265
for n in nx .topological_sort (graph ):
268
266
nodename = str (n )
269
267
inports = []
@@ -274,18 +272,16 @@ def _write_detailed_dot(graph, dotfilename):
274
272
else :
275
273
outport = cd [0 ][0 ]
276
274
inport = cd [1 ]
277
- ipstrip = 'in' + replacefunk (inport )
278
- opstrip = 'out' + replacefunk (outport )
275
+ ipstrip = 'in%s' % _replacefunk (inport )
276
+ opstrip = 'out%s' % _replacefunk (outport )
279
277
edges .append ('%s:%s:e -> %s:%s:w;' % (str (u ).replace ('.' , '' ),
280
278
opstrip ,
281
279
str (v ).replace ('.' , '' ),
282
280
ipstrip ))
283
281
if inport not in inports :
284
282
inports .append (inport )
285
- inputstr = '{IN'
286
- for ip in sorted (inports ):
287
- inputstr += '|<in%s> %s' % (replacefunk (ip ), ip )
288
- inputstr += '}'
283
+ inputstr = ['{IN' ] + ['|<in%s> %s' % (_replacefunk (ip ), ip )
284
+ for ip in sorted (inports )] + ['}' ]
289
285
outports = []
290
286
for u , v , d in graph .out_edges (nbunch = n , data = True ):
291
287
for cd in d ['connect' ]:
@@ -295,10 +291,8 @@ def _write_detailed_dot(graph, dotfilename):
295
291
outport = cd [0 ][0 ]
296
292
if outport not in outports :
297
293
outports .append (outport )
298
- outputstr = '{OUT'
299
- for op in sorted (outports ):
300
- outputstr += '|<out%s> %s' % (replacefunk (op ), op )
301
- outputstr += '}'
294
+ outputstr = ['{OUT' ] + ['|<out%s> %s' % (_replacefunk (oport ), oport )
295
+ for oport in sorted (outports )] + ['}' ]
302
296
srcpackage = ''
303
297
if hasattr (n , '_interface' ):
304
298
pkglist = n ._interface .__class__ .__module__ .split ('.' )
@@ -309,19 +303,23 @@ def _write_detailed_dot(graph, dotfilename):
309
303
srcpackage ,
310
304
srchierarchy )
311
305
text += ['%s [label="%s|%s|%s"];' % (nodename .replace ('.' , '' ),
312
- inputstr ,
306
+ '' . join ( inputstr ) ,
313
307
nodenamestr ,
314
- outputstr )]
308
+ '' . join ( outputstr ) )]
315
309
# write edges
316
310
for edge in sorted (edges ):
317
311
text .append (edge )
318
312
text .append ('}' )
319
- filep = open (dotfilename , 'wt' )
320
- filep .write ('\n ' .join (text ))
321
- filep .close ()
313
+ with open (dotfilename , 'wt' ) as filep :
314
+ filep .write ('\n ' .join (text ))
322
315
return text
323
316
324
317
318
+ def _replacefunk (x ):
319
+ return x .replace ('_' , '' ).replace (
320
+ '.' , '' ).replace ('@' , '' ).replace ('-' , '' )
321
+
322
+
325
323
# Graph manipulations for iterable expansion
326
324
def _get_valid_pathstr (pathstr ):
327
325
"""Remove disallowed characters from path
0 commit comments