spateo.segmentation.align#

Functions to refine staining and RNA alignments.

Module Contents#

Classes#

AlignmentRefiner

Base class for all neural network modules.

NonRigidAlignmentRefiner

Pytorch module to refine alignment between two images by evaluating the

RigidAlignmentRefiner

Pytorch module to refine alignment between two images.

Functions#

refine_alignment(adata[, stain_layer, rna_layer, ...])

Refine the alignment between the staining image and RNA coordinates.

Attributes#

class spateo.segmentation.align.AlignmentRefiner(reference: numpy.ndarray, to_align: numpy.ndarray)[source]#

Bases: 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 to(), etc.

Note

As per the example above, an __init__() call to the parent class must be made before assignment on the child.

Variables:
training bool

Boolean represents whether this module is in training or evaluation mode.

loss(pred)[source]#
optimizer()[source]#
forward()[source]#
train(n_epochs: int = 100)[source]#

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. Dropout, BatchNorm, etc.

Parameters:
mode bool

whether to set training mode (True) or evaluation mode (False). Default: True.

Returns:

self

Return type:

Module

abstract get_params(train=False)[source]#
abstract static transform(x, params, train=False)[source]#
class spateo.segmentation.align.NonRigidAlignmentRefiner(reference: numpy.ndarray, to_align: numpy.ndarray, meshsize: int | None = None)[source]#

Bases: 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.

get_params(train=False)[source]#
static transform(x, params, train=False)[source]#

This method should be used when applying the learned affine transformation to an arbitrary image.

class spateo.segmentation.align.RigidAlignmentRefiner(reference: numpy.ndarray, to_align: numpy.ndarray, theta: numpy.ndarray | None = None)[source]#

Bases: AlignmentRefiner

Pytorch module to refine alignment between two images. Performs Autograd on the affine transformation matrix.

static transform(x, params, train=False)[source]#

This method should be used when applying the learned affine transformation to an arbitrary image.

get_params(train=False)[source]#
spateo.segmentation.align.MODULES[source]#
spateo.segmentation.align.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: str | List[str] | None = None, **kwargs)[source]#

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.

Parameters:
adata

Input Anndata

stain_layer

Layer containing staining image.

rna_layer

Layer containing (unspliced) RNA.

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).

downscale

Downscale matrices by this factor to reduce memory and runtime.

k

Kernel size for Gaussian blur of the RNA matrix.

n_epochs

Number of epochs to run optimization

transform_layers

Layers to transform and overwrite inplace.

**kwargs

Additional keyword arguments to pass to the Pytorch module.