16

Does anyone know of a python library that has DTW implementation? mlpy seems to have what I'm looking for, but I can't seem to install it correctly -- currently awaiting replies from the mailing list so I thought I would scope out other libraries.

C. Reed
  • 2,382
  • 7
  • 30
  • 35
  • ldd /bin/delorean libplutonium.1.21.so => /lib/libplutonium.1.21.so – AJ. Apr 17 '11 at 18:18
  • 7
    People assume that time is a strict progression of cause to effect, but *actually* - from a non-linear, non-subjective viewpoint - it's more like a big ball of wibbly-wobbly timey-wimey, er, stuff. – flow Apr 17 '11 at 18:37
  • [Mr. Special Relativity](http://en.wikipedia.org/wiki/Special_relativity) – C. Reed Apr 21 '11 at 05:48
  • 2
    @C. Reed - it was a play on @flow's Dr. Who quote :P – detly May 05 '11 at 01:18
  • I wrote a C extension to Python to do the central calculation in classic Dynamic Programming / Dynamic Time Warp. It runs typically 500x faster than a straight Python version. See the code and ipython notebook demonstration at https://github.com/dpwe/dp_python . – dpwe Jun 06 '14 at 19:23
  • This lib may help you: https://github.com/talcs/simpledtw – SomethingSomething Aug 01 '18 at 09:54
  • [My answer here](https://stackoverflow.com/a/67397144/7109869) may be of help. – Gonçalo Peres May 05 '21 at 09:21

2 Answers2

24

Had to chime in on this one. To follow up with C's response, here's an implementation that is geared more towards interfacing with data generated in NumPy. I find this to be considerably more useful since typically I'm generating data in Python and want to interface with R resources.

import numpy as np

import rpy2.robjects.numpy2ri
from rpy2.robjects.packages import importr

rpy2.robjects.numpy2ri.activate()

# Set up our R namespaces
R = rpy2.robjects.r
DTW = importr('dtw')

# Generate our data
idx = np.linspace(0, 2*np.pi, 100)
template = np.cos(idx)
query = np.sin(idx) + np.array(R.runif(100))/10

# Calculate the alignment vector and corresponding distance
alignment = R.dtw(query, template, keep=True)
dist = alignment.rx('distance')[0][0]

print(dist)

Note that this is the example problem stated on the DTW site.

Pietro Battiston
  • 7,930
  • 3
  • 42
  • 45
Stefan Novak
  • 763
  • 1
  • 6
  • 13
  • Thanks! One of the things I love about this approach is that it seems as though rpy2 plays smoothly with the multiprocessing module in Python. So if you have a lot of data that you want to process on a multicore machine, it's the way to go! – Stefan Novak May 01 '11 at 21:09
  • 3
    Perhaps this is relevant, I ran into this issue http://stackoverflow.com/questions/2447454/converting-python-objects-for-rpy2 – Leon palafox Oct 28 '12 at 19:13
  • How is this method compared with the Python mlpy.dtw package?> – Sibbs Gambling Sep 20 '13 at 02:32
  • Using R within python must be painfully slow. – Has QUIT--Anony-Mousse May 21 '14 at 12:51
  • 1
    Note that addition of this line is required by recent versions of rpy: `rpy2.robjects.numpy2ri.activate()` – tonigi Nov 12 '15 at 13:54
  • @tonigi added the missing line – Pietro Battiston May 26 '18 at 15:50
  • Questions: 1) from the docs it is not clear to me what the measurement unit is for `dist`. Is this the same unit as the original time series? 2) is `dist` the non-normalized, minimum global distance between the two series? If so, why is it minimum? – FaCoffee Sep 05 '18 at 08:39
10

For the record, I have been able to use a mashup of R, DTW in R, and rpy2. Working with R in Python is surprisingly simple and extends python's statistical capabilities considerably. Here's an example of finding the distance between an offset noisy sine and cosine series:

    import rpy2.robjects as robjects
    r = robjects.r
    r('library("dtw")')
    idx = r.seq(0,6.28,len=100)
    template = r.cos(idx)
    query = r.sin(idx)+r('runif(100)/10')
    alignment=r.dtw(query,template,keep=r('TRUE'))
    robjects.globalenv["alignment"] =  alignment
    dist = r('alignment$distance')
    print(dist)

C. Reed
  • 2,382
  • 7
  • 30
  • 35
  • 1
    Just a note from the future: this question is now superseded by the [feature-equivalent `dtw-python` package](https://dynamictimewarping.github.io/python/) (also found [on PyPI](https://pypi.org/project/dtw-python/)). The rpy2-R-dtw bridge should no longer be necessary. – tonigi Sep 11 '20 at 08:34