Skip to content

Commit 9d8db5e

Browse files
author
CindeeM
committed
BF: fix to Newman Partition to not split partition if eigenvectors are all positive or all negative, clean up docs
1 parent 91fe832 commit 9d8db5e

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

brainx/modularity.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,13 +1270,20 @@ def simulated_annealing(g, p0=None, temperature = 50, temp_scaling = 0.995, tmin
12701270

12711271
def modularity_matrix(g):
12721272
"""Modularity matrix of the graph.
1273+
1274+
Parameters
1275+
----------
1276+
g : NetworkX graph
1277+
input graph
12731278
1274-
The eigenvector corresponding to the largest eigenvalue of the modularity
1275-
matrix is analyzed to assign clusters.
1276-
1279+
Returns
1280+
-------
1281+
B : numpy array
1282+
modularity matrix (graph laplacian)
1283+
12771284
"""
12781285
A = np.asarray(nx.adjacency_matrix(g))
1279-
k = np.sum(A, axis=0)
1286+
k = np.sum(A, axis=0) #vertex degree
12801287
M = np.sum(k) # 2x number of edges
12811288

12821289
return A - ((k * k[:, None]) / float(M))
@@ -1312,15 +1319,13 @@ def _divide_partition(p, max_div=np.inf):
13121319
----------
13131320
p : array of ints
13141321
Node labels.
1315-
B : ndarray
1316-
Modularity matrix.
1322+
max_div : int
1323+
maximum number of divisions (default np.inf)
13171324
13181325
Returns
13191326
-------
1320-
pp, qq : list of ints
1321-
Partitioning of node labels. If the partition is indivisible, then
1322-
only `pp` is returned.
1323-
1327+
out : list of ints
1328+
Partitioning of node labels.
13241329
"""
13251330
p = np.asarray(p)
13261331

@@ -1329,13 +1334,8 @@ def _divide_partition(p, max_div=np.inf):
13291334

13301335
# Construct the subgraph modularity matrix
13311336
A_ = A[p, p[:, None]]
1332-
k_ = np.sum(A_, axis=0)
1333-
M_ = np.sum(k_)
1334-
1335-
B_ = B[p, p[:, None]]
1336-
B_ = B_ - np.diag(k_ - k[p] * M_ / float(M))
1337-
1338-
# w, v = nl.eigh(B_)
1337+
graph_A_ = nx.from_numpy_matrix(A_, nx.Graph())
1338+
B_ = modularity_matrix(graph_A_)
13391339
w, v = sl.eigh(B_, eigvals=(len(B_) - 2, len(B_) - 1))
13401340

13411341
# Find the maximum eigenvalue of the modularity matrix
@@ -1349,6 +1349,9 @@ def _divide_partition(p, max_div=np.inf):
13491349
# to nodes in the first partition and 1 for nodes in the second
13501350
v_max = v[:, n]
13511351
mask = (v_max < 0)
1352+
# if the mask is all true or all false, this will not split the partition
1353+
if not np.any(mask) or not np.any(~mask):
1354+
return [p]
13521355
s = np.ones_like(v_max)
13531356
s[mask] = -1
13541357

0 commit comments

Comments
 (0)