""" 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')

[((12, 0, 12, 63), 'sys.path.append', 'sys.path.append', ({(12, 16, 12, 62): '"""/home/andrewkirby72/phd_work/data_synthesis"""'}, {}), "('/home/andrewkirby72/phd_work/data_synthesis')", False, 'import sys\n'), ((17, 7, 17, 23), 'numpy.ones', 'np.ones', ({(17, 15, 17, 22): '(25, 2)'}, {}), '((25, 2))', True, 'import numpy as np\n'), ((49, 0, 49, 35), 'matplotlib.pyplot.scatter', 'plt.scatter', ({(49, 12, 49, 22): 'rmse[:, (0)]', (49, 24, 49, 34): 'rmse[:, (1)]'}, {}), '(rmse[:, (0)], rmse[:, (1)])', True, 'import matplotlib.pyplot as plt\n'), ((50, 0, 50, 17), 'matplotlib.pyplot.yscale', 'plt.yscale', ({(50, 11, 50, 16): '"""log"""'}, {}), "('log')", True, 'import matplotlib.pyplot as plt\n'), ((51, 0, 51, 22), 'matplotlib.pyplot.ylim', 'plt.ylim', ({(51, 9, 51, 21): '[0.001, 0.1]'}, {}), '([0.001, 0.1])', True, 'import matplotlib.pyplot as plt\n'), ((52, 0, 52, 18), 'matplotlib.pyplot.xlim', 'plt.xlim', ({(52, 9, 52, 17): '[0, 200]'}, {}), '([0, 200])', True, 'import matplotlib.pyplot as plt\n'), ((53, 0, 53, 92), 'matplotlib.pyplot.title', 'plt.title', ({(53, 10, 53, 91): '"""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    )", True, 'import matplotlib.pyplot as plt\n'), ((54, 0, 54, 18), 'matplotlib.pyplot.ylabel', 'plt.ylabel', ({(54, 11, 54, 17): '"""RMSE"""'}, {}), "('RMSE')", True, 'import matplotlib.pyplot as plt\n'), ((55, 0, 55, 29), 'matplotlib.pyplot.xlabel', 'plt.xlabel', ({(55, 11, 55, 28): '"""Training points"""'}, {}), "('Training points')", True, 'import matplotlib.pyplot as plt\n'), ((56, 0, 57, 78), 'matplotlib.pyplot.savefig', 'plt.savefig', ({(56, 12, 57, 77): '"""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    )", True, 'import matplotlib.pyplot as plt\n'), ((42, 10, 42, 47), 'sklearn.metrics.mean_squared_error', 'mean_squared_error', ({(42, 29, 42, 35): 'y_test', (42, 37, 42, 46): 'y_predict'}, {}), '(y_test, y_predict)', False, 'from sklearn.metrics import mean_squared_error\n'), ((46, 17, 46, 29), 'numpy.sqrt', 'np.sqrt', ({(46, 25, 46, 28): 'mse'}, {}), '(mse)', True, 'import numpy as np\n'), ((36, 10, 36, 70), 'sklearn.gaussian_process.kernels.WhiteKernel', 'WhiteKernel', (), '', False, 'from sklearn.gaussian_process.kernels import RBF, WhiteKernel, Matern\n'), ((44, 19, 44, 31), 'numpy.sqrt', 'np.sqrt', ({(44, 27, 44, 30): 'mse'}, {}), '(mse)', True, 'import numpy as np\n'), ((35, 24, 35, 66), 'sklearn.gaussian_process.kernels.RBF', 'RBF', (), '', False, 'from sklearn.gaussian_process.kernels import RBF, WhiteKernel, Matern\n'), ((37, 32, 37, 48), 'sklearn.preprocessing.StandardScaler', 'StandardScaler', ({}, {}), '()', False, 'from sklearn.preprocessing import StandardScaler\n'), ((38, 27, 39, 45), 'sklearn.gaussian_process.GaussianProcessRegressor', 'GaussianProcessRegressor', (), '', False, 'from sklearn.gaussian_process import GaussianProcessRegressor\n')]