Skip to content

Commit c5fb727

Browse files
author
CindeeM
committed
BF: change GraphPartition to use set(range(number_of_nodes)) as _node_set, not nodes defined in graph to avoid index error when using this to index adjacency_matrix
1 parent 5c51d60 commit c5fb727

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

brainx/modularity.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,7 @@ def __init__(self, graph, index):
7575
## add quick check to make sure the passed index is
7676
## a dict of sets
7777
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+
8179
# We'll need the graph's adjacency matrix often, so store it once
8280
self.graph_adj_matrix = nx.adj_matrix(graph)
8381

@@ -91,11 +89,15 @@ def __init__(self, graph, index):
9189
self.num_edges = graph.number_of_edges()
9290

9391
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")
9894

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+
99101
# Now, build the edge information used in modularity computations
100102
self.mod_e, self.mod_a = self._edge_info()
101103

@@ -120,8 +122,8 @@ def _check_allnodes_in_index(self, graph):
120122
all = []
121123
for item in sets:
122124
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]
125127
raise ValueError('index does not contain all nodes: missing %s'%missing)
126128

127129

0 commit comments

Comments
 (0)