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 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 https://github.com/yourusername/twed-library.git
    cd twed-library
    
  2. Create a virtual environment (optional but recommended):

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

    pip install numpy pandas numba
    

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.tar.gz (9.4 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-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: fast_twed-0.0.1.tar.gz
  • Upload date:
  • Size: 9.4 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.tar.gz
Algorithm Hash digest
SHA256 719abaddb52797200bda936e42b0e3218c6b4e35b682898f4350cb8f83cd5991
MD5 ae1e3a658ca5e794a6719e3f31ee14f0
BLAKE2b-256 6a53bbca9800d646bf3b7171106f5c7aeeaf0c9caba475e73502ddf7a2059eb7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fast_twed-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.10

File hashes

Hashes for fast_twed-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6e3f0593a7948922c5a94fe722db3f99598328ac6171e8370713a44b31235caa
MD5 254250c45d99253fb14970c11aad7f88
BLAKE2b-256 6001fec3444f804b7321975a486f759185ec18fad2edf2175c0216c9c55f710e

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