Skip to contents

Turns the positions of cells (or spots, or transcripts) into the image that lddmm() needs. This is STalign.rasterize of the Python package, without the figure it draws: the pixel grid, the kernel and the weights follow it step by step, so the images are the same up to floating point rounding.

Usage

rasterize(
  x,
  y,
  g = 1,
  dx = 30,
  blur = NULL,
  expand = 1.1,
  wavelet_magnitude = FALSE,
  use_windowing = TRUE,
  blur_um = NULL
)

Arguments

x, y

Numeric vectors with the horizontal and the vertical position of each cell. Cells with a missing coordinate are ignored, with a warning.

g

Weight of each cell (recycled to the number of cells), for example its RNA count. The default 1 gives a density image. Unless all weights are 1 they are min-max normalized to [0, 1], so the cells with the smallest weight add nothing, and equal weights give an image of zeros.

dx

Pixel size, in the units of x and y.

blur

Width of the Gaussian kernel in pixels (its standard deviation is 2 * blur pixels). A vector makes a multi-scale image with one channel per entry. The default is 1, unless blur_um is given.

expand

Factor by which the grid extends beyond the bounding box of the cells.

wavelet_magnitude

If TRUE, channel i holds the absolute value of the difference between the kernels i and i - 1 (a difference between scales). blur must then be sorted from greatest to least.

use_windowing

If TRUE, evaluate each kernel on a window of pixels (see above) and not on the whole image.

blur_um

The standard deviation of the kernel in the units of x and y (for example micrometers), instead of blur: blur = blur_um / (2 * dx). The kernel then keeps its physical width at any pixel size. Give blur or blur_um, not both.

Value

A list with

  • x: the coordinates of the pixel columns,

  • y: the coordinates of the pixel rows,

  • M: the image, an array of dimensions c(length(blur), length(y), length(x)), channels first, the first image axis being the rows (y) as in lddmm().

So lddmm(xI = list(src$y, src$x), I = src$M, ...) aligns an image made here.

Details

The grid covers the bounding box of the points, enlarged by expand about its center, with pixels dx apart, so every cloud gets its own grid (the two images given to lddmm() do not need to share one). Each cell then adds a Gaussian kernel centered on its exact position, with standard deviation 2 * blur pixels (2 * blur * dx in the units of x and y). The kernel is evaluated on a square window of pixels around the cell's nearest pixel, extending ceiling(4 * max(blur)) pixels to each side, clipped at the border of the image, and divided by its sum over that window. A cell therefore adds a total of its weight (1 for a density image), and the window holds about 95% of the Gaussian, so the kernel acts like one about 4% narrower. With use_windowing = FALSE the whole image is used instead, which is much slower.

The image is not rescaled. Its values are on the scale of cells per pixel, so for lddmm() you will want to divide it by its maximum (or similar), because the step sizes epL, epT and epV depend on the intensity scale.

See also

lddmm(); vignette("lddmm-merfish") for an alignment of two sets of cells.

Examples

# cell positions (micrometers) of a MERFISH mouse-brain section shipped with the package
file <- system.file("extdata", package = "RSTalign", mustWork = TRUE)
cells <- read.csv(list.files(file, pattern = "S2R3[.]csv[.]gz$", full.names = TRUE))
img <- rasterize(cells$center_x, cells$center_y, dx = 30, blur = 1.5)
dim(img$M)                 # channels, rows (y), columns (x)
#> [1]   1 256 336
image(img$x, img$y, t(img$M[1, , ]), asp = 1)