Skip to main content

Maxspin

Maxspin (maximization of spatial information) is an information theoretic approach to quantifying the degree of spatial organization in spatial transcriptomics (or other spatial omics) data.

Installation

The python package can be installed with:

pip install maxspin

Basic Usage

This package operates on AnnData objects from the anndata package.

We assume the existence of a spatial neighborhood graph. A simple and effective way of doing this is Delaunay triangulation, for example using squidpy.

import squidpy as sq

sq.gr.spatial_neighbors(adata, delaunay=True, coord_type="generic")

Spatial information can then be measured using the spatial_information function.

from maxspin import spatial_information

spatial_information(adata, prior=None)

This adds a spatial_information column to the var metadata.

Similarly, pairwise spatial information can be computed with pairwise_spatial_information. This function will test every pair of genes, which is pretty impractical for large numbers of genes, so it's a good idea to subset the AnnData object before calling this.

from maxspin import pairwise_spatial_information

pairwise_spatial_information(adata, prior=None)

For a more detailed example, check out the tutorial.

Interpreting the spatial information score

The method compute a score for every cell/spot that's in [0,1], like a correlation but typically much smaller, and sums them to arrive at a spatial information score that is then in [0, ncells]. It's possible to normalize for the number of cells by just dividing, but by default a pattern involving more cells is considered more spatially coherent, hence the sum.

Normalization

There are different ways spatial information can be computed. By default, no normalization is done and spatial information is computed on absolute counts. Uncertainty is incorporated using a Gamma-Poisson model.

If prior=None is used, the method makes no attempt to account for estimation uncertainty and computes spatial information directly on whatever is in adata.X.

The recommended way to run spatial_information is with some kind of normalized estimate of expression with some uncertainty estimation. There are two recommended ways of doing this: SCVI and Vanity.

SCVI

SCVI is a convenient and versatile probabilistic model of sequencing experiments, from which we can sample from the posterior to get normalized point estimates with uncertainty.

Using Maxspin with SCVI looks something like this:

import scvi
import numpy as np
from maxspin import spatial_information

scvi.model.SCVI.setup_anndata(adata)
model = scvi.model.SCVI(adata, n_latent=20)

# Sample log-expression values from the posterior.
posterior_samples = np.log(model.get_normalized_expression(return_numpy=True, return_mean=False, n_samples=20, library_size="latent"))
adata_scvi = adata.copy()
adata_scvi.X = np.mean(posterior_samples, axis=0)
adata_scvi.layers["std"] = np.std(posterior_samples, axis=0)

spatial_information(adata_scvi, prior="gaussian")

The tutorial has a more in depth example of using SCVI.

Vanity

I developed the normalization method vanity in part as convenient way to normalize spatial transcriptomics data in a way that provides uncertainty estimates. The preferred way of running vanity + maxspin is then:

from maxspin import spatial_information
from vanity import normalize_vanity

normalize_vanity(adata)
spatial_information(adata, prior="gaussian")

Compared to SCVI, this model more aggressively shrinks low expression genes, which might cause it to miss something very subtle, but is less likely to detect spurious patterns.

Metadata

Release files for maxspin 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for maxspin 0.1.1
File Size Uploaded Provenance
maxspin-0.1.1.tar.gz 15.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for maxspin 0.1.1
File Interpreter ABI Platform
maxspin-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 33.8 kB

Release files / maxspin-0.1.1.tar.gz

Download URL maxspin-0.1.1.tar.gz
Size 15.6 kB
Tags Source
SHA-256 checksum
How to use checksums
7af1e511edfd2f75f608a32efc2ea763e7e741c3a5ca8944c63c663f41902a37
BLAKE2b-256 checksum
How to use checksums
da295985ae2b8ee417d562af6effb80e6ff7655595b6ab7949b0a3d7e6ac83f7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.1 CPython/3.10.10 Linux/6.2.7-arch1-1

Release files / maxspin-0.1.1-py3-none-any.whl

Download URL maxspin-0.1.1-py3-none-any.whl
Size 18.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d1c82c650794fa3736b63ca056da31ddf6d63a0f1109ca865e7ad3f3220a8d23
BLAKE2b-256 checksum
How to use checksums
f0b7ed4e46c0a8fcfde2e9f19c789a00e12e26d064a729f1bc739ea0754cebde
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.1 CPython/3.10.10 Linux/6.2.7-arch1-1

Security for maxspin 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page