Skip to content

Commit 80d411c

Browse files
author
CindeeM
committed
NF: added tests for new type checking methods of GraphPartition, and check that index is a dict
1 parent 365482b commit 80d411c

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

brainx/modularity.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ def __init__(self, graph, index):
7171
objects.
7272
"""
7373
# Store references to the original graph and label dict
74+
if not type(index) == type({}):
75+
raise TypeError('index should be of type dict(), not %s'%type(index))
76+
7477
self.index = copy.deepcopy(index)
7578

7679
## add quick check to make sure the passed index is

brainx/tests/test_modularity.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,5 +755,20 @@ def test_empty_graphpartition():
755755
npt.assert_raises(ValueError, mod.GraphPartition, g, {1: set(g.nodes())})
756756

757757

758+
def test_badindex_graphpartition():
759+
""" when making a GraphPArtition, check index is valid"""
760+
## index should be dict of sets
761+
e = np.loadtxt(os.path.join(os.path.dirname(__file__), 'jazz.net'),
762+
skiprows=3, dtype=int)[:, :2] - 1
763+
g = nx.Graph()
764+
g.add_edges_from(e)
765+
index = {0: set(g.nodes()[:100]), 1: set(g.nodes()[100:])}
766+
gp = mod.GraphPartition(g, index)
767+
nt.assert_true(gp.index == index)
768+
npt.assert_raises(TypeError, mod.GraphPartition, g, {0: g.nodes()})
769+
npt.assert_raises(ValueError, mod.GraphPartition, g, {0:set(g.nodes()[:-1])})
770+
npt.assert_raises(TypeError, mod.GraphPartition, g, g.nodes())
771+
772+
758773
if __name__ == "__main__":
759774
npt.run_module_suite()

0 commit comments

Comments
 (0)