Skip to main content

High-performance trajectory distance calculations in Rust with Python bindings

Project description

traj-dist-rs

A high-performance Rust implementation of trajectory distance algorithms with Python bindings, offering significant speed improvements over the original traj-dist library.

License: MIT Python Version Rust Version

📖 About

traj-dist-rs is a high-performance trajectory distance calculation library written in Rust, providing both native Rust APIs and Python bindings via PyO3. It is a complete rewrite of the original traj-dist library, focusing on performance optimization and modern language features.

Why traj-dist-rs?

  • 🚀 Performance: ~73x faster than Python implementation and ~2.7x faster than Cython implementation on average
  • 🔒 Safety: Rust's memory safety guarantees eliminate common runtime errors
  • 📦 Cross-platform: Supports Linux, macOS, and Windows with native binaries
  • 🔗 Dual API: Use it from Python or Rust with minimal overhead
  • 🎯 Accuracy: All algorithms verified against original implementation with < 1e-8 error margin

✨ Features

Supported Distance Algorithms

  • SSPD - Symmetric Segment-Path Distance
  • DTW - Dynamic Time Warping (with optional matrix return)
  • Discret Frechet - Discrete Fréchet Distance
  • Hausdorff - Hausdorff Distance
  • LCSS - Longest Common Subsequence
  • EDR - Edit Distance on Real sequence
  • ERP - Edit distance with Real Penalty (standard & traj-dist compatible)

Distance Types

  • Euclidean - 2D Euclidean distance
  • Spherical - Haversine distance for geographic coordinates

Additional Features

  • Matrix return for DP-based algorithms (DTW, LCSS, EDR, ERP, Discret Frechet)
  • Precomputed distance matrix support for efficient batch computations
  • Comprehensive error handling for invalid inputs
  • Full Python type hints for better IDE support

🚀 Quick Start

Python

import traj_dist_rs
import numpy as np

# Define trajectories as list of [x, y] coordinates or numpy arrays
traj1 = [[0.0, 0.0], [1.0, 1.0], [2.0, 2.0]]
traj2 = [[0.1, 0.1], [1.1, 1.1], [2.1, 2.1]]

# Calculate SSPD distance
distance = traj_dist_rs.sspd(traj1, traj2, dist_type="euclidean")
print(f"SSPD distance: {distance}")

# Calculate DTW distance (returns DpResult with distance and optional matrix)
result = traj_dist_rs.dtw(traj1, traj2, dist_type="euclidean", use_full_matrix=False)
print(f"DTW distance: {result.distance}")

# Calculate Hausdorff distance
distance = traj_dist_rs.hausdorff(traj1, traj2, dist_type="spherical")
print(f"Hausdorff distance: {distance}")

Rust

use traj_dist_rs::distance::sspd::sspd;
use traj_dist_rs::distance::dtw::dtw;
use traj_dist_rs::distance::base::TrajectoryCalculator;
use traj_dist_rs::distance::distance_type::DistanceType;

fn main() {
    let traj1 = vec![[0.0, 0.0], [1.0, 1.0], [2.0, 2.0]];
    let traj2 = vec![[0.1, 0.1], [1.1, 1.1], [2.1, 2.1]];

    // Calculate SSPD distance
    let dist = sspd(&traj1, &traj2, DistanceType::Euclidean);
    println!("SSPD distance: {}", dist);

    // Calculate DTW distance
    let calculator = TrajectoryCalculator::new(&traj1, &traj2, DistanceType::Euclidean);
    let result = dtw(&calculator, false);
    println!("DTW distance: {}", result.distance);
}

📦 Installation

From PyPI (Python)

pip install traj-dist-rs

From Source

Prerequisites:

  • Rust 1.70 or later
  • Python 3.10, 3.11, 3.12, or 3.13
  • maturin

Build and install:

# Clone the repository
git clone <repository-url>
cd traj-dist-rs

# Install development dependencies
pip install maturin

# Build and install in development mode
maturin develop

# Or build a release wheel
maturin build --release
pip install target/wheels/*.whl

Rust-only build:

cargo build --release

📊 Performance

Compared to the original traj-dist implementation (based on median values from K=1000 trajectory pairs):

Overall Performance

Implementation Average Speedup
Rust vs Python ~73x faster
Rust vs Cython ~2.7x faster

By Distance Type

Euclidean Distance:

  • Rust vs Python: ~389x faster (range: 187x - 595x)
  • Rust vs Cython: ~10x faster (range: 6x - 16x)

Spherical Distance:

  • Rust vs Python: ~76x faster (range: 41x - 167x)
  • Rust vs Cython: ~2.7x faster (range: 1.6x - 5.2x)

Best Performing Algorithms

Rust vs Cython (Euclidean):

  • SSPD: 16.13x faster
  • Hausdorff: 14.07x faster
  • ERP: 12.03x faster

Rust vs Python (Euclidean):

  • DTW: 595x faster
  • Discret Frechet: 554x faster
  • LCSS: 381x faster

For detailed performance analysis with statistics, see docs/performance.md.

📚 Documentation

🧪 Testing

Python Tests

cd traj-dist-rs
pip install pytest numpy polars pyarrow
pytest py_tests/

Rust Tests

cd traj-dist-rs
cargo test

Integration Tests

Run comprehensive integration tests:

bash scripts/pre_build.sh

🤝 Contributing

We welcome contributions! Please see our contributing guidelines:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Run tests and ensure they pass
  5. Format your code (cargo fmt for Rust, black for Python)
  6. Commit your changes (git commit -m 'Add amazing feature')
  7. Push to the branch (git push origin feature/amazing-feature)
  8. Open a Pull Request

Development Workflow

For daily development, use the pre-build script:

bash scripts/pre_build.sh

This script will:

  • Format Rust and Python code
  • Run linting (clippy, ruff)
  • Run all tests (Rust + Python)
  • Generate Python stub files
  • Build Python bindings

🔧 Project Structure

traj-dist-rs/
├── src/
│   ├── distance/       # Distance algorithm implementations
│   ├── binding/        # Python bindings (PyO3)
│   └── lib.rs          # Library entry point
├── tests/              # Rust integration tests
├── py_tests/           # Python integration tests
├── python/             # Python package source
├── docs/               # Documentation
└── scripts/            # Build and utility scripts

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgments

  • Original traj-dist library for algorithm reference
  • PyO3 for Python bindings
  • The Rust community for excellent tooling and libraries

📮 Support

  • Issues: Report bugs and request features via GitHub Issues
  • Discussions: Join discussions about usage and development
  • Documentation: Check the docs directory for detailed guides

🗺️ Roadmap

For information about upcoming features and releases, see roadmap_0.1.0a1.md.


Version: 0.1.0-alpha.1
Last Updated: 2026-02-05

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

traj_dist_rs-0.1.0a3.tar.gz (2.7 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

traj_dist_rs-0.1.0a3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (392.5 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

traj_dist_rs-0.1.0a3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (392.9 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

traj_dist_rs-0.1.0a3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (395.0 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

traj_dist_rs-0.1.0a3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (394.2 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

File details

Details for the file traj_dist_rs-0.1.0a3.tar.gz.

File metadata

  • Download URL: traj_dist_rs-0.1.0a3.tar.gz
  • Upload date:
  • Size: 2.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for traj_dist_rs-0.1.0a3.tar.gz
Algorithm Hash digest
SHA256 35b12dd65ec7340f91e9a3451bd8ad8561ec1b77d15f96f8915619fd3f0fd135
MD5 d4104d53c1a72044c183125f5fb9265a
BLAKE2b-256 0f479ca129a747c0cb3fba00e9e39a62b4e5a2d060893abdac8e115d670b0a98

See more details on using hashes here.

File details

Details for the file traj_dist_rs-0.1.0a3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for traj_dist_rs-0.1.0a3-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ca765d0bbd610746ecc384f1d0323a01f7c936c7379ad5b9398a9ffbee1d7fe1
MD5 dfe894bf870f1d097c6a7f438fa2b61c
BLAKE2b-256 4977e446b215e95b52dab857ab9c3ba360874e636a7064bd1a13c3e115578107

See more details on using hashes here.

File details

Details for the file traj_dist_rs-0.1.0a3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for traj_dist_rs-0.1.0a3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 fffc8e9127bdff7fd68485dc722d00ba91922c4ed9bcdcf0897bc11a99e33d61
MD5 d822257a20c00214e36b29366fa34b90
BLAKE2b-256 55f6644911c77e63245be71ea2cafa5dd8e28a2ad3d2d0fa7c708ae6b8c0ddb3

See more details on using hashes here.

File details

Details for the file traj_dist_rs-0.1.0a3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for traj_dist_rs-0.1.0a3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6af8dbccf30be6c53589a545c594494699687ce6dedbf625b1c4ec63371d07b4
MD5 76314fd8183e6cff3f1a743eb2e78cf5
BLAKE2b-256 c04df6abbd909f52e86db2c55ebce28edcabd825f2446859ab572e1ec8669f66

See more details on using hashes here.

File details

Details for the file traj_dist_rs-0.1.0a3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for traj_dist_rs-0.1.0a3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 77e99bb041c3ea8436fc821760dfb10993328490c4a5c5b0dd285ff4d5d7b8d6
MD5 f24adf70154c3f676b295b15573f7243
BLAKE2b-256 5f212ee3097fd8dfd31bfd89505647ad3e621769418434e823cddfcdc91d5a50

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page