# -*- coding: UTF-8 -*-

import numpy as np
import os
import sys
import time
from figure import Figure
from data_process import data_process
from data_features import data_features
from logger import load_logger
from config import myargparse
from data_analyse import data_analyse
frame = "keras"

if frame == "keras":
    from model.model_keras_1 import train, predict
    os.environ["TF_CPP_MIN_LOG_LEVEL"] = '3'
else:
    raise Exception("Wrong frame seletion")


def main():
    parse = myargparse(discription="training config", add_help=False)
    opt = parse.parse_args_and_yaml()
    logger = load_logger(opt)
    try:
        np.random.seed(opt.Model["random_seed"])
        process = data_process(opt=opt)
        dfs_train, dfs_test = process.get_processed_data([9, 10])
        features = data_features(opt=opt, mean=process.mean, std=process.std)
        if opt.do_train:
            train_X, valid_X, train_Y, valid_Y = features.get_train_data(dfs_train)
            # train_Y = [np.array([y[:, 0] for y in train_Y]), np.array([y[:, 1] for y in train_Y])]
            # valid_Y = [np.array([y[:, 0] for y in valid_Y]), np.array([y[:, 1] for y in valid_Y])]

            train_Y = [np.array([y[:, 3] for y in train_Y])]
            valid_Y = [np.array([y[:, 3] for y in valid_Y])]
            train(opt, [train_X, train_Y, valid_X, valid_Y])
        if opt.do_predict:
            test_X, test_Y, df_Y = features.get_test_data(dfs_test)
            result = predict(opt, test_X)       # 这里输出的是未还原的归一化预测数据
            analyse = data_analyse(opt, logger, process)
            analyse.predict_acc(result, df_Y, predict_all=True)
    except Exception:
        logger.error("Run Error", exc_info=True)


if __name__ == "__main__":
    import argparse
    # argparse方便于命令行下输入参数，可以根据需要增加更多
    # parser = argparse.ArgumentParser()
    # parser.add_argument("-t", "--do_train", default=False, type=bool, help="whether to train")
    # parser.add_argument("-p", "--do_predict", default=True, type=bool, help="whether to train")
    # parser.add_argument("-b", "--batch_size", default=64, type=int, help="batch size")
    # parser.add_argument("-e", "--epoch", default=20, type=int, help="epochs num")
    # args = parser.parse_args()

    # con = Config()
    # for key in dir(args):               # dir(args) 函数获得args所有的属性
    #     if not key.startswith("_"):     # 去掉 args 自带属性，比如__name__等
    #         setattr(con, key, getattr(args, key))   # 将属性值赋给Config
    main()

