Пример #1
0
def dtw_compare(label_json, input_json):

    with open(label_json) as f:
        label = json.load(f)

    with open(input_json) as f:
        ip_data = json.load(f)

    label_list = []
    ip_list = []
    score = []
    score_json = {}

    for _ in range(17):
        temp_x = []
        temp_y = []
        for frame in range(len(label)):
            temp = label[frame]['keypoints']
            temp_x.append(temp[_ * 3])
            temp_y.append(temp[_ * 3 + 1])
        label_list.append(temp_x)
        label_list.append(temp_y)

    for _ in range(17):
        temp_x = []
        temp_y = []
        for frame in range(len(ip_data)):
            temp = ip_data[frame]['keypoints']
            temp_x.append(temp[_ * 3])
            temp_y.append(temp[_ * 3 + 1])
        ip_list.append(temp_x)
        ip_list.append(temp_y)

    for _ in range(17):
        score_x = dtw.distance(label_list[_ * 2], ip_list[_ * 2])
        score_y = dtw.distance(label_list[_ * 2 + 1], ip_list[_ * 2 + 1])
        score_temp = []
        score_temp.append(100 - (score_x * 100))
        score_temp.append(100 - (score_y * 100))
        score.append(np.mean(score_temp))

    score_json['Total_Score'] = np.mean(score)
    score_json['Head'] = np.mean(score[0:5])
    score_json['LShoulder'] = score[5]
    score_json['RShoulder'] = score[6]
    score_json['LElbow'] = score[7]
    score_json['RElbow'] = score[8]
    score_json['LWrist'] = score[9]
    score_json['RWrist'] = score[10]
    score_json['LHip'] = score[11]
    score_json['RHip'] = score[12]
    score_json['LKnee'] = score[13]
    score_json['Rknee'] = score[14]
    score_json['LAnkle'] = score[15]
    score_json['RAnkle'] = score[16]

    for (k, v) in score_json.items():
        print(k, ' : ', v, '\n')

    return score_json
Пример #2
0
def test_overflowdistance():
    maxvalthirtytwobit = 2147483647
    s = np.array([
        [maxvalthirtytwobit, maxvalthirtytwobit, 1, 2, 1, 0, 1, 0, 0],
        [1., 2, 0, 0, 0, 0, 0, 1, 0]])
    d1 = dtw.distance(s[0], s[1], use_c=False)
    d2 = dtw.distance(s[0], s[1], use_c=True)
    print(d1)
    print(d2)
Пример #3
0
def test_penalty_cyclicalshift():
    s1 = np.array([0., 1, 2, 1, 0, 1, 2, 1, 0, 1, 2, 1, 0])
    s2 = np.array([2., 1, 0, 1, 2, 1, 0, 1, 2, 1, 0, 1, 2])
    # plt.plot(s1)
    # plt.plot(s2)
    # plt.show(block=True)
    d1 = dtw.distance(s1, s2)
    d2 = dtw.distance(s1, s2, penalty=1)
    assert (d1) == pytest.approx(math.sqrt(10))
    assert (d2) == pytest.approx(math.sqrt(14))
Пример #4
0
def get_distances(keypoints1, keypoints2):

    distances = list({i: {
        "dist_x": [],
        "dist_y": []
    }} for i in range(len(keypoints1)))

    for i in range(len(distances)):
        distances[i][i]["dist_x"].append(
            dtw.distance(keypoints1[i][i]["x"], keypoints2[i][i]["x"]))
        distances[i][i]["dist_y"].append(
            dtw.distance(keypoints1[i][i]["y"], keypoints2[i][i]["y"]))

    return distances
Пример #5
0
def test_psi_dtw_1b():
    with util_numpy.test_uses_numpy() as np:
        x = np.arange(0, 20, .5)
        s1 = np.sin(x)
        s2 = np.sin(x - 1)
        d = dtw.distance(s1, s2, psi=2)
        np.testing.assert_equal(d, 0.0)
Пример #6
0
def get_dtw_wrapping_path(data, col1, col2):
    '''
    input: 
        data: dataframe 源数据
        col1: 列名
        col2:列名
    output: 输出图像,上方是col1,下方是col2 
    '''
    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_for_dtw_zscore_T[col1,:]
    #     y = array_for_dtw_zscore_T[col2,:]
    x = array_subset_zscore_T[x_idx, :]
    y = array_subset_zscore_T[y_idx, :]
    path = dtw.warping_path(x, y)
    outname = col1 + 'vs' + col2
    ds_xy = dtw.distance(x, y)
    dtwvis.plot_warping(
        x,
        y,
        path,
        filename=
        "D:/Pythoncode/JD_mart/operation_flow_distribution/DTW_for_business/results/%s.png"
        % outname)
    print("%s 和 %s 的DTW距离: %2.4f" % (col1, col2, ds_xy))
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)
Пример #8
0
def filter_cuts(shifted_observed_runs: List[int],
                expected_runs: List[int]) -> Tuple[List[int], List[int], int]:
    """
    Applies dynamic time warping to select cuts in the observed pixel sequence of whitespace runs according to the expected runs
    :param shifted_observed_runs: histogram of whitespace runs encoded as follows: position = start of run, value = length of run
    :param expected_runs: histogram of expected word cuts (see: `expected_runs_for_line`)
    :return: A triplet `(cuts, cuts_indices, distance)` where:
    * cuts: list of x-coordinates of word cuts
    * cuts_indices: indices of selected cuts
    * distance: DTW distance between the two input sequences
    """
    path = dtw.warping_path(expected_runs, shifted_observed_runs)
    distance = dtw.distance(expected_runs, shifted_observed_runs)

    runs_indices = [i for i, x in enumerate(shifted_observed_runs) if x > 0]
    runs_indices.insert(0, 0)

    cuts = []
    cuts_indices = []
    for i, j in path:
        if expected_runs[i] > 0:

            cuts.append(j)
            index_found = j in runs_indices
            if not index_found:
                print(
                    f"DTW associated expected peak in {i} to zero value in {j}",
                    file=sys.stderr)
            else:
                cuts_indices.append(runs_indices.index(j))

    return cuts, cuts_indices, distance
Пример #9
0
def test_distance3_a():
    dist_opts = {"penalty": 0.005, "max_step": 0.011, "window": 3}
    s = np.array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.005, 0.01, 0.015, 0.02, 0.01, 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
    p = np.array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.005, 0.01, 0.015, 0.02, 0.01, 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
    d1 = dtw.distance(s, p, **dist_opts)
    d2 = dtw_c.distance_nogil(s, p, **dist_opts)
    assert d1 == pytest.approx(d2)
Пример #10
0
def _calculate_dtw_over_time_window(window, minimum_close, maximum_close,
                                    pattern, start_window, end_window,
                                    threshold):
    calculations = []

    minimum_value_pattern, maximum_value_pattern = pattern.get_min_max()

    normalized_data = [
        normalize_function_paper(close_price, minimum_close, maximum_close,
                                 minimum_value_pattern, maximum_value_pattern)
        for close_price in window
    ]
    # distance, path = fastdtw(normalized_data, pattern.get_pattern(), dist=euclidean)
    # alignment = dtw(normalized_data, pattern.get_pattern(), keep_internals=True)

    distance = dtw.distance(normalized_data, pattern.get_pattern())
    path = dtw.warping_path(normalized_data, pattern.get_pattern())

    if distance < threshold:

        calculations.append({
            "pattern_name": pattern.__class__.__name__,
            "start_window": start_window,
            "start_date": list_of_dates[start_window],
            "end_window": end_window,
            "end_date": list_of_dates[end_window],
            "distance": distance,
            "path": path,
            "normalized_data": normalized_data,
            "pattern": pattern.get_pattern()
        })

    return calculations
Пример #11
0
def dtwDistance(mainInput, targetInput, mainFile, targetFile):
    distance = dtw.distance(mainInput, targetInput)
    resultDict = {}
    resultDict['mainFile'] = mainFile
    resultDict['targetFile'] = targetFile
    resultDict['distance'] = distance
    return resultDict
Пример #12
0
def dtwdis(input_points, model_points, i, j):
    input_pts = input_points.reshape(2 * j,)
    model_pts = model_points.reshape(2 * i,)
    model_pts = model_pts / np.linalg.norm(model_pts)
    input_pts = input_pts / np.linalg.norm(input_pts)

    return percentage_score(dtw.distance(model_pts, input_pts))
Пример #13
0
def test_distance1_b():
    dist_opts = {}
    s1 = np.array([0., 0.01, 0., 0.01, 0., 0., 0., 0.01, 0.01, 0.02, 0., 0.])
    s2 = np.array([0., 0.02, 0.02, 0., 0., 0.01, 0.01, 0., 0., 0., 0.])
    d1 = dtw.distance(s1, s2, **dist_opts)
    d2 = dtw_c.distance_nogil(s1, s2, **dist_opts)
    assert d1 == d2
    assert d1 == pytest.approx(0.02)
Пример #14
0
def test_distance2_c():
    dist_opts = {}
    s1 = np.array([0.0, 0.0, 2.0, 1.0, 1.0, 0.0, 0.0])
    s2 = np.array([0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0])
    d1 = dtw.distance(s1, s2, **dist_opts)
    d2 = dtw_c.distance_nogil(s1, s2, **dist_opts)
    assert d1 == d2
    assert d1 == pytest.approx(1.0)
Пример #15
0
def test_distance2_c():
    with util_numpy.test_uses_numpy() as np:
        dist_opts = {}
        s1 = np.array([0.0, 0.0, 2.0, 1.0, 1.0, 0.0, 0.0])
        s2 = np.array([0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0])
        d1 = dtw.distance(s1, s2, **dist_opts)
        d2 = dtw.distance_fast(s1, s2, **dist_opts)
        assert d1 == d2
        assert d1 == pytest.approx(1.0)
Пример #16
0
def nearest_to_centroid(xs):
    centroid = mean(xs)

    dists = []
    for x in xs:
        dists.append(dtw.distance(x, centroid))
    min_i = np.argmin(dists)

    return xs[min_i]
Пример #17
0
def test_distance2_bb():
    dist_opts = {'max_step': 0.1}
    s1 = np.array([0.0, 0.0, 2.0, 1.0, 1.0, 0.0, 0.0])
    s2 = np.array([0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0])
    d1 = dtw.distance(s1, s2, **dist_opts)
    d2 = dtw_c.distance_nogil(s1, s2, **dist_opts)
    print(d1, d2)
    assert d1 == d2
    assert d1 == pytest.approx(np.inf)
Пример #18
0
def test_distance1_a():
    # dist_opts = {'max_dist': 0.201, 'max_step': 0.011, 'max_length_diff': 8, 'window': 3}
    dist_opts = {'window': 3}
    s1 = np.array([0., 0.01, 0., 0.01, 0., 0., 0., 0.01, 0.01, 0.02, 0., 0.])
    s2 = np.array([0., 0.02, 0.02, 0., 0., 0.01, 0.01, 0., 0., 0., 0.])
    d1 = dtw.distance(s1, s2, **dist_opts)
    d2 = dtw_c.distance_nogil(s1, s2, **dist_opts)
    assert d1 == d2
    assert d1 == pytest.approx(0.02)
Пример #19
0
def test_distance2_bb():
    with util_numpy.test_uses_numpy() as np:
        dist_opts = {'max_step': 0.1}
        s1 = np.array([0.0, 0.0, 2.0, 1.0, 1.0, 0.0, 0.0])
        s2 = np.array([0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0])
        d1 = dtw.distance(s1, s2, **dist_opts)
        d2 = dtw.distance_fast(s1, s2, **dist_opts)
        print(d1, d2)
        assert d1 == d2
        assert d1 == pytest.approx(np.inf)
Пример #20
0
def test_distance1_a():
    with util_numpy.test_uses_numpy() as np:
        # dist_opts = {'max_dist': 0.201, 'max_step': 0.011, 'max_length_diff': 8, 'window': 3}
        dist_opts = {'window': 3}
        s1 = np.array(
            [0., 0.01, 0., 0.01, 0., 0., 0., 0.01, 0.01, 0.02, 0., 0.])
        s2 = np.array([0., 0.02, 0.02, 0., 0., 0.01, 0.01, 0., 0., 0., 0.])
        d1 = dtw.distance(s1, s2, **dist_opts)
        d2 = dtw.distance_fast(s1, s2, **dist_opts)
        print("X")
        assert d1 == d2
        assert d1 == pytest.approx(0.02)
Пример #21
0
    def computeSimilaryByDTW(self):
        if util.getDayCounts(self._df) < 49:
            print("days: ", util.getDayCounts(self._df))
            print("The duration is too short!!!")
            return

        self._dayDf, self._weekDf = util.getRecordByDay(self._df)

        if self.isEmptyGas():
            return

        #print(self_dayDf)

        weekMean = util.getWeekMeanValue(self._weekDf)
        weekMean = util.normalizeByZeroOne(weekMean)

        dis = [
            dtw.distance(weekMean, features.Stable_Feature),
            dtw.distance(weekMean, features.Concave_Feature),
            dtw.distance(weekMean, features.Convexity_Feature),
            dtw.distance(weekMean, features.Slop_Feature),
            dtw.distance(weekMean, features.Slop_Reverse_Feature),
            dtw.distance(weekMean, features.V_Feature)
        ]

        for i in range(len(dis)):
            if dis[i] < self._similar:
                self._similar = dis[i]
                self._category = i
Пример #22
0
def test_distance1_b():
    with util_numpy.test_uses_numpy() as np:
        dist_opts = {}
        s1 = np.array(
            [0., 0.01, 0., 0.01, 0., 0., 0., 0.01, 0.01, 0.02, 0., 0.])
        s2 = np.array([0., 0.02, 0.02, 0., 0., 0.01, 0.01, 0., 0., 0., 0.])
        d1 = dtw.distance(s1, s2, **dist_opts)
        d2 = dtw.distance_fast(s1, s2, **dist_opts)
        d3, wps = dtw.warping_paths(s1, s2, **dist_opts)
        print(np.power(wps, 2))
        assert d1 == d2
        assert d1 == d3
        assert d1 == pytest.approx(0.02)
Пример #23
0
def distance_control(kpi_series_normal, kpi_series_abnormal):
    time_series_normal = []
    for row in kpi_series_normal:
        temp = float(row[1])
        time_series_normal.append(temp)
    print(time_series_normal)
    time_series_abnormal = []
    for row in kpi_series_abnormal:
        temp = float(row[1])
        time_series_abnormal.append(temp)
    print(time_series_abnormal)
    distance = dtw.distance(time_series_normal, time_series_abnormal)
    print(distance)
    return distance
Пример #24
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")
Пример #25
0
def find_dynamic_time_warp(df, time_var, y_var, end_training_period):

    texto(' ')
    st.markdown('---')
    texto(f'Dynamic time warp con {y_var}', 17)
    texto('Sugerencia: series con mayor distancia son diferentes entre sí.',
          12, 'grey')
    df_filtered = df.query(f'{time_var} <= "{end_training_period}"')
    new_frame = pd.DataFrame(
        index=[m for m in df.columns if m != time_var and 'scaled' not in m])
    for col in df.columns:
        if 'scaled' not in col and col != time_var:
            new_frame.loc[col, 'dtw distance'] = dtw.distance(
                df_filtered[y_var].values, df_filtered[col].values)
    new_frame.sort_values(by='dtw distance', ascending=False, inplace=True)

    st.write(new_frame)
Пример #26
0
def test_distance3_a():
    with util_numpy.test_uses_numpy() as np:
        dist_opts = {"penalty": 0.005, "max_step": 0.011, "window": 3}
        s = np.array([
            0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.005, 0.01,
            0.015, 0.02, 0.01, 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
            0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
            0.
        ])
        p = np.array([
            0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.005, 0.01, 0.015,
            0.02, 0.01, 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
            0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.
        ])
        d1 = dtw.distance(s, p, **dist_opts)
        d2 = dtw.distance_fast(s, p, **dist_opts)
        assert d1 == pytest.approx(d2)
Пример #27
0
def compute_dwt_dist_between_ts_and_list(single_ts, ts_list, r):
    """
    This function computes the list of DTW distances between a single time-series and a list of time-series.

    :param single_ts: single time-series
    :type single_ts: 1d array
    :param ts_list: list of time-series
    :type ts_list: list of 1d array
    :param r: distance upper bound  (for DTW distance computation)
    :type r: float
    :return: list of DTW distances
    :rtype: list of float
    """
    dist_list = []
    for ts in ts_list:
        dist = dtw.distance(single_ts, ts, max_dist=r)
        dist_list.append(dist)
    return dist_list
Пример #28
0
def getdisance(data, datalen):
    # print data
    dismatrix = [[0 for i in range(datalen)] for i in range(datalen)]

    for i in range(datalen):
        for j in range(datalen):
            use_dtw = 0
            dis = 0
            if use_dtw == 0:
                for l in range(len(data[0])):
                    dis = dis + (data[i][l] - data[j][l]) * (data[i][l] -
                                                             data[j][l])
                dis = math.sqrt(dis)
            else:
                dis = dtw.distance(data[i], data[j])
            dismatrix[i][j] = dis
            dismatrix[j][i] = dis
    return dismatrix
Пример #29
0
def make_sequence(k, model, dist_type, df_test, label_df_train, label_df_test):
    arr_test_full, arr_test_norm = get_array(df_test, 24)

    for i in range(len(arr_test_full)):
        distance = []
        for cn in range(k):
            if dist_type == 'ed':
                dist = np.linalg.norm(arr_test_full[i] -
                                      model.cluster_centers_[cn])
            else:
                dist = dtw.distance(arr_test_full[i],
                                    model.cluster_centers_[cn])
            distance.append(dist)
        cluster = np.argmin(distance)
        label_df_test['label'][i] = cluster
    cluster_sequence = pd.concat([label_df_train, label_df_test], axis=0)
    cluster_sequence.sort_index(inplace=True)

    return cluster_sequence
Пример #30
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)