Skip to main content
Yanked

This release has been yanked by its maintainers, and will be ignored by installers, except when explicitly specified.
Consider using release 0.3.1 instead.

gotmap

Grassmann-subspace Optimal Transport mapping: project query single-cell datasets onto a frozen reference atlas.

gotmap learns a cell-to-cell coupling between a query and a reference by representing each cell as a point on a Grassmann manifold (a neighbourhood subspace) and solving an entropic optimal-transport problem over those points. The output is a shared coordinate representation of the query (obsm['X_mapped']) plus a label transfer, both derived from the same coupling.

Install

pip install gotmap
# with the optional GPU construction backends:
pip install "gotmap[gpu]"

gotmap depends on grassmann-ot>=0.3.1, scanpy>=1.9 and numpy. The neural-network transform additionally needs torch.

Quick start

import scanpy as sc
from gotmap import GOTMap

ref   = sc.read_h5ad("reference_atlas.h5ad")
query = sc.read_h5ad("query_sample.h5ad")

mapper = GOTMap(ref_adata=ref, use_rep="X_pca", k=30, p=3, reg=0.1,
                backend="pca")      # 'pca' is ~10-20x faster; small precision cost

mapper.fit(ref)                    # 1. build & cache the reference Grassmann points
mapper.fit(query)                  # 2. build the query points + Sinkhorn OT
                                   #    (reference side is NOT recomputed)

mapped = mapper.transform(query, method="knn")
labels = mapper.transfer_labels(query, label_key="cell_type")

print(mapped.obsm["X_mapped"].shape)        # (n_query, n_components)
print(labels.obs["cell_type_predicted"].value_counts())

method="nn" trains a small MLP on the anchors and the soft coupling, then projects the query; method="knn" (the default) is non-parametric and sufficient for most evaluation pipelines.

Why the reference is built once

The expensive operation is constructing the Grassmann subspaces for every reference cell. Those subspaces depend only on the reference atlas and on k, p, backend and the coordinate dimension -- never on the query. GOTMap.fit(ref) stores them in mapper.U_ref and validates them through GrassmannCache; every later fit(query) / transform(query) calls GrassmannOT.fit_points(U_ref, V_query) and only builds the query side.

Using precomputed point lists

A rotate-batch loop needs each batch's subspaces exactly once. Build them independently and feed the combined reference list directly into the solver:

from grassmann_ot import cells_to_grassmann

all_points = []
for b, adata_b in enumerate(adata_list):
    H = adata_b.obsm["X_pca"].T                 # (dim, n_cells)
    all_points.append(cells_to_grassmann(H, k=30, p=3, backend="pca"))

for held_out in range(len(adata_list)):
    U_ref = [pt for i, pts in enumerate(all_points) if i != held_out
                for pt in pts]
    V_q   = all_points[held_out]

    mapper = GOTMap(ref_adata=ref_atlas, k=30, p=3, reg=0.1)
    mapper.fit_points(U_ref, V_q)               # no subspace construction at all
    mapped = mapper.transform(query_list[held_out], method="knn")

fit_points is the entry point used internally by benchmark_rotate_fast in gotmap-benchmark>=0.6.0.

Frozen references

FrozenReference carries the PCA loadings, gene means and coordinates of an atlas so an unrelated query can be projected into the same space, with no reference-side recomputation. As of 0.4.0 it can also carry a GrassmannCache:

from gotmap import GOTMap, FrozenReference

fr = FrozenReference(ref)
fr.attach_grassmann(mapper.U_ref, k=30, p=3, backend="pca")
fr.save("atlas_frozen/")

fr2 = FrozenReference.load("atlas_frozen/")
mapper2 = GOTMap.from_frozen(fr2, k=30, p=3, reg=0.1)
mapped  = mapper2.transform(query, method="knn")
# fr2.has_grassmann() == True  ->  reference Grassmann points are reused

Backends

cells_to_grassmann and GrassmannOT accept the same backend keyword:

Backend Requirements When to use
loop numpy Baseline / debugging
batched torch Default; medium datasets
pca scipy, numpy Want 10-20x speed, accept small loss
subsampling sklearn, scipy Very large n, tolerate interpolation
faiss faiss + torch Largest datasets, GPU available

Extra options: batch_size, subsample_rate, inner_backend, device.

API

GOTMap(ref_adata=None, use_rep='X_pca', k=30, p=3, reg=0.1, metric='cosine', backend='batched', ...)

Mapper constructor. Parameters mirror GrassmannOT.

.fit(ref) / .fit(query) / .fit(ref, query)

Build the reference subspaces and cache them; then build the query subspaces and solve the coupling. See the docstring for the full dispatch rules.

.fit_points(U_list, V_list, ref_adata=None)

Skip construction entirely and solve directly. U_list is one (k, dim) array per reference cell; V_list is the same for the query.

.transform(query, method='knn', n_neighbors=15, ...)

Return the mapped query (with obsm['X_mapped']) for method in {'knn', 'nn'}.

.transfer_labels(query, label_key, min_confidence=0.0)

Vote reference labels through the coupling. Results are stored as <label_key>_predicted and <label_key>_confidence.

FrozenReference(adata, use_rep='X_pca', ensure_pca=True)

A reference atlas in a reusable coordinate system.

.attach_grassmann(points, k, p, backend) / .has_grassmann()

Add / inspect a serialisable Grassmann cache.

.project_query(query) / .save(path) / .load(path)

Project an arbitrary query; persist the frozen atlas; restore it (cache included) in another process.

GrassmannCache

Validated container returned by .grassmann_cache. Use GrassmannCache.build(H, k, p, backend) to construct, .concatenate(other) to merge batches and .save(path) / .load(path) to persist.

License

MIT

Metadata

Release files for gotmap 0.4.0

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

Source distribution (sdist)

Source distribution for gotmap 0.4.0
File Size Uploaded Provenance Actions
gotmap-0.4.0.tar.gz 20.4 kB Without provenance Details for gotmap-0.4.0.tar.gz

Built distribution (wheel)

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

Total release size: 40.4 kB

Release files / gotmap-0.4.0.tar.gz

Download URL gotmap-0.4.0.tar.gz
Size 20.4 kB
Tags Source
SHA-256 checksum
How to use checksums
406e8cc4800e2c9fe51452c99acd9da541072cfd2e81fa2c814e4e1b1031d887
BLAKE2b-256 checksum
How to use checksums
715c76cafc0fff3994e9f23bea4001d43136930b4a109b55f567741f04465d44
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.18

Release files / gotmap-0.4.0-py3-none-any.whl

Download URL gotmap-0.4.0-py3-none-any.whl
Size 20.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
90fe251367c4695b867d7f75d3cb59aabca7f15baf78eed04f6a8eb4d09476c0
BLAKE2b-256 checksum
How to use checksums
2a42bfe6d03edc3e409c2b2982714fab838b05d7f23e9f9d96036b7628d66f75
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.18

Security for gotmap 0.4.0

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.

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