Skip to content

Commit e2a996e

Browse files
Improve performance for _has_node
- Networkx represents graphs as a dictionary where the nodes are the keys. As such, we can use the built-in __contains__ function for fast lookup - We can also iterate the graphs directly without constructing a NodeView using self._graph.nodes()
1 parent 54f5029 commit e2a996e

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

nipype/pipeline/engine/workflows.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -912,10 +912,12 @@ def _get_all_nodes(self):
912912
return allnodes
913913

914914
def _has_node(self, wanted_node):
915-
for node in self._graph.nodes():
915+
if wanted_node in self._graph:
916+
return True # best case scenario
917+
for node in self._graph: # iterate otherwise
916918
if wanted_node == node:
917919
return True
918-
if isinstance(node, Workflow):
920+
if hasattr(node, "_has_node"): # hasattr is faster than isinstance
919921
if node._has_node(wanted_node):
920922
return True
921923
return False

0 commit comments

Comments
 (0)