Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions bct/algorithms/centrality.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,8 +538,9 @@ def gcoef(W):
for i in range(nr_modules):
#if more than 1 node in the module
if np.sum(ci == i+1) > 1:
kj = np.ones((np.sum(ci == i+1), 1)) * np.sum(ks[ci == i+1, :])
kj[i] /= 2
kj = np.ones((np.sum(ci == i+1), 1)) * np.sum(
ks[ci == i+1, :], axis=0)
kj[:, i] /= 2

kjs[ci == i+1, :] = kj

Expand All @@ -550,7 +551,7 @@ def gcoef(W):
#sum of centralities of neighbors of node within a module
in_mod_nodes, = np.where(ci == j+1)
neighbs, = np.where(W[in_mod_nodes, i] > 0)
cs[i, j] = np.sum(cent[neighbs])
cs[i, j] = np.sum(cent[in_mod_nodes[neighbs]])

#normalize by total connections
ksm = ks / kjs
Expand Down
35 changes: 33 additions & 2 deletions test/centrality_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,38 @@ def test_gateway_coef():
ci, _ = bct.modularity_und(x)
gp, gn = bct.gateway_coef_sign(x, ci)
gpb, gnb = bct.gateway_coef_sign(x, ci, centrality_type='betweenness')
assert np.allclose(np.sum(gp), 87.0141)
assert np.allclose(np.sum(gpb), 87.0742)
assert np.allclose(np.sum(gp), 89.6140)
assert np.allclose(np.sum(gpb), 89.7742)
assert np.all(gn == 0)
assert np.all(gnb == 0)



def test_gateway_coef_handles_later_small_communities():
W = np.ones((6, 6)) - np.eye(6)
ci = np.repeat([1, 2, 3], 2)

gp, gn = bct.gateway_coef_sign(W, ci)

assert np.all(np.isfinite(gp))
assert np.allclose(gp, gp[0])
assert np.all(gn == 0)


def test_gateway_coef_uses_neighbor_node_indices():
W = np.array([
[0, 1, 4, 0, 0, 0],
[1, 0, 0, 2, 0, 0],
[4, 0, 0, 1, 3, 0],
[0, 2, 1, 0, 0, 5],
[0, 0, 3, 0, 0, 1],
[0, 0, 0, 5, 1, 0],
], dtype=float)
ci = np.array([1, 2, 1, 2, 1, 2])

gp, _ = bct.gateway_coef_sign(W, ci)

assert np.allclose(
gp,
[0.62248994, 0.57664286, 0.81934232, 0.81934232, 0.59285185, 0.67035485],
)