Exemplo n.º 1
0
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)
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
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()
Exemplo n.º 4
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_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)
Exemplo n.º 6
0
    def projected_warp_path(curve, refCurve):
        
        refCurve = scaler.fit_transform(refCurve.reshape(-1,1)).squeeze()
        curve = scaler.fit_transform(curve.reshape(-1,1)).squeeze()
        
        dist, cumcost = dtw.warping_paths_fast(refCurve, curve)
        steps = dtw.best_path(cumcost) # return steps of warp path
        
        # define Euclidean line l
        l_start = np.array([0,0])
        l_end = np.array([len(refCurve)-1,len(curve)-1])
        l = LineString([l_start, l_end])
        
        # calculate orthogonal distance to l (sign corresponds to direction of projection) 
        dist_orthogonal = np.cross(l_end-l_start,steps-l_start)/np.linalg.norm(l_end-l_start)
        
        # calculate coordinates z on l of orthogonal projections
        z = list()
        for step in steps:
            p = Point(step)
            z.append(l.project(p))
        z = np.array(z)/l.length*100
        
        # map projected warp path to vector of length 100
        f = interpolate.interp1d(z, dist_orthogonal)    
        mapped_projection = f(np.arange(0,100))

        return dist, mapped_projection
Exemplo n.º 7
0
    def _set_dtw_path_and_distance_dic_list(self, x):
        """!
        @brief Calculates and sets the list of dictionaries with the DTW distance and best path between each neuron and
        the input pattern x. Thus the length of the list is equal to the size of the SOM network.

        @param[in] x (list): Input pattern from the input data set. It should be a list of arrays/floats, where each
        array/float is an observation of the one-dimensional/multidimensional time-series window given as input pattern.
        """
        dtw_dic_list = []

        for neuron_index in range(self._size):
            neuron_dist, neuron_paths = dtw_ndim.warping_paths(
                self._weights[neuron_index],
                x,
                window=self._dtw_params.window,
                max_dist=self._dtw_params.max_dist,
                max_step=self._dtw_params.max_step,
                max_length_diff=self._dtw_params.max_length_diff,
            )
            best_path = dtw.best_path(neuron_paths)
            matching_dic = {
                i: []
                for i in range(len(self._weights[neuron_index]))
            }
            for i, j in best_path:
                matching_dic[i].append(j)
            neuron_dic = {
                "index": neuron_index,
                "dist": neuron_dist,
                "matching_dic": matching_dic,
            }

            dtw_dic_list.append(neuron_dic)
        self.current_dtw_dic_list = dtw_dic_list
Exemplo n.º 8
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)
Exemplo n.º 9
0
def get_lead_DTW_euclid(x, y):
    # x = xy[:,0]
    # y = xy[:,1]
    d, paths = dtw.warping_paths(x, y, window=None, psi=None)
    best_path = dtw.best_path(paths)
    # # dtwvis.plot_warpingpaths( x,  y, paths, best_path)
    lead_E = create_lead_from_path(best_path, x, 0)
    return (lead_E, d)
Exemplo n.º 10
0
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()
Exemplo n.º 11
0
def cal_warping_path(input_seq, template, mask=None):
    if mask is None:
        mask = np.ones(feature_len)
    distance, dtw_mat = dtw_ndim.warping_paths(input_seq*mask, template*mask)
    path = dtw.best_path(dtw_mat)

    # dtw_ndim_visualisation.plot_warping(input_seq, template, path, filename='plot_warping.png')

    # print(distance/len(path))
    return distance/len(path), path
Exemplo n.º 12
0
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"))
Exemplo n.º 13
0
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)
Exemplo n.º 14
0
def test(amplitude, center, width, noise, target_norm, len_a, window):
    source_norm = amplitude * norm.pdf(range(0, 400), center, width)
    source_norm = np.random.normal(source_norm, scale=noise)

    d, paths = dtw.warping_paths(target_norm, source_norm, window, psi=0)
    best_path = dtw.best_path(paths)
    paths = np.array(best_path)

    euclidean = d
    init_euclidean = abs(paths[:, 0] - paths[:, 1])
    amplitude, center, width, noise = str(amplitude), str(center), str(
        width), str(noise)
    return euclidean, init_euclidean
Exemplo n.º 15
0
def best_match_ts(query, ts_dict):
    """
    Applying the brute-force pattern to find the best match for the query from the set of the ts_dict

    :param: query list
    :param: ts_dict

    best_match: (type of dict) {key: time_series_ID, value: time_series_data, distance: similarity_distance(DTW Algorithm),
                                best_path: side-product from dtw algorithm}
    :return: best_match_list [{bm_dict1}, {bm_dict2}, {bm_dict3}], .....]
    """
    query_len = len(query)

    best_match_list = []

    best_of_so_far = float('inf')

    start_time = datetime.datetime.now()

    for key, value in ts_dict.items():
        candidates = slice_list(value, query_len)

        candidates.sort(
            key=lambda each: get_distance(dtw.warping_paths, each, query))

        for i in range(len(candidates)):
            distance, paths = dtw.warping_paths(query, candidates[i])

            if distance < best_of_so_far:
                best_match = dict()

                best_of_so_far = distance
                best_match['ts_id'] = key
                best_match['value'] = candidates[i]
                best_match['distance'] = distance
                best_match['best_path'] = dtw.best_path(paths)

                best_match_list.append(best_match)

            else:
                break

    for match in best_match_list:
        if match['distance'] > best_of_so_far:
            best_match_list.remove(match)

    end_time = datetime.datetime.now()
    print("Time period of the execution for the brute_force: " +
          str((end_time - start_time).microseconds) + "ms")

    return best_match_list
Exemplo n.º 16
0
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")
Exemplo n.º 17
0
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"))
Exemplo n.º 18
0
    def compare_34dim(self, new_video_coordinates, reference_coordinates, i, j,
                      weights):
        # new_video_coordinates = self.normalize(new_video_coordinates)
        new_video_coordinates = new_video_coordinates.reshape(i, 34)
        reference_coordinates = reference_coordinates.reshape(j, 34)
        best_path = dtw.best_path(
            dtw_ndim.warping_paths(new_video_coordinates,
                                   reference_coordinates)[1])
        for body_part in range(17):
            dtw_visualisation.plot_warping(
                new_video_coordinates[:, 2 * body_part],
                reference_coordinates[:, 2 * body_part], best_path,
                "warp" + str(2 * body_part) + ".png")
            dtw_visualisation.plot_warping(
                new_video_coordinates[:, 2 * body_part + 1],
                reference_coordinates[:, 2 * body_part + 1], best_path,
                "warp" + str(2 * body_part + 1) + ".png")
        # Calculating euclidean distance per body part to apply weights
        # max_frames = max(i, j)
        body_part_scores = []
        for body_part_i in range(17):
            v1_part, v2_part = [False] * len(best_path) * 2, [
                False
            ] * len(best_path) * 2
            print(len(v1_part), len(v2_part), len(best_path))
            print(best_path)
            for k, t in enumerate(best_path):
                new_frame, reference_frame = t
                v1_part[k * 2] = new_video_coordinates[new_frame][body_part_i *
                                                                  2]
                v1_part[k * 2 +
                        1] = new_video_coordinates[new_frame][body_part_i * 2 +
                                                              1]
                v2_part[k *
                        2] = reference_coordinates[reference_frame][body_part_i
                                                                    * 2]
                v2_part[k * 2 + 1] = reference_coordinates[reference_frame][
                    body_part_i * 2 + 1]
            v1_part = v1_part / np.linalg.norm(v1_part)
            v2_part = v2_part / np.linalg.norm(v2_part)
            score = self.percentage_score(ed.distance(v1_part, v2_part))
            print(score)
            body_part_scores.append(score)

        return self.apply_weights(weights, body_part_scores), body_part_scores
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)
Exemplo n.º 20
0
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)
Exemplo n.º 21
0
    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")
Exemplo n.º 22
0
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)
Exemplo n.º 23
0
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 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)
Exemplo n.º 25
0
#!/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])
Exemplo n.º 26
0
def test(amplitude, center, width, noise, sdhalf, target_norm,len_a,window,global_max, ii):
    
    b = 0
    
    source_norm1 = skewnorm.pdf(np.arange(0,201),b,200,40)
    source_norm2 = skewnorm.pdf(np.arange(200,400),b,200,sdhalf)
    
    source_norm = np.concatenate((source_norm1[0:200],source_norm2))
    
    t,s = target_norm,source_norm
    
    source_global_norm1,source_global_norm2 = source_norm1/global_max,source_norm2/global_max
    source_global_norm2 = source_global_norm2 - abs(min(target_norm[201:400])-min(source_global_norm2))
    source_global_norm2 = source_global_norm2/max(source_global_norm2)
    source_global_norm = np.concatenate((source_global_norm1[0:200],source_global_norm2))
    
    
    source_norm1,source_norm2 = source_norm1/max(source_norm1),source_norm2/max(source_norm2)
    
    source_norm2 = source_norm2 - abs(min(target_norm[201:400])-min(source_norm2))
    
    source_norm2 = source_norm2/max(source_norm2)
    
    source_norm = np.concatenate((source_norm1[0:200],source_norm2))
    
    
    #total_intensity = (abs(source_norm)).sum()
    
    '''
    Choose the normalisation method by commenting/uncommenting below
    '''
    
    target_norm = target_norm/max(abs(target_norm))
    
    '''
    target_norm = preprocessing.normalize([target_norm])
    target_norm = target_norm.reshape((400,))
    
    source_norm = preprocessing.normalize([source_norm])
    source_norm = source_norm.reshape((400,))
    '''
    
    source_norm = np.random.normal(source_norm, scale=noise)
    
    
    '''
    '''
    
    total_intensity1 = (source_norm).sum()
    total_intensity2 = (target_norm).sum()

    total_intensity = abs(total_intensity1 - total_intensity2)
    
    total_intensity1 = (abs(source_norm)).sum()
    
    target,source = deriv(target_norm),deriv(source_global_norm)
    
    target.insert(0,target[0])
    target.insert(len(target),target[len(target)-1])
    source.insert(0,source[0])
    source.insert(len(source),source[len(source)-1])
    
    target,source = np.asarray(target),np.asarray(source)
    
    D = dtw.distance_fast(target,source,window,psi=0)
    
    
    
    
    target_deriv,source_deriv = deriv(target_norm),deriv(source_norm)
    
    target_deriv.insert(0,target_deriv[0])
    target_deriv.insert(len(target_deriv),target_deriv[len(target_deriv)-1])
    source_deriv.insert(0,source_deriv[0])
    source_deriv.insert(len(source_deriv),source_deriv[len(source_deriv)-1])
    
    target_deriv,source_deriv = np.asarray(target_deriv),np.asarray(source_deriv)
    
    d, paths = dtw.warping_paths_fast(target_deriv, source_deriv, window, psi=0)
    best_path = dtw.best_path(paths)
    paths = np.array(best_path)
    
    
    '''
    
    euclidean = pow((target_norm[paths[:,0]] - source_norm[paths[:,1]]),2)
    euclidean = np.sqrt(euclidean.sum())
    
    '''
    
    euclidean = abs(t[paths[:,0]] - s[paths[:,1]])
    euclidean = euclidean.sum()
    
    
    
    
    C = ['#000000','#FF0000','#FFFF00','#008000','#0000FF','#FF00FF','#C0C0C0','#800000','#808000','#00FFFF','#800080','#808080','#00FF00','#008080','#000080']
    
    
    
    '''
    plt.figure(2)
    plt.scatter(ii,euclidean,c=C[ii%15])
    plt.xlabel('increasing noise from left to right, the colours represent the different standard deviations as explained by the colour key')
    plt.title('warping cost')
    '''
    
    #n = noise/0.01
    
    #euclidean = np.exp(-euclidean*(total_intensity/D))
    euclidean = np.exp(-(euclidean/(total_intensity1/D))-total_intensity/(total_intensity2)*D)
    
    #euc1 = np.exp(-euclidean/(total_intensity1/D))
    #euc2 = np.exp(-total_intensity/(total_intensity1)*D)
    
    init_euclidean = abs(paths[:,0]-paths[:,1])
    
    amplitude,center,width,noise = str(amplitude),str(center),str(width),str(noise)
    
    
    plt.figure(5)
    plt.plot(range(0,400),s,c=C[ii%15])
    plt.plot(range(0,400),t,'k-')
    plt.title('source and target plots before normalisation and with noise')
    
    '''
    plt.figure(6)
    plt.plot(range(0,400),source_no_noise,c=C[ii%15])
    plt.plot(range(0,400),t,'k')
    plt.title('source and target plots before normalisation and without noise')
    '''
    
    plt.figure(7)
    plt.plot(range(0,400),source_norm,c=C[ii%15])
    plt.plot(range(0,400),target_norm,'k-')
    plt.title('source and target plots after normalisation and with noise')
    
    '''
    plt.figure(8)
    plt.plot(range(0,400),source_deriv,c=C[ii%15])
    plt.plot(range(0,400),target_deriv,'k-')
    plt.title('source and target derivative plots after normalisation')
    '''
    
    plt.figure(3)
    plt.scatter(ii,total_intensity1,c=C[ii%15])
    plt.xlabel('increasing noise from left to right, the colours represent the different standard deviations as explained by the colour key')
    plt.title('total intensity')
    
    '''
    plt.figure(4)
    plt.scatter(ii,D,c=C[ii%15])
    plt.xlabel('increasing noise from left to right, the colours represent the different standard deviations as explained by the colour key')
    plt.title('D')
    '''
    
    return euclidean, init_euclidean
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()
Exemplo n.º 28
0
def test(amplitude, center, width, noise, background, target_norm,len_a,window,global_max, ii):
    
    
    source_no_noise = amplitude*norm.pdf(range(0,400),center,width)
    source_no_noise = np.random.normal(source_no_noise, scale=0)
    
    source_max = max(abs(source_no_noise))
    
    background = -source_max/2
    #background = 0.04
    
    source_no_noise = source_no_noise + background
    
    source_max = max(abs(source_no_noise))
    
    source_norm = amplitude*norm.pdf(range(0,400),center,width)
    source_norm = np.random.normal(source_norm, scale=noise)
    
    source_norm = source_norm + background
    
    source_global_norm = source_norm/global_max
    
    #total_intensity = (abs(source_norm)).sum()
    
    t,s = target_norm,source_norm
    
    '''
    Choose the normalisation method by commenting/uncommenting below
    '''
    
    target_norm,source_norm = target_norm/max(abs(target_norm)),source_norm/source_max
    
    '''
    target_norm = preprocessing.normalize([target_norm])
    target_norm = target_norm.reshape((400,))
    
    source_norm = preprocessing.normalize([source_norm])
    source_norm = source_norm.reshape((400,))
    '''
    
    
    '''
    '''
    
    total_intensity = (abs(source_norm)).sum()
    
    target,source = deriv(target_norm),deriv(source_global_norm)
    
    target.insert(0,target[0])
    target.insert(len(target),target[len(target)-1])
    source.insert(0,source[0])
    source.insert(len(source),source[len(source)-1])
    
    target,source = np.asarray(target),np.asarray(source)
    
    D = dtw.distance_fast(target,source,window,psi=0)
    
    
    
    
    target_deriv,source_deriv = deriv(target_norm),deriv(source_norm)
    
    target_deriv.insert(0,target_deriv[0])
    target_deriv.insert(len(target_deriv),target_deriv[len(target_deriv)-1])
    source_deriv.insert(0,source_deriv[0])
    source_deriv.insert(len(source_deriv),source_deriv[len(source_deriv)-1])

    
    target_deriv,source_deriv = np.asarray(target_deriv),np.asarray(source_deriv)
    
    d, paths = dtw.warping_paths_fast(target_deriv, source_deriv, window, psi=0)
    best_path = dtw.best_path(paths)
    paths = np.array(best_path)
    
    
    '''
    
    euclidean = pow((target_norm[paths[:,0]] - source_norm[paths[:,1]]),2)
    euclidean = np.sqrt(euclidean.sum())
    
    '''
    
    euclidean = abs(t[paths[:,0]] - s[paths[:,1]])
    euclidean = euclidean.sum()
    
    
    
    
    C = ['#000000','#FF0000','#FFFF00','#008000','#0000FF','#FF00FF','#C0C0C0','#800000','#808000','#00FFFF','#800080','#808080','#00FF00','#008080','#000080']
    
    
    
    
    plt.figure(2)
    plt.scatter(ii,euclidean,c=C[ii%15])
    plt.xlabel('increasing noise from left to right, the colours represent the different standard deviations as explained by the colour key')
    plt.title('warping cost')
    
    
    euclidean = np.exp(-euclidean/(total_intensity/D))
    init_euclidean = abs(paths[:,0]-paths[:,1])
    amplitude,center,width,noise = str(amplitude),str(center),str(width),str(noise)
    
    
    plt.figure(5)
    plt.plot(range(0,400),s,c=C[ii%15])
    plt.plot(range(0,400),t,'k-')
    plt.title('source and target plots before normalisation and with noise')
    
    
    plt.figure(6)
    plt.plot(range(0,400),source_no_noise,c=C[ii%15])
    plt.plot(range(0,400),t,'k')
    plt.title('source and target plots before normalisation and without noise')
    
    
    plt.figure(7)
    plt.plot(range(0,400),source_norm,c=C[ii%15])
    plt.plot(range(0,400),target_norm,'k-')
    plt.title('source and target plots after normalisation and with noise')
    
    
    plt.figure(8)
    plt.plot(range(0,400),source_deriv,c=C[ii%15])
    plt.plot(range(0,400),target_deriv,'k-')
    plt.title('source and target derivative plots after normalisation')
    
    
    plt.figure(3)
    plt.scatter(ii,total_intensity,c=C[ii%15])
    plt.xlabel('increasing noise from left to right, the colours represent the different standard deviations as explained by the colour key')
    plt.title('total intensity')
    
    
    plt.figure(4)
    plt.scatter(ii,D,c=C[ii%15])
    plt.xlabel('increasing noise from left to right, the colours represent the different standard deviations as explained by the colour key')
    plt.title('D')
    
    
    return euclidean, init_euclidean
Exemplo n.º 29
0
def test(amplitude, center, width, noise, background, target_norm,len_a,window):
    source_norm = amplitude*norm.pdf(range(0,400),center,width)
    source_norm = np.random.normal(source_norm, scale=noise)
    
    background = (max(abs(source_norm)))/2
    
    
    '''
    plt.figure(5)
    plt.plot(range(0,400),source_norm)
    plt.plot(range(0,400),target_norm,'k-')
    '''
    
    
    source_norm = source_norm + background
    
    
    '''
    plt.figure(6)
    plt.plot(range(0,400),source_norm)
    plt.plot(range(0,400),target_norm,'k-')
    '''
    
    
    '''
    Choose the normalisation method by commenting/uncommenting below
    '''
    
    target_norm,source_norm = target_norm/max(abs(target_norm)),source_norm/max(abs(source_norm))
    
    '''
    target_norm = preprocessing.normalize([target_norm])
    target_norm = target_norm.reshape((400,))
    
    source_norm = preprocessing.normalize([source_norm])
    source_norm = source_norm.reshape((400,))
    '''
    
    
    '''
    '''
    
    target_deriv,source_deriv = deriv(target_norm),deriv(source_norm)
    
    target_deriv.insert(0,0)
    target_deriv.insert(len(target_deriv),0)
    source_deriv.insert(0,0)
    source_deriv.insert(len(source_deriv),0)
    
    target_deriv,source_deriv = np.asarray(target_deriv),np.asarray(source_deriv)
    
    d, paths = dtw.warping_paths_fast(target_deriv, source_deriv, window, psi=0)
    best_path = dtw.best_path(paths)
    paths = np.array(best_path)
    
    total_intensity = (abs(source_norm)).sum()
    
    euclidean = pow((target_norm[paths[:,0]] - source_norm[paths[:,1]]),2)
    euclidean = np.sqrt(euclidean.sum())
    euclidean = np.exp(-euclidean/total_intensity)
    init_euclidean = abs(paths[:,0]-paths[:,1])
    amplitude,center,width,noise = str(amplitude),str(center),str(width),str(noise)
    
    plt.figure(1)
    plt.plot(range(0,400),source_norm)  #label='Altered Gaussian: mean='+center+', sd='+width+', amplitude='+amplitude+', noise='+noise
    plt.plot(range(0,400),target_norm,'k-')
    #plt.legend()
    
    plt.figure(2)
    plt.plot(range(0,400),source_deriv)
    plt.plot(range(0,400),target_deriv,'k-')
    
    return euclidean, init_euclidean
Exemplo n.º 30
0
def test(amplitude, center, width, noise, target_norm,len_a,window,global_max, ii):
    
    
    source_no_noise = amplitude*norm.pdf(range(0,400),center,width)
    source_no_noise = np.random.normal(source_no_noise, scale=0)
    
    source_max = max(abs(source_no_noise))
    
    source_norm = amplitude*norm.pdf(range(0,400),center,width)
    source_norm = np.random.normal(source_norm, scale=noise)
    
    source_global_norm = source_norm/global_max
    
    total_intensity = (abs(source_norm)).sum()
    
    t,s = target_norm,source_norm
    
    '''
    Choose the normalisation method by commenting/uncommenting below
    '''
    
    target_norm,source_norm = target_norm/max(abs(target_norm)),source_norm/source_max
    
    '''
    target_norm = preprocessing.normalize([target_norm])
    target_norm = target_norm.reshape((400,))
    
    source_norm = preprocessing.normalize([source_norm])
    source_norm = source_norm.reshape((400,))
    '''
    
    
    '''
    '''
    
    
    
    target,source = deriv(target_norm),deriv(source_global_norm)
    
    target.insert(0,0)
    target.insert(len(target),0)
    source.insert(0,0)
    source.insert(len(source),0)
    
    target,source = np.asarray(target),np.asarray(source)
    
    D = dtw.distance_fast(target,source,window,psi=0)
    
    
    
    
    target_deriv,source_deriv = deriv(target_norm),deriv(source_norm)
    
    target_deriv.insert(0,0)
    target_deriv.insert(len(target_deriv),0)
    source_deriv.insert(0,0)
    source_deriv.insert(len(source_deriv),0)
    
    target_deriv,source_deriv = np.asarray(target_deriv),np.asarray(source_deriv)
    
    d, paths = dtw.warping_paths_fast(target_deriv, source_deriv, window, psi=0)
    best_path = dtw.best_path(paths)
    paths = np.array(best_path)
    
    
    '''
    
    euclidean = pow((target_norm[paths[:,0]] - source_norm[paths[:,1]]),2)
    euclidean = np.sqrt(euclidean.sum())
    
    '''
    
    euclidean = abs(t[paths[:,0]] - s[paths[:,1]])
    euclidean = euclidean.sum()
    
    
    plt.figure(3)
    plt.scatter(ii,euclidean)
    plt.title('warping cost')
    
    
    euclidean = np.exp(-euclidean/(total_intensity/D))
    init_euclidean = abs(paths[:,0]-paths[:,1])
    amplitude,center,width,noise = str(amplitude),str(center),str(width),str(noise)
    
    
    plt.figure(1)
    plt.plot(range(0,400),source_norm)
    
    
    plt.figure(2)
    plt.plot(range(0,400),source_deriv)
    plt.plot(range(0,400),target_deriv,'k-')
    
    
    plt.figure(4)
    plt.scatter(ii,total_intensity)
    plt.title('total intensity')
    
    
    plt.figure(5)
    plt.scatter(ii,D)
    plt.title('D')
    
    
    return euclidean, init_euclidean, target_norm