Skip to content

Commit d47460a

Browse files
author
cindeem
committed
Merge pull request #15 from cindeem/spectral_fix
BugFix spectral Partition
2 parents 33e1ccf + 832309b commit d47460a

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

brainx/modularity.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,6 +1319,7 @@ def newman_partition(g, max_div=np.inf):
13191319
raise ValueError('Adjacency matrix is weighted, need binary matrix')
13201320
## add line to binarize adj_matrix if not binary
13211321
## warning?
1322+
nedges = g.number_of_edges()
13221323
k = np.sum(A, axis=0)
13231324
M = np.sum(A) # 2x number of edges
13241325
B = modularity_matrix(g)
@@ -1350,9 +1351,10 @@ def _divide_partition(p, max_div=np.inf):
13501351
# make sure partition has edges
13511352
if graph_A_.number_of_edges() <= 1:
13521353
return [p]
1353-
B_ = modularity_matrix(graph_A_)
1354-
w, v = sl.eigh(B_, eigvals=(len(B_) - 2, len(B_) - 1))
1354+
## grab the relevent part of the modularity matrix
1355+
Bij = B[p, p[:,None]]
13551356

1357+
w, v = sl.eigh(Bij, eigvals=(len(Bij) - 2, len(Bij) - 1))
13561358
# Find the maximum eigenvalue of the modularity matrix
13571359
# If it is smaller than zero, then we won't be able to
13581360
# increase the modularity any further by partitioning.
@@ -1376,15 +1378,9 @@ def _divide_partition(p, max_div=np.inf):
13761378

13771379
# Compute the increase in modularity due to this partitioning.
13781380
# If it is less than zero, we should rather not have partitioned.
1379-
Bc_mask = np.ones_like(B_)
1380-
Bc_mask[s==1, :] = 0
1381-
Bc_mask[:, s==1] = 0
1382-
Bc = (B_ * Bc_mask).sum(axis=0)
1383-
Bc = B_ - Bc
1384-
q = s[None, :].dot(Bc).dot(s) / (4.0 * graph_A_.number_of_edges())
1385-
q2 = s[None, :].dot(B_).dot(s) / (4.0 * graph_A_.number_of_edges())
1386-
print 'orig delta q', q2, 'new delta q', q
1387-
if q <= 0:
1381+
Bg = Bij - np.diag(Bij.sum(axis=1))
1382+
deltaq = s[None,:].dot(Bg).dot(s) / (4.0 * nedges)
1383+
if deltaq <= 0:
13881384
return [p]
13891385

13901386
# Make the partitioning, and subdivide each

0 commit comments

Comments
 (0)