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_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_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)