def test_distance6(): with util_numpy.test_uses_numpy() as np: s = np.loadtxt(Path(__file__).parent / "rsrc" / "series_0.csv", delimiter=',') l = np.loadtxt(Path(__file__).parent / "rsrc" / "labels_0.csv", delimiter=',') if directory: if not dtwvis.test_without_visualization(): plot_series(s, l) savefig = str(directory / "dts.dot") else: savefig = None prototypeidx = 3 labels = np.zeros(l.shape) labels[l == l[prototypeidx]] = 1 ml_values, cl_values, clf, importances = \ dtww.series_to_dt(s, labels, prototypeidx, window=0, min_ig=0.1, savefig=savefig) logger.debug(f"ml_values = {dict(ml_values)}") logger.debug(f"cl_values = {dict(cl_values)}") weights = dtww.compute_weights_from_mlclvalues(s[prototypeidx], ml_values, cl_values, only_max=False, strict_cl=True) if directory: if not dtwvis.test_without_visualization(): plot_margins(s[prototypeidx], weights, clf, prototypeidx)
def test_distance5(): with util_numpy.test_uses_numpy() as np: s = np.array([ [0., 0, 0, 2, 0, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0], # 0 [0., 0, 2, 0, -2, 0, 2, 0, -2, 0, 2, 0, -2, 0, 0], # 1 [0., 0, 2, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0] # 2 ]) l = np.array([1, 1, 0]) if directory: if not dtwvis.test_without_visualization(): plot_series(s, l) prototypeidx = 0 ml_values, cl_values, clf, importances = dtww.series_to_dt( s, l, prototypeidx, window=4) logger.debug(f"ml_values = {dict(ml_values)}") logger.debug(f"cl_values = {dict(cl_values)}") weights = dtww.compute_weights_from_mlclvalues(s[prototypeidx], ml_values, cl_values, only_max=False, strict_cl=True) if directory: if not dtwvis.test_without_visualization(): plot_margins(s[prototypeidx], weights, clf)
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_distance2(): with util_numpy.test_uses_numpy() as np: s = np.array([[0., 0, 1, 2, 1, 0, 1.3, 0, 0], [0., 0, 1, 2, 1, 0, 1, 0, 0], [0., 1, 2, 0, 0, 0, 0, 0, 0], [0., 1, 2, 0, 0, 0, 0, 0, 0], [1., 2, 0, 0, 0, 0, 0, 1, 1], [1., 2, 0, 0, 0, 0, 0, 1, 1], [1., 2, 0, 0, 1, 0, 0, 1, 1]]) l = np.array([1, 1, 1, 1, 0, 0, 0]) if directory: if not dtwvis.test_without_visualization(): plot_series(s, l) savefig = str(directory / "dts.dot") else: savefig = None prototypeidx = 0 ml_values, cl_values, clfs, importances = \ dtww.series_to_dt(s, l, prototypeidx, max_clfs=50, savefig=savefig) logger.debug(f"ml_values = {dict(ml_values)}") logger.debug(f"cl_values = {dict(cl_values)}") weights = dtww.compute_weights_from_mlclvalues(s[prototypeidx], ml_values, cl_values, only_max=False, strict_cl=True) if not dtwvis.test_without_visualization(): if directory: plot_margins(s[prototypeidx], weights, clfs)
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_clustering_tree_maxdist(): with util_numpy.test_uses_numpy() as np: s = np.array([ [0., 0, 1, 2, 1, 0, 1, 0, 0], [0., 1, 2, 0, 0, 0, 0, 0, 0], [1., 2, 0, 0, 0, 0, 0, 1, 1], [0., 0, 1, 2, 1, 0, 1, 0, 0], [0., 1, 2, 0, 0, 0, 0, 0, 0], [1., 2, 0, 0, 0, 0, 0, 1, 1], [1., 2, 0, 0, 0, 0, 0, 1, 1]]) def test_hook(from_idx, to_idx, distance): assert (from_idx, to_idx) in [(3, 0), (4, 1), (5, 2), (6, 2), (1, 0), (2, 0)] model = clustering.Hierarchical(dtw.distance_matrix_fast, {}, merge_hook=test_hook, show_progress=False, max_dist=0.1) modelw = clustering.HierarchicalTree(model) cluster_idx = modelw.fit(s) assert cluster_idx[0] == {0, 1, 2, 3, 4, 5, 6} if directory: hierarchy_fn = os.path.join(directory, "hierarchy.png") graphviz_fn = os.path.join(directory, "hierarchy.dot") else: file = tempfile.NamedTemporaryFile() hierarchy_fn = file.name + "_hierarchy.png" graphviz_fn = file.name + "_hierarchy.dot" if not dtwvis.test_without_visualization(): modelw.plot(hierarchy_fn) print("Figure saved to", hierarchy_fn) with open(graphviz_fn, "w") as ofile: print(modelw.to_dot(), file=ofile) print("Dot saved to", graphviz_fn)
def test_clustering_centroid(): with util_numpy.test_uses_numpy() as np: s = np.array([ [0., 0, 1, 2, 1, 0, 1, 0, 0], [0., 1, 2, 0, 0, 0, 0, 0, 0], [1., 2, 0, 0, 0, 0, 0, 1, 1], [0., 0, 1, 2, 1, 0, 1, 0, 0], [0., 1, 2, 0, 0, 0, 0, 0, 0], [1., 2, 0, 0, 0, 0, 0, 1, 1], [1., 2, 0, 0, 0, 0, 0, 1, 1]]) # def test_hook(from_idx, to_idx, distance): # assert (from_idx, to_idx) in [(3, 0), (4, 1), (5, 2), (6, 2), (1, 0), (2, 0)] model = clustering.KMedoids(dtw.distance_matrix_fast, {}, k=3, show_progress=False) cluster_idx = model.fit(s) print(cluster_idx) # assert cluster_idx[0] == {0, 1, 2, 3, 4, 5, 6} if not dtwvis.test_without_visualization(): if directory: png_fn = os.path.join(directory, "centroid.png") else: file = tempfile.NamedTemporaryFile() png_fn = file.name + "_centroid.png" model.plot(png_fn) print("Figure saved to", png_fn)
def test_controlchart(): with util_numpy.test_uses_numpy() as np: series = np.zeros((600, 60)) rsrc_fn = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'rsrc', 'synthetic_control.data') with open(rsrc_fn, 'r') as ifile: for idx, line in enumerate(ifile.readlines()): series[idx, :] = line.split() s = [] for idx in range(0, 600, 20): s.append(series[idx, :]) model = clustering.LinkageTree(dtw.distance_matrix_fast, {'parallel': True}) cluster_idx = model.fit(s) if not dtwvis.test_without_visualization(): import matplotlib.pyplot as plt if directory: hierarchy_fn = os.path.join(directory, "hierarchy.png") else: file = tempfile.NamedTemporaryFile() hierarchy_fn = file.name + "_hierarchy.png" fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(10, 10)) show_ts_label = lambda idx: "ts-" + str(idx) # show_ts_label = list(range(len(s))) def curcmap(idx): if idx % 2 == 0: return 'r' return 'g' model.plot(hierarchy_fn, axes=ax, show_ts_label=show_ts_label, show_tr_label=True, ts_label_margin=-10, ts_left_margin=10, ts_sample_length=1, ts_color=curcmap) print("Figure saved to", hierarchy_fn)
def test_linkage_tree(): with util_numpy.test_uses_numpy() as np: s = np.array([ [0., 0, 1, 2, 1, 0, 1, 0, 0], [0., 1, 2, 0, 0, 0, 0, 0, 0], [1., 2, 0, 0, 0, 0, 0, 1, 1], [0., 0, 1, 2, 1, 0, 1, 0, 0], [0., 1, 2, 0, 0, 0, 0, 0, 0], [1., 2, 0, 0, 0, 0, 0, 1, 1], [1., 2, 0, 0, 0, 0, 0, 1, 1]]) model = clustering.LinkageTree(dtw.distance_matrix_fast, {}) cluster_idx = model.fit(s) if directory: hierarchy_fn = os.path.join(directory, "hierarchy.png") graphviz_fn = os.path.join(directory, "hierarchy.dot") else: file = tempfile.NamedTemporaryFile() hierarchy_fn = file.name + "_hierarchy.png" graphviz_fn = file.name + "_hierarchy.dot" if not dtwvis.test_without_visualization(): model.plot(hierarchy_fn) print("Figure saved to", hierarchy_fn) with open(graphviz_fn, "w") as ofile: print(model.to_dot(), file=ofile) print("Dot saved to", graphviz_fn)
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_bug3(): with util_numpy.test_uses_numpy() as np: series = np.array([ np.array([1, 2, 1]), np.array([0., 1, 2, 0, 0, 0, 0, 0, 0]), np.array([1., 2, 0, 0, 0, 0, 0, 1, 1, 3, 4, 5]), np.array([0., 0, 1, 2, 1, 0, 1]), np.array([0., 1, 2, 0, 0, 0, 0, 0]), np.array([1., 2, 0, 0, 0, 0, 0, 1, 1]) ]) ds = dtw.distance_matrix(series) print(ds) model = clustering.LinkageTree(dtw.distance_matrix, {}) cluster_idx = model.fit(series) print(cluster_idx) if directory: fn = directory / "bug3.png" else: file = tempfile.NamedTemporaryFile() fn = Path(file.name + "_bug3.png") if not dtwvis.test_without_visualization(): model.plot(fn, show_ts_label=True)
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 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_trace(): with util_numpy.test_uses_numpy() as np: rsrc_fn = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'rsrc', 'Trace_TRAIN.txt') data = np.loadtxt(rsrc_fn) labels = data[:, 0] # series = data[:, 1:] series = data[labels == 1, 1:][:2,:].copy() # c = series[0, :] print(type(series)) print(series.shape) tic = time.perf_counter() avg = dtw_barycenter.dba_loop(series, c=None, max_it=100, thr=0.000001, nb_initial_samples=4, use_c=True) toc = time.perf_counter() print(f'DBA: {toc - tic:0.4f} sec') if directory and not dtwvis.test_without_visualization(): try: import matplotlib.pyplot as plt except ImportError: raise MatplotlibException("No matplotlib available") fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(10,4)) fn = directory / "test_trace_barycenter.png" for serie in series: ax[0].plot(serie, alpha=0.5) ax[1].plot(avg) fig.savefig(str(fn)) plt.close()
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_subseqsearch_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]) # print(f'{len(series)=}') k = 3 s = [] s_idx = [] w = 22 # window size ws = int(np.floor(w / 2)) # shift size wn = int(np.floor((len(series) - (w - ws)) / ws)) si, ei = 0, w for i in range(wn): s.append(series[si:ei]) s_idx.append(si) si += ws ei += ws tic = time.perf_counter() sa = subsequence_search(query, s) best = sa.kbest_matches(k=k) toc = time.perf_counter() print(f"Searching performed in {toc - tic:0.4f} seconds") # print(sa.distances) # print(best) if directory and not dtwvis.test_without_visualization(): try: import matplotlib.pyplot as plt from matplotlib import gridspec except ImportError: raise MatplotlibException("No matplotlib available") fn = directory / "test_dtw_subseqsearch_eeg.png" ymin, ymax = np.min(series), np.max(series) fig = plt.figure() if k is None: k = len(s) gs = gridspec.GridSpec(3, k, wspace=0.5, hspace=1) ax = fig.add_subplot(gs[0, 0]) ax.plot(query) ax.set_title('Query') ax.set_ylim((ymin, ymax)) for idx, match in enumerate(best): ax = fig.add_subplot(gs[1, idx]) if idx == 0: ax.set_title(f'Best {k} windows') ax.set_ylim((ymin, ymax)) ax.plot(s[match.idx]) ax = fig.add_subplot(gs[2, :]) ax.set_ylim((ymin, ymax)) ax.set_title(f'Series with windows') for idx in s_idx: ax.vlines(idx, ymin, ymax, color='grey', alpha=0.4) ax.plot(series) for idx, match in enumerate(best): ax.vlines(s_idx[match.idx], ymin, ymax, color='red') plt.savefig(fn) plt.close(fig)
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_visualisation_b(): with util_numpy.test_uses_numpy() as np: s1 = np.array([[0, 0], [0, 1], [2, 1], [0, 1], [0, 0]], dtype=np.double) s2 = np.array([[0, 0], [2, 1], [0, 1], [0, .5], [0, 0]], dtype=np.double) d1p, paths = dtw_ndim.warping_paths(s1, s2) path = dtw.best_path(paths) if not dtwvis.test_without_visualization(): fig, ax = dtwndimvis.plot_warpingpaths(s2, s1, paths, path=path) fig.show()
def test_pair(): with util_numpy.test_uses_numpy() as np: s = np.array([[0.5, 1, 2, 3, 2.0, 2.1, 1.0, 0, 0, 0], [0.4, 0, 1, 1.5, 1.9, 2.0, 0.9, 1, 0, 0]]) # s = np.array([ # [5.4407042e-01, 6.5786304e-01, 6.1123908e-01, 5.4541312e-01, 5.3215608e-01, 5.9301252e-01, 5.9528021e-01, 5.3827698e-01, 5.6342901e-01, 5.9595647e-01, 6.3291485e-01, 5.8464636e-01, 6.2318725e-01, 6.0595466e-01, 5.9037231e-01, 6.2702137e-01, 6.1348560e-01, 6.2305276e-01, 5.4601808e-01, 6.4113615e-01, 5.4939481e-01, 6.1069654e-01, 5.7797370e-01, 6.3566551e-01, 5.7421817e-01, 6.0636056e-01, 6.3805334e-01, 6.4059697e-01, 6.0581139e-01, 6.5486549e-01, 6.2933728e-01, 6.7354635e-01, 6.4083011e-01, 6.7106754e-01, 5.9539175e-01, 5.8942609e-01, 6.1587013e-01, 6.6703825e-01, 6.0380075e-01, 6.3386067e-01, 6.4719272e-01, 6.4482981e-01, 6.4368627e-01, 6.2349955e-01, 5.6237133e-01, 6.1063165e-01, 6.0773194e-01, 6.4240876e-01, 5.7791595e-01, 6.8620718e-01, 6.1297880e-01, 1.0087818e+00, 2.3082535e+00, 3.4865161e+00, 3.8273665e+00, 3.8177745e+00, 1.0119979e+00, -1.2480659e+00, -1.8793527e+00, -1.9483470e+00, -1.9013292e+00, -1.9191206e+00, -1.9221467e+00, -1.9814806e+00, -1.9719053e+00, -1.9875261e+00, -1.8793638e+00, -1.9005814e+00, -1.8825048e+00, -1.8736002e+00, -1.8876155e+00, -1.9502441e+00, -1.8904222e+00, -1.8784054e+00, -1.8606558e+00, -1.9099004e+00, -1.8661716e+00, -1.8816032e+00, -1.8314015e+00, -1.7828515e+00, -1.7619036e+00, -1.8435448e+00, -1.8210940e+00, -1.7934786e+00, -1.7384911e+00, -1.7141641e+00, -1.7810861e+00, -1.7017178e+00, -1.7586494e+00, -1.6361580e+00, -1.6761555e+00, -1.6352849e+00, -1.6648909e+00, -1.6537056e+00, -1.5992698e+00, -1.5857011e+00, -1.5706887e+00, -1.5565333e+00, -1.5257045e+00, -1.5343949e+00, -1.5144240e+00, -1.4585244e+00, -1.4340508e+00, -1.3994088e+00, -1.4395239e+00, -1.3883845e+00, -1.3141078e+00, -1.2873144e+00, -1.2437876e+00, -1.2662198e+00, -1.2174011e+00, -1.1404096e+00, -1.0779149e+00, -1.0360995e+00, -1.0235959e+00, -9.4192680e-01, -9.6460812e-01, -9.2692898e-01, -8.0692488e-01, -8.1502916e-01, -8.0227687e-01, -7.7763514e-01, -7.1609000e-01, -7.1871558e-01, -5.9853322e-01, -6.2256531e-01, -6.4287218e-01, -6.1775796e-01, -5.5449064e-01, -5.1017090e-01, -5.0237049e-01, -3.8755927e-01, -4.0832809e-01, -3.6083551e-01, -3.3675164e-01, -2.7108165e-01, -2.3104480e-01, -2.5503431e-01, -1.9585961e-01, -1.3165326e-01, -9.1146626e-02, -1.9168974e-02, -1.3373473e-01, -7.6407845e-02, -6.3702210e-02, -5.8617472e-02, -1.0619203e-02, 7.1343850e-02, 2.1105735e-02, 1.0268053e-01, 1.1936396e-01, 1.1838773e-01, 1.5021707e-01, 1.4664151e-01, 1.4904700e-01, 1.6199376e-01, 2.4104677e-01, 2.3775815e-01, 2.7550593e-01, 3.1824048e-01, 3.7817589e-01, 3.7694541e-01, 3.0430703e-01, 3.5849188e-01, 2.9257194e-01, 3.3785668e-01, 3.7219016e-01, 3.7010148e-01, 4.0480926e-01, 3.7049106e-01, 4.1597004e-01, 4.0382502e-01, 4.3679156e-01, 4.0612548e-01, 4.0152885e-01, 4.7647383e-01, 4.4169304e-01, 4.4384292e-01, 3.8545218e-01, 4.6372423e-01, 4.4630043e-01, 4.4484963e-01, 4.8374203e-01, 4.8585592e-01, 5.3424775e-01, 5.3889814e-01, 4.6434472e-01, 5.1374644e-01, 4.9446973e-01, 5.3983401e-01, 5.2213212e-01, 5.1881301e-01, 5.2453233e-01, 5.6395751e-01, 5.5313703e-01, 6.0116005e-01, 5.5151348e-01, 5.4741656e-01, 5.4669622e-01, 5.8659053e-01, 5.2656235e-01, 5.4569127e-01, 5.7221179e-01, 5.2088122e-01, 5.8197830e-01, 5.7022402e-01, 5.6669003e-01, 5.7069312e-01, 5.6999052e-01, 5.4572128e-01, 5.9778768e-01, 5.7268841e-01, 5.7180281e-01, 6.3555668e-01, 5.0059640e-01, 5.5195177e-01, 5.9130039e-01, 5.5103119e-01, 5.7287569e-01, 5.5867501e-01, 5.9223111e-01, 5.8432069e-01, 5.7091733e-01, 6.0559221e-01, 6.5693579e-01, 5.6511058e-01, 5.5240516e-01, 5.6491058e-01, 5.8311869e-01, 6.3398184e-01, 5.7997700e-01, 6.2535219e-01, 5.8981952e-01, 5.7572385e-01, 6.0549305e-01, 5.3555521e-01, 5.4941540e-01, 5.5352685e-01, 5.9538265e-01, 5.3455956e-01, 5.3054529e-01, 6.1195102e-01, 5.8604957e-01, 5.4530951e-01, 5.5091084e-01, 5.6394117e-01, 6.0954467e-01, 5.3654038e-01, 5.9055258e-01, 5.5313359e-01, 5.8818908e-01, 5.3334192e-01, 5.8794384e-01, 5.8296638e-01, 5.9814783e-01, 6.0032051e-01, 5.6657579e-01, 6.7193951e-01, 6.2128079e-01, 5.5514667e-01, 5.8124140e-01, 6.7463871e-01, 5.7606997e-01, 6.3720999e-01, 5.7822404e-01, 5.8822587e-01, 5.7788588e-01, 5.9806934e-01, 5.8369488e-01, 6.0287742e-01, 5.5426348e-01, 5.1414728e-01, 6.0377304e-01, 5.9633124e-01, 5.8322427e-01], # [5.4137206e-01, 5.7190786e-01, 5.6214670e-01, 5.2095091e-01, 5.2749375e-01, 6.0404306e-01, 5.4052105e-01, 5.8038292e-01, 5.0058210e-01, 5.9781245e-01, 5.5531829e-01, 5.0399817e-01, 4.8721503e-01, 5.2919197e-01, 5.7732146e-01, 5.8306679e-01, 5.6387021e-01, 5.1736838e-01, 5.5365038e-01, 6.3540910e-01, 5.2845206e-01, 5.6114973e-01, 5.0572623e-01, 6.4666921e-01, 5.6247629e-01, 5.6586631e-01, 5.7505220e-01, 6.1367060e-01, 5.9094675e-01, 5.4806871e-01, 5.3635967e-01, 5.8288635e-01, 6.2479649e-01, 5.4263060e-01, 5.3727177e-01, 5.4420961e-01, 4.6516908e-01, 5.1660881e-01, 5.6632066e-01, 5.8851816e-01, 5.5663496e-01, 5.3749507e-01, 6.2990795e-01, 5.2720783e-01, 5.8189400e-01, 6.1033741e-01, 5.6246110e-01, 5.9735452e-01, 5.8936942e-01, 6.2465823e-01, 5.7547395e-01, 5.7821937e-01, 6.3206557e-01, 5.9249883e-01, 5.6148297e-01, 5.9005716e-01, 5.7541260e-01, 6.4415288e-01, 6.1059152e-01, 6.0653983e-01, 6.8802537e-01, 1.0147607e+00, 2.3145332e+00, 3.5409618e+00, 3.8384987e+00, 3.8225459e+00, 3.1880896e+00, -1.3082260e+00, -1.9498738e+00, -1.9228263e+00, -2.0425930e+00, -1.9294838e+00, -2.0234096e+00, -1.9554894e+00, -1.9346159e+00, -1.9389827e+00, -1.9903105e+00, -1.9996996e+00, -1.9364940e+00, -1.9481327e+00, -1.9478419e+00, -1.9529609e+00, -1.8885611e+00, -1.9274720e+00, -1.9067925e+00, -1.8492225e+00, -1.8022711e+00, -1.8571755e+00, -1.8731676e+00, -1.7942706e+00, -1.8352960e+00, -1.8129494e+00, -1.8213582e+00, -1.7435004e+00, -1.7834703e+00, -1.6825393e+00, -1.6991139e+00, -1.6781857e+00, -1.7190823e+00, -1.6429358e+00, -1.5756199e+00, -1.6127048e+00, -1.6271342e+00, -1.5669044e+00, -1.5274913e+00, -1.5603284e+00, -1.5284419e+00, -1.4536210e+00, -1.4746732e+00, -1.4013397e+00, -1.3672767e+00, -1.4367848e+00, -1.3940244e+00, -1.3391982e+00, -1.3087654e+00, -1.2541147e+00, -1.2509465e+00, -1.1480202e+00, -1.1584576e+00, -1.0661748e+00, -1.0838297e+00, -1.0605984e+00, -1.0545557e+00, -9.8054161e-01, -9.7516375e-01, -9.0965554e-01, -9.3395351e-01, -8.3288487e-01, -8.7299559e-01, -7.9129969e-01, -7.9793562e-01, -7.7723834e-01, -6.8966425e-01, -6.8607855e-01, -6.2673536e-01, -6.0840173e-01, -5.7696618e-01, -5.3084902e-01, -5.2748290e-01, -4.3871482e-01, -4.9210561e-01, -4.3981229e-01, -3.2472595e-01, -3.3753481e-01, -2.8367354e-01, -2.7158593e-01, -2.1485086e-01, -2.3176371e-01, -1.5325167e-01, -1.9440216e-01, -1.0415893e-01, -2.4629932e-02, -4.5354485e-02, -3.6301485e-02, 2.0418909e-02, 1.0027844e-01, 8.8132967e-02, 1.2071415e-01, 2.1155213e-02, 9.2120760e-02, 9.1224315e-02, 1.5286046e-01, 1.8740585e-01, 2.0764562e-01, 2.2897012e-01, 1.6721991e-01, 2.4065580e-01, 3.4694248e-01, 1.9462176e-01, 3.1338695e-01, 2.7861734e-01, 3.0417210e-01, 2.9476327e-01, 3.3008070e-01, 3.3258469e-01, 3.5294907e-01, 3.9032386e-01, 3.7415578e-01, 3.7775534e-01, 3.8227615e-01, 4.1445601e-01, 3.7363077e-01, 4.1116205e-01, 4.1717600e-01, 3.9964844e-01, 4.0182791e-01, 3.9896304e-01, 4.6554637e-01, 4.3096288e-01, 4.7565329e-01, 4.7633896e-01, 4.7028428e-01, 4.7656642e-01, 4.2424559e-01, 5.8662101e-01, 4.7144488e-01, 5.0448089e-01, 5.3749633e-01, 4.8611158e-01, 4.8701654e-01, 4.3499943e-01, 4.9157679e-01, 4.8762772e-01, 4.5786105e-01, 5.5207149e-01, 5.5383578e-01, 5.1621105e-01, 5.3562938e-01, 5.3433077e-01, 4.8763878e-01, 5.7401483e-01, 5.9950807e-01, 5.0169837e-01, 5.7199426e-01, 5.7622109e-01, 4.9258166e-01, 5.6401024e-01, 5.8920374e-01, 5.3761485e-01, 5.5162361e-01, 5.0071393e-01, 5.1108397e-01, 5.0957378e-01, 5.0310495e-01, 5.3417705e-01, 5.6347038e-01, 5.5544608e-01, 5.0517001e-01, 5.4170490e-01, 5.5768119e-01, 6.1605762e-01, 5.2684445e-01, 5.6026650e-01, 4.9761039e-01, 5.7826229e-01, 4.9039267e-01, 5.5444683e-01, 5.5327922e-01, 5.7359620e-01, 5.3077316e-01, 5.3897197e-01, 5.1796237e-01, 5.2855358e-01, 5.1671037e-01, 5.9622412e-01, 6.1819121e-01, 5.7296705e-01, 5.5145456e-01, 5.0723387e-01, 5.5503681e-01, 5.4870379e-01, 5.1063454e-01, 5.0387017e-01, 5.2631419e-01, 5.6185307e-01, 5.5370395e-01, 5.5088067e-01, 5.0169928e-01, 5.5612542e-01, 5.0657189e-01, 5.2409481e-01, 5.2284798e-01, 5.9391410e-01, 5.6873552e-01, 5.4726018e-01, 5.8600009e-01, 6.2647159e-01, 6.0961628e-01, 5.5428459e-01, 5.3946531e-01, 5.3351539e-01, 5.5775245e-01, 5.0782548e-01, 5.1376850e-01, 5.2813152e-01] # # [6.4599204e-01, 5.8009481e-01, 6.1606415e-01, 6.1071098e-01, 6.1859521e-01, 6.1274416e-01, 5.6404645e-01, 6.2055753e-01, 5.3794194e-01, 6.3609172e-01, 6.0945935e-01, 5.5504533e-01, 5.6271107e-01, 5.5380374e-01, 6.0596144e-01, 5.6616977e-01, 6.2227118e-01, 5.9801028e-01, 6.3691294e-01, 6.5189082e-01, 5.6928840e-01, 5.9739031e-01, 6.2144372e-01, 5.5423491e-01, 6.1966872e-01, 5.9997056e-01, 5.9309788e-01, 5.9097654e-01, 6.6743532e-01, 6.2585670e-01, 6.4283290e-01, 6.1084533e-01, 6.0601246e-01, 6.5786714e-01, 6.1675659e-01, 5.8428597e-01, 5.7911015e-01, 6.1814446e-01, 6.5467570e-01, 6.2327132e-01, 6.1820943e-01, 6.7392847e-01, 6.0636712e-01, 6.4118791e-01, 6.1282180e-01, 5.8978860e-01, 6.7797797e-01, 7.1699196e-01, 1.0117528e+00, 2.2726181e+00, 3.4698210e+00, 3.8304702e+00, 3.8196334e+00, 3.2619192e+00, 1.0205306e+00, -1.2214097e+00, -1.7390308e+00, -1.8675370e+00, -1.8828808e+00, -1.9184427e+00, -1.8976587e+00, -1.8388410e+00, -1.9245830e+00, -1.8400097e+00, -1.8427256e+00, -1.8667457e+00, -1.7722318e+00, -1.8766957e+00, -1.8453581e+00, -1.7864935e+00, -1.8080930e+00, -1.8282433e+00, -1.7986122e+00, -1.8028363e+00, -1.7642486e+00, -1.7948663e+00, -1.7103064e+00, -1.7080497e+00, -1.7272006e+00, -1.7985161e+00, -1.7146590e+00, -1.6627331e+00, -1.7296342e+00, -1.6556524e+00, -1.6513342e+00, -1.6131169e+00, -1.7312787e+00, -1.6266636e+00, -1.5899808e+00, -1.5773010e+00, -1.5944990e+00, -1.5456211e+00, -1.5613456e+00, -1.5233710e+00, -1.4942540e+00, -1.4886351e+00, -1.4576059e+00, -1.4864930e+00, -1.4694330e+00, -1.3648873e+00, -1.3591080e+00, -1.3182067e+00, -1.2946644e+00, -1.3112402e+00, -1.3143085e+00, -1.3002664e+00, -1.2457161e+00, -1.1826555e+00, -1.1478405e+00, -1.1393425e+00, -1.1818354e+00, -1.0905381e+00, -1.1134195e+00, -1.0568539e+00, -1.0096635e+00, -9.9456280e-01, -9.4615698e-01, -9.6722803e-01, -8.9581465e-01, -8.5329021e-01, -8.0415690e-01, -7.8231323e-01, -7.5747249e-01, -6.9181889e-01, -6.6083636e-01, -7.1778461e-01, -6.3738983e-01, -6.5927655e-01, -5.6273476e-01, -5.4926631e-01, -4.5594428e-01, -4.7428247e-01, -4.5613309e-01, -3.5143676e-01, -3.8044406e-01, -2.8879577e-01, -2.9708038e-01, -3.0873051e-01, -2.5243644e-01, -1.8748736e-01, -1.2681093e-01, -2.0259857e-01, -1.0690861e-01, -1.5195203e-01, -9.5526833e-02, -1.2628887e-01, -3.7242313e-02, -2.3766929e-02, 4.9910492e-02, -8.1735932e-03, 2.0913314e-02, 4.7210633e-02, 6.9717876e-02, 9.9995072e-02, 1.4030909e-01, 1.7352288e-01, 1.3181121e-01, 2.1229484e-01, 1.8366278e-01, 2.1271492e-01, 2.5346393e-01, 1.9855797e-01, 2.6804769e-01, 3.0556837e-01, 3.1518281e-01, 2.9710565e-01, 3.0082077e-01, 3.3285803e-01, 3.6951418e-01, 4.2511496e-01, 3.9051354e-01, 3.3237006e-01, 3.5956342e-01, 3.1772785e-01, 3.8804607e-01, 5.0094976e-01, 4.0347956e-01, 4.5050806e-01, 4.6945845e-01, 4.2596043e-01, 4.1725340e-01, 4.5567870e-01, 4.5973729e-01, 5.3553104e-01, 5.1218158e-01, 4.8497334e-01, 4.8960637e-01, 4.9621481e-01, 4.2483759e-01, 5.0125449e-01, 5.9456671e-01, 5.3640062e-01, 5.0674797e-01, 4.9412346e-01, 5.2386933e-01, 5.6600273e-01, 5.3306648e-01, 5.2529226e-01, 5.1029361e-01, 4.9331903e-01, 5.0693239e-01, 5.1490854e-01, 5.6811201e-01, 5.7341867e-01, 5.7849439e-01, 5.2703544e-01, 5.5386148e-01, 5.8900738e-01, 5.2480575e-01, 5.6596905e-01, 5.2460600e-01, 5.7997364e-01, 6.0908832e-01, 5.8077073e-01, 5.3383030e-01, 6.1826030e-01, 5.1965089e-01, 5.9340016e-01, 5.7225953e-01, 5.7117192e-01, 5.9511521e-01, 5.1910997e-01, 6.1595223e-01, 5.4929296e-01, 5.6567802e-01, 5.6199596e-01, 5.3658150e-01, 5.3073311e-01, 5.0845425e-01, 5.2633724e-01, 6.2759642e-01, 6.0729269e-01, 6.2850204e-01, 5.5503403e-01, 6.4417801e-01, 5.9421709e-01, 5.4251513e-01, 6.3298142e-01, 6.3688128e-01, 5.5519935e-01, 6.1786844e-01, 6.0465278e-01, 5.7177749e-01, 6.0314338e-01, 5.4351271e-01, 6.1584186e-01, 5.4914328e-01, 6.5036590e-01, 5.6558653e-01, 6.4837370e-01, 5.4398120e-01, 5.8070264e-01, 6.1290281e-01, 6.1060894e-01, 5.5958162e-01, 6.4370850e-01, 5.5792888e-01, 6.4400948e-01, 6.3940254e-01, 5.1763647e-01, 6.1151425e-01, 6.1301439e-01, 5.7474884e-01, 5.9040337e-01, 5.9723567e-01, 5.7600326e-01, 6.1296410e-01, 5.3629869e-01, 6.3200031e-01, 6.4605875e-01, 6.6053779e-01, 6.1220579e-01, 5.8413897e-01, 6.4785446e-01, 5.9291996e-01] # ]) t = s.shape[1] # c = np.array([0.0, 0.5, 1.5, 2.5, 2, 2, 1, 0.5, 0, 0]) # c = np.zeros((s.shape[1],)) c = s[0, :] max_it = 1 avgs = [c] tic = time.perf_counter() avg, avgs = dtw_barycenter.dba_loop(s, c, max_it=max_it, thr=0.0001, keep_averages=True, use_c=False) toc = time.perf_counter() print(f'DBA_loop: {toc - tic:0.4f} sec') print(avg) # tic = time.perf_counter() # for it in range(max_it): # avg = dtw_barycenter.dba(s, c, use_c=True) # avgs.append(avg) # c = avg # toc = time.perf_counter() # print(f'DBA: {toc - tic:0.4f} sec') if directory and not dtwvis.test_without_visualization(): try: import matplotlib.pyplot as plt except ImportError: raise MatplotlibException("No matplotlib available") fig, ax = plt.subplots(nrows=max_it, ncols=1) fn = directory / "test_pair_barycenter.png" for it in range(len(avgs)): dtwvis.plot_average(s[0, :], s[1, :], avgs[it], None, None, ax=ax[it]) ax[it].set_title(f'Iteration {it}') fig.savefig(str(fn)) plt.close()
def test_nparray_kmeans(): with util_numpy.test_uses_numpy() as np: k = 4 max_it = 10 max_dba_it = 20 series = np.array([[0., 0, 1, 2, 1, 0, 1, 0, 0], [0., 1, 2, 0, 0, 0, 0, 0, 0], [1., 2, 0, 0, 0, 0, 0, 1, 1], [0., 0, 1, 2, 1, 0, 1, 0, 0], [0., 1, 2, 0, 0, 0, 0, 0, 0], [1., 2, 0, 0, 0, 0, 0, 1, 1]]) print(type(series)) print(series.shape) window = int(series.shape[1] * 1.0) # Perform k-means tic = time.perf_counter() model = KMeans(k=k, max_it=max_it, max_dba_it=max_dba_it, dists_options={"window": window}, initialize_with_kmedoids=False, initialize_with_kmeanspp=True) cluster_idx, performed_it = model.fit(series, use_c=True, use_parallel=False) toc = time.perf_counter() print(f'DBA ({performed_it} iterations: {toc - tic:0.4f} sec') if directory and not dtwvis.test_without_visualization(): try: import matplotlib.pyplot as plt except ImportError: raise MatplotlibException("No matplotlib available") fig, ax = plt.subplots(nrows=k, ncols=2, figsize=(10, 4), sharex='all', sharey='all') fn = directory / "test_nparray_barycenter.png" all_idx = set() for ki in range(k): ax[ki, 0].plot(model.means[ki]) for idx in cluster_idx[ki]: ax[ki, 1].plot(series[idx], alpha=0.3) if idx in all_idx: raise Exception(f'Series in multiple clusters: {idx}') all_idx.add(idx) assert (len(all_idx) == len(series)) fig.savefig(str(fn)) plt.close()
def test_normalize(): 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]) r, path1 = dtw.warp(s1, s2) path2 = dtw.warping_path(s1, s2, psi=2) if not dtwvis.test_without_visualization(): if directory: dtwvis.plot_warp(s1, s2, r, path1, filename=str(directory / "test_normalize1.png")) dtwvis.plot_warping(s1, s2, path2, filename=str(directory / "test_normalize2.png")) r_c = np.array([0., 1., 2., 2., 1., 0.5, 0., 0., 2., 1., 0., 0., 0.]) np.testing.assert_almost_equal(r, r_c, decimal=4)
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 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 not dtwvis.test_without_visualization(): 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 test_bug4(): 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]) path = dtw.warping_path(s1, s2) if directory: fn = directory / "bug4.png" else: file = tempfile.NamedTemporaryFile() fn = Path(file.name + "_bug4.png") if not dtwvis.test_without_visualization(): dtwvis.plot_warping(s1, s2, path, filename=str(fn))
def test_distance7(): with util_numpy.test_uses_numpy() as np: s = np.array([[0.0, 0.3, 0.5, 0.8, 1.0, 0.1, 0.0, 0.1], [0.0, 0.2, 0.3, 0.7, 1.1, 0.0, 0.1, 0.0], [0.1, 0.0, 1.0, 1.0, 1.0, 0.9, 0.0, 0.0], [0.0, 0.0, 1.1, 0.9, 1.0, 1.0, 0.0, 0.0], [0.0, 0.1, 1.1, 1.0, 0.9, 0.9, 0.0, 0.0], [0.0, 0.1, 1.0, 1.1, 0.9, 1.0, 0.0, 0.1], [0.0, 0.1, 0.4, 0.3, 0.2, 0.3, 0.0, 0.0], [0.1, 0.0, 0.2, 0.1, 0.2, 0.2, 0.1, 0.1]]) l = np.array([1, 1, 0, 0, 0, 0, 0, 0]) prototypeidx = 0 if directory: if not dtwvis.test_without_visualization(): plot_series(s, l, prototypeidx) savefig = str(directory / "dts.dot") else: savefig = None ml_values, cl_values, clf, imp = dtww.series_to_dt( s, l, prototypeidx, window=0, min_ig=0.01, savefig=savefig, warping_paths_fnc=dtww.warping_paths) # logger.debug(f"ml_values = {dict(ml_values)}") # logger.debug(f"cl_values = {dict(cl_values)}") weights = dtww.compute_weights_from_mlclvalues(s[prototypeidx], ml_values, cl_values, only_max=False, strict_cl=True) if directory: if not dtwvis.test_without_visualization(): plot_margins(s[prototypeidx], weights, clf, imp)
def test_pair(): with util_numpy.test_uses_numpy() as np: s = np.array([[0.5, 1, 2, 3, 2.0, 2.1, 1.0, 0, 0, 0], [0.4, 0, 1, 1.5, 1.9, 2.0, 0.9, 1, 0, 0]]) t = s.shape[1] # c = np.array([0.0, 0.5, 1.5, 2.5, 2, 2, 1, 0.5, 0, 0]) # c = np.zeros((s.shape[1],)) c = s[0, :] max_it = 1 avgs = [c] tic = time.perf_counter() avg, avgs = dtw_barycenter.dba_loop(s, c, max_it=max_it, thr=0.0001, keep_averages=True, use_c=False) toc = time.perf_counter() print(f'DBA_loop: {toc - tic:0.4f} sec') print(avg) # tic = time.perf_counter() # for it in range(max_it): # avg = dtw_barycenter.dba(s, c, use_c=True) # avgs.append(avg) # c = avg # toc = time.perf_counter() # print(f'DBA: {toc - tic:0.4f} sec') if directory and not dtwvis.test_without_visualization(): try: import matplotlib.pyplot as plt except ImportError: raise MatplotlibException("No matplotlib available") fig, ax = plt.subplots(nrows=max_it, ncols=1) fn = directory / "test_pair_barycenter.png" for it in range(len(avgs)): dtwvis.plot_average(s[0, :], s[1, :], avgs[it], None, None, ax=ax[it]) ax[it].set_title(f'Iteration {it}') fig.savefig(str(fn)) plt.close()
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 not dtwvis.test_without_visualization(): 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_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_plotbug1(): 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]) series = s1, s2 m = clustering.LinkageTree(dtw.distance_matrix, {}) m.fit(series) if not dtwvis.test_without_visualization(): if directory: hierarchy_fn = os.path.join(directory, "clustering.png") else: file = tempfile.NamedTemporaryFile() hierarchy_fn = file.name + "_clustering.png" m.plot(hierarchy_fn) print("Figure save to", hierarchy_fn)