2121from functools import reduce
2222
2323import numpy as np
24- import networkx as nx
2524from future import standard_library
2625
2726from ... import logging , config , LooseVersion
5554logger = logging .getLogger ('nipype.workflow' )
5655PY3 = sys .version_info [0 ] > 2
5756
58- try :
59- dfs_preorder = nx .dfs_preorder
60- except AttributeError :
61- dfs_preorder = nx .dfs_preorder_nodes
62- logger .debug ('networkx 1.4 dev or higher detected' )
63-
6457
6558def _parameterization_dir (param ):
6659 """
@@ -543,6 +536,7 @@ def _create_dot_graph(graph, show_connectinfo=False, simple_form=True):
543536 Ensures that edge info is pickleable.
544537 """
545538 logger .debug ('creating dot graph' )
539+ import networkx as nx
546540 pklgraph = nx .DiGraph ()
547541 for edge in graph .edges ():
548542 data = graph .get_edge_data (* edge )
@@ -569,6 +563,7 @@ def _write_detailed_dot(graph, dotfilename):
569563 struct1:f2 -> struct3:here;
570564 }
571565 """
566+ import networkx as nx
572567 text = ['digraph structs {' , 'node [shape=record];' ]
573568 # write nodes
574569 edges = []
@@ -748,6 +743,7 @@ def evaluate_connect_function(function_source, args, first_arg):
748743
749744
750745def get_levels (G ):
746+ import networkx as nx
751747 levels = {}
752748 for n in nx .topological_sort (G ):
753749 levels [n ] = 0
@@ -891,6 +887,7 @@ def _identity_nodes(graph, include_iterables):
891887 are included if and only if the include_iterables flag is set
892888 to True.
893889 """
890+ import networkx as nx
894891 return [
895892 node for node in nx .topological_sort (graph )
896893 if isinstance (node .interface , IdentityInterface ) and (
@@ -994,6 +991,12 @@ def generate_expanded_graph(graph_in):
994991 and b=[3,4] this procedure will generate a graph with sub-graphs
995992 parameterized as (a=1,b=3), (a=1,b=4), (a=2,b=3) and (a=2,b=4).
996993 """
994+ import networkx as nx
995+ try :
996+ dfs_preorder = nx .dfs_preorder
997+ except AttributeError :
998+ dfs_preorder = nx .dfs_preorder_nodes
999+
9971000 logger .debug ("PE: expanding iterables" )
9981001 graph_in = _remove_nonjoin_identity_nodes (graph_in , keep_iterables = True )
9991002 # standardize the iterables as {(field, function)} dictionaries
@@ -1222,6 +1225,7 @@ def _iterable_nodes(graph_in):
12221225
12231226 Return the iterable nodes list
12241227 """
1228+ import networkx as nx
12251229 nodes = nx .topological_sort (graph_in )
12261230 inodes = [node for node in nodes if node .iterables is not None ]
12271231 inodes_no_src = [node for node in inodes if not node .itersource ]
@@ -1349,6 +1353,7 @@ def export_graph(graph_in,
13491353 Indicates whether to show the edge data on the graph. This
13501354 makes the graph rather cluttered. default [False]
13511355 """
1356+ import networkx as nx
13521357 graph = deepcopy (graph_in )
13531358 if use_execgraph :
13541359 graph = generate_expanded_graph (graph )
@@ -1716,6 +1721,7 @@ def write_workflow_resources(graph, filename=None, append=None):
17161721def topological_sort (graph , depth_first = False ):
17171722 """Returns a depth first sorted order if depth_first is True
17181723 """
1724+ import networkx as nx
17191725 nodesort = list (nx .topological_sort (graph ))
17201726 if not depth_first :
17211727 return nodesort , None
0 commit comments