spateo.segmentation.bp
======================
.. py:module:: spateo.segmentation.bp
.. autoapi-nested-parse::
Belief propagation to compute the marginal probabilities of a spot being
occupied by a cell.
Functions
---------
.. autoapisummary::
spateo.segmentation.bp.create_neighbor_offsets
spateo.segmentation.bp.cell_marginals
spateo.segmentation.bp.run_bp
Module Contents
---------------
.. py:function:: create_neighbor_offsets(neighborhood: numpy.ndarray) -> numpy.ndarray
Helper function to convert a neighborhood mask to coordinate offsets.
:param 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.
.. py:function:: cell_marginals(background_probs: numpy.ndarray, cell_probs: numpy.ndarray, neighborhood: Optional[numpy.ndarray] = None, p: float = 0.6, q: float = 0.4, precision: float = 1e-05, max_iter: int = 100) -> numpy.ndarray
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.
:param background_probs: The probability of each pixel being background (for
instance, computed by taking the PDF of the parameters estmiated by EM).
:param cell_probs: The probability of each pixel being a cell (for instance,
computed by taking the PDF of the parameters estmiated by EM).
:param 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).
:param p: The potential indicating how likely two adjacent cells will be the
same state. Does not necessarily have to be a probability.
:param p: The potential indicating how likely two adjacent cells will be
different states. Does not necessarily have to be a probability.
:param precision: Stop iterations when desired precision is reached, as computed
by the L2-norm of the messages from two consecutive iterations.
:param max_iter: Maximum number of iterations.
:returns: The marginal probability, at each pixel, of the pixel being a cell.
.. py:function:: 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
Compute the marginal probability of each pixel being a cell, using
belief propagation.
:param background_cond: Probability of observing UMIs conditioned on background.
:param cell_cond: Probability of observing UMIs conditioned on cell.
:param k: Neighborhood size
:param square: Whether the neighborhood of each node is a square around it.
If false, the neighborhood is a circle.
:param p: The potential indicating how likely two adjacent cells will be the
same state. Does not necessarily have to be a probability.
:param p: The potential indicating how likely two adjacent cells will be
different states. Does not necessarily have to be a probability.
:param precision: Stop iterations when desired precision is reached, as computed
by the L2-norm of the messages from two consecutive iterations.
:param max_iter: Maximum number of iterations.
:returns: Numpy array of marginal probabilities.