@@ -75,9 +75,7 @@ def __init__(self, graph, index):
75
75
## add quick check to make sure the passed index is
76
76
## a dict of sets
77
77
self ._check_index_contains_sets ()
78
- ## raise useful error if index is missing nodes in graph
79
- self ._check_allnodes_in_index (graph )
80
-
78
+
81
79
# We'll need the graph's adjacency matrix often, so store it once
82
80
self .graph_adj_matrix = nx .adj_matrix (graph )
83
81
@@ -91,11 +89,15 @@ def __init__(self, graph, index):
91
89
self .num_edges = graph .number_of_edges ()
92
90
93
91
if self .num_edges == 0 :
94
- raise ValueError ("TODO: Cannot create a graph partition of only one node." )
95
-
96
- # Store the nodes as a set, needed for many operations
97
- self ._node_set = set (graph .nodes ())
92
+ raise ValueError ("Cannot create a graph partition of only" \
93
+ "if graph has no edges/connections" )
98
94
95
+ # Store the nodes as a set of contiguous integers (indices into
96
+ #the adjacency_matrix), needed for many operations
97
+ self ._node_set = set (range (self .num_nodes ))
98
+ ## raise useful error if index is missing nodes in graph
99
+ self ._check_allnodes_in_index (graph )
100
+
99
101
# Now, build the edge information used in modularity computations
100
102
self .mod_e , self .mod_a = self ._edge_info ()
101
103
@@ -120,8 +122,8 @@ def _check_allnodes_in_index(self, graph):
120
122
all = []
121
123
for item in sets :
122
124
all += list (item )
123
- if not sorted (all ) == sorted (graph . nodes () ):
124
- missing = [x for x in all if not x in graph . nodes () ]
125
+ if not sorted (all ) == sorted (self . _node_set ):
126
+ missing = [x for x in all if not x in self . _node_set ]
125
127
raise ValueError ('index does not contain all nodes: missing %s' % missing )
126
128
127
129
0 commit comments