In [1]:
%matplotlib inline
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
mpl.rc('font', family='serif', size=11)
mpl.rc('savefig', bbox='tight')
In [2]:
import dtw
In [5]:
def run_dtw(t, x, y, xname=None, yname=None, offset=0, vmax=None):
dist, dist_mat, cost, path = dtw.dtw(x[:,np.newaxis], y[:,np.newaxis])
fig = plt.figure(figsize=(24, 6))
plt.subplot(131)
plt.plot(t, x, c='purple', label='x')
plt.plot(t, y, c='green', label='y')
plt.legend(loc=0)
plt.xlabel('time')
plt.ylabel('value')
plt.subplot(132)
cost1 = dist_mat[1:, 1:]
offset = t[0]
extent = (offset, offset+cost1.shape[1], offset, offset+cost1.shape[0])
plt.imshow(cost1[::-1], cmap='viridis', extent=extent, vmin=0.0, vmax=vmax)
plt.axis(extent)
plt.xticks(np.asarray(np.linspace(extent[0], extent[1], 5), int))
plt.yticks(np.asarray(np.linspace(extent[2], extent[3], 5), int))
cb = plt.colorbar()
cb.set_label('Dist', rotation=-90, va='bottom')
plt.xlabel('x')
plt.ylabel('y')
plt.subplot(133)
cost1 = cost[1:, 1:]
offset = t[0]
extent = (offset, offset+cost1.shape[1], offset, offset+cost1.shape[0])
plt.imshow(cost1[::-1], cmap='viridis',extent=extent, vmin=0.0, vmax=vmax)
w = offset + path
plt.plot(w[1], w[0], 'w-')
plt.axis(extent)
plt.xticks(np.asarray(np.linspace(extent[0], extent[1], 5), int))
plt.yticks(np.asarray(np.linspace(extent[2], extent[3], 5), int))
cb = plt.colorbar()
cb.set_label('Cost', rotation=-90, va='bottom')
plt.xlabel('x')
plt.ylabel('y')
#fname = 'cost-{0}-to-{1}'.format(make_fname_safe(xname), make_fname_safe(yname))
#plt.savefig(fname + '.png')
#plt.savefig(fname + '.eps')
print('Warping between {0} and {1}:'.format(xname, yname))
print(' Distance is ', dist)
In [6]:
t = np.linspace(0, 1, 11)
x = y = np.zeros(len(t))
run_dtw(t, x, y)
In [7]:
x = y = np.ones(len(t))
run_dtw(t, x, y)
In [8]:
x = y = np.arange(len(t))
run_dtw(t, x, y)
In [9]:
t = np.linspace(-np.pi, np.pi, 11)
x = y = np.exp(t)
run_dtw(t, x, y)
In [10]:
t = np.linspace(-np.pi, np.pi, 41)
x = y = np.exp(-t**2)
run_dtw(t, x, y)
In [11]:
x = np.zeros(len(t))
y = x + 1
run_dtw(t, x, y)
In [12]:
y = x + 42
run_dtw(t, x, y)
In [13]:
t = np.linspace(0, 4*np.pi, 501)
x = np.sin(t)
y = np.sin(1.5*t)
run_dtw(t, x, y)
In [14]:
t = np.linspace(0, 4*np.pi, 501)
x = np.sin(t)
y = x + 2
run_dtw(t, x, y)
In [15]:
t = np.linspace(-np.pi, np.pi, 501)
x = np.exp(t)
y = x + 10
run_dtw(t, x, y)
In [16]:
t = np.linspace(-np.pi, np.pi, 301)
x = np.exp(-t**2)
y = x + 0.5
run_dtw(t, x, y)
In [17]:
t = np.linspace(-np.pi, np.pi, 301)
x = np.exp(-t**2)
y = x + 1.0
run_dtw(t, x, y)
In [18]:
t = np.linspace(0, 1.0, 101)
x = np.arange(len(t))
y = np.arange(len(t)) - 50
run_dtw(t, x, y)
In [19]:
t = np.linspace(0, 4*np.pi, 501)
x = np.sin(t)
y = np.cos(t)
run_dtw(t, x, y)
In [20]:
t = np.linspace(0, 4*np.pi, 501)
x = np.sin(t)
y = np.sin(t+np.pi)
run_dtw(t, x, y)
In [21]:
t = np.linspace(-np.pi, np.pi, 101)
x = np.exp(t)
y = np.exp(t+1)
run_dtw(t, x, y)
In [22]:
t = np.linspace(-np.pi, np.pi, 301)
x = np.exp(-t**2)
y = np.exp(-(t+1.0)**2)
run_dtw(t, x, y)
In [23]:
t = np.linspace(0, 4*np.pi, 501)
x = np.sin(t)
y = 2*np.sin(t)
run_dtw(t, x, y)
In [24]:
t = np.linspace(-np.pi, np.pi, 301)
x = np.exp(-t**2)
y = 2.0*np.exp(-t**2)
run_dtw(t, x, y)