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^2is 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.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| grassmann_ot-0.2.2.tar.gz | 10.1 kB | Without provenance | Details for grassmann_ot-0.2.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| grassmann_ot-0.2.2-py3-none-any.whl | Python 3 | none | any | Details for grassmann_ot-0.2.2-py3-none-any.whl |
Total release size: 21.6 kB
Release files / grassmann_ot-0.2.2.tar.gz
| Download URL | grassmann_ot-0.2.2.tar.gz |
|---|---|
| Size | 10.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9f8f7ae56f73b36c53d908189ab8d030a1a540d1da924b58341ee03536dae100
|
|
BLAKE2b-256 checksum How to use checksums |
cc73e5b06e2f891a00a104d3a0d89eb354dd357d19c3d332ad804b145b32bbfb
|
| 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.2-py3-none-any.whl
| Download URL | grassmann_ot-0.2.2-py3-none-any.whl |
|---|---|
| Size | 11.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
00868ca11dbc8abc33c7fec7444ef0c08a7652da559683836c1d65192d0d7ade
|
|
BLAKE2b-256 checksum How to use checksums |
99168e8f771df6d706c97d7f6c099851b897c01c440ef4fc085fed476b70c34a
|
| 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.2
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.