Library for time series distances (e.g. Dynamic Time Warping) used in the
DTAI Research Group. The library offers a pure
Python implementation and a fast implementation in C. The C implementation
has only Cython as a dependency. It is compatible with Numpy and Pandas and
implemented such that unnecessary data copy operations are avoided.
Dynamic Subsequence Warping (DSW) to explain (dis)similarities (v2.5)
Subsequence search and local concurrences (v2.3).
Support for multivariate time series (v2.3).
DTW Barycenter Averaging for clustering (v2.2).
Numpy is now an optional dependency, also to compile the C library
(only Cython is required).
The consistent use of ssize_t instead of int allows for larger data structures on 64 bit
machines and be more compatible with Numpy.
The parallelization is now implemented directly in C (included if OpenMP is installed).
The max_dist argument turned out to be similar to Silva and Batista's work
on PrunedDTW [7]. The toolbox now implements a version that is equal to PrunedDTW.
Citing this work
Wannes Meert, Kilian Hendrickx, Toon Van Craenendonck, Pieter Robberechts, Hendrik Blockeel & Jesse Davis.
DTAIDistance (Version v2). Zenodo. http://doi.org/10.5281/zenodo.5901139
Installation
$ pip install dtaidistance
or
$ conda install -c conda-forge dtaidistance
The pip installation requires Numpy as a dependency to compile Numpy-compatible
C code (using Cython). However, this dependency is optional and can be removed
by compiling from source.
The fastest version (30-300 times) uses c directly but requires an array as input (with the double type),
and (optionally) also prunes computations by setting max_dist to the Euclidean upper bound:
The matrix with all warping paths can be visualised as follows:
from dtaidistance import dtw
from dtaidistance import dtw_visualisation as dtwvis
import random
import numpy as np
x = np.arange(0, 20, .5)
s1 = np.sin(x)
s2 = np.sin(x - 1)
random.seed(1)
for idx in range(len(s2)):
if random.random() < 0.05:
s2[idx] += (random.random() - 0.5) / 2
d, paths = dtw.warping_paths(s1, s2, window=25, psi=2)
best_path = dtw.best_path(paths)
dtwvis.plot_warpingpaths(s1, s2, paths, best_path)
Notice the psi parameter that relaxes the matching at the beginning and end.
In this example this results in a perfect match even though the sine waves are slightly shifted.
DTW Distance Measures Between Set of Series
To compute the DTW distance measures between all sequences in a list of sequences, use the method dtw.distance_matrix.
You can set variables to use more or less c code (use_c and use_nogil) and parallel or serial execution
(parallel).
The distance_matrix method expects a list of lists/arrays:
DTW Distance Measures Between Set of Series, limited to block
You can instruct the computation to only fill part of the distance measures matrix.
For example to distribute the computations over multiple nodes, or to only
compare source series to target series.
A distance matrix can be used for time series clustering. You can use existing methods such as
scipy.cluster.hierarchy.linkage or one of two included clustering methods (the latter is a
wrapper for the SciPy linkage method).
from dtaidistance import clustering
# Custom Hierarchical clustering
model1 = clustering.Hierarchical(dtw.distance_matrix_fast, {})
cluster_idx = model1.fit(series)
# Augment Hierarchical object to keep track of the full tree
model2 = clustering.HierarchicalTree(model1)
cluster_idx = model2.fit(series)
# SciPy linkage clustering
model3 = clustering.LinkageTree(dtw.distance_matrix_fast, {})
cluster_idx = model3.fit(series)
For models that keep track of the full clustering tree (HierarchicalTree or LinkageTree), the
tree can be visualised:
model.plot("myplot.png")
Subsequence search
DTAIDistance supports various subsequence search algorithms like
Subsequence Alignment, Subsequence KNN Search and Local Concurrences.
See the documentation
for more information.
Motif Discovery
While methods such as dtw.distance_matrix and subsequence.subsequencesearch
can be used for motif discovery in time series (after windowing), a more
efficient and effective algorithm
based on time warping is available in the
LoCoMotif package.
T. K. Vintsyuk,
Speech discrimination by dynamic programming.
Kibernetika, 4:81–88, 1968.
H. Sakoe and S. Chiba,
Dynamic programming algorithm optimization for spoken word recognition.
IEEE Transactions on Acoustics, Speech and Signal Processing, 26(1):43–49, 1978.
C. S. Myers and L. R. Rabiner,
A comparative study of several dynamic time-warping algorithms for connected-word recognition.
The Bell System Technical Journal, 60(7):1389–1409, Sept 1981.
D. F. Silva, G. E. A. P. A. Batista, and E. Keogh.
On the effect of endpoints on dynamic time warping,
In SIGKDD Workshop on Mining and Learning from Time Series, II. Association for Computing Machinery-ACM, 2016.
DTAI distance code.
Copyright 2016-2026 KU Leuven, DTAI Research Group
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Alternatives
Use instead of dtaidistance: tools that do the same job, closest first.