Ejemplo n.º 1
0
def prepare_data(exp_data, num_data):
    x = np.arange(len(exp_data))
    y = exp_data
    exp_data = np.zeros((len(x), 2))
    exp_data[:, 0] = x
    exp_data[:, 1] = y

    x = np.arange(len(num_data))
    y = num_data
    num_data = np.zeros((len(x), 2))
    num_data[:, 0] = x
    num_data[:, 1] = y

    # quantify the difference between the two curves using PCM
    pcm = 0  # similaritymeasures.pcm(exp_data, num_data)

    # quantify the difference between the two curves using
    # Discrete Frechet distance
    df = 0  # similaritymeasures.frechet_dist(exp_data, num_data)

    # quantify the difference between the two curves using
    # area between two curves
    area = 0  # similaritymeasures.area_between_two_curves(exp_data, num_data)

    # quantify the difference between the two curves using
    # Curve Length based similarity measure
    cl = 0  # similaritymeasures.curve_length_measure(exp_data, num_data)

    # quantify the difference between the two curves using
    # Dynamic Time Warping distance
    dtw, d = similaritymeasures.dtw(exp_data, num_data)

    return pcm, df, area, cl, dtw, d
Ejemplo n.º 2
0
def similarity(sketch, other_sketch):
    """
    sketch = sketchpad json output
    other_sketch = saved json output from Django 
    """
    result = {}
    if isinstance(sketch, dict) and isinstance(other_sketch, dict):
        x, y = create_xy_coords('search', sketch)
        P = np.array([x, y]).T

        x1, y1 = create_xy_coords('other', other_sketch)
        Q = np.array([x1, y1]).T
        dh, ind1, ind2 = directed_hausdorff(P, Q)
        df = similaritymeasures.frechet_dist(P, Q)
        dtw, d = similaritymeasures.dtw(P, Q)
        pcm = similaritymeasures.pcm(P, Q)
        area = similaritymeasures.area_between_two_curves(P, Q)
        cl = similaritymeasures.curve_length_measure(P, Q)

        result = {
            "dh": dh,
            "df": df,
            "dtw": dtw,
            "pcw": pcm,
            "cl": cl,
            "area": area
        }
        return result
    else:
        return {"dh": 0}
def dynamic_time_warping_distance(series1, series2):
    """
    Compute the Dynamic Time Warping distance between two series

    Dynamic time warping is an algorithm used to measure similarity between
    two sequences which may vary in time or speed.
    It works as follows:
        1. Divide the two series into equal points.
        2. Calculate the euclidean distance between the first point in the
            first series and every point in the second series. Store the minimum
            distance calculated. (this is the ‘time warp’ stage)
        3. Move to the second point and repeat 2. Move step by step along points
            and repeat 2 till all points are exhausted.
        4. Repeat 2 and 3 but with the second series as a reference point.
        5. Add up all the minimum distances that were stored and this is a
            true measure of similarity between the two series.

    Args:
        series1 (numpy.ndarray): First series
        series2 (numpy.ndarray): Second series

    Returns:
        Dynamic time warping distance between the two series
    """
    series1_2d = np.zeros((len(series1), 2))
    series1_2d[:, 0] = range(len(series1))
    series1_2d[:, 1] = series1

    series2_2d = np.zeros((len(series2), 2))
    series2_2d[:, 0] = range(len(series2))
    series2_2d[:, 1] = series2

    return similaritymeasures.dtw(series1_2d, series2_2d)[0]
Ejemplo n.º 4
0
def dtw_distance(ref_trj_l, pred_tr_l):
    n_trj = len(pred_tr_l)

    error = 0
    for pred_tr, ref_trj in zip(pred_tr_l, ref_trj_l):
        dtw_dist, d = similaritymeasures.dtw(ref_trj,pred_tr)
        error+= dtw_dist
    error_n = error/n_trj
    return error_n
    def dtw_compare(self, test_1_states: list, test_2_states: list):
        """ The dtw function requires at least two dimensional inputs, for that 1d bins need a second axis that
        represents time.

        :param test_1_states: States for test one, at least 2 dimensions
        :param test_2_states: States for test two, at least 2 dimensions
        :return: Value of dtw
        """
        import similaritymeasures
        d_dtw, _ = similaritymeasures.dtw(test_1_states, test_2_states)
        return d_dtw
Ejemplo n.º 6
0
	def check_similarity(self, song1, option1, option2, sar=16000):
		song1, sr1 = self.loadAudio(song1, sar)
		opt1, sr2 = self.loadAudio(option1, sar)
		opt2, sr3 = self.loadAudio(option2, sar)
		x = range(0,5117)
		song1 = [x, song1[-5117:]]
		opt1 = [x, opt1[-5117:]]
		opt2 = [x, opt2[-5117:]]

		nsong1 = np.array(song1).T
		nopt1 = np.array(opt1).T
		nopt2 = np.array(opt2).T

		print(np.shape(nopt1))

		dtw1, d = similaritymeasures.dtw(opt1, song1)
		dtw2, d = similaritymeasures.dtw(opt2, song1)
		#print("DTW")
		#print(dtw1, dtw2)

		#dtw1, d = fastdtw(nopt1, nsong1, dist=euclidean)
		#dtw2, d = fastdtw(nopt2, nsong1, dist=euclidean)
		#print("fastDTW")
		#print(dtw1, dtw2)
		#dtw1 = similaritymeasures.frechet_dist(nopt1, nsong1)
		#dtw2 = similaritymeasures.frechet_dist(nopt2, nsong1)
		#print("fastDTW")
		#print(dtw1, dtw2)

		#dh1, _, _ = directed_hausdorff(nopt1, nsong1)
		#dh2, _, _ = directed_hausdorff(nopt2, nsong1)
		#print("Directed Hausdorff")
		#print(dh1, dh2)

		#dtw1 = similaritymeasures.pcm(nopt1, nsong1)
		#dtw2 = similaritymeasures.pcm(nopt2, nsong1)
	
		print(dtw1, dtw2)

		return dtw1, dtw2
Ejemplo n.º 7
0
def shape_similarity_measures_all_to_all_unoptimized(data_dict: dict):
    """ Calculates different predefined shape similarity measures from https://pypi.org/project/similaritymeasures/
    Requires the roads to be saved as polylines

    :param data_dict: Dict containing all the test data
    :return: None
    """
    import similaritymeasures
    import time
    start_time = time.time()
    current_ops = 0
    total_ops = len(data_dict)
    print("In total", total_ops * total_ops, "comparison passes and",
          total_ops,
          "loop iterations will have to be completed for shape based input.")

    for name1 in data_dict:
        road1 = data_dict.get(name1)
        road1_coords = road1.get(RoadDicConst.COORD_TUPLE_REP_ALLIGNED.value,
                                 None)
        if road1_coords is None:
            add_coord_tuple_representation(data_dict=data_dict)
            road1_coords = road1.get(
                RoadDicConst.COORD_TUPLE_REP_ALLIGNED.value, None)

        # TODO more
        dicc_dtw = {}
        dicc_dtw_opti = {}
        dicc_frechet = {}
        for name2 in data_dict:
            # TODO optimize
            road2 = data_dict.get(name2)
            road2_coords = road2.get(
                RoadDicConst.COORD_TUPLE_REP_ALLIGNED.value, None)

            d_dtw, _ = similaritymeasures.dtw(road1_coords, road2_coords)
            dicc_dtw[name2] = d_dtw

            d_frechet = similaritymeasures.frechet_dist(
                road1_coords, road2_coords)
            dicc_frechet[name2] = d_frechet

        road1[BehaviorDicConst.COORD_DTW_DIST.value] = dicc_dtw
        road1[BehaviorDicConst.COORD_FRECHET_DIST.value] = dicc_frechet

        current_ops += 1
        print_remaining_time(start_time=start_time,
                             completed_operations=current_ops,
                             total_operations=total_ops)
Ejemplo n.º 8
0
    def dtw_computation(self, gpx_data, gold):

        ### time difference of start to finish
        delta_time = gpx_data[3][-1] - gpx_data[3][0]

        ### interpolate activity
        gpx_data_interpolated = self.interpolate(gpx_data)

        ### compute dynamic time warping
        dtw, d = similaritymeasures.dtw(gpx_data_interpolated, gold)

        print("\nDTW (y): %2.5f" % (dtw))
        print("T [s]:  ", (delta_time))

        return dtw, delta_time
Ejemplo n.º 9
0
	def compare_frechet(self):
		x = np.arange(0, 10*np.pi, np.pi/10)
		s1 = np.cos(x)
		s2 = np.cos(x + np.pi/2)


		plt.plot(s1)
		plt.plot(s2)
		plt.show()

		s1 = np.array([x, s1]).T
		s2 = np.array([x, s2]).T
		dist,_ = similaritymeasures.dtw(s1, s2)


		print(dist)
Ejemplo n.º 10
0
def distance(ovitrap_eggs_i,lwO):
    if(sys.argv[2]==RMSE):
        return utils.rmse(ovitrap_eggs_i[ovitrap_eggs_i!=[None]], lwO[ovitrap_eggs_i!=[None]])
    elif(sys.argv[2]==D):
        return utils.D(ovitrap_eggs_i[ovitrap_eggs_i!=[None]], lwO[ovitrap_eggs_i!=[None]])
    elif(sys.argv[2] in [FRECHET,DTW]):
        ovitrap_eggs_i=np.array(ovitrap_eggs_i,dtype=np.float)#this change None for np.nan
        valid_ovi_idx=~np.isnan(ovitrap_eggs_i)
        reversed_valid_ovi_idx=valid_ovi_idx[::-1]
        first,last=np.argmax(valid_ovi_idx), len(reversed_valid_ovi_idx)-np.argmax(reversed_valid_ovi_idx)-1
        x=np.array([[time_range[idx],lwO[idx]] for idx in range(first,last)])
        y=np.array([ [time_range[idx],ovitrap_eggs_i[idx] ] for idx,isValid in enumerate(valid_ovi_idx) if isValid])
        if(sys.argv[2]==FRECHET): return sm.frechet_dist(x,y)
        if(sys.argv[2]==DTW): return sm.dtw(x,y)[0]
    else:
        print('Metric %s not found'%sys.argv[2])
        quit()
Ejemplo n.º 11
0
def get_best_blur(
    evt_file,
    numiter,
    region_file,
    blur_vals,
    radius_100p,
    binsize=0.2,
    spectrum="core_flux_chart.dat",
    out_root_common="core_psf",
):

    xcen, ycen, ra, dec = get_centroids(evt_file, region_file)
    print(xcen, ycen)

    ecf_fractions = [
        0.01,
        0.025,
        0.05,
        0.075,
        0.1,
        0.2,
        0.3,
        0.4,
        0.5,
        0.6,
        0.7,
        0.8,
        0.85,
        0.9,
        0.95,
        0.99,
    ]
    n_fractions = len(ecf_fractions)
    ecf_profile_values = np.zeros((blur_vals.shape[0], n_fractions),
                                  dtype=float)
    ecf_radii_values = np.zeros((blur_vals.shape[0], n_fractions), dtype=float)

    # generate the profile of the core
    obs_ecf_file = "obs_ecf.fits"
    ecf_calc.punlearn()
    ecf_calc(
        infile=evt_file,
        outfile="obs_ecf.fits",
        xpos=xcen,
        ypos=ycen,
        radius=radius_100p,
        binsize=binsize,
        clobber=True,
        fraction=ecf_fractions,
    )

    for idx, blur in enumerate(blur_vals):
        print(f"Blur={blur}")
        outroot = out_root_common + "_blur_" + str(blur)
        if not Path(outroot + "_projrays.fits").is_file():
            print(f"Simulating PSF")

            simulate_psf(
                infile=evt_file,
                outroot=outroot,
                spectrum=spectrum,
                numiter=numiter,
                ra=ra,
                dec=dec,
                binsize=binsize,
                blur=blur,
            )

            print("Generating the ecf profile")
        out_ecf = generate_ecf_profile(
            outroot + "_projrays.fits",
            blur,
            xcen,
            ycen,
            binsize=binsize,
            ecf_fractions=ecf_fractions,
            radius=radius_100p,
        )
        load_table(idx, out_ecf, colkeys=["r_mid", "fraction"])
        ecf_profile = get_data(idx)
        ecf_profile_values[idx, :] = ecf_profile.y
        ecf_radii_values[idx, :] = ecf_profile.x
        plt.plot(ecf_profile.x, ecf_profile.y, label=f"blur={blur}")
        print("Done")

    load_table(idx + 1, obs_ecf_file, colkeys=["r_mid", "fraction"])
    obs_ecf = get_data(idx + 1)
    plt.plot(obs_ecf.x,
             obs_ecf.y,
             label=f"Observation",
             ls="--",
             lw=2,
             color="black")

    plt.legend()
    plt.xlabel("Radius (ACIS pixels)")
    plt.ylabel("ECF")
    plt.show()

    dtw_vals = np.zeros(idx + 1)
    pcm_vals = np.zeros(idx + 1)
    clm_vals = pcm_vals.copy()
    abtc_vals = pcm_vals.copy()
    for i in range(idx + 1):
        n1 = np.zeros((n_fractions, 2))
        n2 = n1.copy()
        n1[:, 0] = ecf_radii_values[i, :]
        n2[:, 0] = obs_ecf.x
        n1[:, 1] = ecf_profile_values[i, :]
        n2[:, 1] = obs_ecf.y
        dtw_vals[i], _ = similaritymeasures.dtw(n1, n2)
        pcm_vals[i] = similaritymeasures.pcm(n1, n2)
        clm_vals[i] = similaritymeasures.curve_length_measure(n1, n2)
        abtc_vals[i] = similaritymeasures.area_between_two_curves(n1, n2)

    print(f"Blur (Dynamic time warping) {blur_vals[np.argmin(dtw_vals)]}")
    print(f"Blur (Partial Curve Mapping): {blur_vals[np.argmin(pcm_vals)]}")
    print(f"Blur (Curve Length Measure): {blur_vals[np.argmin(clm_vals)]}")
    print(f"Blur (Area Curve Measure): {blur_vals[np.argmin(abtc_vals)]}")
Ejemplo n.º 12
0
 def test_c5_c6_dtw(self):
     r, _ = similaritymeasures.dtw(curve5, curve6)
     self.assertTrue(np.isclose(r, 9000.0))
Ejemplo n.º 13
0
 def test_P_Q_dtw(self):
     r, _ = similaritymeasures.dtw(P, Q)
     self.assertTrue(r, 3.0)
Ejemplo n.º 14
0
 def test_random_dtw(self):
     r, d = similaritymeasures.dtw(curve_a_rand, curve_b_rand)
     path = similaritymeasures.dtw_path(d)
     c = cdist(curve_a_rand, curve_b_rand)
     cost = np.sum(c[path[:, 0], path[:, 1]])
     self.assertTrue(np.isclose(r, cost))
Ejemplo n.º 15
0
 def test_P_Q_dtw_minkowski_p3(self):
     r, _ = similaritymeasures.dtw(P, Q, metric='minkowski', p=3)
     self.assertTrue(r, 3.0)
Ejemplo n.º 16
0
def main():
    a = [(0.39, 0.8), (0.3, 0.45), (0.35, 0.78), (0.4, 0.3), (0.49, 0.8),
         (0.51, 0.23), (0.6, 0.71)]

    dtws_xy = []
    dtws_aal = []
    frechets_xy = []
    frechets_aal = []

    angles = range(0, 360, 30)

    for i, angle in enumerate(angles):
        b = rotate(a, angle)

        dtw_xy, d_xy = dtw(a, b)
        dtws_xy.append(dtw_xy)

        fd_xy = frechet_dist(a, b)
        frechets_xy.append(fd_xy)

        # Convert the trajectories a and b to the trajectories in the AAL space.
        aal_a, aal_b = to_aal([a, b])

        dtw_aal, d_aal = dtw(aal_a, aal_b, metric=d_warp)
        dtws_aal.append(dtw_aal)

        fd_aal = frechet_dist(aal_a, aal_b)
        frechets_aal.append(fd_aal)

        a_angles, a_arc_lengths = zip(*aal_a)
        b_angles, b_arc_lengths = zip(*aal_b)

        f1 = plt.figure(1)
        plt.subplot(4, 3, i + 1)
        plt.plot(a_arc_lengths, a_angles, label="T")
        plt.plot(b_arc_lengths, b_angles, label="Q")
        plt.legend(prop={'size': 6})
        plt.xlabel("Arc-length", fontsize=8)
        plt.ylabel("Angle", fontsize=8)
        plt.xticks(np.linspace(0, 1, num=5),
                   np.linspace(0, 1, num=5),
                   fontsize=8)
        plt.yticks([-np.pi, 0, np.pi],
                   labels=[u"-\u03C0", "0", "\u03C0"],
                   fontsize=8)
        plt.title("Angle={0:.1f} (rad)".format(np.radians(angle)), fontsize=8)
        f1.tight_layout()

        a_xs, a_ys = zip(*a)
        b_xs, b_ys = zip(*b)

        plt.figure(2)
        plt.subplot(4, 3, i + 1, aspect='equal', adjustable='box')
        plt.plot(a_xs, a_ys, label="T")
        plt.plot(b_xs, b_ys, label="Q")
        plt.xlabel("x", fontsize=8)
        plt.ylabel("y", fontsize=8)
        plt.title("Angle={0:.1f} (rad)".format(np.radians(angle)), fontsize=8)
        plt.xticks(np.linspace(0, 1, num=3),
                   np.linspace(0, 1, num=3),
                   fontsize=8)
        plt.yticks(np.linspace(0, 1, num=5),
                   np.linspace(0, 1, num=5),
                   fontsize=8)

    plt.tight_layout()

    angles = [np.radians(x) for x in angles]
    plt.figure(3)
    plt.plot(angles, dtws_xy, label="DTW")
    plt.plot(angles, frechets_xy, label="Frechet")
    plt.title("XY space")
    plt.xlabel("Rotation Angle")
    plt.ylabel("Distances")
    plt.legend(prop={'size': 6})

    plt.figure(4)
    plt.plot(angles, dtws_aal, label="DTW")
    plt.plot(angles, frechets_aal, label="Frechet")
    plt.title("AAL space")
    plt.xlabel("Rotation Angle")
    plt.ylabel("Distances")
    plt.legend(prop={'size': 6})

    plt.show()
Ejemplo n.º 17
0
def stats_between_series(
    xaxis_1: pandas.Series,
    values_1: pandas.Series,
    xaxis_2: pandas.Series,
    values_2: pandas.Series,
    print_: bool = False,
) -> dict:
    """Dynamic time warping and discret frechet distance for measuring similarity between two temporal sequences

    Args:
        xaxis_1 (pandas.Series): index axis of the dataframe 1
        values_1 (pandas.Series): value axis of the dataframe 1
        xaxis_2 (pandas.Series): index axis of the dataframe 2
        values_2 (pandas.Series): value axis of the dataframe 2

    Returns:
        dict: `{"dtw": float, "frechet_dist": float}`
    """

    dataframe_1 = pandas.merge(xaxis_1,
                               values_1,
                               right_index=True,
                               left_index=True)
    dataframe_2 = pandas.merge(xaxis_2,
                               values_2,
                               right_index=True,
                               left_index=True)

    dataframe_1.rename(columns={
        xaxis_1.name: "id",
        values_1.name: "values_1"
    },
                       inplace=True)
    dataframe_2.rename(columns={
        xaxis_2.name: "id",
        values_2.name: "values_2"
    },
                       inplace=True)

    dataframe_1.set_index("id", inplace=True)
    dataframe_2.set_index("id", inplace=True)

    unified = pandas.concat([dataframe_1, dataframe_2], axis=1)
    unified["values_1"] = (pandas.to_numeric(
        unified["values_1"], errors="coerce",
        downcast="float").interpolate().fillna(method="bfill").fillna(
            method="ffill"))
    unified["values_2"] = (pandas.to_numeric(
        unified["values_2"], errors="coerce",
        downcast="float").interpolate().fillna(method="bfill").fillna(
            method="ffill"))

    xaxis_arranged = numpy.arange(len(unified))
    dataframe_values_2 = numpy.array(
        [xaxis_arranged, unified["values_2"].values])
    dataframe_values_1 = numpy.array(
        [xaxis_arranged, unified["values_1"].values])

    dtw, d = similaritymeasures.dtw(dataframe_values_1, dataframe_values_2)

    frechet_dist = similaritymeasures.frechet_dist(dataframe_values_1,
                                                   dataframe_values_2)

    pcm = similaritymeasures.pcm(dataframe_values_1, dataframe_values_2)

    area = similaritymeasures.area_between_two_curves(dataframe_values_1,
                                                      dataframe_values_2)

    std = numpy.abs(
        numpy.nanstd(dataframe_values_2[1]) -
        numpy.nanstd(dataframe_values_1[1]))

    if print_:
        print(
            {
                "dtw": dtw,
                "frechet_dist": frechet_dist,
                "pcm": pcm,
                "area": area,
                "std": std,
            },
            dataframe_values_2,
        )

    return {
        "dtw": dtw,
        "frechet_dist": frechet_dist,
        "pcm": pcm,
        "area": area,
        "std": std,
    }
Ejemplo n.º 18
0
 def test_P_Q_dtw_path(self):
     r, d = similaritymeasures.dtw(P, Q)
     path = similaritymeasures.dtw_path(d)
     c = cdist(P, Q)
     cost = sum(c[path[:, 0], path[:, 1]])
     self.assertTrue(np.isclose(r, cost))
Ejemplo n.º 19
0
def main():
    a = generate_sine_coords(7)

    dtws_xy = []
    dtws_aal = []
    frechets_xy = []
    frechets_aal = []

    num_points = range(14, 7 * 20, 7)

    for num in num_points:
        b = generate_sine_coords(num)

        dtw_xy, d_xy = dtw(a, b)
        dtws_xy.append(dtw_xy)

        fd_xy = frechet_dist(a, b)
        frechets_xy.append(fd_xy)

        # Convert the two curves, a and b, to the AAL space.
        a_aal, b_aal = to_aal([a, b])

        dtw_aal, d_aal = dtw(a_aal, b_aal, metric=d_warp)
        dtws_aal.append(dtw_aal)

        fd_aal = frechet_dist(a_aal, b_aal)
        frechets_aal.append(fd_aal)

        a_angles, a_arc_lengths = zip(*a_aal)
        b_angles, b_arc_lengths = zip(*b_aal)

        plt.figure(1)
        plt.plot(a_arc_lengths, a_angles, label="a")
        plt.plot(b_arc_lengths, b_angles, label="b")
        plt.xlabel("Arc-length", fontsize=8)
        plt.ylabel("Angle", fontsize=8)
        plt.title("AAL Space", fontsize=8)

        a_xs, a_ys = zip(*a)
        b_xs, b_ys = zip(*b)

        plt.figure(2)
        plt.plot(a_xs, a_ys, label="a")
        plt.plot(b_xs, b_ys, label="b")
        plt.xlabel("z", fontsize=8)
        plt.ylabel("y", fontsize=8)
        plt.title("XY Space", fontsize=8)

    num_points = list(num_points)

    plt.figure(3)
    plt.plot(num_points, dtws_xy, label="DTW")
    plt.plot(num_points, frechets_xy, label="Frechet")
    plt.xlabel("Number of nodes")
    plt.ylabel("Distance")
    plt.title("XY space")

    plt.figure(4)
    plt.plot(num_points, dtws_aal, label="DTW")
    plt.plot(num_points, frechets_aal, label="Frechet")
    plt.xlabel("Number of nodes")
    plt.ylabel("Distance")
    plt.title("AAL space")

    plt.show()
Ejemplo n.º 20
0
# quantify the difference between the two curves using
# Discrete Frechet distance
df = similaritymeasures.frechet_dist(exp_data, num_data)

# quantify the difference between the two curves using
# area between two curves
area = similaritymeasures.area_between_two_curves(exp_data, num_data)

# quantify the difference between the two curves using
# Curve Length based similarity measure
cl = similaritymeasures.curve_length_measure(exp_data, num_data)

# quantify the difference between the two curves using
# Dynamic Time Warping distance
dtw, d = similaritymeasures.dtw(exp_data, num_data)

# print the results
print(pcm, df, area, cl, dtw)

# plot the data
fig = plt.figure()
ax1 = fig.add_subplot(111, projection='3d')

ax1.plot_wireframe(x, y, subz)
ax1.plot_wireframe(xx, yy, subzz, color='deeppink')
ax1.set_xlabel('x')
ax1.set_xlabel('y')
ax1.set_xlabel('z')

# plt.plot(exp_data[:, 0], exp_data[:, 1], exp_data[:, 2])
    # quantify the difference between the two curves using
    # Discrete Frechet distance
    #df = similaritymeasures.frechet_dist(TwoDarrayLoop, tdArray)

    # quantify the difference between the two curves using
    # area between two curves
    #area = similaritymeasures.area_between_two_curves(TwoDarrayLoop, tdArray)

    # quantify the difference between the two curves using
    # Curve Length based similarity measure
    #cl = similaritymeasures.curve_length_measure(TwoDarrayLoop, tdArray)

    # quantify the difference between the two curves using
    # Dynamic Time Warping distance
    dtw, d = similaritymeasures.dtw(TwoDarrayLoop, tdArray)

    #print(pcm, '---', area, '---', cl, '---',dtw)
    print('---', dtw)

    #minX = np.min(TwoDarrayLoop[:, 0])
    #plt.figure()
    #plt.plot(TwoDarrayLoop[:, 0], TwoDarrayLoop[:, 1])
    #plt.plot(tdArray[:, 0], tdArray[:, 1])
    #plt.show()

    #maxX = np.max(x)
    #minY = np.min(y)
    #maxY = np.max(y)

    #xi = (x - minX) / (maxX - minX)
Ejemplo n.º 22
0
 def dist_dtw(self, other):
     dist_pos = similaritymeasures.dtw(self.cop_mlap.to_array().T,
                                       other.cop_mlap.to_array().T)[0]
     # print(f'{self} is {dist_pos:.2f} from {other}')
     return dist_pos
Ejemplo n.º 23
0
    Q = Q[::-1]

    f_d, p_i, q_i = FrechetDist.frechetdist_index_2(P, Q)
    print("frechet distance: ", f_d, p_i, q_i)
    f_d, p_i, q_i = FrechetDist.frechetdist_index(P, Q)
    print("frechet distance 2:  ", f_d, p_i, q_i)
    d = simm.frechet_dist(P, Q)
    print("frechet distance: ", d)
    distance = directed_hausdorff(P, Q)
    print("hausdorff distance P-Q: ", distance)
    distance = directed_hausdorff(Q, P)
    print("hausdorff distance Q-P: ", distance)
    px = [p[0] for p in P]
    py = [p[1] for p in P]
    qx = [p[0] for p in Q]
    qy = [p[1] for p in Q]
    plt.scatter(px+qx, py+qy)
    plt.plot(px, py, label="P", color="red")
    plt.plot(qx, qy, label="Q", color="blue")
    plt.plot([px[p_i], qx[q_i]], [py[p_i], qy[q_i]], label="frechet dist", linestyle=":", color="purple")
    plt.show()

    df = simm.area_between_two_curves(P, Q)
    print("area bt two curves: {}".format(df))
    df = simm.dtw(P, Q)
    print("dtw: {}".format(df))
    df = simm.curve_length_measure(P, Q)
    print("curve length: {}".format(df))
    df = simm.pcm(P, Q)
    print("pcm: {}".format(df))
Ejemplo n.º 24
0
def _compute_rec_score(predictions, trues, score_window, smooth_window, rec_error_type):
    """Compute an array of anomaly scores.

    Args:
        predictions (ndarray):
            Predicted values.
        trues (ndarray):
            Ground truth.
        score_window (int):
            Size of the window over which the scores are calculated.
        smooth_window (int):
            Smooth window that will be applied to compute smooth errors.
        rec_error_type (str):
            Reconstruction error types.

    Returns:
        ndarray:
            Array of anomaly scores.
    """
    if (rec_error_type == "point"):
        errors = [abs(y_h - y) for y_h, y in zip(predictions, trues)]
        errors_smoothed = pd.Series(errors).rolling(
            smooth_window, center=True, min_periods=smooth_window // 2).mean().values
        z_scores = stats.zscore(errors_smoothed)
        z_scores = np.clip(z_scores, a_min=0, a_max=None) + 1

    elif (rec_error_type == "area"):
        pd_true = pd.Series(np.asarray(trues).flatten())
        pd_pred = pd.Series(np.asarray(predictions).flatten())
        score_measure_true = pd_true.rolling(score_window, center=True,
                                             min_periods=score_window // 2).apply(integrate.trapz)
        score_measure_pred = pd_pred.rolling(score_window, center=True,
                                             min_periods=score_window // 2).apply(integrate.trapz)
        errors = abs(score_measure_true - score_measure_pred)
        errors_smoothed = pd.Series(errors).rolling(smooth_window, center=True,
                                                    win_type='triang',
                                                    min_periods=smooth_window // 2).mean().values
        z_scores = stats.zscore(errors_smoothed)
        z_scores = np.clip(z_scores, a_min=0, a_max=None) + 1

    elif (rec_error_type == "dtw"):
        # DTW
        i = 0
        similarity_dtw = list()
        length_dtw = (score_window // 2) * 2 + 1
        hafl_length_dtw = length_dtw // 2
        # add padding
        true_pad = np.pad(trues, (hafl_length_dtw, hafl_length_dtw),
                          'constant', constant_values=(0, 0))
        predictions_pad = np.pad(
            predictions,
            (hafl_length_dtw,
             hafl_length_dtw),
            'constant',
            constant_values=(
                0,
                0))

        while i < len(trues) - length_dtw:
            true_data = np.zeros((length_dtw, 2))
            true_data[:, 0] = np.arange(length_dtw)
            true_data[:, 1] = true_pad[i:i + length_dtw]
            preds_data = np.zeros((length_dtw, 2))
            preds_data[:, 0] = np.arange(length_dtw)
            preds_data[:, 1] = predictions_pad[i:i + length_dtw]
            dtw, _ = sm.dtw(true_data, preds_data)
            similarity_dtw = similarity_dtw + [dtw]
            i += 1
        similarity_dtw = [0] * int(length_dtw / 2) + similarity_dtw + [0] * (
            len(trues) - len(similarity_dtw) - int(length_dtw / 2))
        errors = similarity_dtw
        errors_smoothed = pd.Series(errors).rolling(smooth_window, center=True,
                                                    min_periods=smooth_window // 2).mean().values
        z_scores = stats.zscore(errors_smoothed)
        z_scores = np.clip(z_scores, a_min=0, a_max=None) + 1

    return z_scores
Ejemplo n.º 25
0
 def test_c5_c6_dtw_path(self):
     r, d = similaritymeasures.dtw(curve5, curve6)
     path = similaritymeasures.dtw_path(d)
     c = cdist(curve5, curve6)
     cost = sum(c[path[:, 0], path[:, 1]])
     self.assertTrue(np.isclose(r, cost))
Ejemplo n.º 26
0
 def test_P_Q_dtw_cityblock(self):
     r, _ = similaritymeasures.dtw(P, Q, metric='cityblock')
     self.assertTrue(r, 3.0)
Ejemplo n.º 27
0
time_x = new_index/scale_factor
x = time_x.reshape((151,1))
#Carico la media della run#5 per Unemployement Rate
raw_media_run = np.loadtxt("media_run").astype("float64")
#Faccio uno slicing dei primi e un reshape
media_run = raw_media_run[:151].reshape((151,1))
topos = np.hstack((x,media_run))

Discrete_Frechet_distance = []
Dynamic_Time_Warping = []
start = time.time()
for a in range(1,78126): #78126
    y = data[str(a)].to_numpy(np.float64).reshape((151,1))
    cord_new = np.hstack((x,y))
    Discrete_Frechet_distance.append(sm.frechet_dist(topos,cord_new))
    dtw, _ = sm.dtw(topos, cord_new)
    Dynamic_Time_Warping.append(dtw)
    print("Column: " + str(a))
    end = time.time()
    print("Tempo di processamento: " + str(end - start))

Discrete_Frechet_distance_vector = np.array(Discrete_Frechet_distance)
np.savetxt('Discrete_Frechet_distance', Discrete_Frechet_distance_vector, delimiter=',')
Dynamic_Time_Warping_vector = np.array(Dynamic_Time_Warping)
np.savetxt('Dynamic_Time_Warping', Dynamic_Time_Warping_vector, delimiter=',')