-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_dtw.py
58 lines (47 loc) · 1.32 KB
/
test_dtw.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import numpy as np
from numpy.random import rand
from _dtw import dtw, dtw_path
import sys
import time
size = 10000
window = 0.1
if len(sys.argv) > 1:
size = int(sys.argv[1])
if len(sys.argv) > 2:
window = float(sys.argv[2])
x = rand(size)
y = rand(size)
#x = np.array([1, 1, 2, 3, 2, 0])
#y = np.array([0, 1, 1, 2, 3, 2, 1])
print "Computing DTW distance...\n"
t0 = time.clock()
dist = dtw(x, y, window)
tf = time.clock()
print "DTW distance with window={}: {}".format(window, dist)
print "Took {} seconds.".format(tf - t0)
print "\nComputing DTW path...\n"
t0 = time.clock()
path = dtw_path(x, y, window)
tf = time.clock()
print path[:5]
print "..."
print path[-5:]
assert np.isclose(sum([abs(x[xi] - y[yi]) for xi, yi in path]), dist)
#print sum([abs(x[xi] - y[yi]) for xi, yi in path])
print "Took {} seconds.\n".format(tf - t0)
print "Computing DTW distance...\n"
t0 = time.clock()
dist = dtw(x, y, 1.)
tf = time.clock()
print "DTW distance with no window: {}".format(dist)
print "Took {} seconds.".format(tf - t0)
print "\nComputing DTW path...\n"
t0 = time.clock()
path = dtw_path(x, y, 1.)
tf = time.clock()
print path[:5]
print "..."
print path[-5:]
assert np.isclose(sum([abs(x[xi] - y[yi]) for xi, yi in path]), dist)
#print sum([abs(x[xi] - y[yi]) for xi, yi in path])
print "Took {} seconds.".format(tf - t0)