Skip to content

Commit 365482b

Browse files
author
CindeeM
committed
NF: clear error raised if index does not contain all possible nodes
1 parent f0c6ff0 commit 365482b

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

brainx/modularity.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ def __init__(self, graph, index):
7676
## add quick check to make sure the passed index is
7777
## a dict of sets
7878
self._check_index_contains_sets()
79-
79+
## raise useful error if index is missing nodes in graph
80+
self._check_allnodes_in_index(graph)
8081

8182
# We'll need the graph's adjacency matrix often, so store it once
8283
self.graph_adj_matrix = nx.adj_matrix(graph)
@@ -114,6 +115,16 @@ def _check_index_contains_sets(self):
114115
if not all([ x== type(set()) for x in index_types]):
115116
raise TypeError('index values should be of type set():: %s'%(index_types))
116117

118+
def _check_allnodes_in_index(self, graph):
119+
"""Check that index contains all nodes in graph"""
120+
sets = self.index.values()
121+
all = []
122+
for item in sets:
123+
all += list(item)
124+
if not sorted(all) == sorted(graph.nodes()):
125+
missing = [x for x in all if not x in graph.nodes()]
126+
raise ValueError('index does not contain all nodes: missing %s'%missing)
127+
117128

118129
def _edge_info(self, mod_e=None, mod_a=None, index=None):
119130
"""Create the vectors of edge information.

0 commit comments

Comments
 (0)