-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetSpatialExternalMetrics.R
195 lines (186 loc) · 9.63 KB
/
getSpatialExternalMetrics.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
#' Compute external metrics for spatial data
#'
#' Computes a selection of external clustering evaluation metrics for spatial
#' data.
#' @inheritParams getSpatialElementExternalMetrics
#' @param metrics The metrics to compute. See details.
#' @param level The level to calculate the metrics. Options include `"element"`,
#' `"class"` and `"dataset"`.
#' @return A data.frame of metrics.
#' @export
#' @details
#' The allowed values for `metrics` depend on the value of `level`:
#' - If `level = "element"`, the allowed `metrics` are: `"SpatialSPC"`, `"SpatialNPC"`.
#' - If `level = "class"`, the allowed `metrics` are: `"SpatialWH"`,`"SpatialAWH"`, `"SpatialWC"`,`"SpatialAWC"`.
#' - If `level = "dataset"`, the allowed `metrics` are: `"SpatialRI"`,`"SpatialARI"`,`"SpatialWH"`,`"SpatialAWH"`, `"SpatialWC"`,`"SpatialAWC"`,`"SpatialAccuracy"`.
#' @examples
#' data(sp_toys)
#' data <- sp_toys
#' getSpatialExternalMetrics(data$label, data$p1, data[,c("x", "y")], k=6, level="class")
getSpatialExternalMetrics <- function(true, pred, location, k=6, alpha=0.5, level="class",
metrics=c("SpatialWH","SpatialAWH",
"SpatialWC","SpatialAWC"),
fuzzy_true=TRUE, fuzzy_pred=FALSE,
...){
# Map level to the corresponding function
level_functions <- list(
"element" = getSpatialElementExternalMetrics,
"class" = getSpatialClassExternalMetrics,
"dataset" = getSpatialGlobalExternalMetrics
)
.checkMetricsLevel(metrics, level, level_functions, use_default=TRUE,
use_attribute=FALSE)
# Collect all arguments into a list
args <- list(true=true, pred=pred, location=location, k=k, alpha=alpha,
metrics=metrics, fuzzy_true=fuzzy_true, fuzzy_pred=fuzzy_pred,
...)
do.call(level_functions[[level]], args)
}
#' Compute dataset-level external evaluation metrics for spatially-resolved data
#'
#' Computes a selection of external clustering evaluation metrics for spatial
#' data at the dataset level. Options include a series of fuzzy pair-counting
#' metrics and set matching-based accuracy.
#' @inheritParams getFuzzyPartitionMetrics
#' @inheritParams getFuzzyLabel
#' @keywords internal
#' @param true A vector containing the labels of the true classes. Must be a
#' vector of characters, integers, numerics, or a factor, but not a list.
#' @param pred A vector containing the labels of the predicted clusters. Must
#' be a vector of characters, integers, numerics, or a factor, but not a list.
#' @param k The number of neighbors used when calculating the fuzzy
#' class memberships for fuzzy metrics, or when calculating the weighted
#' accuracy.
#' @param metrics a vector of metric names to compute.
#' @param fuzzy_true Logical; whether to compute fuzzy class memberships for `true`.
#' @param fuzzy_pred Logical; whether to compute fuzzy class memberships for `pred`.
#' @param ... Optional params for \code{\link{fuzzyPartitionMetrics}} or
#' \code{\link{findSpatialKNN}}.
#' @return A data.frame of metrics.
getSpatialGlobalExternalMetrics <- function(true, pred, location, k=6, alpha=0.5,
metrics=c("SpatialRI","SpatialARI",
"SpatialWH","SpatialAWH",
"SpatialWC","SpatialAWC",
"SpatialAccuracy"),
fuzzy_true=TRUE, fuzzy_pred=FALSE,
...){
argfindSpatialKNN <- .checkEllipsisArgs(fnList=list(findSpatialKNN, fuzzyPartitionMetrics), ...)[[1]]
argfuzzyPartitionMetrics <- .checkEllipsisArgs(fnList=list(findSpatialKNN, fuzzyPartitionMetrics), ...)[[2]]
if(length(intersect(metrics, c("SpatialRI","SpatialARI","SpatialWH",
"SpatialAWH", "SpatialWC","SpatialAWC")))>0){
hardTrue <- true
hardPred <- pred
fuzzyTrue <- do.call(getFuzzyLabel,
c(argfindSpatialKNN,
list(labels=hardTrue, location=location, k=k,
alpha=alpha)))
fuzzyPred <- do.call(getFuzzyLabel,
c(argfindSpatialKNN,
list(labels=hardPred, location=location, k=k,
alpha=alpha)))
res <- do.call(getFuzzyPartitionGlobalMetrics,
c(argfuzzyPartitionMetrics,
list(hardTrue=hardTrue, fuzzyTrue=fuzzyTrue,
hardPred=hardPred, fuzzyPred=fuzzyPred,
fuzzy_true=fuzzy_true, fuzzy_pred=fuzzy_pred,
metrics=c("fuzzyRI", "fuzzyARI", "fuzzyWH",
"fuzzyAWH", "fuzzyWC", "fuzzyAWC"))))
}else{res <- data.frame(matrix(nrow = 1, ncol = 0))}
if("SpatialAccuracy" %in% metrics){
res$SpatialAccuracy <- do.call(nnWeightedAccuracy,
c(list(true=true, pred=pred,
location=location, k=k),
argfindSpatialKNN))
}
colnames(res) <- sub("fuzzy", "Spatial",colnames(res))
return(res[,metrics, drop=FALSE])
}
#' Compute class-level external evaluation metrics for spatially-resolved data
#'
#' Computes a selection of external clustering evaluation metrics for spatial
#' data at the class/cluster level.
#' @inheritParams getSpatialGlobalExternalMetrics
#' @keywords internal
#' @param k The number of neighbors used when calculating the fuzzy
#' class memberships for fuzzy metrics.
#' @param ... Optional params for \code{\link{fuzzyPartitionMetrics}} or
#' \code{\link{findSpatialKNN}}.
#' @return A data.frame of metrics.
getSpatialClassExternalMetrics <- function(true, pred, location, k=6, alpha=0.5,
metrics=c("SpatialWH","SpatialAWH",
"SpatialWC","SpatialAWC"),
fuzzy_true=TRUE, fuzzy_pred=FALSE,
...){
argfindSpatialKNN <- .checkEllipsisArgs(fnList=list(findSpatialKNN, fuzzyPartitionMetrics), ...)[[1]]
argfuzzyPartitionMetrics <- .checkEllipsisArgs(fnList=list(findSpatialKNN, fuzzyPartitionMetrics), ...)[[2]]
hardTrue <- true
hardPred <- pred
fuzzyTrue <- do.call(getFuzzyLabel,
c(argfindSpatialKNN,
list(labels=hardTrue, location=location, k=k,
alpha=alpha)))
fuzzyPred <- do.call(getFuzzyLabel,
c(argfindSpatialKNN,
list(labels=hardPred, location=location, k=k,
alpha=alpha)))
res <- do.call(getFuzzyPartitionClassMetrics,
c(argfuzzyPartitionMetrics,
list(hardTrue=hardTrue, fuzzyTrue=fuzzyTrue,
hardPred=hardPred, fuzzyPred=fuzzyPred,
fuzzy_true=fuzzy_true, fuzzy_pred=fuzzy_pred,
metrics=c("fuzzyWH","fuzzyAWH", "fuzzyWC", "fuzzyAWC"))))
colnames(res) <- sub("fuzzy", "Spatial",colnames(res))
return(res[,c(metrics, "class","cluster")])
}
#' getSpatialElementExternalMetrics
#'
#' Computes a selection of external clustering evaluation metrics for spatial
#' data at the element level.
#' @inheritParams getSpatialGlobalExternalMetrics
#' @param ... Optional params for [getFuzzyPartitionElementMetrics()] or
#' [findSpatialKNN()].
#' @keywords internal
#' @return A data.frame of metrics.
getSpatialElementExternalMetrics <- function(true, pred, location, k=6, alpha=0.5,
metrics=c("SpatialSPC", "SpatialNPC"),
fuzzy_true=TRUE, fuzzy_pred=FALSE,
...){
parsedArgs <- .checkEllipsisArgs(fnList=list(findSpatialKNN,
getFuzzyPartitionElementMetrics,
getNeighboringPairConcordance),
...)
argfindSpatialKNN <- parsedArgs[[1]]
arggetFuzzyPartitionElementMetrics <- parsedArgs[[2]]
arggetNeighboringPairConcordance <- parsedArgs[[3]]
if("SpatialSPC" %in% metrics){
hardTrue <- true
hardPred <- pred
fuzzyTrue <- do.call(getFuzzyLabel,
c(argfindSpatialKNN,
list(labels=hardTrue, location=location, k=k,
alpha=alpha)))
fuzzyPred <- do.call(getFuzzyLabel,
c(argfindSpatialKNN,
list(labels=hardPred, location=location, k=k,
alpha=alpha)))
SpatialSPC <- do.call(getFuzzyPartitionElementMetrics,
c(list(hardTrue=hardTrue, fuzzyTrue=fuzzyTrue,
hardPred=hardPred, fuzzyPred=fuzzyPred,
fuzzy_true=fuzzy_true, fuzzy_pred=fuzzy_pred,
metrics=c("SpatialSPC")),
arggetFuzzyPartitionElementMetrics))$fuzzySPC
}
if("SpatialNPC" %in% metrics){
SpatialNPC <- do.call(getNeighboringPairConcordance,
c(list(true=true, pred=pred, location=location, k=k),
arggetNeighboringPairConcordance))
}
res <- as.data.frame(lapply(setNames(metrics, metrics), FUN=function(m){
switch(m,
SpatialSPC = SpatialSPC,
SpatialNPC = SpatialNPC,
stop("Unknown metric.")
)})
)
return(res)
}