Metadata-Version: 2.4
Name: torchdeltaflow
Version: 0.1.0
Summary: Flow matching, mini-batch optimal transport coupling, and posterior sampling for inverse problems in PyTorch
Author-email: Phrugsa Limbunlom <phrugsa.lim@gmail.com>
Maintainer-email: Phrugsa Limbunlom <phrugsa.lim@gmail.com>
License: MIT License
        
        Copyright (c) 2026 DeltaFlow Contributors
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/phrugsa-limbunlom/deltaflow
Project-URL: Repository, https://github.com/phrugsa-limbunlom/deltaflow
Project-URL: Issues, https://github.com/phrugsa-limbunlom/deltaflow/issues
Project-URL: Changelog, https://github.com/phrugsa-limbunlom/deltaflow/blob/main/CHANGELOG.md
Keywords: pytorch,flow-matching,rectified-flow,optimal-transport,diffusion-models,generative-models,posterior-sampling,inverse-problems,medical-imaging,deep-learning
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
Classifier: Topic :: Software Development :: Libraries
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: torch>=2.0
Requires-Dist: numpy
Requires-Dist: einops
Requires-Dist: tqdm
Provides-Extra: images
Requires-Dist: pillow>=9.0; extra == "images"
Provides-Extra: ot
Requires-Dist: scipy>=1.10; extra == "ot"
Provides-Extra: all
Requires-Dist: pillow>=9.0; extra == "all"
Requires-Dist: scipy>=1.10; extra == "all"
Provides-Extra: dev
Requires-Dist: pytest>=6.0; extra == "dev"
Requires-Dist: pytest-cov>=2.0; extra == "dev"
Requires-Dist: black>=24.0; extra == "dev"
Requires-Dist: isort>=5.0; extra == "dev"
Requires-Dist: mypy>=1.0; extra == "dev"
Requires-Dist: pillow>=9.0; extra == "dev"
Requires-Dist: scipy>=1.10; extra == "dev"
Provides-Extra: docs
Requires-Dist: mkdocs-material>=9.0.0; extra == "docs"
Requires-Dist: mkdocstrings[python]>=0.18; extra == "docs"
Requires-Dist: pymdown-extensions>=9.0; extra == "docs"
Dynamic: license-file

# DeltaFlow

**Flow matching and anatomy-invariant guidance alignment for radiograph generative pretraining.**

`DeltaFlow` provides composable PyTorch primitives for two things that are
usually bundled together in ad-hoc research code:

- **Flow matching** — a simulation-free generative objective that regresses
  a velocity field onto the conditional velocity of a probability path
  (Lipman et al., 2023), sampled with a simple Euler ODE solver.
- **Delta alignment** — a multi-scale, anatomy-cancelling loss that aligns
  the *guidance-difference* feature `Δh = h_cond - h_uncond` across
  augmented views, instead of aligning raw (anatomy-entangled) features.
  This is the mechanism behind the name: **Delta** is the guidance-difference
  feature, **Flow** is the flow-matching engine that consumes it.

The library targets 2D radiography (chest X-ray, cephalometric, and hand
radiographs), but the core primitives (`interpolants`, `samplers`, `losses`,
`models.projector`) are domain-agnostic.

## What's inside

| Module | Role |
|---|---|
| `deltaflow.core` | `BaseVelocityField` — the interface every backbone implements |
| `deltaflow.interpolants` | Probability paths, e.g. `LinearInterpolant` (rectified flow) |
| `deltaflow.samplers` | `FlowSampler` — Euler ODE integration |
| `deltaflow.losses` | `FlowMatchingLoss`, `DeltaAlignmentLoss` |
| `deltaflow.models` | `MultiScaleProjector` (per-level projection heads), `EMA` |
| `deltaflow.datasets` | Generic radiograph dataset wrappers (chest / cephalo / hand) |

## Install

```bash
pip install -e .
# or, for docs/dev tooling:
pip install -e ".[dev]"
```

## Usage

### Flow matching

```python
import torch
from deltaflow.interpolants import LinearInterpolant
from deltaflow.losses import FlowMatchingLoss
from deltaflow.samplers import FlowSampler

loss_fn = FlowMatchingLoss(interpolant=LinearInterpolant())
loss = loss_fn(model, x1)               # model(x_t, t) -> predicted velocity
loss.backward()

samples = FlowSampler(model).sample(torch.randn(1000, 2), n_steps=50)
```

### Delta alignment

```python
from deltaflow.losses.delta_alignment import DeltaAlignmentLoss
from deltaflow.models import MultiScaleProjector

projector = MultiScaleProjector(feature_dims={"enc_1_4": 256, "bottleneck": 1024})
loss_fn = DeltaAlignmentLoss(projector, lambda_flow=1.0, lambda_align=5.0)

total, loss_dict = loss_fn(
    v_c1, v_u1, target_v1,
    v_c2, v_u2, target_v2,
    feats_u1, feats_c1, feats_u2, feats_c2,
)
```

See [`examples/`](examples/) for runnable, tiered walkthroughs
(`00-foundations/`, `10-sampling/`, `20-training/`, `90-showcase/`).

## Development

```bash
pip install -e ".[dev]"
pytest
```

## Contributing

See [CONTRIBUTING.md](CONTRIBUTING.md).

## License

MIT. See [LICENSE](LICENSE).

## Citation

If you use DeltaFlow in your research, please cite it (see [CITATION.cff](CITATION.cff)).
