Spatially-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
)

Arguments

spe

SpatialExperiment object

prop

Proportion of cells to sample (between 0 and 1)

method

Sampling method: "grid", "kmeans", or "random" (default: "grid")

grid_size

Number of grid cells per dimension for grid method (default: auto-calculated)

seed

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.

min_cells_per_region

Minimum cells required per spatial region (default: 1)

balance_regions

Whether to balance sampling across spatial regions (default: FALSE; if true, might create duplicated spatial units)

verbose

Whether to report progress with message() (default: TRUE)

Value

Subsampled SpatialExperiment object

Examples

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)