np.set_printoptions(linewidth=np.nan) vec1 = [71, 73, 75, 80, 80, 80, 78, 76, 75, 73, 71, 71, 71, 73, 75, 76, 76, 68, 76, 76, 75, 73, 71, 70, 70, 69, 68, 68, 72, 74, 78, 79, 80, 80, 78] vec2 = [69, 69, 73, 75, 79, 80, 79, 78, 76, 73, 72, 71, 70, 70, 69, 69, 69, 71, 73, 75, 76, 76, 76, 76, 76, 75, 73, 71, 70, 70, 71, 73, 75, 80, 80, 80, 78] t = np.array(vec1, np.double) r = np.array(vec2, np.double) f1 = plt.figure() f2 = plt.figure() f3 = plt.figure() for f, case in [(f1, 1), (f2, 2), (f3, 3)]: m = np.zeros((len(t), len(r))) path, distance = dtw(t, r, case=case, start_anchor_slack=0, end_anchor_slack=0) for point in path: m[point[0], point[1]] = 1 np.set_printoptions(formatter={'all':lambda x: str('X') if x != 0 else str(' ')}) np.set_printoptions(precision=2) distance[distance > 1e5] = np.inf stretch = True if stretch: stretch = float(len(t))/len(r) else:
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)
from __future__ import division from _dtw import dtw import numpy as np import time N = 1000 x = np.random.random(N) y = np.random.random(N) iterations = 100 t0 = time.clock() for i in range(iterations): dtw(x, y) dt = (time.clock() - t0) / iterations * 1000 print "%.2f milliseconds per iteration (mean)" % dt