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