consensus_und.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. function ciu = consensus_und(d,tau,reps)
  2. % CONSENSUS_UND Consensus clustering
  3. %
  4. % CIU = CONSENSUS(D,TAU,REPS) seeks a consensus partition of the
  5. % agreement matrix D. The algorithm used here is almost identical to the
  6. % one introduced in Lancichinetti & Fortunato (2012): The agreement
  7. % matrix D is thresholded at a level TAU to remove an weak elements. The
  8. % resulting matrix is then partitions REPS number of times using the
  9. % Louvain algorithm (in principle, any clustering algorithm that can
  10. % handle weighted matrixes is a suitable alternative to the Louvain
  11. % algorithm and can be substituted in its place). This clustering
  12. % produces a set of partitions from which a new agreement is built. If
  13. % the partitions have not converged to a single representative partition,
  14. % the above process repeats itself, starting with the newly built
  15. % agreement matrix.
  16. %
  17. % NOTE: In this implementation, the elements of the agreement matrix must
  18. % be converted into probabilities.
  19. %
  20. % NOTE: This implementation is slightly different from the original
  21. % algorithm proposed by Lanchichinetti & Fortunato. In its original
  22. % version, if the thresholding produces singleton communities, those
  23. % nodes are reconnected to the network. Here, we leave any singleton
  24. % communities disconnected.
  25. %
  26. % Inputs: D, agreement matrix with entries between 0 and 1
  27. % denoting the probability of finding node i in the
  28. % same cluster as node j
  29. % TAU, threshold which controls the resolution of the
  30. % reclustering
  31. % REPS, number of times that the clustering algorithm is
  32. % reapplied
  33. %
  34. % Outputs: CIU, consensus partition
  35. %
  36. % References: Lancichinetti & Fortunato (2012). Consensus clustering in
  37. % complex networks. Scientific Reports 2, Article number: 336.
  38. %
  39. % Richard Betzel, Indiana University, 2012
  40. %
  41. % modified on 3/2014 to include "unique_partitions"
  42. n = length(d); flg = 1;
  43. while flg == 1
  44. flg = 0;
  45. dt = d.*(d >= tau).*~eye(n);
  46. if nnz(dt) == 0
  47. ciu = (1:n)';
  48. else
  49. ci = zeros(n,reps);
  50. for iter = 1:reps
  51. ci(:,iter) = community_louvain(dt);
  52. end
  53. ci = relabel_partitions(ci);
  54. ciu = unique_partitions(ci);
  55. nu = size(ciu,2);
  56. if nu > 1
  57. flg = 1;
  58. d = agreement(ci)./reps;
  59. end
  60. end
  61. end
  62. function cinew = relabel_partitions(ci)
  63. [n,m] = size(ci);
  64. cinew = zeros(n,m);
  65. for i = 1:m
  66. c = ci(:,i);
  67. d = zeros(size(c));
  68. count = 0;
  69. while sum(d ~= 0) < n
  70. count = count + 1;
  71. ind = find(c,1,'first');
  72. tgt = c(ind);
  73. rep = c == tgt;
  74. d(rep) = count;
  75. c(rep) = 0;
  76. end
  77. cinew(:,i) = d;
  78. end
  79. function ciu = unique_partitions(ci)
  80. ci = relabel_partitions(ci);
  81. ciu = [];
  82. count = 0;
  83. c = 1:size(ci,2);
  84. while ~isempty(ci)
  85. count = count + 1;
  86. tgt = ci(:,1);
  87. ciu = [ciu,tgt]; %#ok<AGROW>
  88. dff = sum(abs(bsxfun(@minus,ci,tgt))) == 0;
  89. ci(:,dff) = [];
  90. c(dff) = [];
  91. end