spateo.segmentation.bp

Belief propagation to compute the marginal probabilities of a spot being occupied by a cell.

Functions

create_neighbor_offsets(→ numpy.ndarray)

Helper function to convert a neighborhood mask to coordinate offsets.

cell_marginals(→ numpy.ndarray)

Compute the marginal probablity of each pixel being a cell, as opposed

run_bp(→ numpy.ndarray)

Compute the marginal probability of each pixel being a cell, using

Module Contents

spateo.segmentation.bp.create_neighbor_offsets(neighborhood: numpy.ndarray) numpy.ndarray[source]

Helper function to convert a neighborhood mask to coordinate offsets.

Parameters:
neighborhood

Boolean mask (kernel) indicating the neighborhood to consider.

Returns:

A Numpy array containing offsets for each dimension. Each row is the offsets required to reach a single neighbor.

Raises:

ValueError – If neighborhood does not have odd size dimensions.

spateo.segmentation.bp.cell_marginals(background_probs: numpy.ndarray, cell_probs: numpy.ndarray, neighborhood: numpy.ndarray | None = None, p: float = 0.6, q: float = 0.4, precision: float = 1e-05, max_iter: int = 100) numpy.ndarray[source]

Compute the marginal probablity of each pixel being a cell, as opposed to background. This function calls a fast belief propagation library optimized for grid Markov Random Fields of arbitray dimension.

Parameters:
background_probs

The probability of each pixel being background (for instance, computed by taking the PDF of the parameters estmiated by EM).

cell_probs

The probability of each pixel being a cell (for instance, computed by taking the PDF of the parameters estmiated by EM).

neighborhood

A mask (kernel) indicating the neighborhood of each node to consider. The node is at the center of this array. Defaults to immediate neighbors (no diagonals).

p

The potential indicating how likely two adjacent cells will be the same state. Does not necessarily have to be a probability.

p

The potential indicating how likely two adjacent cells will be different states. Does not necessarily have to be a probability.

precision

Stop iterations when desired precision is reached, as computed by the L2-norm of the messages from two consecutive iterations.

max_iter

Maximum number of iterations.

Returns:

The marginal probability, at each pixel, of the pixel being a cell.

spateo.segmentation.bp.run_bp(background_cond: numpy.ndarray, cell_cond: numpy.ndarray, k: int = 3, square: bool = False, p: float = 0.6, q: float = 0.4, precision: float = 1e-06, max_iter: int = 100) numpy.ndarray[source]

Compute the marginal probability of each pixel being a cell, using belief propagation.

Parameters:
background_cond

Probability of observing UMIs conditioned on background.

cell_cond

Probability of observing UMIs conditioned on cell.

k

Neighborhood size

square

Whether the neighborhood of each node is a square around it. If false, the neighborhood is a circle.

p

The potential indicating how likely two adjacent cells will be the same state. Does not necessarily have to be a probability.

p

The potential indicating how likely two adjacent cells will be different states. Does not necessarily have to be a probability.

precision

Stop iterations when desired precision is reached, as computed by the L2-norm of the messages from two consecutive iterations.

max_iter

Maximum number of iterations.

Returns:

Numpy array of marginal probabilities.