# import matplotlib.pyplot as plt
# from matplotlib import cm
# from mpl_toolkits.mplot3d import axes3d, Axes3D
# from math import e
# import numpy as np
#
# # fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
#
# x = np.arange(-10, 10, 0.01)
# # z = x**2 + y**2
# # y = lambda a: 1/(1+np.exp(-a*x))
# y = lambda a, sigma: np.exp(-(np.abs(x - a)**2)/(2*sigma**2))
# sig1 = y(3,1)
# sig2 = y(0, 0.7)
# sig3 = y(-1, 2)
#
#
# if __name__ == '__main__':
#     # surf = ax.plot_surface(x, y, z, cmap=cm.coolwarm,
#     #                        linewidth=0, antialiased=False)
#     # ax.set_xlabel(r"$\it{\theta_{0}} [-]$")
#     # ax.set_ylabel(r"$\it{\theta_{1}} [-]$")
#     # ax.set_zlabel(r"$\it{cost} [-]$")
#     # # ax.zaxis.set_major_formatter('{x:.02f}')
#     #
#     # plt.figure(figsize=(9, 4))
#     plt.plot(x, sig1, label=r"$f_{i}(x, 3) = \exp\left(-\frac{|x-3|^{2}}{2 \cdot 1^{2}} \right)$")
#     plt.plot(x, sig2, label=r"$f_{i}(x, 0.7) = \exp\left(-\frac{|x|^{2}}{2 \cdot 0.7^{2}} \right)$")
#     plt.plot(x, sig3, label=r"$f_{i}(x,-1) = \exp\left(-\frac{|x+1|^{2}}{2 \cdot 2^{2}} \right)$")
#     plt.grid()
#     plt.legend(fontsize=14)
#     plt.xlabel(r"$\it{x}$", fontsize=15)
#     plt.ylabel(r"$\it{f_{i}}$", fontsize=15)
#     plt.xlim([-6, 6])
#     plt.show()

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

X = [1, 1.3, 2, 2.5, 3.4, 4, 4.5]
Y = [1, 2, 1.9, 3.2, 3.8, 4, 4.3]
X1 = [0.4, 1.2, 1.55, 2.05, 3.25, 4.4, 5.5]
Y1 = [0.4, 2.4, 2.8, 3, 3.2, 3.25, 3.3]
mymodel = np.poly1d(np.polyfit(X, Y, 6))
mymodel1 = np.poly1d(np.polyfit(X1, Y1, 1))
# for x, y in zip(X, Y):
#     if y < mymodel(x):
#         eps = x - abs(mymodel(x) - y)
#         errbox = Rectangle((eps, y), abs(mymodel(x) - y), abs(mymodel(x) - y), alpha=0.52)
#     else:
#         errbox = Rectangle((x, min(y, mymodel(x))),
#                   abs(mymodel(x) - y), abs(mymodel(x) - y), alpha=0.52)
#
#     plt.gca().add_patch(errbox)

# myline = np.linspace(0, 7, 500)
# plt.ylim([0, 5])
# plt.xlim([0, 5])
# plt.grid()
# plt.scatter(X, Y)
# plt.xlabel(r"$\it{x} [-] $")
# plt.ylabel(r"$\it{h} [-] $")
# plt.plot(myline, mymodel(myline), color='green')
#
# plt.figure(figsize=(20, 10))
#
# ax1 = plt.subplot2grid((1,2), (0,0))
# ax1.set_title(label="a)", size=28)
# ax1.grid()
# ax1.scatter(X, Y, s=80)
# ax1.set_xlim([0, 5])
# ax1.set_ylim([0, 5])
# ax1.plot(myline, mymodel(myline), color="green", linewidth=2.5)
# ax1.set_xlabel(r"$\it{x} [-]$", fontsize=28)
# ax1.set_ylabel(r"$\it{h} [-]$", fontsize=28)
#
#
# ax2 = plt.subplot2grid((1,2), (0,1))
# ax2.set_title(label="b)", size=28)
# ax2.grid()
# ax2.scatter(X1, Y1, s=150)
# ax2.set_xlim([0, 5])
# ax2.set_ylim([0, 5])
# ax2.plot(myline, mymodel1(myline), color="green", linewidth=2.5)
# ax2.set_xlabel(r"$\it{x} [-]$", fontsize=28)
# ax2.set_ylabel(r"$\it{h} [-]$", fontsize=28)
#
# plt.show()

# import numpy as np
# import matplotlib.pyplot as plt
# from matplotlib.patches import Circle
#
# circle = Circle((50, 50), radius=30, alpha=0.55)
# n = 200
# x, y = np.random.randint(100, size=(2, n))
# for i in range(n):
#     if pow(x[i] - 50, 2) + pow(y[i] - 50, 2) < 900:
#         plt.scatter(x[i], y[i], None, 'blue')
#     else:
#         plt.scatter(x[i], y[i], None, 'red')
# # plt.scatter(*np.random.randint(100, size = (2, n)))
#
# plt.grid()
# plt.xlabel(r"$\it{x_{0}} [-] $")
# plt.ylabel(r"$\it{x_{1}} [-] $")
# plt.gca().add_patch(circle)
# plt.axis('equal')
# plt.show()



# import numpy as np
# import matplotlib.pyplot as plt
# from matplotlib.patches import Circle
#
# circle = Circle((50, 50), radius=30, alpha=0.55)
# n = 200
# x, y = np.random.randint(100, size=(2, n))
# for i in range(n):
#     if pow(x[i] - 50, 2) + pow(y[i] - 50, 2) < 900:
#         plt.scatter(x[i], y[i], None, 'blue')
#     else:
#         plt.scatter(x[i], y[i], None, 'red')
# # plt.scatter(*np.random.randint(100, size = (2, n)))
#
# plt.grid()
# plt.xlabel(r"$\it{x_{0}} [-] $")
# plt.ylabel(r"$\it{x_{1}} [-] $")
# plt.gca().add_patch(circle)
# plt.axis('equal')
# plt.show()

# import numpy as np
# import matplotlib.pyplot as plt
#
# line = np.linspace(0, 700, 500)
# f1 = np.exp(-0.005*line) + 1.13
# f2 = -(np.exp(-0.005*line) - 1.1)
#
# ax1 = plt.subplot2grid((2,1), (0,0), title="a)")
#
# ax1.plot(line, f1, label=r"$J_{test}(\theta) $")
# ax1.plot(line, f2, label=r"$J_{train}(\theta) $")
# ax1.axhline(y=0.25, color="red", linestyle="--", label=r"$J_{limit}(\theta)$")
# ax1.set_xlabel(r"$\it{m} [-] $")
# ax1.set_ylabel(r"$\it{J} [-] $")
# ax1.grid()
# plt.legend(loc=1)
# plt.xlim([0, 700])
#
#
# f1 = np.exp(-0.01*line) + 0.20
# f2 = -(np.exp(-0.01*line) - 0.15)
#
# ax2 = plt.subplot2grid((2,1), (1,0), sharex=ax1, title="b)")
#
# ax2.plot(line, f1, label=r"$J_{test}(\theta) $")
# ax2.plot(line, f2, label=r"$J_{train}(\theta) $")
# ax2.axhline(y=0.25, color="red", linestyle="--", label=r"$J_{limit}(\theta)$")
# ax2.set_xlabel(r"$\it{m} [-] $")
# ax2.set_ylabel(r"$\it{J} [-] $")
# plt.legend(loc=1)
# plt.xlim([0, 700])
# ax2.set_ylim([0, 0.5])
# ax2.grid()
# plt.show()

# import numpy as np
# import matplotlib.pyplot as plt
#
# X = [1, 2, 3, 4, 5, 6]
# Y = [4.82, 3.25, 1.11, 1.05, 0.98, 0.9]
# elbowX, elbowY = [X[2], Y[2]]
#
# ax1 = plt.subplot2grid((2,1), (0,0), title="a)")
# ax1.scatter(elbowX, elbowY, color="red", zorder=10, label=r"loket")
# ax1.plot(X, Y, 'o-')
# ax1.set_xlabel(r"$\it{K} [-]$")
# ax1.set_ylabel(r"$\it{J} [-]$")
#
# ax1.legend(loc=0)
# ax1.grid()
#
#
# X = [1, 2, 3, 4, 5, 6]
# Y = [2.05, 1.50, 1.25, 1.05, 0.98, 0.9]
# elbowX, elbowY = [X[2], Y[2]]
#
# ax2 = plt.subplot2grid((2,1), (1,0), title="b)")
# # ax2.scatter(elbowX, elbowY, color="red", zorder=10, label=r"elbow")
# ax2.plot(X, Y, 'o-')
# ax2.set_xlabel(r"$\it{K} [-]$")
# ax2.set_ylabel(r"$\it{J} [-]$")
#
# # ax2.legend(loc=0)
# ax2.grid()
# plt.show()

# import numpy as np
# import matplotlib.pyplot as plt
# from matplotlib.patches import Circle, Ellipse
#
# ellipse = Ellipse((-5, 20), -150, 60, alpha=0.55)
# circle = Circle((-5, 70), radius=20, alpha=0.55, color="orange")
# n = 200
# x1, y1 = np.random.randint(0, 50, size=(2, 32))
# y2 = np.random.randint(60, 80, size=(1, 28))
# x2 = np.random.randint(-20, 10, size=(1, 28))
# x3, y3 = np.random.randint(-50, -10, size=(2, 32))
#
#
# ax1 = plt.subplot2grid((2,1), (0,0), title="a)")
# ax1.scatter(x1, y1, color='blue')
# ax1.scatter(x2, y2, color="orange")
# ax1.scatter(x3, y1, color="blue")
#
# # for i in range(n):
# #     if pow(x[i] - 50, 2) + pow(y[i] - 50, 2) < 900:
# #         plt.scatter(x[i], y[i], None, 'blue')
# #     else:
# #         plt.scatter(x[i], y[i], None, 'red')
# # plt.scatter(*np.random.randint(100, size = (2, n)))
#
# ax1.grid()
# ax1.set_xlabel(r"$\it{x_{0}} [-] $")
# ax1.set_ylabel(r"$\it{x_{1}} [-] $")
# ax1.add_patch(ellipse)
# ax1.add_patch(circle)
# ax1.set_xlim([-80, 75])
# ax1.set_ylim([-10, 90])
#
# # ax1.axis('equal')
#
#
# circle1 = Circle((-35, 20), radius=30, alpha=0.55, color="red")
# circle2 = Circle((25, 20), radius=30, alpha=0.55, color="blue")
# circle3 = Circle((-5, 70), radius=20, alpha=0.55, color="orange")
#
# ax2 = plt.subplot2grid((2,1), (1,0), title="b)")
# ax2.scatter(x1, y1, color='blue')
# ax2.scatter(x2, y2, color="orange")
# ax2.scatter(x3, y1, color="red")
#
# # for i in range(n):
# #     if pow(x[i] - 50, 2) + pow(y[i] - 50, 2) < 900:
# #         plt.scatter(x[i], y[i], None, 'blue')
# #     else:
# #         plt.scatter(x[i], y[i], None, 'red')
# # plt.scatter(*np.random.randint(100, size = (2, n)))
#
# ax2.grid()
# ax2.set_xlabel(r"$\it{x_{0}} [-] $")
# ax2.set_ylabel(r"$\it{x_{1}} [-] $")
# ax2.add_patch(circle1)
# ax2.add_patch(circle2)
# ax2.add_patch(circle3)
# # ax2.axis('equal')
# ax2.set_xlim([-75, 75])
# ax2.set_ylim([-10, 90])
#
# plt.show()

# import matplotlib.pyplot as plt
# import numpy as np
#
# x = np.linspace(0, 15, 500)
# y = -np.exp(-0.8*x) + 1
# y2 = 0.0002*pow(x, 3) - 0.0131*pow(x, 2) + 0.2065*x
#
# plt.plot(x, y, label=r"$Acc_{train} - přesnost $")
# plt.plot(x, y2, label=r"$Acc_{test} - přesnost$")
# plt.xlim([0, 15])
# plt.ylim([0, 1.2])
# plt.xlabel(r"$\it{epocha} [-]$")
# plt.ylabel(r"$\it{Acc} [-]$")
# plt.axhline(y=1, color="red", linestyle="--", alpha=0.55)
# plt.grid()
# plt.legend(loc=4)
# plt.show()

# import matplotlib.pyplot as plt
# import numpy as np
# import torch.nn as nn
# import torch
# 
# def ReLU(x):
#     if x > 0:
#         return x
#     else:
#         return 0
# 
# def leaky_relu(x):
#   if x>0 :
#     return x
#   else :
#     return 0.01*x
# a = nn.ReLU()
# b = nn.LeakyReLU(negative_slope=0.04)
# x = np.linspace(-5, 5, 500)
# relu = a(torch.as_tensor(x))
# lrelu = b(torch.as_tensor(x))
# tanh = np.tanh(x)
# 
# 
# plt.figure(figsize=(16, 5))
# 
# ax1 = plt.subplot2grid((1,3), (0,0))
# ax1.set_title(label="a)", size=20)
# ax1.grid()
# ax1.plot(x, tanh, label=r"$\it{tanh\left(x\right)}$", linewidth=2.5)
# ax1.set_xlabel(r"$\it{x}$", fontsize=16)
# ax1.set_ylabel(r"$\it{y}$", fontsize=16)
# ax1.legend(loc=2, fontsize=16)

# 
# ax2 = plt.subplot2grid((1,3), (0,1))
# ax2.set_title(label="b)", size=20)
# ax2.grid()
# ax2.plot(x, relu, label=r"$\it{ReLU\left(x\right)}$", linewidth=2.5)
# ax2.set_xlabel(r"$\it{x}$", fontsize=16)
# ax2.set_ylabel(r"$\it{y}$", fontsize=16)
# ax2.legend(loc=2, fontsize=16)
# 
# ax2 = plt.subplot2grid((1,3), (0,2))
# ax2.set_title(label="c)", size=20)
# ax2.grid()
# ax2.plot(x, lrelu, label=r"$\it{LeakyReLU\left(x\right)}$", linewidth=2.5)
# ax2.set_xlabel(r"$\it{x}$", fontsize=16)
# ax2.set_ylabel(r"$\it{y}$", fontsize=16)
# ax2.legend(loc=2, fontsize=16)
# 
# plt.show()

# import matplotlib.pyplot as plt
# import numpy as np
#
# x = [0, 1, 2, 3, 4, 5, 6, 7]
# y = [11.81, 8.47, 7.66, 7.2, 6.62, 6.41, 12.13, 12.24]
#
# plt.plot(x, y, marker="o")
# plt.xlabel(r"$\it{epocha} [-]$")
# plt.ylabel(r"$\it{ztraty} [-]$")
# plt.xlim([0, 7])
# plt.grid()
#
# plt.show()

# import matplotlib.pyplot as plt
#
#
def plot_learning_curves(file_name1, file_name2):
    lossFile = open(file_name1, 'r').read().split('\n')
    precisionFile = open(file_name2, 'r').read().split('\n')

    epochLosses = []
    losses = []

    epochPrecision = []
    precisions = []

    for f in lossFile:
        try:
            cycle, loss = f.split(';')
        except ValueError:
            pass

        epochLosses.append(int(cycle))
        losses.append(float(loss))

    for f in precisionFile:
        try:
            cycle, precision = f.split(';')
        except ValueError:
            pass

        epochPrecision.append(int(cycle))
        precisions.append(round(float(precision)*100, ndigits=2))

    plt.figure()
    ax1 = plt.subplot2grid((2,1), (0,0))
    ax1.grid()
    ax1.plot(epochLosses, losses)
    ax1.set_xlabel(r"$\it{epocha} [-]$")
    ax1.set_ylabel(r"$\it{ztraty} [-]$")
    ax1.set_xlim([0, 227])

    ax2 = plt.subplot2grid((2,1), (1,0))
    ax2.grid()
    ax2.plot(epochPrecision, precisions, marker="o")
    ax2.set_xlabel(r"$\it{epocha} [-]$")
    ax2.set_ylabel(r"$\it{presnost} [\%]$")
    ax2.set_xlim([10, 223])
    plt.show()


if __name__ == '__main__':
    plot_learning_curves('logbook_set1.csv', 'logbook_precision1.csv')
