From 28a117d91e92996e80c0f52ead1961fc62e9df6b Mon Sep 17 00:00:00 2001 From: Donncha O'Toole Date: Sun, 27 Sep 2026 13:27:02 +0100 Subject: [PATCH 1/4] Test gateway coefficient against BCT indexing --- test/centrality_test.py | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/test/centrality_test.py b/test/centrality_test.py index 56d3842..aa85e41 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, 3, 3]) + + gp, _ = bct.gateway_coef_sign(W, ci) + + assert np.allclose( + gp, + [0.87402367, 0.86176857, 0.83043639, 0.84227071, 0.89866864, 0.88395792], + ) From 72efad5ed51d41f08d1db826c2c443fba94b1849 Mon Sep 17 00:00:00 2001 From: Donncha O'Toole Date: Sun, 27 Sep 2026 13:27:02 +0100 Subject: [PATCH 2/4] Fix gateway coefficient module indexing --- bct/algorithms/centrality.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bct/algorithms/centrality.py b/bct/algorithms/centrality.py index c57f871..9767c29 100644 --- a/bct/algorithms/centrality.py +++ b/bct/algorithms/centrality.py @@ -538,8 +538,8 @@ 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(\n ks[ci == i+1, :], axis=0) + kj[:, i] /= 2 kjs[ci == i+1, :] = kj @@ -550,7 +550,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 From 758e936fba42500764ada13ff6fb6bef6deb9f60 Mon Sep 17 00:00:00 2001 From: Donncha O'Toole Date: Sun, 27 Sep 2026 13:28:47 +0100 Subject: [PATCH 3/4] Separate gateway index regression from crash case --- test/centrality_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/centrality_test.py b/test/centrality_test.py index aa85e41..f02f67c 100644 --- a/test/centrality_test.py +++ b/test/centrality_test.py @@ -38,11 +38,11 @@ def test_gateway_coef_uses_neighbor_node_indices(): [0, 0, 3, 0, 0, 1], [0, 0, 0, 5, 1, 0], ], dtype=float) - ci = np.array([1, 2, 1, 2, 3, 3]) + ci = np.array([1, 2, 1, 2, 1, 2]) gp, _ = bct.gateway_coef_sign(W, ci) assert np.allclose( gp, - [0.87402367, 0.86176857, 0.83043639, 0.84227071, 0.89866864, 0.88395792], + [0.62248994, 0.57664286, 0.81934232, 0.81934232, 0.59285185, 0.67035485], ) From 120acb0d3a4941413460a318487831a38be73f87 Mon Sep 17 00:00:00 2001 From: Donncha O'Toole Date: Sun, 27 Sep 2026 13:32:55 +0100 Subject: [PATCH 4/4] Fix line break in gateway calculation --- bct/algorithms/centrality.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bct/algorithms/centrality.py b/bct/algorithms/centrality.py index 9767c29..c2be852 100644 --- a/bct/algorithms/centrality.py +++ b/bct/algorithms/centrality.py @@ -538,7 +538,8 @@ 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(\n ks[ci == i+1, :], axis=0) + 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