"""Literal Python conversion of fig8_18.m."""

from pathlib import Path

import matplotlib.pyplot as plt
import numpy as np


def fig8_18():
    #  generate data for regression
    # nd=80
    # xd=np.linspace(0,1,nd)
    # yd=np.exp(-10*(xd-0.6)**2)*np.cos(6*np.pi*xd)
    # dis=0.4*(np.random.rand(nd)-0.5)
    # yd=yd+dis
    #
    # data=np.vstack((xd, yd))
    # np.savetxt('/Users/mark/Desktop/data8_14C.txt',data)

    data = np.loadtxt(Path(__file__).with_name("data8_14C.txt"))
    xd = data[0, :]
    yd = data[1, :]
    nd = len(xd)

    # smoother
    D = np.zeros((nd - 2, nd))
    for i in range(nd - 2):
        D[i, i] = 1; D[i, i + 1] = -2; D[i, i + 2] = 1
    lambda_ = 20
    A = np.eye(nd)
    y = np.linalg.inv(A.T @ A + lambda_ * D.T @ D) @ A.T @ yd.reshape(-1, 1)

    plt.close("all")
    # get(gcf)
    # MATLAB Position [7 1115 658 230], scaled to fit the plot window.
    fig, ax = plt.subplots(figsize=(6.58, 3.0), layout="constrained")
    co = np.array([
        [0, 0, 1],
        [0, 0.5, 0],
        [1, 0, 0],
        [0, 0.75, 0.75],
        [0.75, 0, 0.75],
        [0.75, 0.75, 0],
        [0.25, 0.25, 0.25],
    ])
    ax.set_prop_cycle(color=co)

    ax.plot(xd, yd, "-b", linewidth=1)
    ax.axis([0, 1, -1.1, 1.1])
    ax.set_xlabel("x-axis", fontsize=12, fontweight="bold")
    ax.set_ylabel("y-axis", fontsize=12, fontweight="bold")
    ax.set_xticks([0, 0.2, 0.4, 0.6, 0.8, 1.0])
    ax.grid(True)
    ax.tick_params(labelsize=12)
    for label in ax.get_xticklabels() + ax.get_yticklabels():
        label.set_fontweight("bold")

    # ax.plot(xd,y,'r',linewidth=1)

    # fig.savefig('/Users/mark/Desktop/treg.eps')
    plt.show()


if __name__ == "__main__":
    fig8_18()
