How to use the dtaidistance.dtw.best_path function in dtaidistance

To help you get started, we’ve selected a few dtaidistance examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github wannesm / dtaidistance / tests / test_warping.py View on Github external
def test_normalize2():
    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)
github wannesm / dtaidistance / tests / test_dtw2d.py View on Github external
def test_visualisation_a():
    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)
    fig, ax = dtwvis.plot_warping(s1, s2, path)
    fig.show()
github wannesm / dtaidistance / tests / test_dtw2d.py View on Github external
def test_visualisation_b():
    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)
    fig, ax = dtwvis.plot_warpingpaths(s2, s1, paths, path=path)
    fig.show()
github wannesm / dtaidistance / tests / test_warping.py View on Github external
def test_twoleadecg_1(directory=None):
    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"))
github wannesm / dtaidistance / tests / test_bugsvis.py View on Github external
def test_bug2():
    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)
    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)
github wannesm / dtaidistance / dtaidistance / dtw_weighted.py View on Github external
if ignore_idxs is None:
        ignore_idxs = set()
    features = [[0] * (len(series[prototypeidx]) * 2)]  # feature per idx, split in positive and negative
    targets = [0]  # Do cluster
    ml_values = defaultdict(lambda: ([], []))

    # print(f"prototype idx = {prototypeidx}")
    for idx, label in enumerate(labels):
        if idx in ignore_idxs:
            continue
        cur_features = np.zeros(len(series[prototypeidx]) * 2, dtype=np.double)
        cur_features_cnt = np.zeros(len(series[prototypeidx]) * 2, dtype=np.int)
        wp_params = {key: kwargs[key] for key in {'window', 'max_dist', 'max_step', 'max_length_diff',
                     'penalty', 'psi'}.intersection(kwargs.keys())}
        s, paths = warping_paths_fnc(series[prototypeidx], series[idx], **wp_params)
        path = best_path(paths)
        for i_to, i_from in path:
            d = series[prototypeidx][i_to] - series[idx][i_from]
            # print(f"d{idx}({i_to},{i_from}) = {d}")
            if label == 0:
                # Cannot-link
                pass
            elif label == 1:
                # Must-link
                if d < 0:
                    ml_values[i_to][0].append(-d)
                elif d > 0:
                    ml_values[i_to][1].append(d)
            if d <= 0:
                cur_features[i_to * 2] += -d
                cur_features_cnt[i_to * 2] += 1
            if d >= 0: