Skip to content

Commit cc6c42b

Browse files
author
CindeeM
committed
RF: node/index checking now uses set functions, cleanup error string
1 parent 504e56d commit cc6c42b

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

brainx/modularity.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,15 @@ def __init__(self, graph, index):
8989
self.num_edges = graph.number_of_edges()
9090

9191
if self.num_edges == 0:
92-
raise ValueError("Cannot create a graph partition of only"\
93-
"if graph has no edges/connections")
92+
raise ValueError("Cannot create a graph partition "\
93+
"if graph has no edges")
9494

9595
# Store the nodes as a set of contiguous integers (indices into
9696
#the adjacency_matrix), needed for many operations
9797
self._node_set = set(range(self.num_nodes))
9898
self._node_names = graph.nodes()
9999
## raise useful error if index is missing nodes in graph
100-
self._check_allnodes_in_index(graph)
100+
self._check_allnodes_in_index()
101101

102102
# Now, build the edge information used in modularity computations
103103
self.mod_e, self.mod_a = self._edge_info()
@@ -117,16 +117,13 @@ def _check_index_contains_sets(self):
117117
if not all([ x== type(set()) for x in index_types]):
118118
raise TypeError('index values should be of type set():: %s'%(index_types))
119119

120-
def _check_allnodes_in_index(self, graph):
120+
def _check_allnodes_in_index(self):
121121
"""Check that index contains all nodes in graph"""
122122
sets = self.index.values()
123-
all = []
124-
for item in sets:
125-
all += list(item)
126-
if not sorted(all) == sorted(self._node_set):
127-
missing = [x for x in all if not x in self._node_set]
128-
raise ValueError('index does not contain all nodes: missing %s'%missing)
129-
123+
indexnodes = set.union(*sets)
124+
missing = self._node_set.difference(indexnodes)
125+
if missing:
126+
raise ValueError('index does not contain all graph nodes: missing %s'%missing)
130127

131128
def _edge_info(self, mod_e=None, mod_a=None, index=None):
132129
"""Create the vectors of edge information.
@@ -619,7 +616,7 @@ def index_as_node_names(self):
619616
This will return the index (partition) using the graph node names"""
620617
named_part = []
621618
for nmod, part in self.index.iteritems():
622-
named_part.append([self._node_names[x] for x in part])
619+
named_part.append( [self._node_names[x] for x in part] )
623620
return named_part
624621

625622
def check_integrity(self, partition):

0 commit comments

Comments
 (0)