<reponame>iwamura-lab/my_codes<gh_stars>0
#!/usr/bin/env python
"""Code to draw pareto optimal MLPs and all data points."""

import argparse

import matplotlib.pyplot as plt
import numpy as np

if __name__ == "__main__":

    parser = argparse.ArgumentParser()
    parser.add_argument("-d", "--dir", default=".", help="Directory name to read")
    parser.add_argument(
        "--all",
        type=str,
        default="all_feature",
        help="Data file name in which rmse and elapsed time of all MLPs are written.",
    )
    parser.add_argument(
        "--pareto",
        type=str,
        default="pareto_feature",
        help="Data file name in which rmse and elapsed time of pareto optimal MLPs are written.",
    )
    parser.add_argument("-p", "--plot", action="store_true", help="plot mode")
    parser.add_argument(
        "-f", "--figure", default="pareto_optimal.png", help="figure name"
    )
    parser.add_argument("--xlabel", default="# of features", help="X axis label")
    parser.add_argument(
        "--ylabel", default="RMSE(energy) (meV/atom)", help="Y axis label"
    )
    parser.add_argument(
        "--xlim",
        nargs=3,
        type=float,
        default=[None] * 3,
        help="x axis limits. start, stop, step",
    )
    parser.add_argument(
        "--ylim",
        nargs=3,
        type=float,
        default=[None] * 3,
        help="Y axis limits. start, stop, step",
    )
    parser.add_argument(
        "--ticksize", type=int, default=18, help="X axis or Y axis tick size"
    )
    parser.add_argument("--digits", default=None, help="number of digits")
    args = parser.parse_args()

    plt.rcParams["font.family"] = "Times New Roman"
    plt.rcParams["font.size"] = 19

    if ("energy" in args.all) and ("energy" in args.pareto):
        y_id = 2
    elif ("force" in args.all) and ("force" in args.pareto):
        y_id = 1
    else:
        y_id = 2

    mlp_dict = {}
    with open(args.dir + "/" + args.all) as f:
        for line in f:
            s_line = line.split()
            mlp_dict[s_line[-1]] = [float(s_line[0]), float(s_line[y_id])]
    points = np.array(list(mlp_dict.values()))

    fig, ax = plt.subplots()
    fig.set_tight_layout(True)
    fig.patch.set_alpha(0)
    with open(args.dir + "/" + args.pareto) as f:
        s_lines = [line.split() for line in f]
        for i in range(len(s_lines) - 1):
            mlp1 = s_lines[i][-1]
            mlp2 = s_lines[i + 1][-1]
            x = [mlp_dict[mlp1][0], mlp_dict[mlp2][0]]
            y = [mlp_dict[mlp1][1], mlp_dict[mlp2][1]]
            ax.plot(x, y, color="#1f77b4", linestyle="solid", lw=2, zorder=1)
    ax.scatter(
        points[:, 0],
        points[:, 1],
        s=6,
        color="k",
        marker=".",
        zorder=2,
        facecolor="None",
    )

    # graph appearance settings
    plt.xlabel(args.xlabel)
    plt.ylabel(args.ylabel)
    if args.xlim[0] is not None:
        if all([val.is_integer() for val in args.xlim]):
            xticks = [
                tick for tick in range(args.xlim[0], args.xlim[1] + 1, args.xlim[2])
            ]
            ax.set_xticks(xticks)
            ax.set_xticklabels(xticks, fontsize=args.ticksize)
        else:
            xticks = [
                round(tick, 3)
                for tick in np.arange(args.xlim[0], args.xlim[1], args.xlim[2])
            ]
            xticks.append(args.xlim[1])
        ax.set_xlim(args.xlim[0], args.xlim[1])
        ax.set_xticks(xticks)
        ax.set_xticklabels(xticks, fontsize=args.ticksize)
    if args.ylim[0] is not None:
        if all([val.is_integer() for val in args.ylim]):
            ylim = [int(val) for val in args.ylim]
            yticks = [tick for tick in range(ylim[0], ylim[1] + 1, ylim[2])]
        else:
            yticks = [
                round(tick, 3)
                for tick in np.arange(args.ylim[0], args.ylim[1], args.ylim[2])
            ]
            yticks.append(args.ylim[1])
        ax.set_ylim(args.ylim[0], args.ylim[1])
        ax.set_yticks(yticks)
        ax.set_yticklabels(yticks, fontsize=args.ticksize)
    if args.digits is not None:
        plt.gca().yaxis.set_major_formatter(
            plt.FormatStrFormatter("%." + args.digits + "f")
        )

    if args.plot:
        plt.show()
    else:
        plt.savefig(args.figure)

#!/usr/bin/env python
"""Code to draw pareto optimal MLPs and all data points."""

import argparse

import matplotlib.pyplot as plt
import numpy as np

if __name__ == "__main__":

    parser = argparse.ArgumentParser()
    parser.add_argument("-d", "--dir", default=".", help="Directory name to read")
    parser.add_argument(
        "--all",
        type=str,
        default="all_feature",
        help="Data file name in which rmse and elapsed time of all MLPs are written.",
    )
    parser.add_argument(
        "--pareto",
        type=str,
        default="pareto_feature",
        help="Data file name in which rmse and elapsed time of pareto optimal MLPs are written.",
    )
    parser.add_argument("-p", "--plot", action="store_true", help="plot mode")
    parser.add_argument(
        "-f", "--figure", default="pareto_optimal.png", help="figure name"
    )
    parser.add_argument("--xlabel", default="# of features", help="X axis label")
    parser.add_argument(
        "--ylabel", default="RMSE(energy) (meV/atom)", help="Y axis label"
    )
    parser.add_argument(
        "--xlim",
        nargs=3,
        type=float,
        default=[None] * 3,
        help="x axis limits. start, stop, step",
    )
    parser.add_argument(
        "--ylim",
        nargs=3,
        type=float,
        default=[None] * 3,
        help="Y axis limits. start, stop, step",
    )
    parser.add_argument(
        "--ticksize", type=int, default=18, help="X axis or Y axis tick size"
    )
    parser.add_argument("--digits", default=None, help="number of digits")
    args = parser.parse_args()

    plt.rcParams["font.family"] = "Times New Roman"
    plt.rcParams["font.size"] = 19

    if ("energy" in args.all) and ("energy" in args.pareto):
        y_id = 2
    elif ("force" in args.all) and ("force" in args.pareto):
        y_id = 1
    else:
        y_id = 2

    mlp_dict = {}
    with open(args.dir + "/" + args.all) as f:
        for line in f:
            s_line = line.split()
            mlp_dict[s_line[-1]] = [float(s_line[0]), float(s_line[y_id])]
    points = np.array(list(mlp_dict.values()))

    fig, ax = plt.subplots()
    fig.set_tight_layout(True)
    fig.patch.set_alpha(0)
    with open(args.dir + "/" + args.pareto) as f:
        s_lines = [line.split() for line in f]
        for i in range(len(s_lines) - 1):
            mlp1 = s_lines[i][-1]
            mlp2 = s_lines[i + 1][-1]
            x = [mlp_dict[mlp1][0], mlp_dict[mlp2][0]]
            y = [mlp_dict[mlp1][1], mlp_dict[mlp2][1]]
            ax.plot(x, y, color="#1f77b4", linestyle="solid", lw=2, zorder=1)
    ax.scatter(
        points[:, 0],
        points[:, 1],
        s=6,
        color="k",
        marker=".",
        zorder=2,
        facecolor="None",
    )

    # graph appearance settings
    plt.xlabel(args.xlabel)
    plt.ylabel(args.ylabel)
    if args.xlim[0] is not None:
        if all([val.is_integer() for val in args.xlim]):
            xticks = [
                tick for tick in range(args.xlim[0], args.xlim[1] + 1, args.xlim[2])
            ]
            ax.set_xticks(xticks)
            ax.set_xticklabels(xticks, fontsize=args.ticksize)
        else:
            xticks = [
                round(tick, 3)
                for tick in np.arange(args.xlim[0], args.xlim[1], args.xlim[2])
            ]
            xticks.append(args.xlim[1])
        ax.set_xlim(args.xlim[0], args.xlim[1])
        ax.set_xticks(xticks)
        ax.set_xticklabels(xticks, fontsize=args.ticksize)
    if args.ylim[0] is not None:
        if all([val.is_integer() for val in args.ylim]):
            ylim = [int(val) for val in args.ylim]
            yticks = [tick for tick in range(ylim[0], ylim[1] + 1, ylim[2])]
        else:
            yticks = [
                round(tick, 3)
                for tick in np.arange(args.ylim[0], args.ylim[1], args.ylim[2])
            ]
            yticks.append(args.ylim[1])
        ax.set_ylim(args.ylim[0], args.ylim[1])
        ax.set_yticks(yticks)
        ax.set_yticklabels(yticks, fontsize=args.ticksize)
    if args.digits is not None:
        plt.gca().yaxis.set_major_formatter(
            plt.FormatStrFormatter("%." + args.digits + "f")
        )

    if args.plot:
        plt.show()
    else:
        plt.savefig(args.figure)
#!/usr/bin/env python Code to draw pareto optimal MLPs and all data points. # graph appearance settings