def test_bug2(): with util_numpy.test_uses_numpy() as np: s1 = np.array([0, 0, 1, 2, 1, 0, 1, 0, 0], dtype=np.double) s2 = np.array([0.0, 1, 2, 0, 0, 0, 0, 0, 0]) d1a = dtw.distance_fast(s1, s2, window=2) d1b = dtw.distance(s1, s2, window=2) if directory: fn = directory / "warpingpaths.png" else: file = tempfile.NamedTemporaryFile() fn = Path(file.name + "_warpingpaths.png") d2, paths = dtw.warping_paths(s1, s2, window=2) best_path = dtw.best_path(paths) if not dtwvis.test_without_visualization(): dtwvis.plot_warpingpaths(s1, s2, paths, best_path, filename=fn, shownumbers=False) print("Figure saved to", fn) assert d1a == pytest.approx(d2) assert d1b == pytest.approx(d2)
def test_psi_dtw_1d(): with util_numpy.test_uses_numpy() as np: x = np.arange(0, 20, .5) s1 = np.sin(x) s2 = np.sin(x - 1) random.seed(1) for idx in range(len(s2)): if random.random() < 0.05: s2[idx] += (random.random() - 0.5) / 2 # print(f's1 = [' + ','.join(f'{vv:.2f}' for vv in s1) + ']') # print(f's2 = [' + ','.join(f'{vv:.2f}' for vv in s2) + ']') # print('distance_fast') d1 = dtw.distance_fast(s1, s2, psi=2) # print(f'{d1=}') # print('warping_paths') d2, paths = dtw.warping_paths(s1, s2, window=25, psi=2) # print(f'{d2=}') with np.printoptions(threshold=np.inf, linewidth=np.inf): print(paths) # print('warping_paths fast') d3, paths = dtw.warping_paths_fast(s1, s2, window=25, psi=2) # print(f'{d3=}') # print(paths) # print('best_path') best_path = dtw.best_path(paths) if not dtwvis.test_without_visualization(): if directory: dtwvis.plot_warpingpaths(s1, s2, paths, best_path, filename=directory / "test_psi_dtw_1d.png") np.testing.assert_almost_equal(d1, d2) np.testing.assert_almost_equal(d1, d3)
def print_dtw_matrix(series_1, series_2, output_matrix): """ Function to print dtw distance matrix :param series_1: First time series to compare :param series_2: Second time series to compare :param output_matrix: path / file where output matrix will be stored """ contador = 0 len1 = roundup(series_1.__len__()) len2 = roundup(series_2.__len__()) series_1 = series_1[:len1] series_2 = series_2[:len2] series_1 = np.split(series_1, int(len1 / 100)) series_2 = np.split(series_2, int(len2 / 100)) for i in range(series_1.__len__()): d, paths = dtw.warping_paths(series_1[i], series_2[i], window=25, psi=5) best_path = dtw.best_path(paths) dtwvis.plot_warpingpaths(series_1[i], series_2[i], paths, best_path, filename=output_matrix % contador) contador += 1
def test_normalize2_prob(): psi = 0 if dtw.dtw_cc is not None: dtw.dtw_cc.srand(random.randint(1, 100000)) else: print("WARNING: dtw_cc not found") with util_numpy.test_uses_numpy() as np: s1 = np.array([0., 0, 1, 2, 1, 0, 1, 0, 0, 2, 1, 0, 0]) s2 = np.array([0., 1, 2, 3, 1, 0, 0, 0, 2, 1, 0, 0, 0]) d1, paths1 = dtw.warping_paths(s1, s2, psi=psi) d2, paths2 = dtw.warping_paths_fast(s1, s2, psi=psi) # print(np.power(paths1,2)) path1 = dtw.best_path(paths1) path2 = dtw.best_path(paths2) prob_paths = [] for i in range(30): prob_paths.append(dtw.warping_path_prob(s1, s2, d1/len(s1), psi=psi)) if not dtwvis.test_without_visualization(): if directory: fig, ax = dtwvis.plot_warpingpaths(s1, s2, paths1, path1) for p in prob_paths: py, px = zip(*p) py = [pyi + (random.random() - 0.5) / 5 for pyi in py] px = [pxi + (random.random() - 0.5) / 5 for pxi in px] ax[3].plot(px, py, ".-", color="yellow", alpha=0.25) fig.savefig(directory / "normalize2_prob.png") np.testing.assert_almost_equal(d1, d2, decimal=4) np.testing.assert_almost_equal(paths1, paths2, decimal=4) np.testing.assert_almost_equal(path1, path2, decimal=4)
def test_distance1_b(): with util_numpy.test_uses_numpy() as np: s1 = [0, 0, 1, 2, 1, 0, 1, 0, 0] s2 = [0, 1, 2, 0, 0, 0, 0, 0, 0] d2, wps = dtw.warping_paths(s1, s2) print(wps) assert d2 == pytest.approx(math.sqrt(2))
def test_distance1(): with util_numpy.test_uses_numpy() as np: directory = prepare_directory() s1 = np.array([0., 0, 1, 2, 1, 0, 1, 0, 0, 2, 1, 0, 0]) s2 = np.array([0., 1, 2, 3, 1, 10, 1, 0, 2, 1, 0, 0, 0]) d, paths = dtw.warping_paths(s1, s2) # print(d, "\n", paths) if not dtwvis.test_without_visualization(): dtwvis.plot_warpingpaths(s1, s2, paths, filename=directory / "temp1.png") weights = np.full((len(s1), 8), np.inf) weights[:, 2:4] = 0.0 weights[4:7, 2:4] = 10.0 weights[:, 4:6] = 0.0 weights[4:7, 4:6] = 10.0 d, paths = dtww.warping_paths(s1, s2, weights) # print(d, "\n", paths) if not dtwvis.test_without_visualization(): dtwvis.plot_warpingpaths(s1, s2, paths, filename=directory / "temp2.png")
def test_psi_dtw_1a(): with util_numpy.test_uses_numpy() as np: x = np.arange(0, 20, .5) s1 = np.sin(x) s2 = np.sin(x - 1) # Add noise # random.seed(1) # for idx in range(len(s2)): # if random.random() < 0.05: # s2[idx] += (random.random() - 0.5) / 2 d, paths = dtw.warping_paths(s1, s2, psi=2, window=25) path = dtw.warping_path(s1, s2, psi=2) if not dtwvis.test_without_visualization(): if directory: dtwvis.plot_warpingpaths(s1, s2, paths, path, filename=str(directory / "test_psi_dtw_1a.png")) # print(paths[:,:]) # dtwvis.plot_warping(s1, s2, path, filename=os.path.expanduser("~/Desktop/test_psi_dtw_1_1.png")) # path = dtw.best_path(paths) # dtwvis.plot_warpingpaths(s1, s2, paths, path, filename=os.path.expanduser("~/Desktop/test_psi_dtw_1_2.png")) np.testing.assert_equal(d, 0.0)
def get_lead_DTW_euclid(x, y): # x = xy[:,0] # y = xy[:,1] d, paths = dtw.warping_paths(x, y, window=None, psi=None) best_path = dtw.best_path(paths) # # dtwvis.plot_warpingpaths( x, y, paths, best_path) lead_E = create_lead_from_path(best_path, x, 0) return (lead_E, d)
def test_psi_dtw_2a(): x = np.arange(0, 20, .5) s1 = np.sin(x - 1) s2 = np.sin(x) d, paths = dtw.warping_paths(s1, s2, psi=2, window=3) # path = dtw.warping_path(s1, s2, psi=2) # dtwvis.plot_warping(s1, s2, path, filename=os.path.expanduser("~/Desktop/test_psi_dtw_2_1.png")) # path = dtw.best_path(paths) # dtwvis.plot_warpingpaths(s1, s2, paths, path, filename=os.path.expanduser("~/Desktop/test_psi_dtw_2_2.png")) np.testing.assert_equal(d, 0.0)
def test_twoleadecg_1(directory=None): with util_numpy.test_uses_numpy() as np: s1 = np.array([1.8896,-0.23712,-0.23712,-0.20134,-0.16556,-0.20134,-0.16556,-0.12978,-0.058224,0.013335,0.031225,0.10278,0.013335,-0.094004,-0.058224,-0.11189,-0.14767,-0.16556,-0.14767,-0.094004,-0.14767,-0.16556,-0.16556,-0.21923,-0.21923,-0.25501,-0.20134,-0.20134,-0.18345,-0.23712,-0.20134,-0.23712,-0.12978,-0.11189,-0.46969,-1.2747,-2.3481,-2.8133,-2.7775,-2.5986,-2.3839,-2.0082,-1.8651,-1.6146,-1.3463,-1.1495,-0.88115,-0.55914,-0.34446,-0.16556,-0.0045548,0.2459,0.53214,0.65737,0.71104,0.74682,0.76471,0.76471,0.80049,0.81838,0.87204,0.88993,0.97938,0.97938,1.0152,1.0867,1.1583,1.1762,1.212,1.2656,1.2656,1.2477,1.2656,1.1762,1.0867,0.99727,0.88993,0.74682,0.63948,0.58581,0.47847,0.38902]) s2 = np.array([1,0.93163,0.094486,0.094486,0.038006,0.080366,0.080366,0.052126,0.080366,0.12273,0.22157,0.29217,0.41925,0.48985,0.39101,0.39101,0.30629,0.24981,0.19333,0.080366,-0.0043544,-0.018474,-0.089075,-0.11731,-0.14555,-0.17379,-0.21615,-0.27263,-0.20203,-0.315,-0.25851,-0.17379,-0.28675,-0.24439,0.16509,-0.11731,-1.0069,-1.9812,-2.4895,-2.786,-2.9272,-2.4612,-2.0518,-1.8964,-1.8258,-1.7411,-1.6705,-1.2893,-0.99276,-0.65388,-0.37148,-0.30087,-0.046714,0.30629,0.53221,0.65929,0.65929,0.72989,0.74401,0.87109,0.89933,0.95581,0.96993,1.0546,1.1394,1.2523,1.2523,1.2947,1.3088,1.3512,1.2806,1.2806,1.1394,1.097,0.89933,0.72989,0.67341,0.54633,0.37689,0.23569,0.10861,0.080366,-0.074955]) d, paths = dtw.warping_paths(s1, s2, psi=2, window=5) path = dtw.warping_path(s1, s2, psi=2) if directory: dtwvis.plot_warping(s1, s2, path, filename=str(directory / "warping.png")) path = dtw.best_path(paths) dtwvis.plot_warpingpaths(s1, s2, paths, path, filename=str(directory / "warpingpaths.png"))
def test_bug_size(): """Two series of length 1500 should not trigger a size error. The warping paths matrix is of size 1501**2 = 2_253_001. If using 64bit values: 1501**2*64/(8*1024*1024) = 17.2MiB. """ with util_numpy.test_uses_numpy() as np: s1 = np.random.rand(1500) s2 = np.random.rand(1500) d1, _ = dtw.warping_paths_fast(s1, s2) d2, _ = dtw.warping_paths(s1, s2) assert d1 == pytest.approx(d2)
def test(amplitude, center, width, noise, target_norm, len_a, window): source_norm = amplitude * norm.pdf(range(0, 400), center, width) source_norm = np.random.normal(source_norm, scale=noise) d, paths = dtw.warping_paths(target_norm, source_norm, window, psi=0) best_path = dtw.best_path(paths) paths = np.array(best_path) euclidean = d init_euclidean = abs(paths[:, 0] - paths[:, 1]) amplitude, center, width, noise = str(amplitude), str(center), str( width), str(noise) return euclidean, init_euclidean
def test_distance1_b(): with util_numpy.test_uses_numpy() as np: dist_opts = {} s1 = np.array( [0., 0.01, 0., 0.01, 0., 0., 0., 0.01, 0.01, 0.02, 0., 0.]) s2 = np.array([0., 0.02, 0.02, 0., 0., 0.01, 0.01, 0., 0., 0., 0.]) d1 = dtw.distance(s1, s2, **dist_opts) d2 = dtw.distance_fast(s1, s2, **dist_opts) d3, wps = dtw.warping_paths(s1, s2, **dist_opts) print(np.power(wps, 2)) assert d1 == d2 assert d1 == d3 assert d1 == pytest.approx(0.02)
def test_normalize2(): with util_numpy.test_uses_numpy() as np: s1 = np.array([0., 0, 1, 2, 1, 0, 1, 0, 0, 2, 1, 0, 0]) s2 = np.array([0., 1, 2, 3, 1, 0, 0, 0, 2, 1, 0, 0, 0]) d1, paths1 = dtw.warping_paths(s1, s2, psi=2) d2, paths2 = dtw.warping_paths_fast(s1, s2, psi=2) path1 = dtw.best_path(paths1) path2 = dtw.best_path(paths2) if directory: dtwvis.plot_warpingpaths(s1, s2, paths1, path1, filename=directory / "normalize.png") np.testing.assert_almost_equal(d1, d2, decimal=4) np.testing.assert_almost_equal(paths1, paths2, decimal=4) np.testing.assert_almost_equal(path1, path2, decimal=4)
def best_match_ts(query, ts_dict): """ Applying the brute-force pattern to find the best match for the query from the set of the ts_dict :param: query list :param: ts_dict best_match: (type of dict) {key: time_series_ID, value: time_series_data, distance: similarity_distance(DTW Algorithm), best_path: side-product from dtw algorithm} :return: best_match_list [{bm_dict1}, {bm_dict2}, {bm_dict3}], .....] """ query_len = len(query) best_match_list = [] best_of_so_far = float('inf') start_time = datetime.datetime.now() for key, value in ts_dict.items(): candidates = slice_list(value, query_len) candidates.sort( key=lambda each: get_distance(dtw.warping_paths, each, query)) for i in range(len(candidates)): distance, paths = dtw.warping_paths(query, candidates[i]) if distance < best_of_so_far: best_match = dict() best_of_so_far = distance best_match['ts_id'] = key best_match['value'] = candidates[i] best_match['distance'] = distance best_match['best_path'] = dtw.best_path(paths) best_match_list.append(best_match) else: break for match in best_match_list: if match['distance'] > best_of_so_far: best_match_list.remove(match) end_time = datetime.datetime.now() print("Time period of the execution for the brute_force: " + str((end_time - start_time).microseconds) + "ms") return best_match_list
def test_psi_dtw_2a(): with util_numpy.test_uses_numpy() as np: x = np.arange(0, 20, .5) s1 = np.sin(x - 1) s2 = np.sin(x) d, paths = dtw.warping_paths(s1, s2, psi=2, window=3) # try: # path = dtw.warping_path(s1, s2, psi=2) # dtwvis.plot_warping(s1, s2, path, filename=os.path.expanduser("~/Desktop/test_psi_dtw_2_1.png")) # path = dtw.best_path(paths) # dtwvis.plot_warpingpaths(s1, s2, paths, path, filename=os.path.expanduser("~/Desktop/test_psi_dtw_2_2.png")) # except MatplotlibException: # pass np.testing.assert_equal(d, 0.0)
def compute_dtw(county_covid_cases_delta, mobility_type_data, mobility_data_type_index): print(county_covid_cases_delta.shape) print(mobility_type_data.shape) mobility_data_type_name = ['recreation', 'groecry', 'park', 'transit', 'work', 'residential'] distance = dtw_visualize.distance(county_covid_cases_delta, mobility_type_data) print(distance) with open('results/confirmed_cases_dtw_distance.csv','a') as csvfile: csvfile.write(mobility_data_type_name[mobility_data_type_index] + " " + str(distance) + "\n") d, paths = dtw_visualize.warping_paths(county_covid_cases_delta, mobility_type_data, window=25, psi=2) best_path = dtw_visualize.best_path(paths) dtwvis.plot_warpingpaths(county_covid_cases_delta, mobility_type_data, paths, best_path, filename="results/dtw_" + mobility_data_type_name[mobility_data_type_index] +".png") plt.title("confirmed cases vs " + mobility_data_type_name[mobility_data_type_index] + " DTW")
def test_twoleadecg_1(): """Example from http://www.timeseriesclassification.com/description.php?Dataset=TwoLeadECG""" with util_numpy.test_uses_numpy() as np: s1 = np.array([1.8896,-0.23712,-0.23712,-0.20134,-0.16556,-0.20134,-0.16556,-0.12978,-0.058224,0.013335,0.031225,0.10278,0.013335,-0.094004,-0.058224,-0.11189,-0.14767,-0.16556,-0.14767,-0.094004,-0.14767,-0.16556,-0.16556,-0.21923,-0.21923,-0.25501,-0.20134,-0.20134,-0.18345,-0.23712,-0.20134,-0.23712,-0.12978,-0.11189,-0.46969,-1.2747,-2.3481,-2.8133,-2.7775,-2.5986,-2.3839,-2.0082,-1.8651,-1.6146,-1.3463,-1.1495,-0.88115,-0.55914,-0.34446,-0.16556,-0.0045548,0.2459,0.53214,0.65737,0.71104,0.74682,0.76471,0.76471,0.80049,0.81838,0.87204,0.88993,0.97938,0.97938,1.0152,1.0867,1.1583,1.1762,1.212,1.2656,1.2656,1.2477,1.2656,1.1762,1.0867,0.99727,0.88993,0.74682,0.63948,0.58581,0.47847,0.38902]) s2 = np.array([1,0.93163,0.094486,0.094486,0.038006,0.080366,0.080366,0.052126,0.080366,0.12273,0.22157,0.29217,0.41925,0.48985,0.39101,0.39101,0.30629,0.24981,0.19333,0.080366,-0.0043544,-0.018474,-0.089075,-0.11731,-0.14555,-0.17379,-0.21615,-0.27263,-0.20203,-0.315,-0.25851,-0.17379,-0.28675,-0.24439,0.16509,-0.11731,-1.0069,-1.9812,-2.4895,-2.786,-2.9272,-2.4612,-2.0518,-1.8964,-1.8258,-1.7411,-1.6705,-1.2893,-0.99276,-0.65388,-0.37148,-0.30087,-0.046714,0.30629,0.53221,0.65929,0.65929,0.72989,0.74401,0.87109,0.89933,0.95581,0.96993,1.0546,1.1394,1.2523,1.2523,1.2947,1.3088,1.3512,1.2806,1.2806,1.1394,1.097,0.89933,0.72989,0.67341,0.54633,0.37689,0.23569,0.10861,0.080366,-0.074955]) d, paths = dtw.warping_paths(s1, s2, psi=2, window=5) path = dtw.warping_path(s1, s2, psi=2) if not dtwvis.test_without_visualization(): if directory: import matplotlib.pyplot as plt fig, axs = dtwvis.plot_warping(s1, s2, path) # type: plt.Figure, plt.axes.Axes fig.set_size_inches(12, 10) fig.set_dpi(100) fig.savefig(str(directory / "warping.png")) plt.close(fig) path = dtw.best_path(paths) dtwvis.plot_warpingpaths(s1, s2, paths, path, filename=str(directory / "warpingpaths.png"))
def test_distance1(): directory = prepare_directory() s1 = np.array([0., 0, 1, 2, 1, 0, 1, 0, 0, 2, 1, 0, 0]) s2 = np.array([0., 1, 2, 3, 1, 10, 1, 0, 2, 1, 0, 0, 0]) d, paths = dtw.warping_paths(s1, s2) # print(d, "\n", paths) dtwvis.plot_warpingpaths(s1, s2, paths, filename=directory / "temp1.png") weights = np.full((len(s1), 8), np.inf) weights[:, 2:4] = 0.0 weights[4:7, 2:4] = 10.0 weights[:, 4:6] = 0.0 weights[4:7, 4:6] = 10.0 d, paths = dtww.warping_paths(s1, s2, weights) # print(d, "\n", paths) dtwvis.plot_warpingpaths(s1, s2, paths, filename=directory / "temp2.png")
def get_plot_wrapping_paths(data, col1, col2): ''' input: data: dataframe 源数据 col1: 列名 col2:列名 output: 输出图像 ''' indicators = [i for i in data.columns if i not in 'date'] array_subset = data[indicators].values array_subset_zscore = stats.zscore(array_subset) array_subset_zscore_T = array_subset_zscore.T x_idx = indicators.index(col1) y_idx = indicators.index(col2) x = array_subset_zscore_T[x_idx, :] y = array_subset_zscore_T[y_idx, :] d, paths = dtw.warping_paths(x, y, window=25, psi=2) best_path = dtw.best_path(paths) dtwvis.plot_warpingpaths(x, y, paths, best_path)
def dtw_(self, length_min, length_max): path = dtw.warping_path(self.new_real_normal[length_min:length_max], self.ncsimul_y_normal[length_min:length_max]) distance, paths = dtw.warping_paths( self.new_real_normal[length_min:length_max], self.ncsimul_y_normal[length_min:length_max]) dtwvis.plot_warping(self.new_real_normal[length_min:length_max], self.ncsimul_y_normal[length_min:length_max], path, filename="warp" + str(self.test) + ".png") best_path = dtw.best_path(paths) dtwvis.plot_warpingpaths(self.new_real_normal[length_min:length_max], self.ncsimul_y_normal[length_min:length_max], paths, best_path, filename="best_path" + str(self.test) + ".png")
def test_subsequence(): with util_numpy.test_uses_numpy() as np: s1 = np.array([1., 2, 0]) s2 = np.array([1., 0, 1, 2, 1, 0, 1, 0, 0, 0, 0]) penalty = 0.1 psi = [0, 0, len(s2), len(s2)] d1, paths1 = dtw.warping_paths(s1, s2, penalty=penalty, psi=psi) d2, paths2 = dtw.warping_paths_fast(s1, s2, penalty=penalty, psi=psi) path1 = dtw.best_path(paths1) print(paths1) path2 = dtw.best_path(paths2) print(paths2) if not dtwvis.test_without_visualization(): if directory: dtwvis.plot_warpingpaths(s1, s2, paths1, path1, filename=directory / "subseq.png") np.testing.assert_almost_equal(d1, d2, decimal=4) np.testing.assert_almost_equal(paths1, paths2, decimal=4) np.testing.assert_almost_equal(path1, path2, decimal=4) np.testing.assert_almost_equal(paths1[3:4, 0:12][0], [np.inf,1.421,1.005,1.421,2.002,1.000,-1,-1,-1,-1,-1,-1], decimal=3)
def prep_dtw(y, y_, min, max, file_): try: len(y) >= max and len(y_) >= max except: raise NameError('the maximum lengh not respects lenght of inputs') else: path = dtw.warping_path(y[min:max], y_[min:max]) distance, paths = dtw.warping_paths(y[min:max], y_[min:max]) dtwvis.plot_warping(y[min:max], y_[min:max], path, filename=file_ + "warp_results.png") best_path = dtw.best_path(paths) dtwvis.plot_warpingpaths(y[min:max], y_[min:max], paths, best_path, filename=file_ + "best_path_results.png") return path, distance
def main(): s1 = np.array([0., 0, 1, 2, 1, 0, 1, 0, 0, 0, 2, 1, 0, 0]) s2 = np.array([0., 1, 2, 3, 1, 0, 0, 0, 2, 1, 0, 0, 0]) path = dtw.warping_path(s1, s2) dtwvis.plot_warping(s1, s2, path) plt.figure(1) plt.subplot(211) plt.title('Timeseries: s1 & s2') plt.plot(s1) plt.subplot(212) plt.plot(s2) plt.show() dist = dtw.distance(s1, s2) print(dist) plt.figure(2) d, paths = dtw.warping_paths(s1, s2, window=3, psi=2) best_path = dtw.best_path(paths) dtwvis.plot_warpingpaths(s1, s2, paths, best_path)
def d(): dd, _ = dtw.warping_paths(s1, s2) return dd
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Tue Jul 30 16:39:47 2019 @author: sprholst """ from dtaidistance import dtw from dtaidistance import dtw_visualisation as dtwvis import numpy as np import matplotlib.pyplot as plt x = np.arange(0, 20, .5) s1 = np.sin(x) s2 = np.cos(x) d, paths = dtw.warping_paths(s1, s2, window=50, psi=0) best_path = dtw.best_path(paths) dtwvis.plot_warpingpaths(s1, s2, paths, best_path) best_path = np.asarray(best_path) plt.figure(2) plt.plot(best_path[:, 0], best_path[:, 1])
def test_distance1_b(): s1 = [0, 0, 1, 2, 1, 0, 1, 0, 0] s2 = [0, 1, 2, 0, 0, 0, 0, 0, 0] d2, _ = dtw.warping_paths(s1, s2) assert d2 == pytest.approx(math.sqrt(2))
from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter #%% np.random.seed(0) target_norm = norm.pdf(range(0, 400), 200, 40) source_norm = norm.pdf(range(0, 400), 200, 40) noise_t = target_norm.max() / 5.0 noise_s = source_norm.max() / 40.0 target_norm = np.random.normal(target_norm, scale=noise_t) source_norm = np.random.normal(source_norm, scale=noise_s) #target_norm /= noise_t #source_norm /= noise_s d, paths = dtw.warping_paths(target_norm, source_norm, window=1000, psi=0) best_path = dtw.best_path(paths) dtwvis.plot_warpingpaths(target_norm, source_norm, paths, best_path) paths = np.array(best_path) euclidean = pow((target_norm[paths[:, 0]] - source_norm[paths[:, 1]]), 2) init_euclidean = abs(target_norm - source_norm) cov = np.correlate(source_norm, target_norm, mode='same') fig, ax = plt.subplots(3, 1, figsize=(8, 4)) #eucint = np.interp(range(0,1200),paths[:,0],euclidean) #tint = np.interp(range(0,1200),paths[:,0],target_norm[paths[:,0]]) #sint = np.interp(range(0,1200),paths[:,0],source_norm[paths[:,1]]) #ax[0].scatter(range(0, euclidean.shape[0]),euclidean, s=3) #ax[0].scatter(range(0, 1200), eucint, s=3)
for i in range(len(rshoul_eY)): rightArm_eangles.append(angle3pt(rshoul_eX[i],rshoul_eY[i], relbow_eX[i], relbow_eY[i], rwrist_eX[i], rwrist_eY[i])) for i in range(len(rshoul_bY)): rightArm_bangles.append(angle3pt(rshoul_bX[i],rshoul_bY[i], relbow_bX[i], relbow_bY[i], rwrist_bX[i], rwrist_bY[i])) for i in range(len(rshoul_bdY)): rightArm_bdangles.append(angle3pt(rshoul_bdX[i],rshoul_bdY[i], relbow_bdX[i], relbow_bdY[i], rwrist_bdX[i], rwrist_bdY[i])) for i in range(len(lshoul_eY)): leftArm_eangles.append(angle3pt(lshoul_eX[i],lshoul_eY[i], lelbow_eX[i], lelbow_eY[i], lwrist_eX[i], lwrist_eY[i])) for i in range(len(lshoul_bY)): leftArm_bangles.append(angle3pt(lshoul_bX[i],lshoul_bY[i], lelbow_bX[i], lelbow_bY[i], lwrist_bX[i], lwrist_bY[i])) for i in range(len(lshoul_bdY)): leftArm_bdangles.append(angle3pt(lshoul_bdX[i],lshoul_bdY[i], lelbow_bdX[i], lelbow_bdY[i], lwrist_bdX[i], lwrist_bdY[i])) leftArm_b = np.asarray(leftArm_bangles, dtype = np.float32) leftArm_e = np.asarray(leftArm_eangles, dtype = np.float32) #distance = dtw.distance(leftArm_eangles[:250], leftArm_bangles[:250]) #print(distance) d, paths = dtw.warping_paths(leftArm_e, leftArm_b, window = 100, psi = 5) best_path = dtw.best_path(paths) #dtwvis.plot_warping(leftArm_eangles, leftArm_bangles, path, filename = "test10.png") #print(paths) dtwvis.plot_warpingpaths(leftArm_e, leftArm_b, paths, path = best_path, filename = "Graphs/bestpath_window100+psi5.png")
def test_bug2(): with util_numpy.test_uses_numpy() as np: s1 = np.array([ 5.005335029629605081e-01, 5.157722489130834864e-01, 4.804319657333316340e-01, 4.520537745752661318e-01, 4.867408184050183717e-01, 4.806534229629605415e-01, 4.530552579964135518e-01, 4.667067057333316171e-01, 4.567955137333316040e-01, 4.414902037333315876e-01, 4.240597964014319321e-01, 4.225263829008334970e-01, 4.030970017333316280e-01, 4.404482984865574768e-01, 3.852339312962939077e-01, 3.634947117333316435e-01, 3.861488867383516266e-01, 3.413363679008334928e-01, 3.451913457333316004e-01, 3.695692377333316680e-01, 3.434781337333315809e-01, 3.063217006568062506e-01, 2.845283817333316145e-01, 2.955394357333315791e-01, 3.151374838781335619e-01, 2.561411067352764026e-01, 2.301194263297469400e-01, 2.478605028202762184e-01, 1.972828198566299318e-01, 2.150545617333316228e-01, 2.232865857333316273e-01, 2.492665580680986370e-01, 2.144049374050155388e-01, 2.079081117333316520e-01, 1.879600957333316391e-01, 1.638555197333316227e-01, 1.425566689000865583e-01, 2.016327177333316067e-01, 2.290943870240647606e-01, 1.900932117333316296e-01, 1.503233018025057766e-01, 1.970833717333316248e-01, 1.999393777333316191e-01, 2.018818837333316019e-01, 2.554168153357214144e-01, 2.345002377333316179e-01, 2.407103957333316113e-01, 2.762874997333316096e-01, 3.059693477333316203e-01, 3.328774862341668528e-01, 3.583867537333316200e-01, 3.743879884050183016e-01, 4.266385131705089373e-01, 4.445410410742424712e-01, 4.642271795675002033e-01, 4.402678696630802357e-01, 4.814591396296271641e-01, 5.317886460815400840e-01, 5.548714817383517683e-01, 5.062713000716849709e-01, 5.431524597333317050e-01, 5.537961812962939323e-01, 5.720852595675002261e-01, 5.933977447347652534e-01, 5.845479257333316969e-01, 6.133363017333317568e-01, 6.276481431102108877e-01, 6.132085097333317414e-01, 5.922371597333316862e-01, 5.778388756463566089e-01 ]) s2 = np.array([ 5.584292601075275808e-01, 5.214504501075275522e-01, 4.877978901075275542e-01, 5.078206201075274873e-01, 4.769738701075275644e-01, 4.478925501075275428e-01, 4.242528301075275676e-01, 4.307546401075275644e-01, 4.370594201075275187e-01, 4.331284101075275617e-01, 4.810766301075275475e-01, 4.250942801075275335e-01, 3.973955801075275684e-01, 4.380910701075275693e-01, 3.786794801075275552e-01, 3.850050201075275180e-01, 3.576176301075275621e-01, 2.987050201075275302e-01, 3.377542001075275468e-01, 3.262601401075275187e-01, 3.278248801075275276e-01, 3.347294101075275474e-01, 3.222199801075275594e-01, 3.372712101075275304e-01, 2.526810801075275448e-01, 1.774206901075275622e-01, 2.384015601075275825e-01, 2.419624201075275816e-01, 1.694136001075275677e-01, 1.983933401075275715e-01, 2.272449101075275646e-01, 1.490059201075275563e-01, 1.416013701075275744e-01, 1.997542401075275698e-01, 1.791462801075275613e-01, 1.712680901075275819e-01, 1.851759601075275707e-01, 1.450854801075275591e-01, 1.041379601075275718e-01, 9.028068310752757064e-02, 1.358144301075275839e-01, 2.006444701075275616e-01, 2.003521501075275768e-01, 2.100136501075275663e-01, 2.521797401075275280e-01, 2.364524601075275734e-01, 2.236850301075275771e-01, 2.873612101075275205e-01, 3.358473801075275156e-01, 3.288144201075275386e-01, 3.195859301075275605e-01, 3.482947201075275445e-01, 4.032929801075275655e-01, 4.566962501075275682e-01, 5.173766201075274962e-01, 5.463256501075275384e-01, 5.172673701075275465e-01, 5.054312901075275200e-01, 5.344046101075274890e-01, 5.389180101075274898e-01, 5.188896901075275014e-01, 5.484243401075274971e-01, 5.899157901075275934e-01, 5.987863201075275255e-01, 6.357147701075275270e-01, 6.277379101075275525e-01, 5.519873201075274904e-01, 5.634240801075275362e-01, 6.307956401075275332e-01, 6.488636001075275272e-01 ]) res1 = dtw.distance(s1, s2) res2 = dtw.distance(s1, s2, max_dist=.20) res3, _m3 = dtw.warping_paths(s1, s2) res4, _m4 = dtw.warping_paths(s1, s2, max_dist=.20) # print(res1) # print(res2) # print(res3) # print(res4) # np.savetxt('/Users/wannes/Desktop/debug/m3.txt', m3) # np.savetxt('/Users/wannes/Desktop/debug/m4.txt', m4) assert res1 == pytest.approx(res2) assert res1 == pytest.approx(res3) assert res1 == pytest.approx(res4)