def test_dtw_subseq_bug1(): use_c = True with util_numpy.test_uses_numpy() as np: query = np.array([-0.86271501, -1.32160597, -1.2307838, -0.97743775, -0.88183547, -0.71453147, -0.70975136, -0.65238999, -0.48508599, -0.40860416, -0.5567877, -0.39904393, -0.51854679, -0.51854679, -0.23652005, -0.21261948, 0.16978966, 0.21281068, 0.6573613, 1.28355626, 1.88585065, 1.565583, 1.40305912, 1.64206483, 1.8667302]) s1 = np.array([-0.87446789, 0.50009064, -1.43396157, 0.52081263, 1.28752619]) s2 = np.array([1.19125347, 0.78778189, -0.95770272, -1.02133264]) sa = subsequence_alignment(query, s1, use_c=use_c) assert sa.best_match().value == pytest.approx(0.08735692337954708) sa = subsequence_alignment(query, s2, use_c=use_c) assert sa.best_match().value == pytest.approx(0.25535859535443606)
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_dtw_subseq_ndim(): use_c = False with util_numpy.test_uses_numpy() as np: # s1 = np.array([1., 2, 3,1]) # query = np.array([2.0, 3.1]) s1 = np.array([[1., 1], [2, 2], [3, 3], [1, 1]]) query = np.array([[2.0, 2.1], [3.1, 3.0]]) sa = subsequence_alignment(query, s1, use_c=use_c) m = sa.best_match() assert m.segment == [1, 2] assert m.value == pytest.approx(0.07071067811865482)
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)