import quapy as qp
import numpy as np
from os import makedirs
import sys, os
import pickle
import argparse
from common import *
from reviews_experiments import *
from tabular import Table
import itertools

tables_path = './tables_reviews'
MAXTONE = 50  # sets the intensity of the maximum color reached by the worst (red) and best (green) results

makedirs(tables_path, exist_ok=True)

qp.environ['SAMPLE_SIZE'] = SAMPLE_SIZE



METHODS = ['cc', 'acc', 'pcc',
           'pacc',
           'wpacc',
           # 'MAX', 'MS', 'MS2',
           'sldc',
           # 'svmmae',
           # 'hdy',
           # 'ehdymaeds',
           # 'EMdiag', 'EMfull', 'EMtied', 'EMspherical',
           # 'VEMdiag', 'VEMfull', 'VEMtied', 'VEMspherical',
           ]


if __name__ == '__main__':
    results = 'results_reviews'

    datasets = qp.datasets.REVIEWS_SENTIMENT_DATASETS
    evaluation_measures = [qp.error.ae]

    run=0
    for i, eval_func in enumerate(evaluation_measures):
        eval_name = eval_func.__name__

        # Tables evaluation scores for the evaluation measure
        # ----------------------------------------------------
        # fill data table
        table = Table(benchmarks=datasets, methods=METHODS)
        for dataset, method in itertools.product(datasets, METHODS):
            table.add(dataset, method, experiment_errors(results, dataset, method, run, eval_name))

        # write the latex table
        nmethods = len(METHODS)
        tabular = """
                \\resizebox{\\textwidth}{!}{%
                        \\begin{tabular}{|c||""" + ('c|' * nmethods) + '|' + """} \hline
                          & \multicolumn{""" + str(nmethods) + """}{c||}{Quantification methods} \\\\ \hline
                  """
        rowreplace={dataset: nicename(dataset) for dataset in datasets}
        colreplace={method: nicename(method, eval_name, side=True) for method in METHODS}

        tabular += table.latexTabular(benchmark_replace=rowreplace, method_replace=colreplace)
        tabular += 'Rank Average & ' + table.getRankTable().latexAverage()
        tabular += """
            \end{tabular}%
            }
        """

        save_table(f'{tables_path}/tab_results_{eval_name}.tex', tabular)

    print("[Done]")