Skip to content

Commit dcb6ca1

Browse files
author
CindeeM
committed
TST: added tests for newman_parition function, added ValueError if graph passed has weighted matrix
1 parent b0c1bf0 commit dcb6ca1

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

brainx/modularity.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,10 @@ def newman_partition(g, max_div=np.inf):
13071307
13081308
"""
13091309
A = np.asarray(nx.adjacency_matrix(g))
1310+
if not A.sum() == A.astype(bool).sum():
1311+
raise ValueError('Adjacency matrix is weighted, need binary matrix')
1312+
## add line to binarize adj_matrix if not binary
1313+
## warning?
13101314
k = np.sum(A, axis=0)
13111315
M = np.sum(A) # 2x number of edges
13121316
B = modularity_matrix(g)

brainx/tests/test_modularity.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,24 @@ def test_badindex_graphpartition():
795795
{0:set(g.nodes()[:-1])})
796796
npt.assert_raises(TypeError, mod.GraphPartition, g, g.nodes())
797797

798+
def test_newman_partition():
799+
""" Test Newman Partition function """
800+
tmpmat = np.random.random((10,10))
801+
tmpmat[tmpmat < .5] = 0
802+
graph = nx.from_numpy_matrix(tmpmat, nx.Graph(weighted = False))
803+
npt.assert_raises(ValueError, mod.newman_partition, graph)
804+
tmpmat[:] = 0
805+
# test that no edges raises error (from GraphPartition)
806+
graph = nx.from_numpy_matrix(tmpmat, nx.Graph(weighted = False))
807+
npt.assert_raises(ValueError, mod.newman_partition, graph)
808+
tmpmat[:] = 1
809+
util.fill_diagonal(tmpmat, 0)
810+
graph = nx.from_numpy_matrix(tmpmat, nx.Graph(weighted=False))
811+
part = mod.newman_partition(graph)
812+
## if all edges are connected expect only one partition
813+
expected_part = {0: set([0,1,2,3,4,5,6,7,8,9])}
814+
nt.assert_equal(part.index, expected_part)
815+
798816

799817
if __name__ == "__main__":
800818
npt.run_module_suite()

0 commit comments

Comments
 (0)