-
Notifications
You must be signed in to change notification settings - Fork 1
/
hierarchical_cluster.m
136 lines (117 loc) · 3.69 KB
/
hierarchical_cluster.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
function acc = cluster_hierarchical(G)
%acc =cluster_hierarchical(G)
%
% Performs hierarchical_cluster(G) on graphs G
%
% INPUT
% G : collection of graphs stored as the cell of size # of graphs x # of clusters
%
% OUTPUT
% acc: clustering accuracy
%
% The topologial cluserting method is explained in
%
% [1] % Chung, M.K., Huang, S.-G., Carroll, I.C., Calhoun, V.D., Goldsmith, H.H.
% 2023 Topological State-Space Estimation of Functional Human Brain Networks. arXiv:2201:00087
%
% [2] Moo K. Chung, Camille Garcia Ramos, Felipe Branco De Paiva, Jedidiah Mathis, Vivek Prabharakaren,
% Veena A. Nair, Elizabeth Meyerand, Bruce P. Hermann, Jeffery R. Binder, Aaron F. Struck, 2023
% Unified Topological Inference for Brain Networks in Temporal Lobe Epilepsy Using the Wasserstein Distance,
% arXiv:2302.06673
%
%
% The topologial distace is given in
%
% [3] Songdechakraiwut, T., Shen, L., Chung, M.K. 2021 Topological learning and
% its application to multimodal brain network integration, Medical Image
% Computing and Computer Assisted Intervention (MICCAI), LNCS 12902:166-176
%
% [4] Songdechakraiwut, T. Chung, M.K. 2023 Topological learning
% for brain networks, Annals of Applied Statistics 17:403-433, arXiv: 2012.00675
%
% If you are using any part of the code, please reference the above paper.
% The function is downloaded from
% http://pages.stat.wisc.edu/~mchung/publication.html
%
%
% The function is downloaded from
% https://github.com/laplcebeltrami/PH-STAT
%
%
%
% (C) 2020 Moo K. Chung
% University of Wisconsin-Madison
% Contact mkchung@wisc.edu for support
%
% Update history
% December 22, 2021 create
% Otober 24, 2022 updated
% March 25, 2023 made into a function
%% Compute set of births and set of deaths
nG = size(G,1); %number of graphs in each cluster
nC = size(G,2); %number of clusters
nNetworks = nG*nC; %total number of networks
nNodes = size(G{1,1},1); % # of nodes
nEdges = nNodes*(nNodes-1)/2; % # of edges
%C=zeros(nEdges,nNetworks);
C=[];
for j=1:nC
for i=1:nG
edgeweights = adj2vec(G{i,j});
C=[C edgeweights'];
end
end
ytrue=[]; %true labels
for i = 1:nC
% add i to the sequence of clusters
ytrue = [ytrue; repmat(i, nG, 1)];
end
ZWS = linkage(C','ward');
cWS = cluster(ZWS, 'Maxclust',nC);
acc = clustering_accuracy(ytrue, cWS);
%clustergram(ZWS) %, 'RowLabels', [], 'ColumnLabels', [], 'Linkage', 'complete', 'Dendrogram', 'row')
%------------------------
function vec = adj2vec(adj);
%
% function vec = adj2vec(adj);
% vectorize the square matrix and produce the vector of elemenets
% in upper triangle. For example, from the distance matrix, it produces the pdist.
%
% INPUT:
% adj has to be bigger than 2 x 2 matrix
%
%
% (C) 2019 Moo K. Chung
% University of Wisconsin-Madison
% mkchung@wics.edu
%
n=size(adj,1);
vec=adj(1,2:end);
if n>=2
for i=2:n
vec= [vec adj(i,i+1:end)];
end;
end
%-------------
function [accuracy C]=clustering_accuracy(ytrue,ypred)
% function accuracy=cluster_accuracy(ytrue,ypred)
%
% Find the clustering accuracy of prediction, given the true labels
%
% INPUT
% ytrue = a vector of true labels
% ypred = a vector of the predicted labels
% OUTPUT
% acc = Accuracy of clustering results
%
% (C) 2021 Moo K. Chung
% University of Wisconsin-Madison
% Contact mkchung@wisc.edu for support
%
% Update history
% 2021 Nov 28
% 2023 Apri 11 function name changed from clustering_accuracy.m to cluster_accuracy.m
n=length(ytrue); % number of samples
C = confusionmat(ytrue,ypred);
M=matchpairs(C, 0, 'max');
accuracy=sum(C(sub2ind(size(C), M(:,1), M(:,2))))/n;