Skip to main content

A Numba version of TWED

Project description

Time Warp Edit Distance (TWED) Library

A high-performance Python library for computing the Time Warp Edit Distance (TWED) between time series [*]. This implementation leverages Numba for just-in-time (JIT) compilation, offering significant speed-ups for dynamic programming and backtracking operations. The library supports both NumPy arrays and pandas DataFrames.

Features

  • TWED Calculation: Compute the TWED between two time series using dynamic programming.
  • Edit Path Recovery: Retrieve the optimal sequence of operations (matches or deletions) that align the two time series.
  • Multi-format Support: Works with both NumPy arrays (time stamps provided as NumPy arrays or inferred by default) and pandas DataFrames (using DataFrame indices as time stamps).
  • Performance Optimizations:
    • Utilizes Numba JIT for accelerated computation.
    • Minimizes per-iteration memory allocations by replacing array operations with scalar variable comparisons.
    • Optional parallelization with Numba’s prange for large-scale time series.

Installation

  1. Clone the repository:

    git clone git@github.com:sashakolpakov/fast-twed.git
    cd fast-twed
    
  2. Create a virtual environment (optional but recommended):

    python -m venv env
    source env/bin/activate  # On Windows: env\Scripts\activate
    
  3. Install the package:

    pip install --upgrade pip # Optional
    pip install -e .
    

Example usage with Numpy arrays

import numpy as np
from twed import twed 

# Create example time series data
a = np.array([[1.0], [2.0], [3.0]])
b = np.array([[1.5], [2.5], [3.5]])

# Optionally, provide custom time stamps; otherwise, default sequential indices will be used.
ts_a = np.array([0.0, 1.0, 2.0])
ts_b = np.array([0.0, 1.0, 2.0])

# Compute TWED without the edit path
distance = twed(a, b, ts_a=ts_a, ts_b=ts_b, nu=0.001, lam=1.0)
print("TWED:", distance)

Example usage with Pandas DataFrames

import pandas as pd
from twed import twed

# Create example DataFrames; index will be used as time stamps
df_a = pd.DataFrame({'value': [1.0, 2.0, 3.0]}, index=[0.0, 1.0, 2.0])
df_b = pd.DataFrame({'value': [1.5, 2.5, 3.5]}, index=[0.0, 1.0, 2.0])

# Compute TWED and also get the optimal edit path
distance, edit_path = twed(df_a, df_b, nu=0.001, lam=1.0, path_out=True)
print("TWED:", distance)
print("Edit Path:", edit_path)

References

[*] Marteau, Pierre-François. "Time warp edit distance with stiffness adjustment for time series matching." IEEE transactions on pattern analysis and machine intelligence 31.2 (2008): 306-318; http://dx.doi.org/10.1109/TPAMI.2008.76.

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

fast_twed-0.0.1.post1.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

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

fast_twed-0.0.1.post1-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file fast_twed-0.0.1.post1.tar.gz.

File metadata

  • Download URL: fast_twed-0.0.1.post1.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.10

File hashes

Hashes for fast_twed-0.0.1.post1.tar.gz
Algorithm Hash digest
SHA256 e66cbf6f577eab1d74885bf621f389b9e79cced5820f27ebc2182059663f6481
MD5 827915777ff0fa0a52ba1786d2a09fc2
BLAKE2b-256 04dc67084451636b89bfa41d86d296683e4a11e534396fb4ad470c376af1dd88

See more details on using hashes here.

File details

Details for the file fast_twed-0.0.1.post1-py3-none-any.whl.

File metadata

File hashes

Hashes for fast_twed-0.0.1.post1-py3-none-any.whl
Algorithm Hash digest
SHA256 c0ee0a1d5a8ea72251a761c26a8ce1129b3821f46c932ae884b464a18f178097
MD5 ebebca681feef695c53e1fa3dfebe8c7
BLAKE2b-256 b0b8f2b7880f4ab0a446e63bd2a04120534e05984a66ba087fbb958c6d5e9684

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