Skip to main content

Time Warped Edit Distance (TWED)

This is a python wrapper for Marteau's implementation of this 2009 paper [1] introducing the Time Warped Edit Distance (TWED, also TWE Distance), slightly modified and provided here. TWED is an elastic distance measure for comparing time series sequences that has been shown to be a promising contender for the popular Dynamic Time Warping (DTW) distance and edit distances that have been adjusted to work on real numbers, such as the Edit Distance on Real Sequences (EDR) and Edit Distance with Real Penalty (ERP) [1, 2]. Like the edit distances, it comprises a penalty for mismatches, and, like DTW, it allows to control for elasticity. In contrast to DTW and the edit distance measures, it is a proper distance metric, as it fulfills the triangle inequality. Like DTW and the edit distances, TWED has a time complexity of O(mn), where m and n are the lengths of the time series sequences compared. Hence, it may not be appropriate for very long sequences and large time series databases. However, the fulfillment of the metric properties allows for using some established speed-up tricks in down-stream algorithms, like nearest neighbor retrieval, that can compensate for the complexity.

Install

Using pip and PyPI

The module can be installed from PyPI with

pip install pytwed

numpy needs to be installed as the only requirement.

Using pip locally

The module can also be installed locally, by checking out this repo or downloading the code and running the following commands in the directory of setup.py

pip install -r requirements.txt
python setup.py build
python setup.py install

To test the installation run the following:

python test/test_pytwed.py

Using build script

The example_build.sh provides an example command to build the sources without pip.

Usage

The module can then be loaded and used as follows:

import pytwed
import numpy as np

seq1 = np.array([[1.0], [2.1], [3.2], [4.3], [5.4]]).astype('float64')
seq2 = np.array([[9.8], [7.6], [6.5], [4.3], [2.1]]).astype('float64')
ts1 = np.array([1., 2., 3., 4., 5.]).astype('float64')
ts2 = ts1.copy()
pytwed.twed(seq1, seq2, ts1, ts2, nu = 0.001, lmbda = 1.0, p = 2, fast=True)

Sequences seq1 and seq2 should be of type numpy.array with shapes (m, d) and (n, d), where m and n are the lengths of the sequences and d is the dimensionality of the sequences. The TWED can be provided with time stamps ts1 and ts2 with type numpy.array. If None is provied (the default), then equally spaced sequences are assumed (they are set to ts1=np.arange(len(seq1)) and ts2=np.arange(len(seq2))).

The distance function has three parameters that can be chosen/tuned. Parameter p refers to the L^p norm that is used for the distance calculation locally for individual elements. For p=2, the Euclidean norm is used, and it is a useful default. Parameter nu (should be > 0, default: 0.001) is a non-negative constant which characterizes the stiffness of the elastic TWED measure. Parameter lmbda (should be >= 0, default: 1.0) is a constant penalty that punishes the editing efforts. The parameters should be tuned for each individual problem and dataset using training data minimizing your task's objective on a hold-out set. Parameter grids to try are for instance nu in [1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1] and lmbda in [0, .25, .5, .75, 1.0] (see [1] for more details).

This package provides two implementations: A fast implementation as a C extension and a pure Python implementation. The Python implementation should only be used as reference implementation. The C implementation is about two orders of magnitude faster. With parameter fast=True the C implementation is used.

References

[1] 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. Url: http://dx.doi.org/10.1109/TPAMI.2008.76).

[2] Serra, Joan, and Josep Ll Arcos. "An empirical evaluation of similarity measures for time series classification." Knowledge-Based Systems 67 (2014): 305-314.

Metadata

Release files for pytwed 1.0.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytwed 1.0.9
File Size Uploaded Provenance
pytwed-1.0.9.tar.gz 11.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pytwed 1.0.9
File Interpreter ABI Platform
pytwed-1.0.9-cp38-cp38-win_amd64.whl CPython 3.8 CPython 3.8 Windows x86-64 Details

Total release size: 29.4 kB

Release files / pytwed-1.0.9.tar.gz

Download URL pytwed-1.0.9.tar.gz
Size 11.3 kB
Tags Source
SHA-256 checksum
How to use checksums
325ae8ca61a5f777c7460a00b87d91a8a4230b8a9bc05f8179436206187871a7
BLAKE2b-256 checksum
How to use checksums
4bd40fe62b9c80c614d64a8b3b8cc6c41adb76be5335c6feeec9cbecee5e1048
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/49.6.0.post20201009 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.8.6

Release files / pytwed-1.0.9-cp38-cp38-win_amd64.whl

Download URL pytwed-1.0.9-cp38-cp38-win_amd64.whl
Size 18.0 kB
Tags CPython 3.8 Windows x86-64
SHA-256 checksum
How to use checksums
9d229fb9ebbfd274a54cc7ad0f61baa7a3a187a995d3967b73d79f529791430f
BLAKE2b-256 checksum
How to use checksums
47a0c8df9a9ce6eeabceab9a7217e4f3541750c12e698d8c7300d8ba260cfd34
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/49.6.0.post20201009 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.8.6

Security for pytwed 1.0.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

1.0.9 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page