Skip to main content

tsdistances is a Python library (with Rust backend) for computing various pairwise distances between sets of time series data.

Project description

tsdistances

Introduction

tsdistances is a Python library (with Rust backend) for computing various pairwise distances between sets of time series data.

It provides efficient implementation of elastic distance measures such as Dynamic Time Warping (DTW), Longest Common Subsequence (LCSS), Time Warping Edit (TWE), and many others.

The library is designed to be fast and scalable, leveraging parallel computation and GPU support via Vulkan for improved performance.

Features

  1. Multiple Distance Measures: Supports a wide range of time series distance measures:

    • Euclidean

    • CATCH22 Euclidean

    • Edit Distance with Real Penalty (ERP) optionally with GPU support

    • Longest Common Subsequence (LCSS) optionally with GPU support

    • Dynamic Time Warping (DTW) optionally with GPU support

    • Derivative Dynamic Time Warping (DDTW) optionally with GPU support

    • Weighted Dynamic Time Warping (WDTW) optionally with GPU support

    • Weighted Derivative Dynamic Time Warping (WDDTW) optionally with GPU support

    • Amerced Dynamic Time Warping (ADTW) optionally with GPU support

    • Move-Split-Merge (MSM) optionally with GPU support

    • Time Warp Edit Distance (TWE) optionally with GPU support

    • Shape-Based Distance (SBD)

    • MPDist

  2. Parallel Computation: Utilizes multiple CPU cores to speed up computations.

  3. GPU Acceleration: Optional GPU support based on Vulkan for even faster computations with Rust-GPU.

Benchmark

To evaluate the performance of our time series distance computation library, we conducted a comparative analysis with existing libraries.

We selected AEON as the primary competitor due to its comprehensive implementation of distance metrics, making it the most suitable for direct comparison. Several other libraries were considered, and while we did not conduct a full benchmark on all datasets, we reported their execution times on a subset of the UCR Archive datasets.

sthread par gpu
ACSF1 31.60 5.74 1.41
Adiac 5.27 0.84 1.02
Beef 0.47 0.08 0.09
CBF 1.51 0.25 0.28
ChlorineConcentration 131.53 22.73 7.36
CinCECGTorso 524.77 86.92 7.85
CricketX 47.93 8.35 1.82
DiatomSizeReduction 0.59 0.09 0.24
DistalPhalanxOutlineCorrect 2.45 0.37 0.57
ECG200 0.28 0.04 0.05
EthanolLevel 925.06 169.63 38.18
FreezerRegularTrain 100.48 16.94 4.69
FreezerSmallTrain 18.54 3.18 1.11
Ham 5.43 0.93 0.45
Haptics 149.35 27.49 3.82
HouseTwenty 65.37 11.64 1.20
ItalyPowerDemand 0.16 0.03 0.12
MixedShapesSmallTrain 804.75 150.01 16.66
NonInvasiveFetalECGThorax1 3398.64 724.58 107.42
ShapesAll 312.02 54.83 6.90
Strawberry 19.38 3.42 2.35
UWaveGestureLibraryX 1016.30 196.54 21.05
Wafer 462.68 81.98 16.08

Computation times (in seconds) of our method across 23 datasets, comparing single-threaded, parallelized, and GPU implementations.

Installation

PIP

If you use pip, you can install tsdistances with:

    $ pip install tsdistances

From Source

This can be done by going through the following steps in sequence:

  1. Install the latest Rust compiler
  2. Install maturin: pip install maturin
  3. maturin develop --release to build the library, if want to build also the gpu part: a. Install LunarG Vulkan SDK b. Either install SPIRV-Tools or compile tsdistances_gpu with cargo build --release --no-default-features --use-compiled-tools

Usage

Example 1: Compute DTW Distance on CPU and GPU

        
    import numpy as np
    import tsdistances

    # Generate two random time series (1-D arrays of length 100)
    np.random.seed(0)
    x1 = np.random.rand(100)
    x2 = np.random.rand(100)

    # Compute DTW distance on CPU
    cpu_distance = tsdistances.dtw_distance(x1, x2, device='cpu')
    print(f"DTW distance (CPU): {cpu_distance}")

    gpu_distance = tsdistances.dtw_distance(x1, x2, device='gpu')

    print(f"DTW distance (GPU): {gpu_distance}")

Example 2: Pairwise Distances with Multiple Time Series and Parallel Computation

    import numpy as np
    import tsdistances

    # Generate a batch of 10 random time series (each of length 50)
    np.random.seed(42)
    X = np.random.rand(10, 50)

    # Pairwise DTW distances within the set X (on CPU, single thread)
    pairwise_distances = tsdistances.dtw_distance(X, par=False, device='cpu')
    print("Pairwise DTW distance matrix (CPU, 4 jobs):")
    print(pairwise_distances)

    # Compare two batches: compute distances between each element of X and each element of Y
    Y = np.random.rand(8, 50)
    batch_distances = tsdistances.dtw_distance(X, Y, par=True, device='cpu')
    print("Batch DTW distance matrix (X vs Y):")
    print(batch_distances)

Notes

  1. device='gpu' enables GPU acceleration.

  2. par controls parallelism. Set it to True to use all available CPU cores.

  3. If v is not provided, the function computes pairwise distances within u.

Important: Results will differ between CPU and GPU due to f64ing-point precision:

CPU computations use f6464 (double precision) for higher numerical accuracy.

GPU computations use f6432 (single precision) for better performance.
For instance, on an RTX 4090:

    FP32 performance: 82.58 TFLOPS

    FP64 performance: 1.29 TFLOPS (1:64 rate)
    Using f6432 on GPU drastically improves speed but introduces small numerical differences compared to CPU results.

Testing and Validation

All distance implementations in tsdistances are tested against AEON, a widely-used Python library for time series analysis and distances. This ensures that the results are correct and consistent with established benchmarks in the field.

To run the correctness tests, simply use pytest:

pytest -v tests/test_correctness_cpu.py

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

tsdistances-0.1.7.tar.gz (616.0 kB view details)

Uploaded Source

Built Distributions

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

tsdistances-0.1.7-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

tsdistances-0.1.7-cp312-cp312-macosx_11_0_arm64.whl (1.3 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

tsdistances-0.1.7-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

tsdistances-0.1.7-cp311-cp311-macosx_11_0_arm64.whl (1.3 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

tsdistances-0.1.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

tsdistances-0.1.7-cp310-cp310-macosx_11_0_arm64.whl (1.3 MB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

File details

Details for the file tsdistances-0.1.7.tar.gz.

File metadata

  • Download URL: tsdistances-0.1.7.tar.gz
  • Upload date:
  • Size: 616.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.9.4

File hashes

Hashes for tsdistances-0.1.7.tar.gz
Algorithm Hash digest
SHA256 4bedd56d9662731b14b821375f79846ae85feb9e6049275e7d701751d1509e98
MD5 689019ccddbbf5372a8277e7e0d86840
BLAKE2b-256 842275cd7664069db054109c1a7b2530f0246b2435b00eccaba2c34300747a8b

See more details on using hashes here.

File details

Details for the file tsdistances-0.1.7-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for tsdistances-0.1.7-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3efc97dade594e92cda6b45deafe27efe10e1bde36b2f8bbaac1148969493909
MD5 097d511d977a4468d483f08db0e48863
BLAKE2b-256 299df4a9db13c70273827619863e1615293a41886ce9395ecb1b82f5f56c1e88

See more details on using hashes here.

File details

Details for the file tsdistances-0.1.7-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tsdistances-0.1.7-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e11b2c83c3954e544906014890428c4c33668b2cf145d2d27afb4f61ea1bee7a
MD5 24229229a13afcc45774ec44ddb5dc17
BLAKE2b-256 e346e714f96cb72cbb97ddcf0a5dc544bc225dbf4e2243492cfac47fce52eecc

See more details on using hashes here.

File details

Details for the file tsdistances-0.1.7-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for tsdistances-0.1.7-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 59a146ec7c7d6616ea8907b112c988de448bb8126dbfc68b11b4d880399dac4c
MD5 871ae0944fd112e40584245eb4ce4356
BLAKE2b-256 54f7d56c437d746647c172df0c7021cea7f354cea8d4281f14c9169565248518

See more details on using hashes here.

File details

Details for the file tsdistances-0.1.7-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tsdistances-0.1.7-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bd382c051359786de0eb5f7ada9856f70cda178c96c06056763103983e4f29c1
MD5 794018745b7ab4a85ecd6797d6929f7c
BLAKE2b-256 5f4d786e84f589aad5546a8ddc1ea00f5c0c2b80e984b171858f14b11c86c535

See more details on using hashes here.

File details

Details for the file tsdistances-0.1.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for tsdistances-0.1.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6a42c7c572c0fc87c828106b4e7265d41ef6fafe500db2855e5ef6b71f3cd280
MD5 a05ad58147043e24a70d8d4cec734a60
BLAKE2b-256 f3161410f8b53b2e0858ac77f836c5fa2b62ee1c5db3eef071e02de9315cfb7a

See more details on using hashes here.

File details

Details for the file tsdistances-0.1.7-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tsdistances-0.1.7-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 1e1dff6fe47492cbf46dd8c0e897c2f86e3beb58a50d86eacef664016480c75a
MD5 db614156cb1f3cfa3b54c82427ed1d5d
BLAKE2b-256 4bccbdec853df36e88884e6ddb09f80ce55e63f2daa78886d9d4eeafa3a0de8c

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