spateo.segmentation.align ========================= .. py:module:: spateo.segmentation.align .. autoapi-nested-parse:: Functions to refine staining and RNA alignments. Attributes ---------- .. autoapisummary:: spateo.segmentation.align.MODULES Classes ------- .. autoapisummary:: spateo.segmentation.align.AlignmentRefiner spateo.segmentation.align.NonRigidAlignmentRefiner spateo.segmentation.align.RigidAlignmentRefiner Functions --------- .. autoapisummary:: spateo.segmentation.align.refine_alignment Module Contents --------------- .. py:class:: AlignmentRefiner(reference: numpy.ndarray, to_align: numpy.ndarray) Bases: :py:obj:`torch.nn.Module` Base class for all neural network modules. Your models should also subclass this class. Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes:: import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self): super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x)) Submodules assigned in this way will be registered, and will have their parameters converted too when you call :meth:`to`, etc. .. note:: As per the example above, an ``__init__()`` call to the parent class must be made before assignment on the child. :ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool .. py:attribute:: reference .. py:attribute:: to_align .. py:attribute:: __optimizer :value: None .. py:attribute:: weight .. py:attribute:: history .. py:method:: loss(pred) .. py:method:: optimizer() .. py:method:: forward() .. py:method:: train(n_epochs: int = 100) Set the module in training mode. This has any effect only on certain modules. See documentations of particular modules for details of their behaviors in training/evaluation mode, if they are affected, e.g. :class:`Dropout`, :class:`BatchNorm`, etc. :param mode: whether to set training mode (``True``) or evaluation mode (``False``). Default: ``True``. :type mode: bool :returns: self :rtype: Module .. py:method:: get_params(train=False) :abstractmethod: .. py:method:: transform(x, params, train=False) :staticmethod: :abstractmethod: .. py:class:: NonRigidAlignmentRefiner(reference: numpy.ndarray, to_align: numpy.ndarray, meshsize: Optional[int] = None) Bases: :py:obj:`AlignmentRefiner` Pytorch module to refine alignment between two images by evaluating the thin-plate-spline (TPS) for non-rigid alignment. Performs Autograd on the displacement matrix between source and destination points. .. py:attribute:: src_points .. py:attribute:: displacement .. py:method:: get_params(train=False) .. py:method:: transform(x, params, train=False) :staticmethod: This method should be used when applying the learned affine transformation to an arbitrary image. .. py:class:: RigidAlignmentRefiner(reference: numpy.ndarray, to_align: numpy.ndarray, theta: Optional[numpy.ndarray] = None) Bases: :py:obj:`AlignmentRefiner` Pytorch module to refine alignment between two images. Performs Autograd on the affine transformation matrix. .. py:method:: transform(x, params, train=False) :staticmethod: This method should be used when applying the learned affine transformation to an arbitrary image. .. py:method:: get_params(train=False) .. py:data:: MODULES .. py:function:: refine_alignment(adata: anndata.AnnData, stain_layer: str = SKM.STAIN_LAYER_KEY, rna_layer: str = SKM.UNSPLICED_LAYER_KEY, mode: typing_extensions.Literal[rigid, non-rigid] = 'rigid', downscale: float = 1, k: int = 5, n_epochs: int = 100, transform_layers: Optional[Union[str, List[str]]] = None, **kwargs) Refine the alignment between the staining image and RNA coordinates. There are often small misalignments between the staining image and RNA, which results in incorrect aggregation of pixels into cells based on staining. This function attempts to refine these alignments based on the staining and (unspliced) RNA masks. :param adata: Input Anndata :param stain_layer: Layer containing staining image. :param rna_layer: Layer containing (unspliced) RNA. :param mode: The alignment mode. Two modes are supported: * rigid: A global alignment method that finds a rigid (affine) transformation matrix * non-rigid: A semi-local alignment method that finds a thin-plate-spline with a mesh of certain size. By default, each cell in the mesh consists of 1000 x 1000 pixels. This value can be modified by providing a `binsize` argument to this function (specifically, as part of additional **kwargs). :param downscale: Downscale matrices by this factor to reduce memory and runtime. :param k: Kernel size for Gaussian blur of the RNA matrix. :param n_epochs: Number of epochs to run optimization :param transform_layers: Layers to transform and overwrite inplace. :param \*\*kwargs: Additional keyword arguments to pass to the Pytorch module.