R/spatialSampler.R
spatialSampler.RdSpatially-aware subsampling of SpatialExperiment objects
spatialSampler(
spe,
prop = 0.1,
method = "grid",
grid_size = NULL,
seed = 123,
min_cells_per_region = 1,
balance_regions = FALSE,
verbose = TRUE
)SpatialExperiment object
Proportion of cells to sample (between 0 and 1)
Sampling method: "grid", "kmeans", or "random" (default: "grid")
Number of grid cells per dimension for grid method (default: auto-calculated)
Random seed for reproducibility (default: 123). The function sets the seed with withr::local_seed(), so the session's random-number state is restored on exit.
Minimum cells required per spatial region (default: 1)
Whether to balance sampling across spatial regions (default: FALSE; if true, might create duplicated spatial units)
Whether to report progress with message() (default: TRUE)
Subsampled SpatialExperiment object
set.seed(1)
sce <- logNormTransf(scuttle::mockSCE(ncells = 300, ngenes = 50))
spe <- SpatialExperiment::SpatialExperiment(
assays = list(logcounts = logcounts(sce)),
spatialCoords = cbind(x = runif(300), y = runif(300))
)
sub <- spatialSampler(spe, prop = 0.2, method = "grid")
ncol(sub)