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.

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
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.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.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 grassmann-ot 0.3.1
File Size Uploaded Provenance Actions
grassmann_ot-0.3.1.tar.gz 12.9 kB Without provenance Details for grassmann_ot-0.3.1.tar.gz

Built distribution (wheel)

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

Total release size: 26.9 kB

Release files / grassmann_ot-0.3.1.tar.gz

Download URL grassmann_ot-0.3.1.tar.gz
Size 12.9 kB
Tags Source
SHA-256 checksum
How to use checksums
68355e5dc7a92db61085b55650a7ffa02c1f5fb55d800a63bdad1da70760b7de
BLAKE2b-256 checksum
How to use checksums
55e78dcf6a1c68999b0716f053b5c8490b8a859e747f920096430e7e3473d83a
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.1-py3-none-any.whl

Download URL grassmann_ot-0.3.1-py3-none-any.whl
Size 14.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
56d4c831bd819b31f15d6aa10a3c88ac5ff28bd6efd4812b2c47c5e1f497506b
BLAKE2b-256 checksum
How to use checksums
201dfdac2fd8ee6450953342008119992e488e21f8c280cae90c7330bdc011ab
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.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.

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