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.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.2.3
File Size Uploaded Provenance Actions
grassmann_ot-0.2.3.tar.gz 10.1 kB Without provenance Details for grassmann_ot-0.2.3.tar.gz

Built distribution (wheel)

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

Total release size: 21.6 kB

Release files / grassmann_ot-0.2.3.tar.gz

Download URL grassmann_ot-0.2.3.tar.gz
Size 10.1 kB
Tags Source
SHA-256 checksum
How to use checksums
ba305ae37685c260e7a04540bef4ac3c57dd2b0a97879bb0fb5b556cc4dd6e6b
BLAKE2b-256 checksum
How to use checksums
eef4d6c9e7eecce5260d83328e07dd9c6d0dee0fd0b97a9507517cc6ab7f0d12
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.3-py3-none-any.whl

Download URL grassmann_ot-0.2.3-py3-none-any.whl
Size 11.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
95c6d5c14ffa884018aee1ef2e4eb3c9b64372fd79865a967eccf2ea77345fab
BLAKE2b-256 checksum
How to use checksums
ef4c4071a3a95ebd3272dacdb33d1435eb490ad480dd43cacb60b22859a9e7dc
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.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