예제 #1
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
예제 #2
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()
예제 #3
0
def test_distance1_a():
    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)
        d1 = dtw_ndim.distance(s1, s2)
        d1p, paths = dtw_ndim.warping_paths(s1, s2)
        # print(d1, d1p)
        assert d1 == pytest.approx(d1p)
예제 #4
0
def test_distance1_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)
    d1 = dtw_ndim.distance(s1, s2)
    print(d1)
    d1p, paths = dtw_ndim.warping_paths(s1, s2)
    print(d1p)
    print(paths)
    assert d1 == pytest.approx(d1p)
예제 #5
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()
예제 #6
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
예제 #7
0
def test_distance2():
    test_A = [[2.65598039, 0.93622549, -0.04169118],
              [2.02625, 0.965625, -0.050625],
              [0.41973039, 0.85968137, 0.38970588],
              [0.3669697, -0.03221591, 0.09734848],
              [0.68905971, -0.65101381, -0.70944742],
              [0.14142316, -0.81769481, -0.44556277],
              [0.2569697, -0.6330303, -0.35503788],
              [-0.39339286, 0.02303571, -0.48428571],
              [-0.8275, -0.02125, -0.0325],
              [-0.65293269, -0.29504808, 0.30557692]]

    test_B = [[-1.54647436e+00, -3.76602564e-01, -8.58974359e-01],
              [-1.14283907e+00, -8.50961538e-01, -5.42974022e-01],
              [-4.86715587e-01, -8.62221660e-01, -6.32211538e-01],
              [3.54672740e-02, -4.37500000e-01, -4.41801619e-01],
              [7.28618421e-01, -4.93421053e-03, -8.90625000e-01],
              [1.03525641e+00, 1.25000000e-01, -8.50961538e-01],
              [5.24539474e-01, 1.07828947e-01, -3.99375000e-01],
              [5.04464286e-01, 3.76275510e-01, -6.74744898e-01],
              [1.20897959e+00, 1.10793367e+00, -1.45681122e+00],
              [8.70535714e-01, 8.73724490e-01, -1.01275510e+00]]

    with util_numpy.test_uses_numpy() as np:
        test_A = np.array(test_A)
        test_B = np.array(test_B)

        d1 = dtw_ndim.distance(test_A, test_B, use_c=False)
        d2 = dtw_ndim.distance(test_A, test_B, use_c=True)

        d3, paths = dtw_ndim.warping_paths(test_A, test_B)
        d4, paths = dtw_ndim.warping_paths(test_A, test_B, use_c=True)

        assert d1 == pytest.approx(d2)
        assert d1 == pytest.approx(d3)
        assert d1 == pytest.approx(d4)
예제 #8
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