Skip to main content

grassmann-ot

Differentiable cell-to-cell anchoring via Grassmann subspace distance + Optimal Transport, backed by PyTorch. Compute your gene-program composition matrices H_X and H_Y with any tool (NMF, scVI, Harmony, PCA, BiDictionaryNMF...), then let GrassmannOT turn them into soft / hard cell anchors with gradients.

This is the PyTorch / differentiable edition (v0.2.0). The public API is identical to the NumPy edition; the only additions are gradient support and PyTorch device / dtype control.

Install

pip install grassmann-ot

Or from source:

git clone https://github.com/yourname/grassmann-ot.git
cd grassmann-ot
pip install .

Quick start

import torch
from grassmann_ot import GrassmannOT

# H_X, H_Y: (r, n_cells) from YOUR tool of choice.
# Tensors with requires_grad=True make the whole pipeline differentiable.
H_X = torch.rand(20, 300, requires_grad=True)
H_Y = torch.rand(20, 250, requires_grad=True)

ot = GrassmannOT(k=30, p=3, reg=0.1)
gamma = ot.fit(H_X, H_Y)          # soft coupling, shape (n_X, n_Y)

# Gradients flow back to H_X / H_Y through the Grassmann distance and Sinkhorn
loss = gamma.sum()
loss.backward()

# Hard one-to-one anchors (Hungarian, no gradients)
matches = ot.fit(H_X, H_Y, return_hard=True)

Parameters

Parameter Default Meaning
k 30 Neighborhood size for local subspace
p 3 Subspace dimension
metric cosine Neighbor metric in composition space
reg 0.1 Entropic regularization for Sinkhorn
a, b None Source / target marginals (uniform if None)
k_iter 10 Gradient iterations for basis refinement

fit(H_X, H_Y, requires_grad=False) drops the gradient path and runs fully in torch.no_grad() for lower memory and faster execution.

Low-level API

from grassmann_ot import cells_to_grassmann, grassmann_cost_matrix

U = cells_to_grassmann(H_X, k=30, p=3)
V = cells_to_grassmann(H_Y, k=30, p=3)
C = grassmann_cost_matrix(U, V)   # (n_X, n_Y), differentiable w.r.t. U, V

The returned bases are PyTorch tensors (stored in a GrassmannPoints container) and can be moved with .to(device), detached with .detach(), or converted back to NumPy with .numpy().

Why "differentiable"?

  • kNN graph construction runs once on CPU (non-differentiable, expected).
  • Local subspaces are fitted by optimizing an orthonormal basis on the Stiefel manifold, so each basis has a gradient path back to its neighborhood.
  • The cost C[i,j] = p - ||U_i^T V_j||_F^2 is a smooth function of the bases.
  • Sinkhorn is implemented in the log-domain and is fully differentiable.

This lets you embed GrassmannOT inside a larger model and train end to end, for example by adding an OT loss to a gene-program inference network.

Metadata

Release files for grassmann-ot 0.2.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 grassmann-ot 0.2.0
File Size Uploaded Provenance Actions
grassmann_ot-0.2.0.tar.gz 10.0 kB Without provenance Details for grassmann_ot-0.2.0.tar.gz

Built distribution (wheel)

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

Total release size: 21.4 kB

Release files / grassmann_ot-0.2.0.tar.gz

Download URL grassmann_ot-0.2.0.tar.gz
Size 10.0 kB
Tags Source
SHA-256 checksum
How to use checksums
cefd3382464505f890b1c3ca77824256d2a76a898a3e87d22a81694716c97330
BLAKE2b-256 checksum
How to use checksums
006ae35e2f66bf0d1a5b94bbcf512bef1b37af9f7b2cdd82f129d05d035b5914
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.2.0-py3-none-any.whl

Download URL grassmann_ot-0.2.0-py3-none-any.whl
Size 11.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ab6ae4192d87b2fd59132a9ad53c016de1ddeba10ecdc97c88ea9e48f3f082c6
BLAKE2b-256 checksum
How to use checksums
7afc78ebbf40e96a3b5f9ca7898affa216ff0ff454d3cbe362cd9d5fd1268fa2
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.2.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