# Path: experiments/exp_setup.py
# SDCIT_RESULT_DIR = SDCIT_WORKING_DIR + '/results'
# 
# SDCIT_FIGURE_DIR = SDCIT_WORKING_DIR + '/figures'
# 
# Path: sdcit/utils.py
# def AUPC(p_values: Union[List, np.ndarray]) -> float:
#     """Area Under Power Curve"""
#     p_values = np.array(p_values)
# 
#     # CDF of p-values
#     xys = [(uniq_v, np.mean(p_values <= uniq_v)) for uniq_v in np.unique(p_values)]
# 
#     area, prev_x, prev_y = 0, 0, 0
#     for x, y in xys:
#         area += (x - prev_x) * prev_y
#         prev_x, prev_y = x, y
# 
#     area += (1 - prev_x) * prev_y
#     return area


import collections
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import scipy
import scipy.stats
import seaborn as sns
from os.path import exists
from experiments.exp_setup import SDCIT_RESULT_DIR, SDCIT_FIGURE_DIR
from sdcit.utils import AUPC
    aupc_df = pd.DataFrame({'algorithm': [str(v) for v in aupc_data[:, 0]],
                            'noise': [int(float(v)) for v in aupc_data[:, 1]],
                            'independent': [int(v) for v in aupc_data[:, 2]],
                            'N': [int(v) for v in aupc_data[:, 3]],
                            'AUPC': [float(v) for v in aupc_data[:, 4]]})
    aupc_df['dimension'] = (aupc_df['noise'] + 1).astype(int)

    aupc_df = aupc_df[aupc_df['independent'] == 0]
    aupc_df["algo-N"] = aupc_df["algorithm"].map(str) + aupc_df["N"].map(lambda xxx: ' (' + str(xxx) + ')')
    sns_setting()
    for k, gdf in aupc_df.groupby(['algorithm', 'N']):
        gdf = gdf[gdf['dimension'] <= 5]
        if k[1] == 400:
            plt.plot(gdf['dimension'], gdf['AUPC'], markers[(k[0])], c=color_palettes[method_color_codes[k[0]]] if k[1] == 400 else color_palettes[-0 + method_color_codes[k[0]]],
                     ls='-' if k[1] == 400 else ':', label=algo_name(str(k[0])))
        else:
            plt.plot(gdf['dimension'], gdf['AUPC'], markers[(k[0])], c=color_palettes[method_color_codes[k[0]]] if k[1] == 400 else color_palettes[-0 + method_color_codes[k[0]]],
                     ls='-' if k[1] == 400 else ':', label='_nolegend_')
    plt.axes().set_xlabel('dimension')
    plt.axes().set_ylabel('Area Under Power Curve')
    plt.axes().set_ylim([0.45, 1.05])
    sns.despine()
    plt.savefig(SDCIT_FIGURE_DIR + '/postnonlinear_aupc.pdf', transparent=True, bbox_inches='tight', pad_inches=0.02)
    plt.close()


def draw_aupc_postnonlinear_highdim():
    data = 'postnonlinear'
    aupc_data = []
    for algo in all_algos:

# Path: experiments/exp_setup.py
# SDCIT_RESULT_DIR = SDCIT_WORKING_DIR + '/results'
# 
# SDCIT_FIGURE_DIR = SDCIT_WORKING_DIR + '/figures'
# 
# Path: sdcit/utils.py
# def AUPC(p_values: Union[List, np.ndarray]) -> float:
#     """Area Under Power Curve"""
#     p_values = np.array(p_values)
# 
#     # CDF of p-values
#     xys = [(uniq_v, np.mean(p_values <= uniq_v)) for uniq_v in np.unique(p_values)]
# 
#     area, prev_x, prev_y = 0, 0, 0
#     for x, y in xys:
#         area += (x - prev_x) * prev_y
#         prev_x, prev_y = x, y
# 
#     area += (1 - prev_x) * prev_y
#     return area


# Path: experiments/draw_figures.py
import collections
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import scipy
import scipy.stats
import seaborn as sns
from os.path import exists
from experiments.exp_setup import SDCIT_RESULT_DIR, SDCIT_FIGURE_DIR
from sdcit.utils import AUPC

    aupc_df = pd.DataFrame({'algorithm': [str(v) for v in aupc_data[:, 0]],
                            'noise': [int(float(v)) for v in aupc_data[:, 1]],
                            'independent': [int(v) for v in aupc_data[:, 2]],
                            'N': [int(v) for v in aupc_data[:, 3]],
                            'AUPC': [float(v) for v in aupc_data[:, 4]]})
    aupc_df['dimension'] = (aupc_df['noise'] + 1).astype(int)

    aupc_df = aupc_df[aupc_df['independent'] == 0]
    aupc_df["algo-N"] = aupc_df["algorithm"].map(str) + aupc_df["N"].map(lambda xxx: ' (' + str(xxx) + ')')
    sns_setting()
    for k, gdf in aupc_df.groupby(['algorithm', 'N']):
        gdf = gdf[gdf['dimension'] <= 5]
        if k[1] == 400:
            plt.plot(gdf['dimension'], gdf['AUPC'], markers[(k[0])], c=color_palettes[method_color_codes[k[0]]] if k[1] == 400 else color_palettes[-0 + method_color_codes[k[0]]],
                     ls='-' if k[1] == 400 else ':', label=algo_name(str(k[0])))
        else:
            plt.plot(gdf['dimension'], gdf['AUPC'], markers[(k[0])], c=color_palettes[method_color_codes[k[0]]] if k[1] == 400 else color_palettes[-0 + method_color_codes[k[0]]],
                     ls='-' if k[1] == 400 else ':', label='_nolegend_')
    plt.axes().set_xlabel('dimension')
    plt.axes().set_ylabel('Area Under Power Curve')
    plt.axes().set_ylim([0.45, 1.05])
    sns.despine()
    plt.savefig(SDCIT_FIGURE_DIR + '/postnonlinear_aupc.pdf', transparent=True, bbox_inches='tight', pad_inches=0.02)
    plt.close()


def draw_aupc_postnonlinear_highdim():
    data = 'postnonlinear'
    aupc_data = []
    for algo in all_algos:

        df = pd.read_csv(SDCIT_RESULT_DIR + '/' + algo.lower() + '_' + data + '.csv', names=names[(algo, data)])