/
ECS.R
1724 lines (1519 loc) · 69.3 KB
/
ECS.R
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#' @importFrom foreach %dopar%
NULL
#' The Element-Centric Clustering Similarity
#'
#' @description Calculates the average element-centric similarity between two
#' clustering results
#'
#' @param clustering1 The first clustering result, which can be one of:
#' * A numeric/character/factor vector of cluster labels for each element.
#' * A samples x clusters matrix/Matrix::Matrix of nonzero membership values.
#' * An hclust object.
#' @param clustering2 The second clustering result, which can be one of:
#' * A numeric/character/factor vector of cluster labels for each element.
#' * A samples x clusters matrix/Matrix::Matrix of nonzero membership values.
#' * An hclust object.
#' @param alpha A numeric giving the personalized PageRank damping factor;
#' 1 - alpha is the restart probability for the PPR random walk.
#' @param ppr_implementation_cl1 Choose a implementation for personalized
#' page-rank calculation for the first clustering:
#' * "prpack": use PPR algorithms in igraph.
#' * "power_iteration": use power_iteration method.
#' @param row_normalize_cl1 Whether to normalize all rows in the first clustering
#' so they sum to one before calculating ECS. It is recommended to set this to
#' TRUE, which will lead to slightly different ECS values compared to clusim.
#' @param r_cl1 A numeric hierarchical scaling parameter for the first clustering.
#' @param rescale_path_type_cl1 A string; rescale the hierarchical height of
#' the first clustering by:
#' * "max" : the maximum path from the root.
#' * "min" : the minimum path form the root.
#' * "linkage" : use the linkage distances in the clustering.
#' @param dist_rescaled_cl1 A logical: if TRUE, the linkage distances of the first
#' clustering are linearly rescaled to be in-between 0 and 1.
#' @param ppr_implementation_cl2 Choose a implementation for personalized
#' page-rank calculation for the second clustering:
#' * "prpack": use PPR algorithms in igraph.
#' * "power_iteration": use power_iteration method.
#' @param row_normalize_cl2 Whether to normalize all rows in the second clustering
#' so they sum to one before calculating ECS. It is recommended to set this to
#' TRUE, which will lead to slightly different ECS values compared to clusim.
#' @param r_cl2 A numeric hierarchical scaling parameter for the second clustering.
#' @param rescale_path_type_cl2 A string; rescale the hierarchical height of
#' the second clustering by:
#' * "max" : the maximum path from the root.
#' * "min" : the minimum path form the root.
#' * "linkage" : use the linkage distances in the clustering.
#' @param dist_rescaled_cl2 A logical: if TRUE, the linkage distances of the second
#' clustering are linearly rescaled to be in-between 0 and 1.
#'
#' @return The average element-wise similarity between the two Clusterings.
#' @export
#' @md
#'
#' @examples
#' km.res = kmeans(mtcars, centers=3)$cluster
#' hc.res = hclust(dist(mtcars))
#' element_sim(km.res, hc.res)
element_sim = function(clustering1,
clustering2,
alpha = 0.9,
r_cl1 = 1,
rescale_path_type_cl1 = "max",
ppr_implementation_cl1 = "prpack",
dist_rescaled_cl1 = FALSE,
row_normalize_cl1 = TRUE,
r_cl2 = 1,
rescale_path_type_cl2 = "max",
ppr_implementation_cl2 = "prpack",
dist_rescaled_cl2 = FALSE,
row_normalize_cl2 = TRUE) {
element_scores = element_sim_elscore(clustering1,
clustering2,
alpha,
r_cl1,
rescale_path_type_cl1,
ppr_implementation_cl1,
dist_rescaled_cl1,
row_normalize_cl1,
r_cl2,
rescale_path_type_cl2,
ppr_implementation_cl2,
dist_rescaled_cl2,
row_normalize_cl2)
return(mean(element_scores))
}
#' The Element-Centric Clustering Similarity for each Element
#'
#' @description Calculates the element-wise element-centric similarity between
#' two clustering results.
#'
#' @param clustering1 The first clustering result, which can be one of:
#' * A numeric/character/factor vector of cluster labels for each element.
#' * A samples x clusters matrix/Matrix::Matrix of nonzero membership values.
#' * An hclust object.
#' @param clustering2 The second clustering result, which can be one of:
#' * A numeric/character/factor vector of cluster labels for each element.
#' * A samples x clusters matrix/Matrix::Matrix of nonzero membership values.
#' * An hclust object.
#' @param alpha A numeric giving the personalized PageRank damping factor;
#' 1 - alpha is the restart probability for the PPR random walk.
#' @param ppr_implementation_cl1 Choose a implementation for personalized
#' page-rank calculation for the first clustering:
#' * "prpack": use PPR algorithms in igraph.
#' * "power_iteration": use power_iteration method.
#' @param row_normalize_cl1 Whether to normalize all rows in the first clustering
#' so they sum to one before calculating ECS. It is recommended to set this to
#' TRUE, which will lead to slightly different ECS values compared to clusim.
#' @param r_cl1 A numeric hierarchical scaling parameter for the first clustering.
#' @param rescale_path_type_cl1 A string; rescale the hierarchical height of
#' the first clustering by:
#' * "max" : the maximum path from the root.
#' * "min" : the minimum path form the root.
#' * "linkage" : use the linkage distances in the clustering.
#' @param dist_rescaled_cl1 A logical: if TRUE, the linkage distances of the first
#' clustering are linearly rescaled to be in-between 0 and 1.
#' @param ppr_implementation_cl2 Choose a implementation for personalized
#' page-rank calculation for the second clustering:
#' * "prpack": use PPR algorithms in igraph.
#' * "power_iteration": use power_iteration method.
#' @param row_normalize_cl2 Whether to normalize all rows in the second clustering
#' so they sum to one before calculating ECS. It is recommended to set this to
#' TRUE, which will lead to slightly different ECS values compared to clusim.
#' @param r_cl2 A numeric hierarchical scaling parameter for the second clustering.
#' @param rescale_path_type_cl2 A string; rescale the hierarchical height of
#' the second clustering by:
#' * "max" : the maximum path from the root.
#' * "min" : the minimum path form the root.
#' * "linkage" : use the linkage distances in the clustering.
#' @param dist_rescaled_cl2 A logical: if TRUE, the linkage distances of the second
#' clustering are linearly rescaled to be in-between 0 and 1.
#'
#' @return Vector of element-centric similarity between the two clusterings for
#' each element.
#' @export
#' @md
#'
#' @references Gates, A. J., Wood, I. B., Hetrick, W. P., & Ahn, Y. Y. (2019).
#' Element-centric clustering comparison unifies overlaps and hierarchy.
#' Scientific reports, 9(1), 1-13. https://doi.org/10.1038/s41598-019-44892-y
#'
#' @examples
#' km.res = kmeans(iris[,1:4], centers=8)$cluster
#' hc.res = hclust(dist(iris[,1:4]))
#' element_sim_elscore(km.res, hc.res)
element_sim_elscore = function(clustering1,
clustering2,
alpha = 0.9,
r_cl1 = 1,
rescale_path_type_cl1 = "max",
ppr_implementation_cl1 = "prpack",
dist_rescaled_cl1 = FALSE,
row_normalize_cl1 = TRUE,
r_cl2 = 1,
rescale_path_type_cl2 = "max",
ppr_implementation_cl2 = "prpack",
dist_rescaled_cl2 = FALSE,
row_normalize_cl2 = TRUE) {
# if both clusterings are membership vectors, calculate the ecs without
# creating a Clustering object
if(any(class(clustering1) %in% c("numeric", "integer", "factor", "character")) &&
any(class(clustering2) %in% c("numeric", "integer", "factor", "character"))) {
if (length(clustering1) != length(clustering2)){
stop('clustering1 and clustering2 do not have the same length.')
}
if (any(names(clustering1) != names(clustering2))) {
stop('Not all elements of clustering1 and clustering2 are the same.')
}
node.scores = corrected_l1_mb(clustering1,
clustering2,
alpha)
names(node.scores) = names(clustering1)
return(node.scores)
}
if(methods::is(clustering1, "hclust")) {
clustering1 = create_clustering(clustering_result = clustering1,
alpha = alpha,
r = r_cl1,
rescale_path_type = rescale_path_type_cl1,
ppr_implementation = ppr_implementation_cl1,
dist_rescaled = dist_rescaled_cl1)
} else if(is.matrix(clustering1) | methods::is(clustering1, "Matrix")) {
clustering1 = create_clustering(clustering_result = clustering1,
alpha = alpha,
ppr_implementation = ppr_implementation_cl1,
row_normalize = row_normalize_cl1)
} else
clustering1 = create_clustering(clustering1,
alpha = alpha)
if(methods::is(clustering2, "hclust")) {
clustering2 = create_clustering(clustering_result = clustering2,
alpha = alpha,
r = r_cl2,
rescale_path_type = rescale_path_type_cl2,
ppr_implementation = ppr_implementation_cl2,
dist_rescaled = dist_rescaled_cl2)
} else if (is.matrix(clustering2) | methods::is(clustering2, "Matrix")) {
clustering2 = create_clustering(clustering_result = clustering2,
alpha = alpha,
ppr_implementation = ppr_implementation_cl2,
row_normalize = row_normalize_cl2)
} else {
clustering2 = create_clustering(clustering2,
alpha = alpha)
}
# Make sure clusterings are comparable
if (clustering1@n_elements != clustering2@n_elements){
stop('clustering1 and clustering2 do not have the same length.')
} else if (any(names(clustering1) != names(clustering2))){
stop('Not all elements of clustering1 and clustering2 are the same.')
}
# use the corrected L1 similarity
node.scores = corrected_L1(clustering1@affinity_matrix,
clustering2@affinity_matrix,
clustering1@alpha)
names(node.scores) = names(clustering1)
return(node.scores)
}
# create the cluster -> element dictionary (list) for flat, disjoint clustering
create_clu2elm_dict = function(clustering){
clu2elm_dict = list()
for (i in unique(clustering)){
clu2elm_dict[[i]] = which(clustering == i)
}
return(clu2elm_dict)
}
# create the cluster -> element dictionary (list) for hierarchical clustering
create_clu2elm_dict_hierarchical = function(clustering){
n.clusters = nrow(clustering$merge)
clu2elm_dict = list()
# add NA to initialize list
clu2elm_dict[[2*n.clusters+1]] = NA
# add single member clusters for leaf nodes
for (i in 1:(n.clusters+1)){
clu2elm_dict[[i]] = i
}
# traverse hc tree and add nodes to clu2elm_dict
for (i in 1:n.clusters){
for (j in 1:2){
index = clustering$merge[i,j]
if (index<0){ # for leaf nodes, just append them
clu2elm_dict[[i+n.clusters+1]] = c(clu2elm_dict[[i+n.clusters+1]],
-index)
} else { # for previous clusters, append all leaf nodes in cluster
clu2elm_dict[[i+n.clusters+1]] = c(clu2elm_dict[[i+n.clusters+1]],
clu2elm_dict[[index+n.clusters+1]])
}
}
}
# remove the NA that was added in the beginning
clu2elm_dict[[2*n.clusters+1]] = clu2elm_dict[[2*n.clusters+1]][-1]
return(clu2elm_dict)
}
# create element -> cluster dictionary (list) for flat, overlapping clustering
create_elm2clu_dict_overlapping = function(clustering){
elm2clu_dict = list()
for (i in 1:nrow(clustering)){
elm2clu_dict[[i]] = which(clustering[i,]>0)
}
return(elm2clu_dict)
}
# create element -> cluster dictionary (list) for hierarchical clustering: only
# maps elements to leaf node singleton clusters
create_elm2clu_dict_hierarchical = function(clustering){
elm2clu_dict = list()
for (i in 1:length(clustering$order)){
elm2clu_dict[[i]] = i
}
return(elm2clu_dict)
}
# corrected L1 distance
corrected_L1 = function(x, y, alpha){
res = 1 - 1/(2 * alpha) * rowSums(abs(x - y))
return(res)
}
# corrected L1 distance for two membership vectors
corrected_l1_mb = function(mb1, mb2, alpha = 0.9) {
n = length(mb1)
if(!is.character(mb1))
mb1 = as.character(mb1)
if(!is.character(mb2))
mb2 = as.character(mb2)
clu2elm_dict_1 = create_clu2elm_dict(mb1)
clu2elm_dict_2 = create_clu2elm_dict(mb2)
# the possible number of different ecs values is n x m
# where n is the number of clusters of the first partition
# and n the number of clusters of the second partition
unique_ecs_vals = matrix(NA, nrow = length(clu2elm_dict_1), ncol = length(clu2elm_dict_2))
rownames(unique_ecs_vals) = names(clu2elm_dict_1)
colnames(unique_ecs_vals) = names(clu2elm_dict_2)
ecs = rep(0, n)
ppr1 = rep(0, n)
ppr2 = rep(0, n)
# iterate through each point of the membership vector
for(i in 1:n) {
# check if the similarity between of this pair of clusters was already calculated
if(is.na(unique_ecs_vals[mb1[i], mb2[i]])) {
clusterlist1 = clu2elm_dict_1[[mb1[i]]]
Csize1 = length(clusterlist1)
# get the values from the affinity matrix of the first partition
ppr1[clusterlist1] = alpha / Csize1
ppr1[i] = 1.0 - alpha + alpha / Csize1
clusterlist2 = clu2elm_dict_2[[mb2[i]]]
Csize2 = length(clusterlist2)
# get the values from the affinity matrix of the second partition
ppr2[clusterlist2] = alpha / Csize2
ppr2[i] = 1.0 - alpha + alpha / Csize2
# calculate the sum of the difference between the affinity matrices
ecs[i] = sum(abs(ppr1-ppr2)) # could be optimized?
ppr1[clusterlist1] = 0
ppr2[clusterlist2] = 0
# store the similarity between the pair of clusters
unique_ecs_vals[mb1[i], mb2[i]] = ecs[i]
} else {
# if yes, just copy the value
ecs[i] = unique_ecs_vals[mb1[i], mb2[i]]
}
}
# perform the last calculations to obtain the ECS at each point
return(1 - 1 / (2 * alpha) * ecs)
}
# PPR calculation for partition
ppr_partition = function(clustering, alpha=0.9){
ppr = matrix(0, nrow=clustering@n_elements, ncol=clustering@n_elements)
for (i in 1:length(clustering@clu2elm_dict)){
clusterlist = clustering@clu2elm_dict[[i]]
Csize = length(clusterlist)
ppr_res = matrix(alpha/Csize, nrow=Csize, ncol=Csize)
diag(ppr_res) = 1.0 - alpha + alpha/Csize
ppr[clusterlist, clusterlist] = ppr_res
}
return(ppr)
}
# Create the cluster-induced element graph for overlapping clustering
make_cielg_overlapping = function(bipartite_adj){
proj1 = diag(x = 1 / rowSums(bipartite_adj), nrow = nrow(bipartite_adj)) %*%
bipartite_adj
proj2 = bipartite_adj %*%
diag(x = 1 / colSums(bipartite_adj), nrow = ncol(bipartite_adj))
projected_adj = proj1 %*% t(proj2)
cielg = igraph::graph_from_adjacency_matrix(projected_adj,
weighted=TRUE,
mode="directed")
return(cielg)
}
# create linkage distances vector
get_linkage_dist = function(hierarchical_clustering, dist_rescaled){
n.elements = length(hierarchical_clustering$order)
if (dist_rescaled){
maxdist = max(hierarchical_clustering$height)
# add 1s at beggining to account for leaf node singleton clusters
linkage_dist = c(rep(1, n.elements),
1.0 - hierarchical_clustering$height/maxdist)
} else {
# add 0s at beginning to account for leaf node singleton clusters
linkage_dist = c(rep(0, n.elements), hierarchical_clustering$height)
}
return(linkage_dist)
}
# helper function to calculate path distances in graph
dist_path = function(hierarchical_clustering,
rescale_path_type){
n.elements = length(hierarchical_clustering$order)
# dist to node from final cluster containing all elements
path_to_node = rep(0, 2*n.elements-1)
for (i in rev(1:(n.elements-1))){
for (j in 1:2){
index = hierarchical_clustering$merge[i,j]
if (index < 0){
index = -index
} else {
index = index + n.elements
}
if (rescale_path_type=="min"){
if (path_to_node[index] == 0){
path_to_node[index] = path_to_node[i + n.elements] + 1
} else {
path_to_node[index] = min(path_to_node[index],
path_to_node[i + n.elements] + 1)
}
} else {
path_to_node[index] = max(path_to_node[index],
path_to_node[i + n.elements] + 1)
}
}
}
# dist to node from leaves
path_from_node = rep(0, 2*n.elements-1)
for (i in 1:(n.elements-1)){
index = hierarchical_clustering$merge[i,]
for (j in 1:2){
if (index[j] < 0){
index[j] = -index[j]
} else {
index[j] = index[j] + n.elements
}
}
if (rescale_path_type=="min"){
path_from_node[i + n.elements] = min(path_from_node[index[1]],
path_from_node[index[2]]) + 1
} else {
path_from_node[i + n.elements] = max(path_from_node[index[1]],
path_from_node[index[2]]) + 1
}
}
return(list(to=path_to_node, from=path_from_node))
}
# rescale the hierarchical clustering path
rescale_path = function(hierarchical_clustering,
rescale_path_type){
# get path distances
path.dist = dist_path(hierarchical_clustering, rescale_path_type)
path_from_node = path.dist$from
path_to_node = path.dist$to
n.elements = length(hierarchical_clustering$order)
rescaled_level = rep(0, n.elements)
for (i in 1:(2*n.elements-1)){
total_path_length = path_from_node[i] + path_to_node[i]
if (total_path_length != 0){
rescaled_level[i] = path_to_node[i] / total_path_length
}
}
return(rescaled_level)
}
# Create the cluster-induced element graph for hierarchical clustering
make_cielg_hierarchical = function(hierarchical_clustering,
clu2elm_dict,
r,
rescale_path_type,
dist_rescaled=FALSE){
n.elements = length(hierarchical_clustering$order)
# rescale paths
if (rescale_path_type == "linkage"){
cluster_height = get_linkage_dist(hierarchical_clustering,
dist_rescaled)
} else if (rescale_path_type %in% c("min", "max")) {
cluster_height = rescale_path(hierarchical_clustering,
rescale_path_type)
} else {
stop(paste0("rescale_path_type must be one of linkage, min or max."))
}
# weight function for different heights
weight_function = function(clust) return(exp(r * (cluster_height[clust])))
bipartite_adj = matrix(0, nrow = n.elements, ncol = 2*n.elements - 1)
for (i in 1:length(clu2elm_dict)){
element_list = clu2elm_dict[[i]]
cstrength = weight_function(i)
for (el in element_list){
bipartite_adj[el, i] = cstrength
}
}
proj1 = diag(x = 1 / rowSums(bipartite_adj), nrow = nrow(bipartite_adj)) %*%
bipartite_adj
proj2 = bipartite_adj %*%
diag(x = 1 / colSums(bipartite_adj), nrow = ncol(bipartite_adj))
projected_adj = proj1 %*% t(proj2)
cielg = igraph::graph_from_adjacency_matrix(projected_adj,
weighted=TRUE,
mode="directed")
return(cielg)
}
# utility function to find vertices with the same cluster memberships.
find_groups_in_cluster = function(clustervs, elementgroupList){
clustervertex = unique(clustervs)
groupings = list()
index = 1
for (i in 1:length(elementgroupList)){
current = elementgroupList[[i]]
if (length(intersect(current, clustervertex))>0){
groupings[[index]] = current
index = index + 1
}
}
return(groupings)
}
# numerical calculation of affinity matrix using PPR
numerical_ppr_scores = function(cielg, clustering, ppr_implementation="prpack"){
if (!(ppr_implementation %in% c("prpack", "power_iteration"))){
stop('ppr_implementation argument must be one of prpack or power_iteration')
}
# keep track of all clusters an element is a member of
elementgroupList = list()
for (i in 1:length(clustering@elm2clu_dict)){
element = names(clustering)[i]
# collapse clusters into single string
clusters = paste(clustering@elm2clu_dict[[i]], collapse=",")
elementgroupList[[clusters]] = c(elementgroupList[[clusters]], element)
}
ppr_scores = matrix(0,
nrow=igraph::vcount(cielg),
ncol=igraph::vcount(cielg))
# we have to calculate the ppr for each connected component
comps = igraph::components(cielg)
for (i.comp in 1:comps$no){
members = which(comps$membership == i.comp)
clustergraph = igraph::induced_subgraph(cielg, members, impl="auto")
cc_ppr_scores = matrix(0,
nrow=igraph::vcount(clustergraph),
ncol=igraph::vcount(clustergraph))
if (ppr_implementation == "power_iteration"){
W_matrix = get_sparse_transition_matrix(clustergraph)
}
elementgroups = find_groups_in_cluster(members, elementgroupList)
for (elementgroup in elementgroups){
# we only have to solve for the ppr distribution once per group
vertex.index = match(elementgroup[1], members)
if (ppr_implementation == "prpack"){
pers = rep(0, length=length(members))
pers[vertex.index] = 1
ppr_res = igraph::page_rank(clustergraph,
directed=TRUE,
weights=NULL,
damping=clustering@alpha,
personalized=pers,
algo="prpack")
cc_ppr_scores[vertex.index,] = ppr_res$vector
} else if (ppr_implementation == "power_iteration"){
cc_ppr_scores[vertex.index,] = calculate_ppr_with_power_iteration(
W_matrix,
vertex.index,
alpha=clustering@alpha,
repetition=1000,
th=0.0001)
}
# the other vertices in the group are permutations of that solution
elgroup.size = length(elementgroup)
if (elgroup.size >= 2){
for (i2 in 2:elgroup.size){
v2 = match(elementgroup[i2], members)
cc_ppr_scores[v2,] = cc_ppr_scores[vertex.index,]
cc_ppr_scores[v2, vertex.index] = cc_ppr_scores[vertex.index, v2]
cc_ppr_scores[v2, v2] = cc_ppr_scores[vertex.index, vertex.index]
}
}
}
ppr_scores[members, members] = cc_ppr_scores
}
return(ppr_scores)
}
# utility function to get a row-normalized sparse transition matrix
get_sparse_transition_matrix = function(graph){
transition_matrix = igraph::as_adjacency_matrix(graph, attr="weight", sparse = FALSE)
transition_matrix = diag(x = 1 / rowSums(transition_matrix), nrow = nrow(transition_matrix)) %*%
transition_matrix
return(transition_matrix)
}
# power iteration calculation of PPR
calculate_ppr_with_power_iteration = function(W_matrix, index, alpha=0.9,
repetition=1000, th=1e-4){
total_length = nrow(W_matrix)
e_s = matrix(0, nrow = 1, ncol = total_length)
e_s[1, index] = 1
p = matrix(0, nrow = 1, ncol = total_length)
p[1, index] = 1
for (i in 1:repetition){
new_p = ((1-alpha) * e_s) + ((alpha) * (p %*% W_matrix))
if (max(abs(new_p - p)) < th){
p = new_p
break
}
p = new_p
}
return(p)
}
#' Pairwise Comparison of Clusterings
#' @description Compare a set of clusterings by calculating their pairwise
#' average element-centric clustering similarities.
#'
#' @param clustering_list The list of clustering results, each of which is either:
#' * A numeric/character/factor vector of cluster labels for each element.
#' * A samples x clusters matrix/Matrix::Matrix of nonzero membership values.
#' * An hclust object.
#' @param output_type A string specifying whether the output should be a
#' matrix or a data.frame.
#' @param alpha A numeric giving the personalized PageRank damping factor;
#' 1 - alpha is the restart probability for the PPR random walk.
#' @param ppr_implementation Choose a implementation for personalized
#' page-rank calculation:
#' * "prpack": use PPR algorithms in igraph.
#' * "power_iteration": use power_iteration method.
#' @param row_normalize Whether to normalize all rows in clustering_result
#' so they sum to one before calculating ECS. It is recommended to set this to
#' TRUE, which will lead to slightly different ECS values compared to clusim.
#' @param r A numeric hierarchical scaling parameter.
#' @param rescale_path_type A string; rescale the hierarchical height by:
#' * "max" : the maximum path from the root.
#' * "min" : the minimum path form the root.
#' * "linkage" : use the linkage distances in the clustering.
#' @param dist_rescaled A logical: if TRUE, the linkage distances are linearly
#' rescaled to be in-between 0 and 1.
#' @param ncores the number of parallel R instances that will run the code.
#' If the value is set to 1, the code will be run sequentially.
#'
#' @return A matrix or data.frame containing the pairwise ECS values.
#' @export
#' @md
#'
#' @references Gates, A. J., Wood, I. B., Hetrick, W. P., & Ahn, Y. Y. (2019).
#' Element-centric clustering comparison unifies overlaps and hierarchy.
#' Scientific reports, 9(1), 1-13. https://doi.org/10.1038/s41598-019-44892-y
#'
#' @examples
#' # cluster across 20 random seeds
#' clustering.list = lapply(1:20, function(x) kmeans(mtcars, centers=3)$cluster)
#' element_sim_matrix(clustering.list, output_type="matrix")
element_sim_matrix = function(clustering_list,
output_type="matrix",
alpha = 0.9,
r = 1,
rescale_path_type = "max",
ppr_implementation = "prpack",
dist_rescaled = FALSE,
row_normalize = TRUE,
ncores = 1) {
if (!(output_type %in% c("data.frame", "matrix"))){
stop('output_type must be data.frame or matrix.')
}
# check if all objects are flat disjoint membership vectors
are_all_flat_disjoint = sapply(clustering_list, function(x) {
any(class(x) %in% c("numeric", "integer", "factor", "character"))
})
are_all_viable_objects = sapply(clustering_list, function(x) {
is.matrix(x) || methods::is(x, "hclust") || methods::is(x, "Matrix")
})
if(any(are_all_flat_disjoint == FALSE) &&
any(are_all_viable_objects == FALSE)) {
stop("Please ensure each entry in clustering_list is from these following classes: numeric, factor, character, matrix, Matrix, hclust.")
}
# if the condition is met, perform element frustration using only the membership vector
if(all(are_all_flat_disjoint == TRUE)) {
return(element_sim_matrix_flat_disjoint(mb_list = clustering_list,
ncores = ncores,
alpha = alpha,
output_type = output_type))
}
clustering_list = create_clustering_list(object_list = clustering_list,
ncores = ncores,
alpha = alpha,
r = r,
rescale_path_type = rescale_path_type,
ppr_implementation = ppr_implementation,
dist_rescaled = dist_rescaled,
row_normalize = row_normalize)
n.clusterings = length(clustering_list)
sim_matrix = matrix(NA, nrow=n.clusterings, ncol=n.clusterings)
for (i in 1:(n.clusterings-1)){
i.aff = clustering_list[[i]]@affinity_matrix
for (j in (i+1):n.clusterings){
j.aff = clustering_list[[j]]@affinity_matrix
sim_matrix[i, j] = mean(corrected_L1(i.aff, j.aff, alpha))
}
}
diag(sim_matrix) = 1
if (output_type=="data.frame"){
sim_matrix = data.frame(i.clustering=rep(1:n.clusterings,
n.clusterings),
j.clustering=rep(1:n.clusterings,
each=n.clusterings),
element_sim=c(sim_matrix))
}
return(sim_matrix)
}
# calculate the similarity matrix when all the objects are flat disjoint memberships
element_sim_matrix_flat_disjoint = function(mb_list, ncores = 1, alpha = 0.9, output_type="matrix") {
if (!(output_type %in% c("data.frame", "matrix"))){
stop('output_type must be data.frame or matrix.')
}
n_clusterings = length(mb_list)
first_index = unlist(sapply(1:(n_clusterings-1), function(i) { rep(i, n_clusterings-i)}))
second_index = unlist(sapply(1:(n_clusterings-1), function(i) { (i+1):n_clusterings}))
n_combinations = n_clusterings * (n_clusterings-1) / 2
ncores = min(ncores, n_combinations, parallel::detectCores())
if(ncores > 1) {
# create a parallel backend
sim_matrix_cluster <- parallel::makeCluster(
ncores,
type = "PSOCK"
)
doParallel::registerDoParallel(cl = sim_matrix_cluster)
} else {
# create a sequential backend
foreach::registerDoSEQ()
}
i = NA
# calculate the ecs between every pair of partitions
ecs_values = foreach::foreach(i = 1:n_combinations, .export = c("corrected_l1_mb", "create_clu2elm_dict"), .noexport = c("my_cluster"), .combine = "c") %dopar% {
mean(corrected_l1_mb(mb_list[[first_index[i]]],
mb_list[[second_index[i]]],
alpha))
}
if(ncores > 1)
# terminate the processes if a parallel backend was created
parallel::stopCluster(cl = sim_matrix_cluster)
sim_matrix = matrix(NA, nrow=n_clusterings, ncol=n_clusterings)
sim_matrix[lower.tri(sim_matrix, diag=FALSE)]= ecs_values
sim_matrix = t(sim_matrix)
diag(sim_matrix) = 1
if (output_type=="data.frame") {
sim_matrix = data.frame(i.clustering=rep(1:n_clusterings,
n_clusterings),
j.clustering=rep(1:n_clusterings,
each=n_clusterings),
element_sim=c(sim_matrix))
}
return(sim_matrix)
}
# determine whether two flat disjoint membership vectors are identical
are_identical_memberships = function(mb1, mb2) {
# calculate the contingency table between the two partitions
contingency_table = (table(mb1, mb2) != 0)
if(nrow(contingency_table) != ncol(contingency_table)) {
return(FALSE)
}
no_different_elements = colSums(contingency_table)
# if a column has two or more nonzero entries, it means the cluster is split
# thus the two partitions are not identical
if(any(no_different_elements != 1)) {
return(FALSE)
}
no_different_elements = rowSums(contingency_table)
# if a row has two or more nonzero entries, it means the cluster is split
# thus the two partitions are not identical
return(all(no_different_elements == 1))
}
# merge partitions that are identical (meaning the ecs threshold is 1)
# the order parameter is indicating whether to sort the merged objects
# based on their frequency
merge_identical_partitions = function(clustering_list,
order = TRUE) {
# merge the same memberships into the same object
merged_partitions = list(clustering_list[[1]])
n_partitions = length(clustering_list)
if(n_partitions == 1) {
return(clustering_list)
}
for(i in 2:n_partitions) {
# n_partitions = length(merged_partitions)
n_merged_partitions = length(merged_partitions)
assigned_partition = -1
# for each partition, look into the merged list and see if there is a perfect match
for(j in 1:n_merged_partitions) { # for(j in 1:n_partitions) {}
are_identical = are_identical_memberships(merged_partitions[[j]]$mb, clustering_list[[i]]$mb)
if(are_identical) {
assigned_partition = j
break
}
}
if(assigned_partition != -1) {
# if a match was found, update the frequency of the partition
merged_partitions[[assigned_partition]]$freq = merged_partitions[[assigned_partition]]$freq + clustering_list[[i]]$freq
} else {
# if not, add the partition to the list of merged partitions
merged_partitions[[n_merged_partitions+1]] = clustering_list[[i]]
}
}
# order the newly merged partitions based on their frequencies
if(order) {
ordered_indices = order(sapply(merged_partitions, function(x) { x$freq }), decreasing = T)
merged_partitions = merged_partitions[ordered_indices]
}
merged_partitions
}
# merge the partitions when the ecs threshold is not 1
merge_partitions_ecs = function(partition_list,
ecs_thresh = 0.99,
ncores = 1,
order = TRUE) {
partition_groups = list()
nparts = length(partition_list)
if(nparts == 1) {
return(partition_list)
}
# calculate the pairwise ecs between the partitions
sim_matrix = element_sim_matrix_flat_disjoint(lapply(partition_list, function(x) { x$mb }),
ncores = ncores)
for(i in 1:nparts) {
sim_matrix[i,i] = NA
partition_groups[[as.character(i)]] = i
}
while(length(partition_groups) > 1) {
# if the similarity between any pair of partitions is below the threshold
# we do not perform any merging
if(max(sim_matrix, na.rm = T) < ecs_thresh) {
break
}
# find the pair of partitions that are the most similar wrt ECS
index = which.max(sim_matrix)[1]
first_cluster = index %% nparts
second_cluster = index %/% nparts + 1
if(first_cluster == 0) {
first_cluster = nparts
second_cluster = second_cluster - 1
}
# update the partition group of the first partition, where we add the second one
partition_groups[[as.character(first_cluster)]] = c(partition_groups[[as.character(first_cluster)]],
partition_groups[[as.character(second_cluster)]])
# remove the partition group of the second partition that will be merged
partition_groups[[as.character(second_cluster)]] = NULL
# update the similarities in a single-linkeage fashion
# iterate only through the names of the groups of partitions, as the others
# are already filled with NAs
for(i in as.numeric(names(partition_groups))) {
if(first_cluster < i) {
if(!is.na(sim_matrix[first_cluster, i])) {
sim_matrix[first_cluster, i] = min(sim_matrix[first_cluster, i], sim_matrix[second_cluster, i], sim_matrix[i, second_cluster], na.rm = T)
}
} else {
if(!is.na(sim_matrix[i, first_cluster])) {
sim_matrix[i, first_cluster] = min(sim_matrix[i, first_cluster], sim_matrix[second_cluster, i], sim_matrix[i, second_cluster], na.rm = T)
}
}
}
sim_matrix[second_cluster, ] = NA
sim_matrix[, second_cluster] = NA
}
merged_index = 1
merged_partitions = list()
# update the frequencies of the partition groups
for(kept_partition in names(partition_groups)) {
merged_partitions[[merged_index]] = partition_list[[as.numeric(kept_partition)]]
merged_partitions[[merged_index]]$freq = sum(sapply(partition_groups[[kept_partition]], function(x) { partition_list[[as.numeric(x)]]$freq }))
merged_index = merged_index +1
}
# order the newly merged partitions based on their frequencies
if(order) {
ordered_indices = order(sapply(merged_partitions, function(x) { x$freq }), decreasing = T)
merged_partitions = merged_partitions[ordered_indices]
}
merged_partitions
}
#' Merge Partitions
#' @description Merge flat disjoint clusterings whose pairwise ECS score is
#' above a given threshold. The merging is done using a complete linkage approach.
#'
#' @param partition_list A list of flat disjoint membership vectors.
#' @param ecs_thresh A numeric: the ecs threshold.
#' @param ncores The number of parallel R instances that will run the code.
#' If the value is set to 1, the code will be run sequentially.
#' @param order A logical: if TRUE, order the partitions based on their frequencies.
#' @return a list of the merged partitions
#' @export
#' @examples
#' initial_list = list(c(1,1,2), c(2,2,2), c('B','B','A'))
#' merge_partitions(initial_list, 0.99)
merge_partitions = function(partition_list,
ecs_thresh = 1,
ncores = 1,
order = TRUE) {
# check the parameters
if(!is.numeric(ecs_thresh) || length(ecs_thresh) > 1)
stop("ecs_thresh parameter should be numeric")
if(!is.numeric(ncores) || length(ncores) > 1)
stop("ncores parameter should be numeric")
# convert ncores to an integer
ncores = as.integer(ncores)
if(!is.logical(order))
stop("order parameter should be logical")
# check the type of object that is provided in the list
if(class(partition_list[[1]]) != "list") { # the elements should be membership vectors
partition_list = lapply(partition_list, function(x) {
list(mb = x,
freq = 1)
})
} else {
if("partitions" %in% names(partition_list)) { # the list contains the partition field, created by `get_resolution_importance` method
part_list = merge_partitions(partition_list$partitions, ecs_thresh, ncores, order)
ec_consistency = weighted_element_consistency(lapply(part_list, function(x) {
x$mb
}),
sapply(part_list, function(x) {
x$freq
}),
ncores = ncores)
return(list(partitions = part_list, ecc = ec_consistency))
}
if(!all(c("mb", "freq") %in% names(partition_list[[1]]))) {