diff --git a/bct/algorithms/centrality.py b/bct/algorithms/centrality.py index c57f871..c2be852 100644 --- a/bct/algorithms/centrality.py +++ b/bct/algorithms/centrality.py @@ -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 @@ -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 diff --git a/test/centrality_test.py b/test/centrality_test.py index 56d3842..f02f67c 100644 --- a/test/centrality_test.py +++ b/test/centrality_test.py @@ -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], + )