Metadata-Version: 2.5
Name: semantic-kinematics
Version: 0.1.0
Summary: Video to flow physics: object detection, tracking, and Lagrangian gradient regression for measuring dynamic flow features in groups of tracked objects.
Project-URL: Repository, https://github.com/TannerHarms/SemanticKinematics
Project-URL: Paper, https://doi.org/10.1016/j.physd.2026.135335
Project-URL: LGR, https://github.com/TannerHarms/Lagrangian-Gradient-Regression
Project-URL: Issues, https://github.com/TannerHarms/SemanticKinematics/issues
Author: Tanner Harms
License: MIT
License-File: LICENSE
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Physics
Requires-Python: >=3.12
Requires-Dist: lagrangian-gradient-regression>=0.1.0
Requires-Dist: matplotlib>=3.8
Requires-Dist: netcdf4>=1.6
Requires-Dist: numpy>=1.26
Requires-Dist: opencv-python-headless>=4.8
Requires-Dist: pandas>=2.0
Requires-Dist: pillow>=10
Requires-Dist: platformdirs>=3.0
Requires-Dist: pyarrow>=14
Requires-Dist: pydantic>=2.5
Requires-Dist: pyyaml>=6.0
Requires-Dist: scikit-image>=0.22
Requires-Dist: scipy>=1.11
Requires-Dist: torch>=2.6
Requires-Dist: torchvision>=0.21
Requires-Dist: tqdm>=4.65
Requires-Dist: typer>=0.9
Requires-Dist: xarray>=2023.12
Provides-Extra: dev
Requires-Dist: pytest-cov; extra == 'dev'
Requires-Dist: pytest>=7; extra == 'dev'
Requires-Dist: ruff; extra == 'dev'
Description-Content-Type: text/markdown

# SemanticKinematics

Video to flow physics: detect objects in video with a fine-tuned Mask R-CNN,
track them across frames, calibrate pixel coordinates to the world, and measure
Lagrangian flow features (vorticity, FTLE, LAVD, and more) from the tracked
group using [Lagrangian Gradient Regression (LGR)](https://github.com/TannerHarms/Lagrangian-Gradient-Regression).

This is the software behind Harms, Brunton & McKeon,
*Semantic kinematic analysis from groups of tracked objects*
(Physica D **496**, 135335, 2026;
[doi:10.1016/j.physd.2026.135335](https://doi.org/10.1016/j.physd.2026.135335);
preprint [arXiv:2408.16190](https://arxiv.org/abs/2408.16190)).

```text
video ──> detect (tiled Mask R-CNN) ──> track (cross-correlation) ──> calibrate
      ──> kinematics (LGR: velocity gradients + Lagrangian metrics) ──> render
```

The pipeline is config-driven (one YAML file per experiment), every stage is
individually runnable and cached under content-hash keys, and
detection/tracking schemes are pluggable registries — a custom detector is
~10 lines of code (see `semantic_kinematics.detect.Detector`).

## Status

Modernized from the original research code (kept verbatim under `legacy/`
until the parity review completes). This is an alpha release: interfaces may
still change.

## Installation

Python >= 3.12. Install torch first so you control the CUDA/CPU choice:

```bash
# CUDA (pick the index matching your driver) ...
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu126
# ... or CPU-only
pip install torch torchvision --index-url https://download.pytorch.org/whl/cpu

pip install semantic-kinematics
```

This also installs [Lagrangian Gradient Regression](https://github.com/TannerHarms/Lagrangian-Gradient-Regression)
(`lagrangian-gradient-regression` on PyPI, imported as `lgr`).

The SAM-based render overlays are optional. They need Meta's Segment Anything,
which Meta does not publish on PyPI, and a 2.45 GB checkpoint:

```bash
pip install "segment-anything @ git+https://github.com/facebookresearch/segment-anything.git@dca509fe793f601edb92606367a655c15ac00fdf"
```

To work on the package itself, clone the repository and run `pip install -e ".[dev]"`.

## Quickstart

```bash
semkin models fetch maskrcnn_lab_debris
semkin run configs/examples/alldebris_short.yaml
```

or `python examples/quickstart.py`. Useful commands: `semkin validate <cfg>`
(schema + registry + model checks, no compute), `semkin info <cfg>` (stage
keys and cache status), `semkin registry` (everything pluggable),
`semkin run -f track <cfg>` (force one stage and downstream).

## Model zoo

Weights are GitHub Release assets (tag `models-v1`), fetched on demand into
a local cache and SHA256-verified. `semkin models list` shows cache status.

| Name | Architecture | Domain | Epochs |
|---|---|---|---|
| `maskrcnn_pond_debris` | Mask R-CNN ResNet50-FPN **v2** | field pond debris (TurtlePonds, 35 mm) | 150 |
| `maskrcnn_lab_debris` | Mask R-CNN ResNet50-FPN **v2** | lab debris (SplitFlow) | 100 |
| `maskrcnn_sheep_flow` | Mask R-CNN ResNet50-FPN **v2** | sheep herds (aerial) | 100 |
| `sam_vit_h` | SAM ViT-H | stock upstream checkpoint (Meta CDN) | — |

Each registry entry pins the training-time inference settings
(`builder_kwargs`) recovered from the original models — reproducing them is
part of reproducing the paper. See [docs/models.md](https://github.com/TannerHarms/SemanticKinematics/blob/main/docs/models.md) and
[docs/specs/model_registry.md](https://github.com/TannerHarms/SemanticKinematics/blob/main/docs/specs/model_registry.md).

## Data and outputs

- Committed: two small fixture clips (`data/videos/`) and 44 hand-annotated
  training frames (`data/annotations/turtle_ponds/`). Nothing bigger enters
  git (enforced by a test).
- Outputs: `artifacts/<run>/<stage>/<key>/` with a manifest per stage; see
  [docs/specs/pipeline_stages.md](https://github.com/TannerHarms/SemanticKinematics/blob/main/docs/specs/pipeline_stages.md).
- Model cache: `platformdirs.user_cache_dir("semantic_kinematics")` —
  `%LOCALAPPDATA%\semantic_kinematics\semantic_kinematics\Cache` on Windows,
  `~/.cache/semantic_kinematics` on Linux; override `SEMKIN_CACHE_DIR`.
  `SEMKIN_OFFLINE=1` forbids downloads.

## Testing

```bash
pytest              # fast suite: unit tests + synthetic end-to-end pipeline
pytest --runslow    # adds real model inference (needs cached models)
```

See [docs/specs/testing.md](https://github.com/TannerHarms/SemanticKinematics/blob/main/docs/specs/testing.md).

## Development

```powershell
# Windows: creates the venv OUTSIDE OneDrive, installs CUDA torch,
# this package editable, and the local lgr checkout editable.
scripts\dev_setup.ps1              # or -Torch cpu
```

Point your IDE at `%LOCALAPPDATA%\venvs\semkin\Scripts\python.exe`.
Lint with `ruff check src tests examples`. House conventions mirror the lgr
repo; see [docs/specs/index.md](https://github.com/TannerHarms/SemanticKinematics/blob/main/docs/specs/index.md).

## Citation

See [CITATION.cff](https://github.com/TannerHarms/SemanticKinematics/blob/main/CITATION.cff).

## License

MIT — see [LICENSE](https://github.com/TannerHarms/SemanticKinematics/blob/main/LICENSE).
