基于弗雷歇距离的轨迹相似度画图(plot)
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.collections import LineCollection
from matplotlib.ticker import MaxNLocator
from shapesimilarity import shape_similarity
import seaborn as sns
def get_data(path):
df = pd.read_excel(path, header=None)
df2 = df.iloc[:, [i % step == 0 for i in range(len(df.columns))]]
data = df.values
data = data.reshape(2, -1)
return data.T, df2.values.reshape(2, -1).T
def getpath(scen, tra):
return "../track/Scen" + str(tra) + "Tra" + str(scen) + ".xlsx"
def draw(arr, arr2, color, title, xlabel, ylabel, sc1, x1, sc2, x2):
drawplot(arr, Dividing_Line_Segments(arr, color), Dividing_Line_Segments(arr2, color), title,
xlabel,
ylabel, sc1, x1, sc2, x2)
def Frechet(CarA, CarB):
return shape_similarity(CarA, CarB)
def Dividing_Line_Segments(arr, color):
segments = np.stack((arr[:-1], arr[1:]), axis=1)
cmap = color
colors = color_map(arr[1:, :-1], cmap)
line_segments = LineCollection(segments, colors=colors, linewidths=linewidth, linestyles='solid', cmap=cmap)
return line_segments
def color_map(data, cmap):
"""数值映射为颜色"""
dmin, dmax = np.nanmin(data), np.nanmax(data)
cmo = plt.cm.get_cmap(cmap)
cs, k = list(), 256 / cmo.N
for i in range(cmo.N):
c = cmo(i)
for j in range(int(i * k), int((i + 1) * k)):
cs.append(c)
cs = np.array(cs)
data = np.uint8(255 * (data - dmin) / (dmax - dmin))
return cs[data]
def drawplot(arr, seg1, seg2, title, xlabel, ylabel, sc1, x1, sc2, x2):
ax = plt.axes([0.12, 0.35, 0.82, 0.28])
ax.set_ylim(ly)
ax.set_xlim(lx)
if xlabel:
plt.xlabel(xlabel)
if ylabel:
plt.ylabel(ylabel)
plt.title(title)
ax.add_collection(seg1)
ax.add_collection(seg2)
plt.savefig("C:/Users/asus/Desktop/Sim/SimGNN-master/scen2/" + "S" + str(sc1) + "T" + str(x1) + "-" + "S" + str(
sc2) + "T" + str(x2))
plt.show()
def getgraph(scx, x, scy, y):
path1 = getpath(scx, x)
path2 = getpath(scy, y)
da, data = get_data(path1)
da2, data2 = get_data(path2)
similarity = Frechet(data, data2)
print("S" + str(scx) + "T" + str(x) + "-S" + str(scy) + "T" + str(y) + "'s similarity=" + str(similarity))
draw(da, da2, color,
"S" + str(scx) + "T" + str(x) + "-S" + str(scy) + "T" + str(y) + "'s similarity=" + str(similarity),
"Longitudinal", "Lateral", scx, x, scy, y)
color = "viridis_r"
step = 50
cblabel = "asd"
lx = (0, 150)
ly = (-5, 5)
linewidth = 2
cnt = 0
for i in range(1, 4):
for j in range(1, 4):
if j <= i:
continue
cnt += 4
getgraph(i, 1, j, 1)
getgraph(i, 1, j, 2)
getgraph(i, 2, j, 1)
getgraph(i, 2, j, 2)
print(cnt)