Skip to content

Commit 46e43c5

Browse files
committed
Deactivate dbscan examples if no lpSolve detected.
1 parent bf14660 commit 46e43c5

1 file changed

Lines changed: 43 additions & 27 deletions

File tree

R/fdadbscan.R

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
#' @return An object of class [`caps`].
1010
#'
1111
#' @export
12-
#' @examples
12+
#' @examplesIf requireNamespace("lpSolve", quietly = TRUE)
13+
#'
1314
#' #----------------------------------
1415
#' # Extracts 15 out of the 30 simulated curves in `simulated30_sub` data set
1516
#' idx <- c(1:5, 11:15)
@@ -33,22 +34,25 @@
3334
#' plot(out, type = "amplitude")
3435
#' # Or the estimated warping functions with:
3536
#' plot(out, type = "phase")
36-
fdadbscan <- function(x, y,
37-
is_domain_interval = FALSE,
38-
transformation = c("identity", "srvf"),
39-
warping_class = c("none", "shift", "dilation", "affine", "bpd"),
40-
centroid_type = "mean",
41-
metric = c("l2", "normalized_l2", "pearson"),
42-
cluster_on_phase = FALSE,
43-
use_verbose = FALSE,
44-
warping_options = c(0.15, 0.15),
45-
maximum_number_of_iterations = 100L,
46-
number_of_threads = 1L,
47-
parallel_method = 0L,
48-
distance_relative_tolerance = 0.001,
49-
use_fence = FALSE,
50-
check_total_dissimilarity = TRUE,
51-
compute_overall_center = FALSE) {
37+
fdadbscan <- function(
38+
x,
39+
y,
40+
is_domain_interval = FALSE,
41+
transformation = c("identity", "srvf"),
42+
warping_class = c("none", "shift", "dilation", "affine", "bpd"),
43+
centroid_type = "mean",
44+
metric = c("l2", "normalized_l2", "pearson"),
45+
cluster_on_phase = FALSE,
46+
use_verbose = FALSE,
47+
warping_options = c(0.15, 0.15),
48+
maximum_number_of_iterations = 100L,
49+
number_of_threads = 1L,
50+
parallel_method = 0L,
51+
distance_relative_tolerance = 0.001,
52+
use_fence = FALSE,
53+
check_total_dissimilarity = TRUE,
54+
compute_overall_center = FALSE
55+
) {
5256
call <- rlang::call_match(defaults = TRUE)
5357
callname <- rlang::call_name(call)
5458
callargs <- rlang::call_args(call)
@@ -81,17 +85,24 @@ fdadbscan <- function(x, y,
8185
centroid_name <- centroid_type_args$name
8286
centroid_extra <- centroid_type_args$extra
8387

84-
if (centroid_name != "medoid" && parallel_method == 1L)
85-
cli::cli_abort("Parallelization on the distance calculation loop is only available for computing medoids.")
88+
if (centroid_name != "medoid" && parallel_method == 1L) {
89+
cli::cli_abort(
90+
"Parallelization on the distance calculation loop is only available for computing medoids."
91+
)
92+
}
8693

8794
callargs$centroid_type <- centroid_name
8895
callargs$centroid_extra <- centroid_extra
8996

90-
if (warping_class == "none" && cluster_on_phase)
91-
cli::cli_abort("It makes no sense to cluster based on phase variability if no alignment is performed.")
97+
if (warping_class == "none" && cluster_on_phase) {
98+
cli::cli_abort(
99+
"It makes no sense to cluster based on phase variability if no alignment is performed."
100+
)
101+
}
92102

93-
if (use_verbose)
103+
if (use_verbose) {
94104
cli::cli_alert_info("Computing the distance matrix...")
105+
}
95106

96107
D <- fdadist(
97108
x = x,
@@ -111,19 +122,23 @@ fdadbscan <- function(x, y,
111122
dbscan::dbscan(obj, minPts = .min_pts)
112123
})
113124
sils <- sapply(results, \(.res) {
114-
if (length(unique(.res$cluster)) == 1) return(NA)
125+
if (length(unique(.res$cluster)) == 1) {
126+
return(NA)
127+
}
115128
mean(cluster::silhouette(.res$cluster, D)[, "sil_width"])
116129
})
117130

118-
if (all(is.na(sils)))
131+
if (all(is.na(sils))) {
119132
dbres <- results[[1]]
120-
else
133+
} else {
121134
dbres <- results[[which.max(sils)]]
135+
}
122136
labels <- dbres$cluster
123137
n_clusters <- length(unique(labels[labels > 0]))
124138

125-
if (use_verbose)
139+
if (use_verbose) {
126140
cli::cli_alert_info("Aligning all curves with respect to their centroid...")
141+
}
127142

128143
kmresults <- lapply(1:n_clusters, function(k) {
129144
cluster_ids <- which(labels == k)
@@ -152,8 +167,9 @@ fdadbscan <- function(x, y,
152167
)
153168
})
154169

155-
if (use_verbose)
170+
if (use_verbose) {
156171
cli::cli_alert_info("Consolidating output...")
172+
}
157173

158174
original_curves <- array(dim = c(N, L, M))
159175
original_curves[labels == 0, , ] <- y[labels == 0, , ]

0 commit comments

Comments
 (0)