""" Plot a training curve for the 6D data simulator of CT*
"""

import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, WhiteKernel, Matern
from sklearn.metrics import mean_squared_error
from sklearn.pipeline import Pipeline
import sys
sys.path.append(r'/home/andrewkirby72/phd_work/data_synthesis')
from GP_machine_learning.GP_machine_learning_functions import *
from regular_array_sampling.functions import regular_array_monte_carlo

# create array to store results for plotting
rmse = np.ones((25, 2))
noise = 0.01
# create array of sampled regular array layouts
#cand_points = regular_array_monte_carlo(10000)
# create testing points
X_test, y_test = create_testing_points_regular(noise)

n = 0
n_target = 0
n_train = 0
while n_train < 200:
    n_target = 100 +100*n

    # create training points
    X_train, y_train, n_train = \
        create_training_points_irregular(n_target, noise)

    # fit GP regression and calculate rmse
    kernel = 1.0 ** 2 * RBF(length_scale=[1., 1., 1., 1., 1., 1.]) \
        + WhiteKernel(noise_level=1e-5, noise_level_bounds=[1e-10, 1])
    pipe = Pipeline([('scaler', StandardScaler()),
                    ('gp', GaussianProcessRegressor(kernel=kernel,
                     n_restarts_optimizer=20))])
    pipe.fit(X_train, y_train)
    y_predict = pipe.predict(X_test)
    mse = mean_squared_error(y_test, y_predict)
    # report rmse
    print(n_train, np.sqrt(mse))
    rmse[n, 0] = n_train
    rmse[n, 1] = np.sqrt(mse)
    n += 1

plt.scatter(rmse[:, 0], rmse[:, 1])
plt.yscale('log')
plt.ylim([1e-3, 1e-1])
plt.xlim([0, 200])
plt.title('Training curve RBF - 6D 1% noise - irregular array training - max change halved')
plt.ylabel('RMSE')
plt.xlabel('Training points')
plt.savefig('analysis/GP_machine_learning_plots/\
gp_training_curve_RBF_irregular_training_maxchangehalved_regular_testing.png')

[((392, 454), 'sys.path.append', 'sys.path.append', (['"""/home/andrewkirby72/phd_work/data_synthesis"""'], {}), "('/home/andrewkirby72/phd_work/data_synthesis')\n", (407, 454), False, 'import sys\n'), ((644, 660), 'numpy.ones', 'np.ones', (['(25, 2)'], {}), '((25, 2))\n', (651, 660), True, 'import numpy as np\n'), ((1634, 1673), 'matplotlib.pyplot.scatter', 'plt.scatter', (['rmse[:, (0)]', 'rmse[:, (1)]'], {}), '(rmse[:, (0)], rmse[:, (1)])\n', (1645, 1673), True, 'import matplotlib.pyplot as plt\n'), ((1670, 1687), 'matplotlib.pyplot.yscale', 'plt.yscale', (['"""log"""'], {}), "('log')\n", (1680, 1687), True, 'import matplotlib.pyplot as plt\n'), ((1688, 1710), 'matplotlib.pyplot.ylim', 'plt.ylim', (['[0.001, 0.1]'], {}), '([0.001, 0.1])\n', (1696, 1710), True, 'import matplotlib.pyplot as plt\n'), ((1711, 1729), 'matplotlib.pyplot.xlim', 'plt.xlim', (['[0, 200]'], {}), '([0, 200])\n', (1719, 1729), True, 'import matplotlib.pyplot as plt\n'), ((1730, 1832), 'matplotlib.pyplot.title', 'plt.title', (['"""Training curve RBF - 6D 1% noise - irregular array training - max change halved"""'], {}), "(\n    'Training curve RBF - 6D 1% noise - irregular array training - max change halved'\n    )\n", (1739, 1832), True, 'import matplotlib.pyplot as plt\n'), ((1823, 1841), 'matplotlib.pyplot.ylabel', 'plt.ylabel', (['"""RMSE"""'], {}), "('RMSE')\n", (1833, 1841), True, 'import matplotlib.pyplot as plt\n'), ((1842, 1871), 'matplotlib.pyplot.xlabel', 'plt.xlabel', (['"""Training points"""'], {}), "('Training points')\n", (1852, 1871), True, 'import matplotlib.pyplot as plt\n'), ((1872, 2008), 'matplotlib.pyplot.savefig', 'plt.savefig', (['"""analysis/GP_machine_learning_plots/gp_training_curve_RBF_irregular_training_maxchangehalved_regular_testing.png"""'], {}), "(\n    'analysis/GP_machine_learning_plots/gp_training_curve_RBF_irregular_training_maxchangehalved_regular_testing.png'\n    )\n", (1883, 2008), True, 'import matplotlib.pyplot as plt\n'), ((1478, 1515), 'sklearn.metrics.mean_squared_error', 'mean_squared_error', (['y_test', 'y_predict'], {}), '(y_test, y_predict)\n', (1496, 1515), False, 'from sklearn.metrics import mean_squared_error\n'), ((1609, 1621), 'numpy.sqrt', 'np.sqrt', (['mse'], {}), '(mse)\n', (1616, 1621), True, 'import numpy as np\n'), ((1172, 1233), 'sklearn.gaussian_process.kernels.WhiteKernel', 'WhiteKernel', ([], {'noise_level': '(1e-05)', 'noise_level_bounds': '[1e-10, 1]'}), '(noise_level=1e-05, noise_level_bounds=[1e-10, 1])\n', (1183, 1233), False, 'from sklearn.gaussian_process.kernels import RBF, WhiteKernel, Matern\n'), ((1553, 1565), 'numpy.sqrt', 'np.sqrt', (['mse'], {}), '(mse)\n', (1560, 1565), True, 'import numpy as np\n'), ((1117, 1165), 'sklearn.gaussian_process.kernels.RBF', 'RBF', ([], {'length_scale': '[1.0, 1.0, 1.0, 1.0, 1.0, 1.0]'}), '(length_scale=[1.0, 1.0, 1.0, 1.0, 1.0, 1.0])\n', (1120, 1165), False, 'from sklearn.gaussian_process.kernels import RBF, WhiteKernel, Matern\n'), ((1265, 1281), 'sklearn.preprocessing.StandardScaler', 'StandardScaler', ([], {}), '()\n', (1279, 1281), False, 'from sklearn.preprocessing import StandardScaler\n'), ((1311, 1375), 'sklearn.gaussian_process.GaussianProcessRegressor', 'GaussianProcessRegressor', ([], {'kernel': 'kernel', 'n_restarts_optimizer': '(20)'}), '(kernel=kernel, n_restarts_optimizer=20)\n', (1335, 1375), False, 'from sklearn.gaussian_process import GaussianProcessRegressor\n')]