#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Sep  8 10:04:10 2025

Load results generated by ProposedAccuracyCV.py and print accuracy.
"""

import numpy as np
from tabulate import tabulate


if __name__ == "__main__":
    loadnames = ["retina_labelAdaDetect_LR_SVC",
                 "retinalOCT_partial_labelAdaDetect_LR_SVC",
                 "WBC_partial_labelAdaDetect_LR_SVC",
                 "FEMNIST_partial_labelAdaDetect_LR_SVC",
                 "MNIST_partial_fn_labelAdaDetect_LR_SVC",
                 "MNIST_partial_ln_labelAdaDetect_LR_SVC"]
    names = ["(S1)", "(S2)", "(S3)", "(S4)", "(S5)", "(S6)"]

    scores = np.zeros((len(loadnames), 6, 2), dtype=np.float32)
    for idx, loadname in enumerate(loadnames):
        in_data = np.load(f"{loadname}.npz")

        model_scores_baselines_COD_mean = np.mean(in_data["model_scores_baselines_COD"]*100, axis=0)
        model_scores_baselines_COD_std = np.std(in_data["model_scores_baselines_COD"]*100, axis=0)
        scores[idx, 1, 0] = model_scores_baselines_COD_mean
        scores[idx, 1, 1] = model_scores_baselines_COD_std

        model_scores_partoracles_th_mean = np.mean(in_data["model_scores_partoracles_th"]*100, axis=0)
        model_scores_partoracles_th_std = np.std(in_data["model_scores_partoracles_th"]*100, axis=0)
        bought_data_partoracles_th_mean = np.mean(in_data["bought_data_partoracles_th"], axis=0)
        scores[idx, 0, 0] = model_scores_partoracles_th_mean
        scores[idx, 0, 1] = model_scores_partoracles_th_std
        # print(bought_data_partoracles_th_mean)

        model_scores_proposed_mean = np.mean(in_data["model_scores_proposed"]*100, axis=0)
        model_scores_proposed_std = np.std(in_data["model_scores_proposed"]*100, axis=0)
        bought_data_proposed_mean = np.mean(in_data["bought_data_proposed"], axis=0)
        scores[idx, 2:, 0] = model_scores_proposed_mean
        scores[idx, 2:, 1] = model_scores_proposed_std
    
    headers = ["Oracle 2", "Random", "Storey", "Quantile", "Fisher", "Sum"]
    table = list()
    for idx in range(6):
        table.append([f"{names[idx]}"] + [f"{scores[idx, i, 0]:.3f} ({scores[idx, i, 1]:.3f})" for i in range(6)])
    print(tabulate(table, headers, tablefmt="latex", floatfmt=(".4f")))
