#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author:andrew
@file:Draw_Result.py
@email:admin@marques22.com
@email:2021022362@m.scnu.edu.cn
@time:2022/10/07
"""
from pathlib import Path

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import patchworklib as pw
# import seaborn as sns
import yaml
from plotnine import *

# from matplotlib import pyplot as plt
#
# plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
# plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号
from tqdm import tqdm


base_path = Path("../exam")
save_path = Path("../output/20221007")
save_path.mkdir(exist_ok=True, parents=True)
# 比较对象
all_exam_path = list(base_path.iterdir())
compare_path = [
    # '001',
    '002',
    # '003',
    # '004',
    '005',
    '011',
    '012',
    '013',
]


#
# compare_path = [
#     '001',
#     '002',
#     '003',
#     '004',
# ]

def load_data():
    columns = ["exam_name", "Fold_index", "sampNo", "severity", "origin_P", "origin_N", "pred_P", "pred_N", "T",
               "F", "TP", "TN", "FP", "FN", "acc", "recall", "spec", "pre", "NPV", "F1score", "support"]
    df_event_all_test_result = pd.DataFrame(columns=columns)
    df_segment_all_test_result = pd.DataFrame(columns=columns)
    df_event_test_result = pd.DataFrame(columns=columns)
    df_segment_test_result = pd.DataFrame(columns=columns)

    for index, exam_name in enumerate(compare_path):
        exam_path = base_path / exam_name / 'output'
        # all_sub_exam_path = list(exam_path.glob(f'*output*'))

        exam_path = list(exam_path.glob(f'output_*'))[0]
        print(exam_path)

        sub_fold_path = list(exam_path.glob("KFold_*"))
        sub_fold_path.sort()

        for sub_index, sub_exam_path in enumerate(sub_fold_path):
            segment_result_path = sub_exam_path / "segments_results"
            segment_test_result_path = segment_result_path / "test"
            segment_all_test_result_path = segment_result_path / "all_test"

            event_result_path = sub_exam_path / "events_results"
            event_test_result_path = event_result_path / "test"
            event_all_test_result_path = event_result_path / "all_test"

            for sub_samp in segment_test_result_path.glob("*_test_segment_all_metrics.csv"):
                df_temp = pd.read_csv(sub_samp, encoding="gbk")

                df_segment_test_result.loc[len(df_segment_test_result)] = [exam_name, sub_exam_path.name,
                                                                           *df_temp.iloc[0].tolist()]
                for i in range(5, len(df_temp)):
                    df_segment_test_result.loc[len(df_segment_test_result)] = [exam_name, sub_exam_path.name,
                                                                               *df_temp.iloc[i].tolist()]

            for sub_samp in segment_all_test_result_path.glob("*_all_test_segment_all_metrics.csv"):
                df_temp = pd.read_csv(sub_samp, encoding="gbk")

                for i in range(0, len(df_temp)):
                    df_segment_all_test_result.loc[len(df_segment_all_test_result)] = [exam_name, sub_exam_path.name,
                                                                                       *df_temp.iloc[i].tolist()]

            for sub_samp in event_test_result_path.glob("*_test_event_all_metrics.csv"):
                df_temp = pd.read_csv(sub_samp, encoding="gbk")

                df_event_test_result.loc[len(df_event_test_result)] = [exam_name, sub_exam_path.name,
                                                                       *df_temp.iloc[0].tolist()]
                for i in range(5, len(df_temp)):
                    df_event_test_result.loc[len(df_event_test_result)] = [exam_name, sub_exam_path.name,
                                                                           *df_temp.iloc[i].tolist()]

            for sub_samp in event_all_test_result_path.glob("*_all_test_event_all_metrics.csv"):
                df_temp = pd.read_csv(sub_samp, encoding="gbk")

                for i in range(0, len(df_temp)):
                    df_event_all_test_result.loc[len(df_event_all_test_result)] = [exam_name, sub_exam_path.name,
                                                                                   *df_temp.iloc[i].tolist()]

        df_event_test_result.to_csv(save_path / "event_test_result_metrics.csv", index=False, encoding="GBK")
        df_segment_test_result.to_csv(save_path / "segment_test_result_metrics.csv", index=False, encoding="GBK")
        df_event_all_test_result.to_csv(save_path / "event_all_test_result_metrics.csv", index=False, encoding="GBK")
        df_segment_all_test_result.to_csv(save_path / "segment_all_test_segment_metrics.csv", index=False,
                                          encoding="GBK")

    return

if __name__ == '__main__':
    pass