k-Nearest neighbor cluster label smoothing.

smoothLabels(
  se,
  cluster_names = NULL,
  coord_names = NULL,
  k = 15L,
  prop_thres = 0.5,
  max_iter = 10,
  verbose = TRUE
)

Arguments

se

A SpatialExperiment, SingleCellExperiment or SummarizedExperiment object with cluster labels in colData(se).

cluster_names

A string vector of label names to smooth. If NULL, smooths labels in colData(se) matching /^clust/

coord_names

A string vector specifying the names in colData corresponding to spatial coordinates.

k

An integer scalar specifying number of neighbors for smooething.

prop_thres

A numeric scalar \(\in [0,1]\) specifying a label proportions threshold If the fraction of neighbors with a certain label exceeds this proportion, change the label of the current sample (default: 0.5).

max_iter

An integer scalar specifying the max number of smoothing iterations. Set to -1 for smoothing to convergence.

verbose

A logical scalar specifying verbosity.

Value

A SpatialExperiment / SingleCellExperiment / SummarizedExperiment object with smoothed cluster labels in colData(se) suffixed with '_smooth'.

Details

As described in SpiceMix (https://doi.org/10.1038/s41588-022-01256-z). Implemented for labels that can be coerced to numeric only.

Examples

data(rings)
spe <- computeBanksy(rings, assay_name = "counts", M = 1, k_geom = c(15, 30))
#> Computing neighbors...
#> Spatial mode is kNN_median
#> Parameters: k_geom=15
#> Done
#> Computing neighbors...
#> Spatial mode is kNN_median
#> Parameters: k_geom=30
#> Done
#> Computing harmonic m = 0
#> Using 15 neighbors
#> Done
#> Computing harmonic m = 1
#> Using 30 neighbors
#> Centering
#> Done
spe <- runBanksyPCA(spe, M = 1, lambda = 0.2, npcs = 20)
spe <- clusterBanksy(spe, M = 1, lambda = 0.2, resolution = 1)
spe <- smoothLabels(spe, cluster_names = "clust_M1_lam0.2_k50_res1")
#> Iteration 1
#> Change: 21
#> Iteration 2
#> Change: 16
#> Iteration 3
#> Change: 13
#> Iteration 4
#> Change: 6
#> Iteration 5
#> Change: 3
#> Iteration 6
#> Change: 0