diff --git a/bct/algorithms/clustering.py b/bct/algorithms/clustering.py index 9b06b03..968802b 100644 --- a/bct/algorithms/clustering.py +++ b/bct/algorithms/clustering.py @@ -73,7 +73,7 @@ def agreement_weighted(ci, wts): Parameters ---------- - ci : MxN np.ndarray + ci : NxM np.ndarray set of M (possibly degenerate) partitions of N nodes wts : Mx1 np.ndarray relative weight of each partition @@ -84,12 +84,12 @@ def agreement_weighted(ci, wts): weighted agreement matrix ''' ci = np.array(ci) - m, n = ci.shape + n, m = ci.shape wts = np.array(wts) / np.sum(wts) D = np.zeros((n, n)) for i in range(m): - d = dummyvar(ci[i, :].reshape(1, n)) + d = dummyvar(ci[:, i].reshape(n, 1)) D += np.dot(d, d.T) * wts[i] return D diff --git a/test/clustering_test.py b/test/clustering_test.py index 26dda21..36a241c 100644 --- a/test/clustering_test.py +++ b/test/clustering_test.py @@ -105,8 +105,26 @@ def test_transitivity_bd(): def test_agreement_weighted(): - # this function is very hard to use or interpret results from - pass + # Test whether agreement gives the same results as + # agreement_weighted when the weights are all the same + ci = np.array([[1, 1, 2, 2, 3], + [1, 2, 2, 3, 3], + [1, 1, 2, 3, 3]]).T + wts = np.ones(ci.shape[1]) + + D_agreement = bct.agreement(ci) + D_weighted = bct.agreement_weighted(ci, wts) + + # Undo the normalization and fill the diagonal with zeros + # in D_weighted to get the same result as D_agreement + D_weighted = D_weighted * ci.shape[1] + np.fill_diagonal(D_weighted, 0) + + print('agreement matrix:') + print(D_agreement) + print('weighted agreement matrix:') + print(D_weighted) + assert (D_agreement == D_weighted).all() def test_agreement(): # Case 1: nodes > partitions