def test_fast_dtw(): """Testing 'fast_dtw'""" # Parameter x1 = np.array([1, -1, 1, 1, 1, -1]) x2 = np.ones(6) x3 = -np.ones(6) # Test 1 cost_actual = fast_dtw(x1, x2, window_size=2) cost_desired = 2 np.testing.assert_equal(cost_actual, cost_desired) # Test 2 cost_actual = fast_dtw(x1, x3, window_size=2) cost_desired = 4 np.testing.assert_equal(cost_actual, cost_desired) # Test 3 cost_actual = fast_dtw(x2, x3, window_size=2) cost_desired = 6 np.testing.assert_equal(cost_actual, cost_desired)
def plot_fastdtw(x, y, window_size, dist='absolute', output_file=None): """Plot the optimal warping path between two time series subject to a constraint region. Parameters ---------- x : np.array, shape = [n_features] first time series y : np.array, shape = [n_features] first time series window_size : int size of the window for PAA dist : str or callable (default = 'absolute') cost distance between two real numbers. Possible values: - 'absolute' : absolute value of the difference - 'square' : square of the difference - callable : first two parameters must be real numbers and it must return a real number. output_file : str or None (default = None) if str, save the figure. """ # Check input data if not (isinstance(x, np.ndarray) and x.ndim == 1): raise ValueError("'x' must be a 1-dimensional np.ndarray.") if not (isinstance(y, np.ndarray) and y.ndim == 1): raise ValueError("'y' must be a 1-dimensional np.ndarray.") if x.size != y.size: raise ValueError("'x' and 'y' must have the same size.") # Size of x x_size = x.size # Check parameters if not isinstance(window_size, int): raise TypeError("'window_size' must be an integer.") if window_size < 1: raise ValueError("'window_size' must be greater or equal than 1.") if window_size > x_size: raise ValueError( "'window_size' must be lower or equal than the size 'x'.") if not (callable(dist) or dist in ['absolute', 'square']): raise ValueError( "'dist' must be a callable or 'absolute' or 'square'.") region, D, path = fast_dtw(x, y, window_size=window_size, approximation=False, dist=dist, return_path=True) x_1 = np.arange(x_size + 1) z_1 = np.zeros([x_size + 1, x_size + 1]) for i in range(x_size): for j in region[i]: z_1[j, i] = 0.5 for i in range(len(path)): z_1[path[i][0], path[i][1]] = 1 plt.pcolor(x_1, x_1, z_1, edgecolors='k', cmap='Greys') plt.xlabel('x', fontsize=20) plt.ylabel('y', fontsize=20) if output_file is not None: plt.savefig(output_file) # Show plot plt.show()
import numpy as np import matplotlib.pyplot as plt from pyts.utils import fast_dtw # Parameters n_samples, n_features = 2, 48 # Toy dataset rng = np.random.RandomState(41) x, y = rng.randn(n_samples, n_features) # Dynamic Time Warping region, D, path = fast_dtw(x, y, dist='absolute', window_size=6, approximation=False, return_path=True) # Visualize the result timestamps = np.arange(n_features + 1) matrix = np.zeros([n_features + 1, n_features + 1]) for i in range(n_features): for j in region[i]: matrix[j, i] = 0.5 for i in range(len(path)): matrix[path[i][0], path[i][1]] = 1 plt.figure(figsize=(8, 8)) plt.pcolor(timestamps, timestamps, matrix, edgecolors='k', cmap='Greys') plt.xlabel('x', fontsize=20)