Skip to content

Commit ace8468

Browse files
author
Jessica R. Cohen
committed
Updated modularity.py to convert networkx sparse matrices to dense matrices so they can be converted to numpy arrays
1 parent bc17cee commit ace8468

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

brainx/modularity.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def __init__(self, graph, index):
7474
self._check_index_contains_sets()
7575

7676
# We'll need the graph's adjacency matrix often, so store it once
77+
self.graph_adj_matrix = nx.adj_matrix(graph).todense() # Must convert to dense matrix before making into a numpy array (line was previously: self.graph_adj_matrix = nx.adj_matrix(graph))
7778
self.graph_adj_matrix = nx.adj_matrix(graph)
7879
#make sure adj_matrix is binary otherwise raise exception
7980
if not self.graph_adj_matrix.sum() == \
@@ -1290,7 +1291,7 @@ def modularity_matrix(g):
12901291
modularity matrix (graph laplacian)
12911292
12921293
"""
1293-
A = np.asarray(nx.adjacency_matrix(g))
1294+
A = np.asarray(nx.adjacency_matrix(g).todense()) # Must convert to dense matrix before making into a numpy array (line was previously: A = np.asarray(nx.adjacency_matrix(g)))
12941295
k = np.sum(A, axis=0) #vertex degree
12951296
M = np.sum(k) # 2x number of edges
12961297

@@ -1314,7 +1315,7 @@ def newman_partition(g, max_div=np.inf):
13141315
Estimated optimal partitioning.
13151316
13161317
"""
1317-
A = np.asarray(nx.adjacency_matrix(g))
1318+
A = np.asarray(nx.adjacency_matrix(g).todense()) # Must convert to dense matrix before making into a numpy array (line was previously:(A = np.asarray(nx.adjacency_matrix(g)))
13181319
if not A.sum() == A.astype(bool).sum():
13191320
raise ValueError('Adjacency matrix is weighted, need binary matrix')
13201321
## add line to binarize adj_matrix if not binary

0 commit comments

Comments
 (0)