Skip to contents

Spatially stratified random sample points from a 3D image by slicing along the Z-axis and applying 2D stratified sampling using R package sf to each slice. This approach leverages the efficiency of sf's 2D spatial indexing for 3D data.

Usage

stratified_sampling_3D_via_2D(
  points,
  cellsize = c(600, 600),
  z_cellsize = 600,
  num_samples_per_stratum = 1
)

Arguments

points

a data frame contains all points in a 3D image with X, Y, Z coordinates.

cellsize

a vector of length 2 contains the size of each grid square in X and Y dimensions. Default c(600,600).

z_cellsize

numeric value specifying the thickness of each Z slice. Default 600.

num_samples_per_stratum

number of points selected from each 3D grid cell (X,Y,Z). Default 1.

Value

Return a vector contains indices of sampled points from the original data frame.

Details

This function performs 3D stratified sampling by: 1. Binning points into Z slices of thickness z_cellsize 2. For each Z slice, applying 2D stratified sampling using stratified_sampling_sf 3. Combining sampled indices from all slices

The approach is efficient for large 3D datasets as it leverages sf's optimized 2D spatial operations rather than implementing full 3D spatial indexing.

See also

stratified_sampling_sf for 2D stratified sampling

Examples


# Create example 3D data
set.seed(123)
tissue_3d <- data.frame(
  X = runif(1000, 0, 1000),
  Y = runif(1000, 0, 1000), 
  Z = runif(1000, 0, 100)
)

# Sample points with 3D stratification
pt_idx <- stratified_sampling_3D_via_2D(tissue_3d, 
                                         cellsize = c(200, 200), 
                                         z_cellsize = 200)