def plot_predictions(inX, iny, iny_pred, prog_num, bp, test):
    plt.cla()
    plot_inputs = inX.T[2]
    plt.plot(plot_inputs, iny, 'bx')
    plt.plot(plot_inputs, iny_pred, 'ro')
    plt.savefig("gaussianprocess_" + str(prog_num) + "_" + str(bp) + "_" + test +".png")
    bp_rms = ascdata.RMSE(iny, iny_pred)
    print "rms total for prognum", prog_num, "at breakpoint", bp, bp_rms
    bp_r2 = ascdata.r2(iny, iny_pred)
    print "r2 total for prognum", prog_num, "at breakpoint", bp, bp_r2
def plot_predictions(prog_num, bp, inX, iny, wridge, all, test):
    plt.cla()
    X_bp, y_bp = ascdata.get_bp_data(prog_num, bp, inX, iny)
    y_bp_pred = np.dot(X_bp, wridge)
    plot_inputs = X_bp.T[2]
    plt.plot(plot_inputs, y_bp, "b^")
    plt.plot(plot_inputs, y_bp_pred, "go")
    plt.savefig("bayes1" + "_" + str(prog_num) + "_" + str(bp) + "_" + test + "_" + all + ".png")
    rms_bp = ascdata.RMSE(y_bp, y_bp_pred)
    print "rms for prognum", prog_num, "at breakpoint", bp, rms_bp
    r2_bp = ascdata.r2(y_bp, y_bp_pred)
    print "r2 score for prognum", prog_num, "at breakpoint", bp, r2_bp
def get_kfold_scores(inX, iny, k):
    N = inX.shape[0]
    kf = KFold(N, k, shuffle=True)
    mses=[]
    r2s = []
    for train_index, test_index in kf:
        kf_X_train, kf_X_test = inX[train_index], inX[test_index]
        kf_y_train, kf_y_test = iny[train_index], iny[test_index]
        kf_y_test_pred = gp.predict(kf_X_test)
        mse = ascdata.MSE(kf_y_test, kf_y_test_pred)
        mses.append(mse)
        print "mse:", mse
        r2 = ascdata.r2(kf_y_test, kf_y_test_pred)
        r2s.append(r2)
        print "r2:", r2
    overall_mse = np.mean(mses)
    overall_rmse = sqrt(overall_mse)
    overall_r2 = np.mean(r2s)
    print "KFold cross validation MSE:", overall_mse
    print "KFold cross validation RMSE:", overall_rmse
    print "KFold cross validation r2:", overall_r2
def get_kfold_scores(inX, iny, k):
    N = inX.shape[0]
    kf = KFold(N, k, shuffle=True)
    mses = []
    r2s = []
    for train_index, test_index in kf:
        kf_X_train, kf_X_test = inX[train_index], inX[test_index]
        kf_y_train, kf_y_test = iny[train_index], iny[test_index]
        kf_X_train = np.concatenate((kf_X_train, np.ones((kf_X_train.shape[0], 1))), 1)
        kf_X_test = np.concatenate((kf_X_test, np.ones((kf_X_test.shape[0], 1))), 1)
        wridge = get_wridge_given_sets(kf_X_train, kf_y_train, kf_X_test, kf_y_test)
        kf_y_test_pred = np.dot(kf_X_test, wridge)
        mse = mean_squared_error(kf_y_test, kf_y_test_pred)
        mses.append(mse)
        print "mse:", mse
        r2 = ascdata.r2(kf_y_test, kf_y_test_pred)
        r2s.append(r2)
        print "r2:", r2
    overall_r2 = np.mean(r2s)
    overall_mse = np.mean(mses)
    overall_rmse = sqrt(overall_mse)
    print "KFold cross validation MSE:", overall_mse
    print "KFold cross validation RMSE:", overall_rmse
    print "KFold cross validation r2:", overall_r2
    Aopt = optimized_params
    print "Aopt = ", Aopt

    return Aopt, X_train_less, y_train, s_train, X_test_less, y_test, s_test, LLHs, LLH_xs

Aopt_all, X_train_all, y_train_all, s_train_all, X_test_all, y_test_all, s_test_all, LLHs, LLH_xs = get_Aopt(X, y)

# ### PLOT PREDICTIONS ###
print "PLOTTING PREDICTIONS"

y_test_pred = y_bp_pred = get_predictions(Aopt_all, X_test_all, s_test_all)
rms = ascdata.RMSE(y_test_all, y_test_pred)
print "total rms:", rms

r2 = ascdata.r2(y_test_all, y_test_pred)
print "total r2:", r2

# # Get cross validation RMS
# def get_kfold_scores(inX, iny, k):
#     N = inX.shape[0]
#     kf = KFold(N, k, shuffle=True)
#     mses=[]
#     r2s=[]
#     for train_index, test_index in kf:
#         kf_X_train, kf_X_test = inX[train_index], inX[test_index]
#         kf_y_train, kf_y_test = iny[train_index], iny[test_index]
#         kf_X_train = np.concatenate((kf_X_train, np.ones((kf_X_train.shape[ 0 ], 1))), 1)
#         kf_X_test = np.concatenate((kf_X_test, np.ones((kf_X_test.shape[ 0 ], 1))), 1)
#         X_test_less, s_test = ascdata.split_X_s(X_test)
#         wridge = get_wridge_given_sets(kf_X_train, kf_y_train, kf_X_test, kf_y_test)
# X_bp3, y_bp3 = ascdata.get_bp_data(2, 4198375, X, y)
# X_bp3_test, y_bp3_test = ascdata.get_bp_data(2, 4198375, X_test, y_test)

### GET PREDICTIONS ###
print "GETTING PREDICTIONS"
gp = gaussian_process.GaussianProcess(theta0=1e-1, thetaL=1e-3, thetaU=1, nugget=y_train+20)
gp.fit(X_train, y_train)
# Returns predictions for inXtest after training on inXtrain and inytrain.
def get_predictions(inXtest):
    y_pred, sigma2_pred = gp.predict(inXtest, eval_MSE=True)
    return y_pred

y_pred_test = get_predictions(X_test)
rms_test = ascdata.RMSE(y_test, y_pred_test)
print "rms test", rms_test
r2_test = ascdata.r2(y_test, y_pred_test)
print "r2 test", r2_test

def plot_actualvpredicted(y, y_pred):
    plt.cla()
    plt.scatter(y_pred, y)
    ones = range(int(max(y_pred)))
    plt.plot(ones, ones)
    plt.savefig("gaussianprocess" + "_" + "actualvpredicted" + ".png")

print "plotting actual v. predicted"
plot_actualvpredicted(y_test, y_pred_test)

# Get cross validation RMS and r^2
def get_kfold_scores(inX, iny, k):
    N = inX.shape[0]