-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetFuzzyPartitionMetrics.R
301 lines (285 loc) · 13.3 KB
/
getFuzzyPartitionMetrics.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
#' Compute external metrics for fuzzy clusterings
#'
#' Computes a selection of external fuzzy clustering evaluation metrics.
#' @param hardTrue An atomic vector coercible to a factor or integer vector
#' containing the true hard labels.
#' @param fuzzyTrue A object coercible to a numeric matrix with membership
#' probability of elements (rows) in clusters (columns).
#' @param fuzzyPred A object coercible to a numeric matrix with membership
#' probability of elements (rows) in clusters (columns).
#' @param metrics The metrics to compute. See details.
#' @param level The level to calculate the metrics. Options include
#' `"element"`, `"class"` and `"dataset"`.
#' @inheritParams fuzzyPartitionMetrics
#' @inheritParams fuzzyHardMetrics
#' @inheritParams fuzzyHardSpotConcordance
#' @inheritParams getPairConcordance
#' @param ... Optional arguments for \code{\link{fuzzyPartitionMetrics}}:
#' `tnorm`. Only useful when `fuzzy_true=TRUE` and `fuzzy_pred=TRUE`.
#' @details
#' The allowed values for `metrics` depend on the value of `level`:
#' - If `level = "element"`, the allowed `metrics` are: `"fuzzySPC"`.
#' - If `level = "class"`, the allowed `metrics` are: `"fuzzyWH"`,
#' `"fuzzyAWH"`, `"fuzzyWC"`, `"fuzzyAWC"`.
#' - If `level = "dataset"`, the allowed `metrics` are: `"fuzzyRI"`,
#' `"fuzzyARI"`, `"fuzzyWH"`, `"fuzzyAWH"`, `"fuzzyWC"`, `"fuzzyAWC"`.
#' @return A dataframe of metric results.
#' @export
#' @examples
#'# generate fuzzy partitions:
#' m1 <- matrix(c(0.95, 0.025, 0.025,
#' 0.98, 0.01, 0.01,
#' 0.96, 0.02, 0.02,
#' 0.95, 0.04, 0.01,
#' 0.95, 0.01, 0.04,
#' 0.99, 0.005, 0.005,
#' 0.025, 0.95, 0.025,
#' 0.97, 0.02, 0.01,
#' 0.025, 0.025, 0.95),
#' ncol = 3, byrow=TRUE)
#' m2 <- matrix(c(0.95, 0.025, 0.025,
#' 0.98, 0.01, 0.01,
#' 0.96, 0.02, 0.02,
#' 0.025, 0.95, 0.025,
#' 0.02, 0.96, 0.02,
#' 0.01, 0.98, 0.01,
#' 0.05, 0.05, 0.95,
#' 0.02, 0.02, 0.96,
#' 0.01, 0.01, 0.98),
#' ncol = 3, byrow=TRUE)
#' colnames(m1) <- colnames(m2) <- LETTERS[seq_len(3)]
#' getFuzzyPartitionMetrics(fuzzyTrue=m1,fuzzyPred=m2, level="class")
#'
#' # generate a fuzzy truth:
#' fuzzyTrue <- matrix(c(
#' 0.95, 0.025, 0.025,
#' 0.98, 0.01, 0.01,
#' 0.96, 0.02, 0.02,
#' 0.95, 0.04, 0.01,
#' 0.95, 0.01, 0.04,
#' 0.99, 0.005, 0.005,
#' 0.025, 0.95, 0.025,
#' 0.97, 0.02, 0.01,
#' 0.025, 0.025, 0.95),
#' ncol = 3, byrow=TRUE)
#' # a hard truth:
#' hardTrue <- apply(fuzzyTrue,1,FUN=which.max)
#' # some predicted labels:
#' hardPred <- c(1,1,1,1,1,1,2,2,2)
#' getFuzzyPartitionMetrics(hardPred=hardPred, hardTrue=hardTrue,
#' fuzzyTrue=fuzzyTrue, nperms=3, level="class")
#'
getFuzzyPartitionMetrics <- function(hardTrue=NULL, fuzzyTrue=NULL,
hardPred=NULL, fuzzyPred=NULL,
metrics=c("fuzzyWH", "fuzzyAWH",
"fuzzyWC", "fuzzyAWC"),
level="class",
nperms=NULL, verbose=TRUE,
returnElementPairAccuracy=FALSE,
BPPARAM=BiocParallel::SerialParam(),
useNegatives=TRUE, usePairs=NULL, ...){
if(verbose){
mc <- match.call()
mc <- mc[intersect(names(mc),
c("hardTrue","fuzzyTrue","hardPred","fuzzyPred"))]
paste0(names(mc),"=",unlist(mc), collapse=", ")
}
if(!is.null(fuzzyTrue)){
fuzzy_true <- TRUE
}else{
stopifnot(!is.null(hardTrue))
fuzzy_true <- FALSE
}
if(!is.null(fuzzyPred)){
fuzzy_pred <- TRUE
}else{
stopifnot(!is.null(hardPred))
fuzzy_pred <- FALSE
}
level_functions <- list(
"element" = getFuzzyPartitionElementMetrics,
"class" = getFuzzyPartitionClassMetrics,
"dataset" = getFuzzyPartitionGlobalMetrics
)
.checkMetricsLevel(metrics, level, level_functions, use_default=TRUE,
use_attribute=FALSE)
# Collect all arguments into a list
args <- list(
"element" = list(hardTrue=hardTrue, fuzzyTrue=fuzzyTrue,
hardPred=hardPred, fuzzyPred=fuzzyPred,
fuzzy_true=fuzzy_true, fuzzy_pred=fuzzy_pred,
metrics=metrics,
useNegatives=useNegatives, verbose=verbose,
usePairs=usePairs),
"class" = list(hardTrue=hardTrue, fuzzyTrue=fuzzyTrue,
hardPred=hardPred, fuzzyPred=fuzzyPred,
fuzzy_true=fuzzy_true, fuzzy_pred=fuzzy_pred,
metrics=metrics, nperms=nperms, verbose=verbose,
returnElementPairAccuracy=returnElementPairAccuracy,
BPPARAM=BPPARAM, ...),
"dataset" = list(hardTrue=hardTrue, fuzzyTrue=fuzzyTrue,
hardPred=hardPred, fuzzyPred=fuzzyPred,
fuzzy_true=fuzzy_true, fuzzy_pred=fuzzy_pred,
metrics=metrics, nperms=nperms, verbose=verbose,
returnElementPairAccuracy=returnElementPairAccuracy,
BPPARAM=BPPARAM, ...)
)
do.call(level_functions[[level]], args[[level]])
}
.cal_fuzzyPartitionMetrics <- function(hardTrue=NULL, fuzzyTrue=NULL,
hardPred=NULL, fuzzyPred=NULL,
fuzzy_true=TRUE, fuzzy_pred=FALSE,
nperms=NULL, verbose=TRUE,
returnElementPairAccuracy=FALSE,
BPPARAM=BiocParallel::SerialParam(),
...){
if(fuzzy_true & fuzzy_pred){
stopifnot(!(is.null(fuzzyTrue)|is.null(fuzzyPred)))
message("Comparing between a fuzzy truth and a fuzzy prediction...")
res <- fuzzyPartitionMetrics(fuzzyTrue, fuzzyPred, nperms=nperms,
verbose=verbose,
returnElementPairAccuracy=returnElementPairAccuracy,
BPPARAM=BPPARAM, ...)
}else if(fuzzy_true & (!fuzzy_pred)){
stopifnot(!(is.null(hardTrue)|is.null(fuzzyTrue)|is.null(hardPred)))
message("Comparing between a fuzzy truth and a hard prediction...")
res <- fuzzyHardMetrics(hardTrue, fuzzyTrue, hardPred, nperms=nperms,
verbose=verbose,
returnElementPairAccuracy=returnElementPairAccuracy,
BPPARAM=BPPARAM)
}else if((!fuzzy_true) & fuzzy_pred){
stopifnot(!(is.null(hardTrue)|is.null(fuzzyPred)|is.null(hardPred)))
message("Comparing between a hard truth and a fuzzy prediction...")
res <- fuzzyHardMetrics(hardPred, fuzzyPred, hardTrue, nperms=nperms,
verbose=verbose,
returnElementPairAccuracy=returnElementPairAccuracy,
BPPARAM=BPPARAM)
res <- .switchListItem(res, "fuzzyWH", "fuzzyWC")
res <- .switchListItem(res, "fuzzyAWH", "fuzzyAWC")
}else if((!fuzzy_true) & (!fuzzy_pred)){
stop("You are comparing between two hard clusterings! Use function
`getPartitionMetrics()` to do this.")
}
return(res)
}
getFuzzyPartitionGlobalMetrics <- function(hardTrue=NULL, fuzzyTrue=NULL,
hardPred=NULL, fuzzyPred=NULL,
fuzzy_true=TRUE, fuzzy_pred=FALSE,
metrics=c("fuzzyRI", "fuzzyARI",
"fuzzyWH", "fuzzyAWH",
"fuzzyWC", "fuzzyAWC"),
nperms=NULL, verbose=TRUE,
returnElementPairAccuracy=FALSE,
BPPARAM=BiocParallel::SerialParam(),
...){
res <- .cal_fuzzyPartitionMetrics(hardTrue=hardTrue, fuzzyTrue=fuzzyTrue,
hardPred=hardPred, fuzzyPred=fuzzyPred,
fuzzy_true=fuzzy_true,
fuzzy_pred=fuzzy_pred,
nperms=nperms, verbose=verbose,
returnElementPairAccuracy=returnElementPairAccuracy,
BPPARAM=BPPARAM, ...)
fuzzyMetrics <- res
fuzzyRI <- fuzzyMetrics$NDC
fuzzyARI <- fuzzyMetrics$ACI
fuzzyWH <- fuzzyMetrics$fuzzyWH$global
fuzzyAWH <- fuzzyMetrics$fuzzyAWH$global
fuzzyWC <- fuzzyMetrics$fuzzyWC$global
fuzzyAWC <- fuzzyMetrics$fuzzyAWC$global
res <- lapply(setNames(metrics, metrics), FUN=function(m){
switch(m,
fuzzyRI = fuzzyRI,
fuzzyARI = fuzzyARI,
fuzzyWH = fuzzyWH,
fuzzyAWH = fuzzyAWH,
fuzzyWC = fuzzyWC,
fuzzyAWC = fuzzyAWC
)
})
res <- unlist(res)
return(data.frame(t(res)))
}
getFuzzyPartitionClassMetrics <- function(hardTrue=NULL, fuzzyTrue=NULL,
hardPred=NULL, fuzzyPred=NULL,
fuzzy_true=TRUE, fuzzy_pred=FALSE,
metrics=c("fuzzyWH", "fuzzyAWH",
"fuzzyWC", "fuzzyAWC"),
nperms=NULL, verbose=TRUE,
returnElementPairAccuracy=FALSE,
BPPARAM=BiocParallel::SerialParam(),
...){
res <- .cal_fuzzyPartitionMetrics(hardTrue=hardTrue, fuzzyTrue=fuzzyTrue,
hardPred=hardPred, fuzzyPred=fuzzyPred,
fuzzy_true=fuzzy_true,
fuzzy_pred=fuzzy_pred,
nperms=nperms, verbose=verbose,
returnElementPairAccuracy=returnElementPairAccuracy,
BPPARAM=BPPARAM, ...)
fuzzyMetrics <- res
fuzzyWH <- fuzzyMetrics$fuzzyWH$perPartition
fuzzyAWH <- fuzzyMetrics$fuzzyAWH$perPartition
fuzzyWC <- fuzzyMetrics$fuzzyWC$perPartition
fuzzyAWC <- fuzzyMetrics$fuzzyAWC$perPartition
res_class <- lapply(setNames(metrics, metrics), FUN=function(m){
switch(m,
fuzzyWC = fuzzyWC,
fuzzyAWC = fuzzyAWC
)
})[metrics %in% c("fuzzyWC", "fuzzyAWC")]
res_class <- as.data.frame(res_class)
res_class$class <-seq_along(fuzzyWC)
res_cluster <- lapply(setNames(metrics, metrics), FUN=function(m){
switch(m,
fuzzyWH = fuzzyWH,
fuzzyAWH = fuzzyAWH
)
})[metrics %in% c("fuzzyWH", "fuzzyAWH")]
res_cluster <- as.data.frame(res_cluster)
res_cluster$cluster <- seq_along(fuzzyWH)
res <- .rbind_na(res_class, res_cluster)
rownames(res) <- NULL
return(res)
}
#' getFuzzyPartitionElementMetrics
#'
#' Computes a selection of external fuzzy clustering evaluation metrics at the
#' element level.
#' @param metrics The metrics to compute. Currently only `"fuzzySPC"` is
#' included at the element level.
#' @inheritParams fuzzyHardSpotConcordance
#' @inheritParams getPairConcordance
#' @inheritParams getFuzzyPartitionMetrics
#' @param fuzzy_true Logical; whether the truth is fuzzy.
#' @param fuzzy_pred Logical; whether the prediction is fuzzy.
#' @param usePairs Logical; whether to compute over pairs instead of elements.
#' Only useful when `fuzzy_true=TRUE` and `fuzzy_pred=FALSE`.
#' @keywords internal
#' @return A dataframe of metric values.
getFuzzyPartitionElementMetrics <- function(hardTrue=NULL, fuzzyTrue=NULL,
hardPred=NULL, fuzzyPred=NULL,
fuzzy_true=TRUE, fuzzy_pred=FALSE,
metrics=c("fuzzySPC"),
useNegatives=TRUE, verbose=TRUE,
usePairs=TRUE){
if(fuzzy_true & fuzzy_pred){
stopifnot(!(is.null(fuzzyTrue)|is.null(fuzzyPred)))
message("Comparing between a fuzzy truth and a fuzzy prediction...")
res <- fuzzySpotConcordance(fuzzyTrue, fuzzyPred)
}else if(fuzzy_true & (!fuzzy_pred)){
stopifnot(!(is.null(hardTrue)|is.null(fuzzyTrue)|is.null(hardPred)))
message("Comparing between a fuzzy truth and a hard prediction...")
res <- fuzzyHardSpotConcordance(hardTrue, fuzzyTrue, hardPred,
useNegatives=useNegatives, verbose=verbose)
}else if((!fuzzy_true) & fuzzy_pred){
stopifnot(!(is.null(hardTrue)|is.null(fuzzyPred)|is.null(hardPred)))
message("Comparing between a hard truth and a fuzzy prediction...")
res <- fuzzyHardSpotConcordance(hardPred, fuzzyPred, hardTrue,
useNegatives=useNegatives, verbose=verbose)
}else if((!fuzzy_true) & (!fuzzy_pred)){
stop("You are comparing between two hard clusterings! Use function
`getPartitionMetrics()` to do this.")
}
res <- data.frame(res)
colnames(res) <- "fuzzySPC"
return(res)
}