#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import pandas as pd

pd.set_option('display.width', 2000)  # nezalamuj tabulky

from funkce.nacteni import *  # znaceni, channels, pocty_znacek, signal, sig_df_to_array
from funkce.vypocet import *
from pac_fac_all_frequency_bands import *
from sklearn import metrics

# --- výpis počtů značek věech subjektů
seegs = [15, 32, 45, 48]
# seegs = [15]
okoli = 150
okoli = 60


def vypocet_roc(seegs, okoli):
    """
    This function returns AUC, FPR, TPR, Thresholds and main_df

    Parameters:
    -----------
    seeg - patient number, e.g. 15, 32 or 48

    Returns:
    --------
    main_df - concatenated dataframes for further exploration (-> function vypocet_all_in_one_roc_seeg)

    Quick_call: main_df_seegs, df_AUC_fpr_tpr_sorted = vypocet_roc(seegs, okoli)

    """
    # definice nových df
    df_AUC_fpr_tpr = pd.DataFrame(columns=['Seeg', 'Parametr', 'Target', 'AUC', 'fpr', 'tpr', 'thresholds'])
    i = 0

    import matplotlib.pyplot as plt
    from sklearn.metrics import roc_auc_score

    main_df_seegs_list = []

    for s in seegs:
        # výpočty pro jednotlivé pacienty + výpočet pro concat databázi

        print('Počítám seeg:' + str(s) + '')

        #  vytvoření concat databáze pro funkcni vypocet_all_in_one_roc_seeg
        main_df = vypocet_metod_a_vykresleni([s], okoli)
        main_df['seeg'] = s
        plt.close('all')

        # Target od nejvyššího dle parametru
        # main_df_sorted = main_df.sort_values(['fac'], ascending=False)

        # binarizace dat
        # main_df_sorted['resection_0_1'] = np.where(main_df_sorted.resection == 'yes',1,main_df_sorted.resection_0_1)

        # výpočet pro jednotlivé pacienty

        # --- dělení podle FR a vFR, uFR smazáno
        main_df_bez_uFR = main_df.copy()
        # mazání řádků s uFR
        main_df_bez_uFR = main_df_bez_uFR[~(main_df_bez_uFR.astype(str) == 'uFR').any(1)]

        main_df_bez_uFR['FR_vFR_0_1'] = main_df_bez_uFR['group']

        # stačilo by brát podle group 0, 1, není třeba vytvářet nobý df
        main_df_bez_uFR.loc[main_df_bez_uFR['group'] == 0, 'FR_vFR_0_1'] = 0
        main_df_bez_uFR.loc[main_df_bez_uFR['group'] == 1, 'FR_vFR_0_1'] = 1

        # --- dělení podle resekovaných kanálů
        main_df['resection_0_1'] = main_df['resection']

        main_df.loc[main_df['resection'] == 'yes', 'resection_0_1'] = 1
        main_df.loc[main_df['resection'] == 'no', 'resection_0_1'] = 0

        # --- dělení podle FR a (vFR+uFR) kanálů
        main_df['FR_vFR_uFR_0_0_1'] = main_df['group']

        main_df.loc[main_df['group'] == 0, 'FR_vFR_uFR_0_0_1'] = 0
        main_df.loc[main_df['group'] == 1, 'FR_vFR_uFR_0_0_1'] = 0
        main_df.loc[main_df['group'] == 2, 'FR_vFR_uFR_0_0_1'] = 1

        # --- dělení podle (FR+vFR) a uFR kanálů
        main_df['FR_vFR_uFR_0_1_1'] = main_df['group']

        main_df.loc[main_df['group'] == 0, 'FR_vFR_uFR_0_1_1'] = 0
        main_df.loc[main_df['group'] == 1, 'FR_vFR_uFR_0_1_1'] = 1
        main_df.loc[main_df['group'] == 2, 'FR_vFR_uFR_0_1_1'] = 1

        # volba typu target
        # target = 'resection_0_1'
        # target = 'FR_vFR_uFR_0_0_1'
        # target = 'FR_vFR_uFR_0_1_1'
        # target = 'FR_vFR_0_1' # v df main_df_bez_uFR

        # pozdjei volba parametru
        # par = 'pse'
        # par = 'fac'
        # par = 'pac'

        target = ['resection_0_1', 'FR_vFR_uFR_0_0_1', 'FR_vFR_uFR_0_1_1', 'FR_vFR_0_1']
        par = ['pse', 'fac', 'pac', 'shannon_entropy', 'sample_entropy0', 'sample_entropy1']  # , 'shannon_entropy0']

        # --- Výpočet různých kombinací metod a Target (targetu)
        for tar in target:
            for p in par:

                if tar == 'FR_vFR_0_1':
                    y_true = main_df_bez_uFR[str(tar)].values
                    y_scores = main_df_bez_uFR[str(p)].values
                    scores = roc_auc_score(y_true, y_scores)

                    fpr, tpr, thresholds = metrics.roc_curve(y_true, y_scores)


                else:
                    y_true = main_df[str(tar)].values
                    y_scores = main_df[str(p)].values
                    scores = roc_auc_score(y_true, y_scores)

                    fpr, tpr, thresholds = metrics.roc_curve(y_true, y_scores)

                df_AUC_fpr_tpr.loc[i] = [s, p, tar, scores, fpr, tpr, thresholds]

                i = i + 1

        # main_df_seegs_list.append(main_df)

        main_df_seegs_list.append(main_df)

    if len(seegs) > 1:
        main_df_seegs = pd.concat(main_df_seegs_list, sort=True)
        main_df_seegs = main_df_seegs.reset_index(drop=True)

    else:
        main_df_seegs = main_df

    df_AUC_fpr_tpr_sorted = df_AUC_fpr_tpr.sort_values(by=['AUC'], ascending=False)

    return main_df_seegs, df_AUC_fpr_tpr_sorted


# main_df_seegs, df_AUC_fpr_tpr_sorted = vypocet_roc(seegs, okoli)


#  ---- výpočet pro concat databázi:

# --- dělení podle resekovaných kanálů - u concat df nemá smysl (někteří mají dobrý outcome, jiní špatný)
# main_df_seegs['resection_0_1'] = main_df['resection']
# main_df_seegs.loc[main_df_seegs['resection'] == 'yes', 'resection_0_1'] = 1
# main_df_seegs.loc[main_df_seegs['resection'] == 'no', 'resection_0_1'] = 0

# resekce
# main_df_seegs_sorted = main_df_seegs.sort_values(['resection_0_1'], ascending=False)



def zmena_AUC_dle_metod():

    """

    :return:

    Quick call: main_df_seegs, df_AUC_fpr_tpr_sorted_zmena, df_seegs_AUC_fpr_tpr_sorted_zmena = zmena_AUC_dle_metod()
    """
    # načtení proměnných
    #main_df_seegs, df_AUC_fpr_tpr_sorted = vypocet_all_in_one_roc_seeg()
    main_df_seegs, df_AUC_all, df_AUC_fpr_tpr_sorted, df_seegs_AUC_fpr_tpr_sorted = vypocet_all_in_one_roc_seeg()

    del df_AUC_all #nebudu tady nic měnit, dál s ní nepočítám -> mažu(vytvářím ho znovu concatem do .csv)

   # nový df
    df_AUC_fpr_tpr_sorted_zmena = df_AUC_fpr_tpr_sorted.copy()
    df_seegs_AUC_fpr_tpr_sorted_zmena = df_seegs_AUC_fpr_tpr_sorted.copy()



     # target

    zmena_metody_target = ['fac', 'pse']
    zmena_sloupce = ['AUC', 'fpr', 'tpr'] #neměnit
    targets = ['FR_vFR_uFR_0_0_1', 'FR_vFR_uFR_0_1_1', 'FR_vFR_0_1']

    for tar in targets:
        for metoda in zmena_metody_target:
            for sloupec in zmena_sloupce:
                df_AUC_fpr_tpr_sorted_zmena.loc[(df_AUC_fpr_tpr_sorted_zmena['Parametr'] == metoda) & (df_AUC_fpr_tpr_sorted_zmena['Target'] == tar), sloupec] = 1 - df_AUC_fpr_tpr_sorted_zmena.loc[df_AUC_fpr_tpr_sorted_zmena['Parametr'] == metoda, sloupec]
                df_seegs_AUC_fpr_tpr_sorted_zmena.loc[(df_seegs_AUC_fpr_tpr_sorted_zmena['Parametr'] == metoda) & (df_seegs_AUC_fpr_tpr_sorted_zmena['Target'] == tar), sloupec] = 1 - df_seegs_AUC_fpr_tpr_sorted_zmena.loc[df_seegs_AUC_fpr_tpr_sorted_zmena['Parametr'] == metoda, sloupec]


    #df_AUC_fpr_tpr_sorted.loc[df_AUC_fpr_tpr_sorted['Parametr'] == 'fac', 'AUC'] = 1 - df_AUC_fpr_tpr_sorted.loc[df_AUC_fpr_tpr_sorted['Parametr'] == 'fac', 'AUC']
    #df_AUC_fpr_tpr_sorted.loc[df_AUC_fpr_tpr_sorted['Parametr'] == 'fac', 'fpr'] = 1 - df_AUC_fpr_tpr_sorted.loc[df_AUC_fpr_tpr_sorted['Parametr'] == 'fac', 'fpr']
    #df_AUC_fpr_tpr_sorted.loc[df_AUC_fpr_tpr_sorted['Parametr'] == 'fac', 'tpr'] = 1 - df_AUC_fpr_tpr_sorted.loc[df_AUC_fpr_tpr_sorted['Parametr'] == 'fac', 'tpr']


    # resekce

    zmena_metody_resekce = ['fac', 'pac', 'pse', 'shannon_entropy']
    zmena_sloupce = ['AUC', 'fpr', 'tpr'] #neměnit

    for metoda in zmena_metody_resekce:
        for sloupec in zmena_sloupce:
            df_AUC_fpr_tpr_sorted_zmena.loc[(df_AUC_fpr_tpr_sorted_zmena['Parametr'] == metoda) & (df_AUC_fpr_tpr_sorted_zmena['Target'] == 'resection_0_1'), sloupec] = 1 - df_AUC_fpr_tpr_sorted_zmena.loc[df_AUC_fpr_tpr_sorted_zmena['Parametr'] == metoda, sloupec]
            #funkční, ale souhrn prozatím neexistuje:
            #df_seegs_AUC_fpr_tpr_sorted_zmena.loc[(df_seegs_AUC_fpr_tpr_sorted_zmena['Parametr'] == metoda) & (df_seegs_AUC_fpr_tpr_sorted_zmena['Target'] == 'resection_0_1'), sloupec] = 1 - df_seegs_AUC_fpr_tpr_sorted_zmena.loc[df_seegs_AUC_fpr_tpr_sorted_zmena['Parametr'] == metoda, sloupec]


    df_AUC_zmena_all = pd.concat([df_AUC_fpr_tpr_sorted_zmena, df_seegs_AUC_fpr_tpr_sorted_zmena])

    df_AUC_zmena_all.to_pickle("Dataframes/df_AUC_zmena_all" + str(seegs) + "_okoli_" + str(2 * okoli) + ".pkl")
    df_AUC_zmena_all.to_csv("Dataframes/df_AUC_zmena_all" + str(seegs) + "_okoli_" + str(2 * okoli) + ".csv", sep='\t',encoding='utf-8')

    #main_df_seegs.to_pickle("Dataframes/main_df_seegs" + str(seegs) + "_okoli_" + str(2 * okoli) + ".pkl")
    df_AUC_fpr_tpr_sorted_zmena.to_pickle("Dataframes/df_AUC_zmena_fpr_tpr_sorted" + str(seegs) + "_okoli_" + str(2 * okoli) + ".pkl")
    df_seegs_AUC_fpr_tpr_sorted_zmena.to_pickle("Dataframes/df_seegs_AUC_zmena_fpr_tpr_sorted" + str(seegs) + "_okoli_" + str(2 * okoli) + ".pkl")

    return main_df_seegs, df_AUC_fpr_tpr_sorted_zmena, df_seegs_AUC_fpr_tpr_sorted_zmena





def vypocet_all_in_one_roc_seeg():
    """
    This function returns AUC, FPR, TPR, Thresholds and main_df

    Parameters:
    -----------
    seeg - patient number, e.g. 15, 32 or 48

    Returns:
    --------
    main_df - concatenated dataframes for further exploration (-> function vypocet_all_in_one_roc_seeg)

    Quick_call: main_df_seegs, df_AUC_all, df_AUC_fpr_tpr_sorted, df_seegs_AUC_fpr_tpr_sorted = vypocet_all_in_one_roc_seeg()

    """

    main_df_seegs, df_AUC_fpr_tpr_sorted = vypocet_roc(seegs, okoli)
    # main_df_seegs, df_AUC_fpr_tpr_sorted_zmena = zmena_AUC_dle_metod()


    from sklearn.metrics import roc_auc_score

    # --- dělení podle FR a vFR, uFR smazáno
    main_df_seegs_bez_uFR = main_df_seegs.copy()
    # mazání řádků s uFR
    main_df_seegs_bez_uFR = main_df_seegs_bez_uFR[~(main_df_seegs_bez_uFR.astype(str) == 'uFR').any(1)]
    main_df_seegs_bez_uFR['FR_vFR_0_1'] = main_df_seegs_bez_uFR['group']

    # stačilo by brát podle group 0, 1, není třeba vytvářet nobý df
    main_df_seegs_bez_uFR.loc[main_df_seegs_bez_uFR['group'] == 0, 'FR_vFR_0_1'] = 0
    main_df_seegs_bez_uFR.loc[main_df_seegs_bez_uFR['group'] == 1, 'FR_vFR_0_1'] = 1

    # --- dělení podle FR a (vFR+uFR) kanálů
    # main_df_seegs['FR_vFR_uFR_0_0_1'] = main_df['group']
    main_df_seegs.loc[main_df_seegs['group'] == 0, 'FR_vFR_uFR_0_0_1'] = 0  # obr.hodnoty (dříve (1 1 0)
    main_df_seegs.loc[main_df_seegs['group'] == 1, 'FR_vFR_uFR_0_0_1'] = 0
    main_df_seegs.loc[main_df_seegs['group'] == 2, 'FR_vFR_uFR_0_0_1'] = 1

    # --- dělení podle (FR+vFR) a uFR kanálů
    # main_df_seegs['FR_vFR_uFR_0_1_1'] = main_df['group']
    main_df_seegs.loc[main_df_seegs['group'] == 0, 'FR_vFR_uFR_0_1_1'] = 0  # obr.hodnoty (dříve 0 1 1)
    main_df_seegs.loc[main_df_seegs['group'] == 1, 'FR_vFR_uFR_0_1_1'] = 1
    main_df_seegs.loc[main_df_seegs['group'] == 2, 'FR_vFR_uFR_0_1_1'] = 1

    target = ['FR_vFR_uFR_0_0_1', 'FR_vFR_uFR_0_1_1', 'FR_vFR_0_1']
    par = ['pse', 'fac', 'pac', 'shannon_entropy', 'sample_entropy0', 'sample_entropy1']  # , 'shannon_entropy0']

    # definice nových df
    df_seegs_AUC_fpr_tpr = pd.DataFrame(columns=['Seeg', 'Parametr', 'Target', 'AUC', 'fpr', 'tpr', 'thresholds'])
    i = 0

    for tar in target:
        for p in par:

            if tar == 'FR_vFR_0_1':
                y_true = main_df_seegs_bez_uFR[str(tar)].values
                y_scores = main_df_seegs_bez_uFR[str(p)].values
                scores = roc_auc_score(y_true, y_scores)

                fpr, tpr, thresholds = metrics.roc_curve(y_true, y_scores)


            else:
                y_true = main_df_seegs[str(tar)].values
                y_scores = main_df_seegs[str(p)].values
                scores = roc_auc_score(y_true, y_scores)

                fpr, tpr, thresholds = metrics.roc_curve(y_true, y_scores)

            # df_seegs_AUC_fpr_tpr.loc[i] = [str('all seegs'), p, tar, scores, fpr, tpr, thresholds]
            all_seegs = 0
            df_seegs_AUC_fpr_tpr.loc[i] = [all_seegs, p, tar, scores, fpr, tpr, thresholds]
            # df_seegs_AUC_fpr_tpr.loc[i] = [str('all seegs'), p, tar, scores, fpr, tpr, thresholds]

            i = i + 1

    df_seegs_AUC_fpr_tpr_sorted = df_seegs_AUC_fpr_tpr.sort_values(by=['AUC'], ascending=False)

    # výstupy

    df_AUC_fpr_tpr_sorted = df_AUC_fpr_tpr_sorted.reset_index(drop=True)
    df_seegs_AUC_fpr_tpr_sorted = df_seegs_AUC_fpr_tpr_sorted.reset_index(drop=True)

    df_AUC_all = pd.concat([df_AUC_fpr_tpr_sorted, df_seegs_AUC_fpr_tpr_sorted])
    df_AUC_all_sorted = df_AUC_all.sort_values(by=['Parametr', 'Target', 'AUC'], ascending=False)

    df_AUC_all.to_pickle("Dataframes/df_AUC_all" + str(seegs) + "_okoli_" + str(2 * okoli) + ".pkl")

    # df_AUC_all = pd.read_pickle("Dataframes/df_AUC_all" + str(seegs) + "_okoli_" + str(2 * okoli) + ".pkl")

    df_AUC_all.to_csv("Dataframes/df_AUC_all" + str(seegs) + "_okoli_" + str(2 * okoli) + ".csv", sep='\t',
                      encoding='utf-8')

    main_df_seegs.to_pickle("Dataframes/main_df_seegs" + str(seegs) + "_okoli_" + str(2 * okoli) + ".pkl")
    df_AUC_fpr_tpr_sorted.to_pickle(
        "Dataframes/df_AUC_fpr_tpr_sorted" + str(seegs) + "_okoli_" + str(2 * okoli) + ".pkl")
    df_seegs_AUC_fpr_tpr_sorted.to_pickle(
        "Dataframes/df_seegs_AUC_fpr_tpr_sorted" + str(seegs) + "_okoli_" + str(2 * okoli) + ".pkl")

    return main_df_seegs, df_AUC_all, df_AUC_fpr_tpr_sorted, df_seegs_AUC_fpr_tpr_sorted


def vykresleni_roc():

    """
    This function returns graphical representation of AUC - ROC curve

    Parameters:
    -----------
    seeg - patient number, e.g. 15, 32 or 48

    Returns:
    --------

    Quick_call:
    vykresleni_roc()

    vykresleni_roc('vsechny_seegs')
    vykresleni_roc('souhrn_seegs')


    """
    seegs = [15,32,45,48]
    okoli = 150
    okoli = 60

    import matplotlib.pyplot as plt
    import numpy as np
    import pandas as pd

    try:
        df_AUC_fpr_tpr_sorted = pd.read_pickle(
            "Dataframes/df_AUC_zmena_fpr_tpr_sorted" + str(seegs) + "_okoli_" + str(2 * okoli) + ".pkl")
        df_seegs_AUC_fpr_tpr_sorted = pd.read_pickle(
            "Dataframes/df_seegs_AUC_zmena_fpr_tpr_sorted" + str(seegs) + "_okoli_" + str(2 * okoli) + ".pkl")
    except:
        #main_df_seegs, df_AUC_all, df_AUC_fpr_tpr_sorted, df_seegs_AUC_fpr_tpr_sorted = vypocet_all_in_one_roc_seeg()
        main_df_seegs, df_AUC_fpr_tpr_sorted_zmena, df_seegs_AUC_fpr_tpr_sorted_zmena = zmena_AUC_dle_metod()
        df_AUC_fpr_tpr_sorted = df_AUC_fpr_tpr_sorted_zmena.copy()
        df_seegs_AUC_fpr_tpr_sorted = df_seegs_AUC_fpr_tpr_sorted_zmena.copy()



    volba = 0

    moznosti_volby_df = ['vsechny_seegs', 'souhrn_seegs']
    #volba_df = 'vsechny_seegs'
    #volba_df = 'souhrn_seegs'

    for volba_df in moznosti_volby_df:

        if volba_df == 'vsechny_seegs':
            df_AUC = df_AUC_fpr_tpr_sorted
            target = ['resection_0_1', 'FR_vFR_uFR_0_0_1', 'FR_vFR_uFR_0_1_1', 'FR_vFR_0_1']
            pars = ['pse', 'fac', 'pac', 'shannon_entropy', 'sample_entropy0', 'sample_entropy1']  # , 'shannon_entropy0']

            """nutno dořešit vykreslování - jsou zde 4x vybrané fpr"""


        elif volba_df == 'souhrn_seegs':
            df_AUC = df_seegs_AUC_fpr_tpr_sorted
            target = ['FR_vFR_uFR_0_0_1', 'FR_vFR_uFR_0_1_1', 'FR_vFR_0_1']
            pars = ['pse', 'fac', 'pac', 'shannon_entropy', 'sample_entropy0', 'sample_entropy1']  # , 'shannon_entropy0']

        s = 15

        def vykresleni_targety(s):
            '''přidat barvičky do grafu a do legendy názvy metod'''
            colors = ['purple', 'green', 'red', 'orange', 'dodgerblue', 'grey']

            for tar in target:

                c_i = 0

                print(str(volba_df) + ', počítám target ' + str(tar))

                plt.figure()
                plt.plot([0, 1], [0, 1], color='navy', linestyle='--')

                for par in pars:

                    """výběr na základě parametru"""

                    if volba_df == 'vsechny_seegs':
                        subset_temp = df_AUC[(df_AUC['Seeg'] == s) & (df_AUC['Target'] == tar) & (df_AUC['Parametr'] == par)]  # df[(df.a != -1) & (df.b != -1)]

                    elif volba_df == 'souhrn_seegs':
                        subset_temp = df_AUC[
                            (df_AUC['Target'] == tar) & (df_AUC['Parametr'] == par)]  # df[(df.a != -1) & (df.b != -1)]

                    subsets_temp_fpr = subset_temp['fpr'].reset_index(drop=True)
                    subsets_temp_tpr = subset_temp['tpr'].reset_index(drop=True)

                    # fpr_se = subset_temp['fpr'].first() #ndarray
                    # tpr_se = subset_temp['tpr'][volba]
                    # leg = subset_temp['Parametr']
                    leg_metoda = subset_temp.Parametr.values
                    leg_AUC = np.round(subset_temp.AUC.values, 2)
                    # leg_metoda = subset_temp.Parametr.values


                    """zjistit, proč je těch fpr, tpr víc.. resp.proč potřebuji indexovat"""
                    fpr_se = subsets_temp_fpr[volba]
                    tpr_se = subsets_temp_tpr[volba]

                    # plt.legend(['AUC: ' + str(round(df_seegs_AUC_fpr_tpr_sorted.loc[volba].AUC,2))+ '', 'diagonal'], loc='lower right') #, loc="lower right")

                    plt.plot(fpr_se, tpr_se, label=(str(leg_metoda) + ", AUC: " + str(leg_AUC)),
                             color=colors[c_i])  # , color='darkorange')
                    c_i = c_i + 1
                    # plt.legend('metoda: ' + str(leg))

                plt.legend(loc=0)
                plt.xlabel('False Positive Rate')
                plt.ylabel('True Positive Rate')

                if volba_df == 'vsechny_seegs':
                    plt.title('Receiver operating characteristic_seeg: ' + str(s) + ',' '\n' ' target: ' + str(
                        tar))  # + ', y_true:'+ str(df_seegs_AUC_fpr_tpr_sorted.loc[volba].Target)+ '')
                    plt.savefig("Grafy/ROC_seeg_" + str(s) + "_all_parameters_target_" + str(tar) + ".png")


                elif volba_df == 'souhrn_seegs':
                    plt.title('Receiver operating characteristic_seeg: ' + str(volba_df) + ',' '\n' ' target: ' + str(
                        tar))  # + ', y_true:'+ str(df_seegs_AUC_fpr_tpr_sorted.loc[volba].Target)+ '')
                    plt.savefig("Grafy/ROC_seeg_" + str(volba_df) + "_all_parameters_target_" + str(tar) + ".png")

                plt.show()




        if volba_df == 'vsechny_seegs':
            seegs = [15,32, 45, 48]

            for s in seegs:
                vykresleni_targety(s)

        elif volba_df == 'souhrn_seegs':
            vykresleni_targety(s) # zde je s nadbytečné





        plt.close('all')


        """vykreslení pro jednotlivé pacienty a jednotlivé metody"""
        volba = 0

        df_AUC = df_AUC_fpr_tpr_sorted

        target = ['resection_0_1', 'FR_vFR_uFR_0_0_1', 'FR_vFR_uFR_0_1_1', 'FR_vFR_0_1']
        target = ['FR_vFR_uFR_0_0_1', 'FR_vFR_uFR_0_1_1', 'FR_vFR_0_1']

        pars = ['pse', 'fac', 'pac', 'shannon_entropy', 'sample_entropy0', 'sample_entropy1']  # , 'shannon_entropy0']

        for s in seegs:

            '''přidat barvičky do grafu a do legendy názvy metod'''
            colors = ['purple', 'green', 'red', 'orange', 'dodgerblue', 'grey']

            c_i = 0


            for par in pars:

                print(str(volba_df) + ', počítám metodu ' + str(par) + ', seeg ' + str(s))

                plt.figure()
                plt.plot([0, 1], [0, 1], color='navy', linestyle='--')

                a = 0.3

                for tar in target:

                    """výběr na základě parametru"""

                    subset_temp = df_AUC[(df_AUC['Seeg'] == s) & (df_AUC['Target'] == tar) & (df_AUC['Parametr'] == par)]  # df[(df.a != -1) & (df.b != -1)]

                    subsets_temp_fpr = subset_temp['fpr'].reset_index(drop=True)
                    subsets_temp_tpr = subset_temp['tpr'].reset_index(drop=True)

                    leg_metoda = subset_temp.Parametr.values
                    leg_target = subset_temp.Target.values
                    leg_AUC = np.round(subset_temp.AUC.values, 2)

                    """zjistit, proč je těch fpr, tpr víc.. resp.proč potřebuji indexovat"""
                    fpr_se = subsets_temp_fpr[volba]
                    tpr_se = subsets_temp_tpr[volba]

                    plt.plot(fpr_se, tpr_se, label=(str(leg_target) + " AUC: " + str(leg_AUC)), color=colors[c_i], alpha = a)
                    a = a + 0.3


                c_i = c_i + 1

                plt.legend(loc=0)
                plt.xlabel('False Positive Rate')
                plt.ylabel('True Positive Rate')

                plt.title('Receiver operating characteristic_seeg: ' + str(s) + ',' '\n' ' parameter: ' + str(par))
                plt.savefig("Grafy/ROC_seeg_" + str(s) + "_all_targets_parameter_" + str(par) + ".png")

                plt.show()




        plt.close('all')


        """vykreslení pro jednotlivé metody a všechny pacienty"""
        volba = 0

        df_AUC = df_AUC_fpr_tpr_sorted

        target = ['resection_0_1', 'FR_vFR_uFR_0_0_1', 'FR_vFR_uFR_0_1_1', 'FR_vFR_0_1']
        target = ['FR_vFR_uFR_0_0_1', 'FR_vFR_uFR_0_1_1', 'FR_vFR_0_1']

        pars = ['pse', 'fac', 'pac', 'shannon_entropy', 'sample_entropy0', 'sample_entropy1']  # , 'shannon_entropy0']

        for par in pars:

            '''přidat barvičky do grafu a do legendy názvy metod'''
            colors = ['green', 'red', 'blue']

            c_i = 0

            print(str(volba_df) + ', počítám metodu ' + str(par))
            plt.figure(figsize=[9,5])
            plt.suptitle('Receiver operating characteristic_seeg: ' + str(seegs) + ',' '\n' ' parameter: ' + str(par))
            #plt.plot([0, 1], [0, 1], color='navy', linestyle='--')

            #plt.figure(fig+30, figsize=(9,9)) #20,5/9,9
            #plt.suptitle('Hlavní title, metoda ' +str(par)+'')

            tar_i = 1

            for tar in target:

                plt.subplot(1,3,tar_i)

                a = 0.2

                for s in seegs:

                    """výběr na základě parametru"""

                    subset_temp = df_AUC[(df_AUC['Seeg'] == s) & (df_AUC['Target'] == tar) & (df_AUC['Parametr'] == par)]  # df[(df.a != -1) & (df.b != -1)]

                    subsets_temp_fpr = subset_temp['fpr'].reset_index(drop=True)
                    subsets_temp_tpr = subset_temp['tpr'].reset_index(drop=True)

                    leg_metoda = subset_temp.Parametr.values
                    leg_target = subset_temp.Target.values
                    leg_seeg = subset_temp.Seeg.values
                    leg_AUC = np.round(subset_temp.AUC.values, 2)

                    """zjistit, proč je těch fpr, tpr víc.. resp.proč potřebuji indexovat"""
                    fpr_se = subsets_temp_fpr[volba]
                    tpr_se = subsets_temp_tpr[volba]

                    plt.plot(fpr_se, tpr_se, label=('Pacient: ' +str(leg_seeg) + ', AUC:' + str(leg_AUC)), color=colors[c_i], alpha = a, linewidth=2)
                    #plt.legend(bbox_to_anchor=(0.5,0), loc=9, borderaxespad=0.)


                    a = a + 0.25

                c_i = c_i + 1

                plt.tight_layout()

                plt.subplots_adjust(top=0.85) #, bottom=0.166)

                plt.legend(loc=9)
                plt.legend(bbox_to_anchor=(0.5,-0.2), loc=9, borderaxespad=0.)
                plt.xlabel('False Positive Rate')
                plt.ylabel('True Positive Rate')

                plt.plot([0, 1], [0, 1], color='navy', linestyle='--')


                plt.title('Target ' + str(tar))
                plt.savefig("Grafy/ROC_all_seegs_all_targets_subplot_parameter_" + str(par) + ".png")

                plt.show()

                tar_i = tar_i + 1

        plt.close('all')




def vykresli_density(seegs):
    import seaborn as sns
    import matplotlib
    import matplotlib.pyplot as plt
    matplotlib.use('Agg')

    try:
        main_df_seegs = pd.read_pickle("Dataframes/main_df_seegs" + str(seegs) + "_okoli_" + str(2 * okoli) + ".pkl")
    except:
        main_df_seegs, df_AUC_all, df_AUC_fpr_tpr_sorted, df_seegs_AUC_fpr_tpr_sorted = vypocet_all_in_one_roc_seeg()

    # met = 'fac'#'shannon_entropy' #'sample_entropy1'
    mets = ['pse', 'fac', 'pac', 'shannon_entropy', 'sample_entropy0', 'sample_entropy1']

    # metoda = 'metoda'#'Frequency-amplitude coupling'
    #seegs = [15, 32, 45, 48]

    for met in mets:
        for seeg in seegs:
            subset = main_df_seegs[main_df_seegs['seeg'] == seeg]

            plt.figure(figsize=(5,3))
            sns.boxplot(x=subset[met], y=subset['resection'], hue=subset['resection'],
                        hue_order=['no', 'yes'])  # , palette="Greys")
            # sns.boxplot(x='resection', y="sample_entropy1", data=subset, width=0.35, order=['no', 'yes'], orient='v', palette="Greys") #,
            plt.title('Rozdělení podle resekce pro: ' + str(met) + ', pacient ' + str(seeg))
            plt.savefig("Grafy/dist_boxplot_" + str(met) + '_pacient_' + str(seeg) + ".png")
            # plt.show()

            subset_res_no = subset[subset['resection'] == 'no']
            subset_res_yes = subset[subset['resection'] == 'yes']

            plt.figure()
            sns.distplot(subset_res_no[met], hist=False, kde=True, kde_kws={'shade': True, 'linewidth': 3},
                         label='resection: no')
            sns.distplot(subset_res_yes[met], hist=False, kde=True, kde_kws={'shade': True, 'linewidth': 3},
                         label='resection: yes')
            plt.title('Distribuční funkce rozdělení pro: ' + str(met) + ', pacient ' + str(seeg))

            plt.tight_layout()
            plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)

            plt.savefig("Grafy/dist_plot_" + str(met) + '_pacient_' + str(seeg) + ".png")
            # plt.show()

    # sns.FacetGrid(subset['pse'], hue = subset['resection'])

    return print('ok')
