Skip to content

Commit f0c6ff0

Browse files
author
CindeeM
committed
NF: method in GraphPArtition to check that index is a dict containing sets
1 parent 371517f commit f0c6ff0

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

brainx/modularity.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,12 @@ def __init__(self, graph, index):
7272
"""
7373
# Store references to the original graph and label dict
7474
self.index = copy.deepcopy(index)
75-
#self.graph = graph
7675

76+
## add quick check to make sure the passed index is
77+
## a dict of sets
78+
self._check_index_contains_sets()
79+
80+
7781
# We'll need the graph's adjacency matrix often, so store it once
7882
self.graph_adj_matrix = nx.adj_matrix(graph)
7983

@@ -102,6 +106,15 @@ def copy(self):
102106
def __len__(self):
103107
return len(self.index)
104108

109+
110+
def _check_index_contains_sets(self):
111+
""" the index in a GraphPartition is a dict of node sets
112+
validate that the values of this dict are all of type(set)"""
113+
index_types = [ type(x) for x in self.index.values() ]
114+
if not all([ x== type(set()) for x in index_types]):
115+
raise TypeError('index values should be of type set():: %s'%(index_types))
116+
117+
105118
def _edge_info(self, mod_e=None, mod_a=None, index=None):
106119
"""Create the vectors of edge information.
107120

brainx/tests/test_modularity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ def test_adjust_partition():
752752
def test_empty_graphpartition():
753753
g = nx.Graph()
754754
g.add_node(1)
755-
npt.assert_raises(ValueError, mod.GraphPartition, g, {1: g.nodes()})
755+
npt.assert_raises(ValueError, mod.GraphPartition, g, {1: set(g.nodes())})
756756

757757

758758
if __name__ == "__main__":

0 commit comments

Comments
 (0)