Skip to content

Commit 7c7facd

Browse files
committed
added 'colored' graph to graph2use
1 parent 2cfa242 commit 7c7facd

File tree

1 file changed

+38
-19
lines changed

1 file changed

+38
-19
lines changed

nipype/pipeline/engine.py

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -412,10 +412,14 @@ def write_graph(self, dotfilename='graph.dot', graph2use='hierarchical',
412412
Parameters
413413
----------
414414
415-
graph2use: 'orig', 'hierarchical' (default), 'flat', 'exec'
415+
graph2use: 'orig', 'hierarchical' (default), 'flat', 'exec', 'colored'
416416
orig - creates a top level graph without expanding internal
417417
workflow nodes;
418418
flat - expands workflow nodes recursively;
419+
hierarchical - expands workflow nodes recursively with a
420+
notion on hierarchy;
421+
colored - expands workflow nodes recursively with a
422+
notion on hierarchy in color;
419423
exec - expands workflows to depict iterables
420424
421425
format: 'png', 'svg'
@@ -426,7 +430,7 @@ def write_graph(self, dotfilename='graph.dot', graph2use='hierarchical',
426430
False.
427431
428432
"""
429-
graphtypes = ['orig', 'flat', 'hierarchical', 'exec']
433+
graphtypes = ['orig', 'flat', 'hierarchical', 'exec', 'colored']
430434
if graph2use not in graphtypes:
431435
raise ValueError('Unknown graph2use keyword. Must be one of: ' +
432436
str(graphtypes))
@@ -439,11 +443,17 @@ def write_graph(self, dotfilename='graph.dot', graph2use='hierarchical',
439443
else:
440444
base_dir = os.getcwd()
441445
base_dir = make_output_dir(base_dir)
442-
if graph2use == 'hierarchical':
446+
if graph2use == 'hierarchical' or graph2use == 'colored':
443447
dotfilename = os.path.join(base_dir, dotfilename)
444-
self.write_hierarchical_dotfile(dotfilename=dotfilename,
445-
colored=False,
446-
simple_form=simple_form)
448+
if graph2use == 'colored':
449+
self.write_hierarchical_dotfile(dotfilename=dotfilename,
450+
colored=True,
451+
simple_form=simple_form)
452+
453+
else:
454+
self.write_hierarchical_dotfile(dotfilename=dotfilename,
455+
colored=False,
456+
simple_form=simple_form)
447457
format_dot(dotfilename, format=format)
448458
else:
449459
graph = self._graph
@@ -454,11 +464,9 @@ def write_graph(self, dotfilename='graph.dot', graph2use='hierarchical',
454464
export_graph(graph, base_dir, dotfilename=dotfilename,
455465
format=format, simple_form=simple_form)
456466

457-
def write_hierarchical_dotfile(self, dotfilename=None, colored=True,
467+
def write_hierarchical_dotfile(self, dotfilename=None, colored=False,
458468
simple_form=True):
459469
dotlist = ['digraph %s{' % self.name]
460-
if colored:
461-
dotlist.append(' ' + 'colorscheme=pastel28;')
462470
dotlist.append(self._get_dot(prefix=' ', colored=colored,
463471
simple_form=simple_form))
464472
dotlist.append('}')
@@ -827,18 +835,18 @@ def _generate_flatgraph(self):
827835
self._graph.remove_nodes_from(nodes2remove)
828836
logger.debug('finished expanding workflow: %s', self)
829837

830-
def _get_dot(self, prefix=None, hierarchy=None, colored=True,
831-
simple_form=True):
838+
def _get_dot(self, prefix=None, hierarchy=None, colored=False,
839+
simple_form=True, level=0):
832840
"""Create a dot file with connection info
833841
"""
834842
if prefix is None:
835843
prefix = ' '
836844
if hierarchy is None:
837845
hierarchy = []
838-
level = (len(prefix) / 2) + 1
846+
colorset = ['#FFFFC8','#0000FF','#B4B4FF','#E6E6FF','#FF0000',
847+
'#FFB4B4','#FFE6E6','#00A300','#B4FFB4','#E6FFE6']
848+
839849
dotlist = ['%slabel="%s";' % (prefix, self.name)]
840-
if colored:
841-
dotlist.append('%scolor=%d;' % (prefix, level))
842850
for node in nx.topological_sort(self._graph):
843851
fullname = '.'.join(hierarchy + [node.fullname])
844852
nodename = fullname.replace('.', '_')
@@ -847,24 +855,35 @@ def _get_dot(self, prefix=None, hierarchy=None, colored=True,
847855
if not simple_form:
848856
node_class_name = '.'.join(node_class_name.split('.')[1:])
849857
if hasattr(node, 'iterables') and node.iterables:
850-
dotlist.append(('%s[label="%s", style=filled, colorscheme'
851-
'=greys7 color=2];') % (nodename,
858+
dotlist.append(('%s[label="%s", shape=box3d,'
859+
'style=filled, color=black, colorscheme'
860+
'=greys7 fillcolor=2];') % (nodename,
852861
node_class_name))
853862
else:
854-
dotlist.append('%s[label="%s"];' % (nodename,
855-
node_class_name))
863+
if colored:
864+
dotlist.append(('%s[label="%s", style=filled,'
865+
' fillcolor="%s"];')
866+
% (nodename,node_class_name,
867+
colorset[level]))
868+
else:
869+
dotlist.append(('%s[label="%s"];')
870+
% (nodename,node_class_name))
871+
856872
for node in nx.topological_sort(self._graph):
857873
if isinstance(node, Workflow):
858874
fullname = '.'.join(hierarchy + [node.fullname])
859875
nodename = fullname.replace('.', '_')
860876
dotlist.append('subgraph cluster_%s {' % nodename)
861877
if colored:
878+
dotlist.append(prefix + prefix + 'edge [color="%s"];' % (colorset[level+1]))
862879
dotlist.append(prefix + prefix + 'style=filled;')
880+
dotlist.append(prefix + prefix + 'fillcolor="%s";' % (colorset[level+2]))
863881
dotlist.append(node._get_dot(prefix=prefix + prefix,
864882
hierarchy=hierarchy + [self.name],
865883
colored=colored,
866-
simple_form=simple_form))
884+
simple_form=simple_form, level=level+3))
867885
dotlist.append('}')
886+
if level==6:level=2
868887
else:
869888
for subnode in self._graph.successors_iter(node):
870889
if node._hierarchy != subnode._hierarchy:

0 commit comments

Comments
 (0)