Skip to content
Merged

1.9 #66

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ export(sc_metrics_pixels)
export(sc_metrics_supercells)
export(sc_slic)
export(sc_slic_convergence)
export(sc_slic_get_params)
export(sc_slic_points)
export(sc_slic_raster)
export(sc_slic_set_params)
export(sc_tune_compactness)
export(supercells)
export(use_adaptive)
Expand Down
5 changes: 3 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

* Added `outcomes` argument to `sc_slic()`, `sc_slic_points()`, and `sc_slic_raster()`; replaces `metadata` for controlling returned fields
* Iteration diagnostics API redesigned: `iter_diagnostics` and `sc_plot_iter_diagnostics()` replaced by `sc_slic_convergence()` with a `plot()` method
* Added `sc_slic_get_params()` and `sc_slic_set_params()` for reading/writing stored `sc_slic()` parameters
* Added `use_meters()` for map-distance step values (replacing `in_meters()`)
* Added `use_adaptive()` for adaptive compactness mode (replacing `compactness = "auto"`)
* Added experimental `sc_merge_supercells()` for adjacency-constrained greedy merging
* Added `sc_dist_vec_cpp()` (C++ distance wrapper) to support merge utilities
* Updated metrics API (`sc_metrics_pixels()`, `sc_metrics_supercells()`, `sc_metrics_global()`) to better reuse `sc_slic()` metadata and improve scaling/compactness handling
* Updated `sc_tune_compactness()` to align with the revised compactness/step workflows
* Documentation and vignettes updated (pkgdown refresh, new articles, and revised examples)

# supercells 1.8
Expand Down
4 changes: 0 additions & 4 deletions R/cpp11.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# Generated by cpp11: do not edit by hand

sc_dist_vec_cpp <- function(a, b, dist_name, dist_fun) {
.Call(`_supercells_sc_dist_vec_cpp`, a, b, dist_name, dist_fun)
}

sc_metrics_global_cpp <- function(clusters, centers_xy, centers_vals, vals, step, compactness, adaptive_compactness, dist_name, dist_fun) {
.Call(`_supercells_sc_metrics_global_cpp`, clusters, centers_xy, centers_vals, vals, step, compactness, adaptive_compactness, dist_name, dist_fun)
}
Expand Down
4 changes: 2 additions & 2 deletions R/helpers-general.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@
# normalize compactness input for slic/metrics workflows
.sc_util_prep_compactness = function(compactness) {
if (is.numeric(compactness) && length(compactness) == 1 && !is.na(compactness) && compactness > 0) {
return(list(value = compactness, adaptive = FALSE, adaptive_method = NULL))
return(list(value = compactness, adaptive = FALSE, compactness_method = "constant"))
}

if (inherits(compactness, "sc_adaptive")) {
return(list(value = 0, adaptive = TRUE, adaptive_method = compactness$method))
return(list(value = 0, adaptive = TRUE, compactness_method = compactness$method))
}
stop("The 'compactness' argument must be a single positive number or use_adaptive()", call. = FALSE)
}
25 changes: 14 additions & 11 deletions R/helpers-metrics.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,28 @@
if (missing(compactness)) {
compactness = attr(sc, "compactness")
}
adaptive_method = attr(sc, "adaptive_method")
if (!is.null(adaptive_method) && is.null(compactness)) {
compactness = 0
}
compactness_method = attr(sc, "compactness_method")
if (missing(step)) {
step = attr(sc, "step")
}
if (is.null(compactness) || is.null(step)) {
if (is.null(step)) {
stop("Both 'compactness' and 'step' are required", call. = FALSE)
}

if (!is.null(adaptive_method)) {
if (!is.character(adaptive_method) || length(adaptive_method) != 1 || is.na(adaptive_method) ||
adaptive_method != "local_max") {
stop("The 'adaptive_method' attribute must be 'local_max' or NULL", call. = FALSE)
if (!is.null(compactness_method)) {
if (!is.character(compactness_method) || length(compactness_method) != 1 || is.na(compactness_method) ||
!(compactness_method %in% c("constant", "local_max"))) {
stop("The 'compactness_method' attribute must be 'constant' or 'local_max'", call. = FALSE)
}
compactness_prep = list(value = 0, adaptive = TRUE, adaptive_method = adaptive_method)
}

if (identical(compactness_method, "local_max")) {
compactness_prep = list(value = 0, adaptive = TRUE, compactness_method = "local_max")
} else {
compactness_prep = .sc_util_prep_compactness(compactness)
if (!is.null(compactness_method) && compactness_prep$compactness_method != compactness_method) {
stop("The provided compactness method conflicts with 'compactness_method' attribute", call. = FALSE)
}
}
step_prep = .sc_util_step_to_cells(raster, step)
step = step_prep$step
Expand Down Expand Up @@ -99,7 +102,7 @@
step_meta = step_prep$step_meta,
compactness = compactness_prep$value,
adaptive_compactness = compactness_prep$adaptive,
adaptive_method = compactness_prep$adaptive_method,
compactness_method = compactness_prep$compactness_method,
spatial_scale = spatial_scale,
step_scale = step_scale
)
Expand Down
10 changes: 7 additions & 3 deletions R/helpers-sc_slic.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
outcomes = outcomes,
compactness = compactness_prep$value,
adaptive_compactness = compactness_prep$adaptive,
adaptive_method = compactness_prep$adaptive_method,
compactness_method = compactness_prep$compactness_method,
clean = clean, iter = iter,
verbose = verbose, verbose_cpp = verbose_cpp))
}
Expand Down Expand Up @@ -196,9 +196,13 @@

slic_sf = .sc_slic_select_outcomes(slic_sf, prep$outcomes)

compactness_attr = prep$compactness
if (isTRUE(prep$adaptive_compactness)) {
compactness_attr = NA_real_
}
attr(slic_sf, "step") = prep$step_meta
attr(slic_sf, "compactness") = prep$compactness
attr(slic_sf, "adaptive_method") = prep$adaptive_method
attr(slic_sf, "compactness") = compactness_attr
attr(slic_sf, "compactness_method") = prep$compactness_method
attr(slic_sf, "dist_fun") = prep$dist_fun_input
cls = class(slic_sf)
cls = c(setdiff(cls, "data.frame"), "supercells", "data.frame")
Expand Down
Loading