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1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 6, 42, 40, 26, 26, 24, 24, 24, 26, 26, 12, 10, 10, 8, 6, 6, 6, 6, 8, 6, 6, 6, 8, 6, 6, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]


# def remove_outLiers(points):
# 	model = LinearLeastSquaresModel()
# 	minimumNumber = int(len(points)*0.3)
# 	iterations = 1
# 	Y = [p.y for p in points]
# 	mY = sum(Y)/len(Y)
# 	fitDist = mY*0.5
# 	numberOfcloses = int(len(points)*0.5)
# 	inliers = ransac(points, model, minimumNumber, 1, fitDist, numberOfcloses, debug=False,return_all=True, random=False)

# 	return inliers

def leastSquares(points):
	x_d = []
	y_d = []
	for point in points:
		(x, y) = point

		x_d.append(x)
		y_d.append(y)

	n=len(x_d)

	B=numpy.array(y_d)
	A=numpy.array(([[x_d[j], 1] for j in range(n)]))
	X=numpy.linalg.lstsq(A,B)[0]
	a=X[0]; b=X[1]
	#print("Line is: y=",a,"x+",b)

	Xplot = []
	Yplot = []

	x = min(x_d)
	y = a*x + b
	Xplot.append(x)
	Yplot.append(y)

	x = max(x_d)
	y = a*x + b
	Xplot.append(x)
	Yplot.append(y)
	return (Xplot, Yplot, a, b)

fig = plt.figure()

ax1 = fig.add_subplot(1,1,1)
#ax1.set_title('before adjust', fontname='serif', fontsize=16)

templateSpectre = [2, 3, 4, 5, 6, 7, 0, 1]
print("len(templateSpectre) = ", len(templateSpectre))
#print("len(testSpectre) = ", len(testSpectre))

#UFPE
correctPairsIndices =  [(9, 0), (10, 1), (14, 5), (15, 7), (16, 8), (12, 2), (17, 9), (20, 13), (21, 14), (22, 15), (23, 16), (0, 17), (2, 19), (4, 20), (5, 22), (8, 23)]
#correctPairsIndices = [(4, 23), (5, 22), (6, 20), (7, 17), (8, 15), (0, 9), (1, 8), (2, 1), (3, 0)]
#correctPairsIndices = [(14, 0), (21, 3), (25, 6), (33, 5), (2, 2), (4, 9), (8, 1), (11, 7), (38, 8)]


#correctPairsIndices = [(11, 15), (14, 11), (23, 5), (24, 3), (35, 16), (16, 17), (25, 6), (37, 14), (5, 13), (7, 12), (9, 0)]

correctPairsIndices =  [(11, 15), (14, 11), (23, 5), (24, 3), (35, 16), (16, 17), (25, 6), (37, 14), (5, 13), (7, 12), (9, 0)]
Y = [15, 11, 17, 5, 3, 6, 16, 14, 13, 12, 0]


#[(0, 17), (2, 19), (4, 20), (5, 22), (8, 23), (9, 0), (11, 1), (14, 5), (15, 7), (16, 8), (12, 2), (17, 9), (20, 13), (21, 14), (22, 15), (23, 16)]


# tau:  [0, 1, 2, 3, 4, 5, 6, 7, 8]
# Q:   [23, 22, 20, 17, 15, 9, 8, 1, 0]

# UFPE
#Y = [0, 1, 2, 5, 7, 8, 9, 13, 14, 15, 16, 17, 19, 20, 22, 23]#[y for (x,y) in correctPairsIndices]
#Y = [23, 22, 20, 17, 15, 9, 8, 1, 0]
#Y = [0, 3, 6, 5, 8, 2, 9, 1, 7]

#tau:  [5, 7, 11, 14, 25, 37]
#Y =  [15, 11, 6, 14, 13, 12]

X = [x for (x, y) in correctPairsIndices if y in Y]



X.sort()

new_pairs = list(zip(X, Y))
print("new_pairs: ", new_pairs)

T = X#[0, 1, 2, 3, 4, 5, 6, 7]
Q = Y#[1, 3, 4, 5, 6, 7, 1, 8]

points = list(zip(T, Q))

(Xplot, Yplot, a, b) = leastSquares(points)

#plt.plot(T, Q, marker='o', linestyle=':', color='b')
plt.plot(T, Q, marker='o', markersize='5.0', linestyle='None', color='b')
plt.plot(Xplot, Yplot, linestyle='-', linewidth=2.0, color='c')
plt.plot(T, Q, linestyle='--', linewidth=1.5, color='m')
#ax1.text(0, 6.5, r'Template vertex: $T_i$', fontsize=11)
#ax1.text(0, 6.2, r'Query vertex: $Q_j$', fontsize=11)


#for (t, q) in zip(T, Q):
	#text = r"$(T_" + str(t) + ", Q_" + str(q) + ")$" 
	#plt.text(t-0.5, q+0.5, r"$(\tau_{" + str(t) + "}, Q_{" + str(q) + "})$", weight='semibold', fontname='Courier', style='italic', fontsize=21, color='black')


# for (t, q) in zip(T, Q):
# 	#text = "$(T_" + str(t) + ", Q_" + str(q) + ")$" 
# 	plt.text(t+0.2, q, r"$(T_" + str(t) + ", Q_" + str(q) + ")$", fontname='serif', style='italic', fontsize=14, color='black')

# T = [6, 7]
# Q = [0, 1]
# plt.plot(T, Q, marker='x', linestyle=':', color='r')


plt.show()