Metadata-Version: 2.5
Name: grassmann-ot
Version: 0.2.2
Summary: Differentiable cell-to-cell anchoring via Grassmann subspace distance and Optimal Transport (PyTorch backend).
Project-URL: Homepage, https://github.com/ZengFLab/grassmann-ot
Project-URL: Issues, https://github.com/ZengFLab/grassmann-ot/issues
Author-email: Your Name <you@example.com>
License: 404: Not Found
License-File: LICENSE
Keywords: anchors,deep-learning,grassmann,optimal-transport,pytorch,single-cell
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Bio-Informatics
Requires-Python: >=3.8
Requires-Dist: numpy>=1.20
Requires-Dist: pot>=0.8
Requires-Dist: scikit-learn>=1.0
Requires-Dist: scipy>=1.6
Requires-Dist: torch>=1.10
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == 'dev'
Provides-Extra: test
Requires-Dist: pytest>=7.0; extra == 'test'
Description-Content-Type: text/markdown

# 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

```bash
pip install grassmann-ot
```

Or from source:

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

## Quick start

```python
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

```python
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.
