def get_distance_matrix(self): """! @brief Calculates distance matrix (U-matrix). @details The U-Matrix visualizes based on the distance in input space between a weight vector and its neighbors on map. @return (list) Distance matrix (U-matrix). @see show_distance_matrix() @see get_density_matrix() """ distance_matrix = [[0.0] * self._cols for i in range(self._rows)] for i in range(self._rows): for j in range(self._cols): neuron_index = i * self._cols + j if self._conn_type == type_conn.func_neighbor: self._create_connections(type_conn.grid_eight) for neighbor_index in self._neighbors[neuron_index]: distance_matrix[i][j] += dtw_ndim.distance( self._weights[neuron_index], self._weights[neighbor_index], window=self._dtw_params.window, max_step=self._dtw_params.max_step, max_length_diff=self._dtw_params.max_length_diff, ) distance_matrix[i][j] /= len(self._neighbors[neuron_index]) return distance_matrix
def compute_ndim_dwt_dist_between_ts_and_list(single_ts, ts_list, max_dist=None): dist_list = [] for ts in ts_list: dist = dtw_ndim.distance(single_ts, ts, max_dist=max_dist) dist_list.append(dist) return dist_list
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)
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)
def make_pairwise_distance(seqs): dtw_pairs=make_dtw_pairs(seqs) names=list(seqs.keys()) n_ts=len(names) for i in range(1,n_ts): print(i) for j in range(0,i): name_i,name_j=names[i],names[j] distance_ij=dtw_ndim.distance(seqs[name_i],seqs[name_j]) dtw_pairs.set(name_i,name_j,distance_ij) dtw_pairs.set(name_j,name_i,distance_ij) return dtw_pairs
def make_pairwise_distance(ts_dataset): names = list(ts_dataset.keys()) n_ts = len(names) pairs_dict = {name_i: {name_i: 0.0} for name_i in names} for i in range(1, n_ts): print(i) for j in range(0, i): name_i, name_j = names[i], names[j] distance_ij = dtw_ndim.distance(ts_dataset[name_i], ts_dataset[name_j]) pairs_dict[name_i][name_j] = distance_ij pairs_dict[name_j][name_i] = distance_ij return pairs_dict
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)
def dtwdis_new(self, model_points, input_points): return self.percentage_score( dtw_ndim.distance(model_points, input_points))