Skip to main content

grassmann-ot

Cell-to-cell anchoring via Grassmann subspace distance + Optimal Transport, now with fast, selectable construction backends for large n.

Install

pip install grassmann-ot

For the GPU backends:

pip install torch faiss-gpu   # or faiss-cpu

Quick start

import numpy as np
from grassmann_ot import GrassmannOT

# H_X, H_Y come from any external tool (NMF, scVI, Harmony, ...)
# shape: (n_gene_programs, n_cells)
H_X, H_Y = np.random.rand(20, 300), np.random.rand(20, 250)

ot = GrassmannOT(k=30, p=3, reg=0.1, backend="batched")
gamma   = ot.fit(H_X, H_Y)        # soft coupling, shape (n_X, n_Y)
matches = ot.get_hard_anchors()   # one-to-one anchors, shape (K, 2)

Backends (new in 0.3.0)

cells_to_grassmann and GrassmannOT now accept a backend parameter:

Backend Requirements Use when
loop numpy Baseline / debugging
batched torch Default; medium datasets
pca scipy, numpy Want 10-20x speed, accept small precision loss
subsampling sklearn, scipy Very large n, tolerate interpolation
faiss faiss + torch Largest datasets, GPU available
from grassmann_ot import cells_to_grassmann

U = cells_to_grassmann(H_X, k=30, p=3, backend="pca")
V = cells_to_grassmann(H_Y, k=30, p=3, backend="pca")

Extra options: batch_size, subsample_rate, inner_backend, device, faiss_prefer ('auto' | 'gpu' | 'cpu'; only used when backend='faiss').

Faiss CPU/GPU auto-detection (new in 0.3.2)

The faiss backend automatically detects whether your installed faiss supports GPU and falls back to CPU if it does not. You do not need a different code path for faiss-cpu vs faiss-gpu:

# Works unchanged whether you have faiss-cpu or faiss-gpu installed.
ot = GrassmannOT(k=30, p=3, reg=0.1, backend="faiss")  # auto: GPU if available
gamma = ot.fit(H_X, H_Y)

# Or force a specific mode:
cells_to_grassmann(H_X, backend="faiss", faiss_prefer="cpu")   # force CPU
cells_to_grassmann(H_X, backend="faiss", faiss_prefer="gpu")   # error if no GPU

faiss_prefer='auto' (the default) silently uses CPU when faiss.StandardGpuResources is unavailable, so faiss-cpu users no longer hit AttributeError: module 'faiss' has no attribute 'StandardGpuResources'. The detection is independent of the torch SVD stage, so you can use faiss-CPU kNN with torch GPU SVD or any mix.

You can also skip construction and feed precomputed points directly:

ot = GrassmannOT(k=30, p=3, reg=0.1)
gamma = ot.fit_points(U_list, V_list)

Parameters

Parameter Default Meaning
k 30 Neighborhood size
p 3 Subspace dimension
metric cosine Neighbor metric
reg 0.1 Sinkhorn entropic regularization
backend batched Grassmann construction strategy
batch_size 512 Batch size for torch SVD
subsample_rate 0.1 Fraction exact-computed in subsampling
inner_backend pca Backend for seed cells in subsampling
faiss_prefer auto 'auto'/gpu/cpu for backend='faiss'
a, b None Source / target marginals (uniform)

Benchmark

python examples/benchmark_backends.py

Fast hard anchors (new in 0.3.1)

get_hard_anchors() used to call linear_sum_assignment on a padded cost matrix, which is O(N^3) in the number of cells and becomes the bottleneck at a few thousand cells. The default is now 'mutual', which derives a one-to-one matching directly from the soft coupling gamma in O(n) time:

ot = GrassmannOT(k=30, p=3, reg=0.1, backend="batched")
ot.fit(H_X, H_Y)

anchors = ot.get_hard_anchors()                       # default: mutual, O(n)
anchors = ot.get_hard_anchors(method="row_argmax")    # fastest, may repeat targets
anchors = ot.get_hard_anchors(method="hungarian")     # exact, slow O(N^3)
anchors = ot.get_hard_anchors(method="hungarian_topk", k=200)  # balanced
anchors = ot.get_hard_anchors(method="lapjv")         # pip install lapjv
costs   = ot.get_hard_anchors(method="mutual", return_cost=True)[1]
Method Complexity One-to-one Needs extra dep
mutual (default) O(n) yes -
row_argmax O(n) no -
col_argmax O(n) no -
hungarian_topk O(n·k^2) yes -
hungarian O(N^3) yes -
lapjv ~O(N^3) yes lapjv

Calling get_hard_anchors() with no arguments keeps the old return shape, so existing code works unchanged.

Changelog

0.3.2

  • The faiss backend now auto-detects GPU/CPU and falls back to CPU when faiss.StandardGpuResources is unavailable, so faiss-cpu users no longer hit AttributeError.
  • New faiss_prefer parameter ('auto' / 'gpu' / 'cpu') on both cells_to_grassmann and GrassmannOT.
  • Faiss import is now guarded against NameError and other non-ImportError failures during faiss initialization, so a broken faiss install no longer breaks import grassmann_ot.
  • Backend selection is independent of the torch SVD device, allowing faiss-CPU kNN with torch GPU SVD (or any combination).

0.3.1

  • get_hard_anchors now accepts a method argument; the default 'mutual' is O(n) instead of O(N^3), fixing the large-n slowdown.
  • New methods: mutual, row_argmax, col_argmax, hungarian_topk (with full-Hungarian fallback on infeasible restricted problems), and lapjv.
  • The zero-argument call signature and return shape are unchanged.

0.3.0

  • New module grassmann_ot._grassmann_fast with five backends.
  • GrassmannOT exposes backend, batch_size, subsample_rate, inner_backend, device.
  • New fit_points() to use precomputed Grassmann point lists.
  • Fixed linear_sum_assignment result unpacking.

Metadata

Release files for grassmann-ot 0.3.3

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

Source distribution (sdist)

Source distribution for grassmann-ot 0.3.3
File Size Uploaded Provenance Actions
grassmann_ot-0.3.3.tar.gz 14.8 kB Without provenance Details for grassmann_ot-0.3.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for grassmann-ot 0.3.3
File Interpreter ABI Platform Actions
grassmann_ot-0.3.3-py3-none-any.whl Python 3 none any Details for grassmann_ot-0.3.3-py3-none-any.whl

Total release size: 31.0 kB

Release files / grassmann_ot-0.3.3.tar.gz

Download URL grassmann_ot-0.3.3.tar.gz
Size 14.8 kB
Tags Source
SHA-256 checksum
How to use checksums
3df84fb0cb6710270a63157a09f74d9c1cd8837eeddbbde31a76b63e95384b32
BLAKE2b-256 checksum
How to use checksums
c41afdc5a204461e825da338020dabd540d6c6b1bb45e53cb019e93c7425adc1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.18

Release files / grassmann_ot-0.3.3-py3-none-any.whl

Download URL grassmann_ot-0.3.3-py3-none-any.whl
Size 16.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2dcd8f229e02417d89d862adeefdf3aa7994a1ea5388718e621fdd61bf549cda
BLAKE2b-256 checksum
How to use checksums
a53212bb37edd7c493bcf039bc516643c14dc09eb40f87a8084b431171fec0c3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.18

Security for grassmann-ot 0.3.3

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