def plot_itakura(n_timestamps_1, n_timestamps_2, max_slope=1., ax=None): region = itakura_parallelogram(n_timestamps_1, n_timestamps_2, max_slope) max_slope, min_slope = _get_itakura_slopes(n_timestamps_1, n_timestamps_2, max_slope) if ax is None: _, ax = ax.subplots(1, 1, figsize=(n_timestamps_1 / 2, n_timestamps_2 / 2)) mask = np.zeros((n_timestamps_2, n_timestamps_1)) for i, (j, k) in enumerate(region.T): mask[j:k, i] = 1. ax.imshow(mask, origin='lower', cmap='Wistia') sz = max(n_timestamps_1, n_timestamps_2) x = np.arange(-1, sz + 1) low_max_line = ((n_timestamps_2 - 1) - max_slope * (n_timestamps_1 - 1)) +\ max_slope * np.arange(-1, sz + 1) up_min_line = ((n_timestamps_2 - 1) - min_slope * (n_timestamps_1 - 1)) +\ min_slope * np.arange(-1, sz + 1) diag = (n_timestamps_2 - 1) / (n_timestamps_1 - 1) * np.arange(-1, sz + 1) ax.plot(x, diag, 'black', lw=1) ax.plot(x, max_slope * np.arange(-1, sz + 1), 'b', lw=1.5) ax.plot(x, min_slope * np.arange(-1, sz + 1), 'r', lw=1.5) ax.plot(x, low_max_line, 'g', lw=1.5) ax.plot(x, up_min_line, 'y', lw=1.5) for i in range(n_timestamps_1): for j in range(n_timestamps_2): ax.plot(i, j, 'o', color='green', ms=1) ax.set_xticks(np.arange(-.5, max(n_timestamps_1, n_timestamps_2), 1), minor=True) ax.set_yticks(np.arange(-.5, max(n_timestamps_1, n_timestamps_2), 1), minor=True) ax.grid(which='minor', color='b', linestyle='--', linewidth=1) ax.set_xlim((-0.5, n_timestamps_1 - 0.5)) ax.set_ylim((-0.5, n_timestamps_2 - 0.5)) return ax
def test_actual_results_itakura_parallelogram(params, arr_desired): """Test that the actual results are the expected ones.""" arr_actual = itakura_parallelogram(**params) np.testing.assert_allclose(arr_actual, arr_desired, atol=1e-5, rtol=0.)
def test_parameter_check_itakura_parallelogram(params, error, err_msg): """Test parameter validation.""" with pytest.raises(error, match=re.escape(err_msg)): itakura_parallelogram(**params)
plt.subplot(2, 2, 2) plt.pcolor(timestamps_1, timestamps_2, matrix_sakoechiba.T, edgecolors='k', cmap='Greys') plt.xlabel('x', fontsize=12) plt.ylabel('y', fontsize=12) plt.title("{0}\nDTW(x, y) = {1:.2f}".format('sakoechiba', dtw_sakoechiba), fontsize=14) # Dynamic Time Warping: itakura slope = 1.2 dtw_itakura, path_itakura = dtw( x, y, dist='square', method='itakura', options={'max_slope': slope}, return_path=True ) parallelogram = itakura_parallelogram(n_timestamps_1, n_timestamps_2, max_slope=slope) matrix_itakura = np.zeros((n_timestamps_1 + 1, n_timestamps_2 + 1)) for i in range(n_timestamps_1): matrix_itakura[i, np.arange(*parallelogram[:, i])] = 0.5 matrix_itakura[tuple(path_itakura)] = 1. plt.subplot(2, 2, 3) plt.pcolor(timestamps_1, timestamps_2, matrix_itakura.T, edgecolors='k', cmap='Greys') plt.xlabel('x', fontsize=12) plt.ylabel('y', fontsize=12) plt.title("{0}\nDTW(x, y) = {1:.2f}".format('itakura', dtw_itakura), fontsize=14) # Dynamic Time Warping: multiscale resolution, radius = 5, 2 dtw_multiscale, path_multiscale = dtw(
matrix_sakoechiba, edgecolors='k', cmap='Greys') plt.xlabel('x', fontsize=20, labelpad=-10) plt.ylabel('y', fontsize=20, labelpad=-10) plt.title("{0}\nDTW(x, y) = {1:.2f}".format('sakoechiba', dtw_sakoechiba), fontsize=16) # Dynamic Time Warping: itakura dtw_itakura, path_itakura = dtw(x, y, dist='square', method='itakura', options={'max_slope': 2.}, return_path=True) parallelogram = itakura_parallelogram(n_timestamps, max_slope=2.) matrix_itakura = np.zeros((n_timestamps + 1, n_timestamps + 1)) for i in range(n_timestamps): matrix_itakura[i, np.arange(*parallelogram[:, i])] = 0.5 matrix_itakura[tuple(path_itakura)[::-1]] = 1. plt.subplot(2, 2, 3) plt.pcolor(timestamps, timestamps, matrix_itakura, edgecolors='k', cmap='Greys') plt.xlabel('x', fontsize=20, labelpad=-10) plt.ylabel('y', fontsize=20, labelpad=-10) plt.title("{0}\nDTW(x, y) = {1:.2f}".format('itakura', dtw_itakura), fontsize=16)