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 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_dtw_subseq1(): with util_numpy.test_uses_numpy() as np: query = np.array([1., 2, 0]) series = np.array([1., 0, 1, 2, 1, 0, 2, 0, 3, 0, 0]) sa = subsequence_alignment(query, series) mf = sa.matching_function() # print(f'{mf=}') match = sa.best_match() # print(match) # print(f'Segment={match.segment}') # print(f'Path={match.path}') if not dtwvis.test_without_visualization(): try: import matplotlib.pyplot as plt except ImportError: raise MatplotlibException("No matplotlib available") if directory: plt.plot(mf) plt.savefig(directory / "subseq_matching.png") dtwvis.plot_warpingpaths(query, series, sa.warping_paths(), match.path, filename=directory / "subseq_warping.png") plt.close() best_k = sa.kbest_matches(k=3) assert match.path == [(0, 2), (1, 3), (2, 4)] assert [m.segment for m in best_k] == [[2, 4], [5, 7], [0, 1]]
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 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_dtw_subseq_eeg(): with util_numpy.test_uses_numpy() as np: data_fn = Path(__file__).parent / 'rsrc' / 'EEGRat_10_1000.txt' data = np.loadtxt(data_fn) series = np.array(data[1500:1700]) query = np.array(data[1331:1352]) sa = subsequence_alignment(query, series) match = sa.best_match() kmatches = list(sa.kbest_matches(k=15, overlap=0)) segments = [m.segment for m in kmatches] segments_sol = [[38, 56], [19, 37], [167, 185], [124, 143], [84, 100], [59, 77], [150, 162], [101, 121], [0, 15]] assert segments == segments_sol if directory and not dtwvis.test_without_visualization(): try: import matplotlib.pyplot as plt except ImportError: raise MatplotlibException("No matplotlib available") fn = directory / "test_dtw_subseq_eeg1.png" fig = plt.figure(figsize=(20, 30)) dtwvis.plot_warpingpaths(query, series, sa.warping_paths(), match.path, figure=fig) plt.savefig(fn) plt.close(fig) fn = directory / "test_dtw_subseq_eeg2.png" startidx, endidx = match.segment fig = plt.figure() plt.plot(query, label='query') plt.plot(series[startidx:endidx], label='best match') plt.legend() plt.savefig(fn) plt.close(fig) fn = directory / "test_dtw_subseq_eeg3.png" fig = plt.figure(figsize=(20, 10)) fig, ax = dtwvis.plot_warpingpaths(query, series, sa.warping_paths(), path=-1, figure=fig) print(f'plotting {len(kmatches)} matches') for kmatch in kmatches: dtwvis.plot_warpingpaths_addpath(ax, kmatch.path) plt.savefig(fn) plt.close(fig)
def time_series_dtw_mapping_path(s1,s2,max_shift_rate=0.005,image_output=None): T0=time.time() print("find warping paths...",end=" ") d, paths = dtaidistance.dtw.warping_paths_fast(s1, s2,window=int(max_shift_rate*max(len(s1),len(s2))),max_step=2 ,penalty=0.1,psi=500) print("done(use time: {0})".format(time.time()-T0)) T0=time.time() print("\tfind best path...",end=" ") best_path = dtaidistance.dtw.best_path(paths) print("done(use time: {0})".format(time.time()-T0)) if(image_output): dtwvis.plot_warpingpaths(s1, s2, paths, best_path, filename=image_output) del paths return (best_path)
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 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_dtw_localconcurrences_short(): with util_numpy.test_uses_numpy() as np: series = np.array([0, -1, -1, 0, 1, 2, 1, 0, 0, 0, 1, 3, 2, 1, 0, 0, 0, -1, 0]) gamma = 1 threshold_tau = 70 delta = -2 * np.exp(-gamma * np.percentile(series, threshold_tau)) # -len(series)/2 # penalty delta_factor = 0.5 tau = np.exp(-gamma * np.percentile(series, threshold_tau)) # threshold # print(f'{tau=}, {delta=}') buffer = 10 minlen = 3 lc = local_concurrences(series, gamma=gamma, tau=tau, delta=delta, delta_factor=delta_factor, penalty=1) matches = [] for match in lc.kbest_matches(k=100, minlen=minlen, buffer=buffer): if match is None: break matches.append(match) assert [(m.row, m.col) for m in matches] == [(10, 17), (4, 19)] if directory and not dtwvis.test_without_visualization(): try: import matplotlib.pyplot as plt except ImportError: raise MatplotlibException("No matplotlib available") fn = directory / "test_dtw_localconcurrences_short.png" fig = plt.figure() fig, ax = dtwvis.plot_warpingpaths(series, series, lc.wp, path=-1, figure=fig) for match in matches: dtwvis.plot_warpingpaths_addpath(ax, match.path) plt.savefig(fn) plt.close(fig)
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_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 test_dtw_localconcurrences_eeg(): with util_numpy.test_uses_numpy() as np: data_fn = Path(__file__).parent / 'rsrc' / 'EEGRat_10_1000.txt' data = np.loadtxt(data_fn) series = np.array(data[1500:1700]) gamma = 1 # domain = 2 * np.std(series) # affinity = np.exp(-gamma * series) # print(f'Affinity in [{np.min(affinity)}, {np.max(affinity)}]\n' # f' {np.mean(affinity)} +- {np.std(affinity)}\n' # f' {np.exp(-gamma * np.mean(series))} +- {np.exp(-gamma * np.std(series))}\n' # f' {np.exp(-gamma * np.percentile(series, 75))} / {np.exp(-gamma * np.median(series))} / {np.exp(-gamma * np.percentile(series, 25))}\n') tau_stddev = 0.40 diffp = tau_stddev * np.std(series) delta = -2 * np.exp(-gamma * diffp**2) # -len(series)/2 # penalty delta_factor = 0.5 tau = np.exp(-gamma * diffp**2) # threshold print(f'{tau=}, {delta=}') # tau=0.8532234738897421, delta=-1.7064469477794841 buffer = 10 minlen = 20 lc = local_concurrences(series, gamma=gamma, tau=tau, delta=delta, delta_factor=delta_factor) print(f'{lc.tau=}, {lc.delta=}') matches = [] for match in lc.kbest_matches(k=100, minlen=minlen, buffer=buffer): if match is None: break matches.append(match) print([(m.row, m.col) for m in matches]) # assert [(m.row, m.col) for m in matches] == [(84, 95), (65, 93), (50, 117), (117, 200), (32, 180), # (160, 178), (96, 139), (138, 181), (71, 200), (71, 117), # (73, 137), (52, 138), (12, 117), (117, 178), (117, 160), # (30, 160), (32, 52), (30, 117), (117, 135), (160, 200), # (178, 200), (11, 52), (71, 160), (134, 160), (135, 200), # (30, 200), (50, 200), (11, 73), (50, 160), (12, 33), (11, 137), # (36, 143), (11, 179), (88, 160), (66, 178), (11, 93)] if directory and not dtwvis.test_without_visualization(): try: import matplotlib.pyplot as plt except ImportError: raise MatplotlibException("No matplotlib available") fn = directory / "test_dtw_localconcurrences.png" fig = plt.figure() fig, ax = dtwvis.plot_warpingpaths(series, series, lc.wp, path=-1, figure=fig) for match in matches: dtwvis.plot_warpingpaths_addpath(ax, match.path) plt.savefig(fn) plt.close(fig)
def test_distance3(): s = np.array([[0., 0, 1, 2, 1, 0, 1.3, 0, 0], [0., 1, 2, 0, 0, 0, 0, 0, 0]]) w = np.array([[np.inf, np.inf, 0., 0., 0., 0., np.inf, np.inf], [np.inf, np.inf, 1.1, 1., 0., 0., np.inf, np.inf], [np.inf, np.inf, 1.1, 1., 0., 0., np.inf, np.inf], [np.inf, np.inf, 0., 0., 2., 2.2, np.inf, np.inf], [np.inf, np.inf, 0., 0., 1., 1.1, np.inf, np.inf], [np.inf, np.inf, 0., 0., 0., 0., np.inf, np.inf], [np.inf, np.inf, 0., 0., 1.3, 1.43, np.inf, np.inf], [np.inf, np.inf, 0., 0., 0., 0., np.inf, np.inf], [np.inf, np.inf, 0., 0., 0., 0., np.inf, np.inf]]) d, paths = dtww.warping_paths(s[0], s[1], w, window=0) path = dtw.best_path(paths) if directory: wp_fn = directory / "warping_paths.png" dtwvis.plot_warpingpaths(s[0], s[1], paths, path, filename=wp_fn)
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 test_distance3(): with util_numpy.test_uses_numpy() as np: s = np.array([[0., 0, 1, 2, 1, 0, 1.3, 0, 0], [0., 1, 2, 0, 0, 0, 0, 0, 0]]) w = np.array([[np.inf, np.inf, 0., 0., 0., 0., np.inf, np.inf], [np.inf, np.inf, 1.1, 1., 0., 0., np.inf, np.inf], [np.inf, np.inf, 1.1, 1., 0., 0., np.inf, np.inf], [np.inf, np.inf, 0., 0., 2., 2.2, np.inf, np.inf], [np.inf, np.inf, 0., 0., 1., 1.1, np.inf, np.inf], [np.inf, np.inf, 0., 0., 0., 0., np.inf, np.inf], [np.inf, np.inf, 0., 0., 1.3, 1.43, np.inf, np.inf], [np.inf, np.inf, 0., 0., 0., 0., np.inf, np.inf], [np.inf, np.inf, 0., 0., 0., 0., np.inf, np.inf]]) d, paths = dtww.warping_paths(s[0], s[1], w, window=0) path = dtw.best_path(paths) if not dtwvis.test_without_visualization(): if directory: wp_fn = directory / "warping_paths.png" dtwvis.plot_warpingpaths(s[0], s[1], paths, path, filename=wp_fn)
template_deriv.insert(0, 0) template_deriv.insert(len(template_deriv), 0) query_deriv, template_deriv = np.asarray(query_deriv), np.asarray( template_deriv) plt.figure() plt.plot(query) plt.plot(template) d, paths = dtw.warping_paths_fast(query_deriv, template_deriv, window=1000, psi=0) best_path = dtw.best_path(paths) dtwvis.plot_warpingpaths(query, template, paths, best_path) best_path = np.asarray(best_path) L = np.arange(0, len(best_path), 20) x1, x2, y1, y2 = [], [], [], [] for i in L: x11 = best_path[i, 0] x22 = best_path[i, 1] y11 = d1[x11] y22 = D2[x22] x1.append(x11) x2.append(x22)
#!/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])
#%% 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) ax[0].plot(euclidean) #ax[0].plot(range(0,cov.shape[0]), cov) #ax[1].plot(target_norm)
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")
#Using point DTW distanceRshoulX = dtw.distance(rshoul_bX[:250], rshoul_eX[:250]) distanceRshoulY = dtw.distance(rshoul_bY[:250], rshoul_eY[:250]) distanceRelbowX = dtw.distance(relbow_bX[:250], relbow_eX[:250]) distanceRelbowY = dtw.distance(relbow_bY[:250], relbow_eY[:250]) distanceRwristX = dtw.distance(rwrist_bX[:250], rwrist_eX[:250]) distanceRwristY = dtw.distance(rwrist_bY[:250], rwrist_eY[:250]) distance = distanceRshoulX + distanceRshoulY + distanceRelbowX + distanceRelbowY + distanceRwristX + distanceRwristY distX = distanceRshoulX + distanceRelbowX + distanceRwristX distY = distanceRshoulY + distanceRelbowY + distanceRwristY print(distance) print(dtw.distance(bdxy, exy)) d, paths = dtw.warping_paths(bdxy, exy) best_path = dtw.best_path(paths) dtwvis.plot_warping(bdxy, exy, best_path, filename="Graphs/test_point_badX.png") #print(paths) dtwvis.plot_warpingpaths(bdxy, exy, paths, path=best_path, filename="Graphs/testXX.png")
#dynamic time warping import stampProcessor import numpy as np mode = 'propagationDelayCorrection' coarseTau = 10000 shift = -51318536.0 # We define two sequences x, y as numpy array # where y is actually a sub-sequence from x timeStampAlice = np.load('../data/aliceBobtimeStampAlice.npy') timeStampBob = np.load('../data/aliceBobtimeStampBob.npy') coarseTimebinAlice = stampProcessor.timebin(coarseTau, timeStampAlice) coarseTimebinBob = stampProcessor.timebin(coarseTau, timeStampBob) s1 = coarseTimebinAlice[500000:505000] s2 = coarseTimebinAlice[500000:505000] print('len(x)', len(s1)) print('len(y)', len(s2)) from dtaidistance import dtw from dtaidistance import dtw_visualisation as dtwvis path = dtw.warping_path(s1, s2) dtwvis.plot_warping(s1, s2, path, filename="warp.png") d, paths = dtw.warping_paths(s1, s2, window=25, psi=2) best_path = dtw.best_path(paths) dtwvis.plot_warpingpaths(s1, s2, paths, best_path, filename="path.png")
def dynamic_time_warping(metrics, output_path, stimulus_pairs, trial_dictionary): output_path = os.path.join(output_path, "DynamicTimeWarping") if not os.path.exists(output_path): os.makedirs(output_path) for metric in metrics: metric_dir = os.path.join(output_path, metric) if not os.path.exists(metric_dir): os.makedirs(metric_dir) for first_stimulus, second_stimulus in stimulus_pairs: first_values = [] second_values = [] first_text = [] second_text = [] for trial in TRIALS_FOR_STIMULUS[first_stimulus][1:]: for window in trial_dictionary[trial]: first_values.append( float(trial_dictionary[trial][window][metric])) first_text.append(f'{trial}_{window}') for trial in TRIALS_FOR_STIMULUS[second_stimulus][1:]: for window in trial_dictionary[trial]: second_values.append( float(trial_dictionary[trial][window][metric])) second_text.append(f'{trial}_{window}') if len(first_values) <= len(second_values): query = first_values template = second_values query_text = first_text template_text = second_text title = first_stimulus.split( ' ')[0] + "+" + second_stimulus.split(' ')[0] else: query = second_values template = first_values query_text = second_text template_text = first_text title = second_stimulus.split( ' ')[0] + "+" + first_stimulus.split(' ')[0] query = np.array(query) template = np.array(template) query_normalized = (query - query.min()) / (query.max() - query.min()) template_normalized = (template - template.min()) / ( template.max() - template.min()) _, paths = dtw.warping_paths(query_normalized, template_normalized, window=10, psi=0) best_path = dtw.best_path(paths) metric_file = os.path.join(metric_dir, f'{title}.txt') log(f'Similarity: {1 - paths[best_path[-1][0] + 1][best_path[-1][1] + 1] / len(best_path)}', file=metric_file) for pair in best_path: log(f'\tPair: {pair}. Match: {query_text[pair[0]]} {template_text[pair[1]]}', file=metric_file) fig, axes = dtwvis.plot_warpingpaths(query, template, paths, best_path) axes[0].texts[0].set_visible(False) axes[0].text( 0, 0, "Similarity = {:.4f}".format( 1 - paths[best_path[-1][0] + 1][best_path[-1][1] + 1] / len(best_path))) plt.savefig(os.path.join(metric_dir, f'{title}.png')) plt.close()