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

"""
popis funkcí
"""


import pandas as pd
import scipy
import seaborn as sns
import numpy as np
pd.set_option('display.width', 2000)  # nezalamuj tabulky
from funkce.nacteni import znaceni, info_o_patologii, channels, signal, sig_df_to_array
from funkce.D_file_read import read_d_header
from fnusa_sigproc.methods_uni import pse, pac, fac
from pyentrp.entropy import shannon_entropy, sample_entropy
import matplotlib.pyplot as plt
from funkce.data_resampling import resample_signals

volba_df_vzorkovaci_frekvence = '5kHz'
#volba_df_vzorkovaci_frekvence = '2kHz'

if volba_df_vzorkovaci_frekvence == '2kHz':
    target = ['FR_vFR_uFR_0_0_1', 'FR_vFR_uFR_0_1_1', 'FR_vFR_0_1']
    okoli = 120
    main_df_statistika = sig_FR = pd.read_pickle("Dataframes_" + str(volba_df_vzorkovaci_frekvence) + "/main_df_seegs[15, 32, 45, 48]_okoli_"+ str(okoli) + ".pkl") #
elif volba_df_vzorkovaci_frekvence == '5kHz':
    main_df_statistika = sig_FR = pd.read_pickle("Dataframes_" + str(volba_df_vzorkovaci_frekvence) + "/main_df_seegs[15, 32, 45, 48]_okoli_300.pkl") #


# přidání ještě FR_vFR (v předchozích skriptech řešeno vytvořením nového df bez uFR, tady stačí nevybírat 'nan' hodnoty)
main_df_statistika.loc[main_df_statistika['group'] == 0, 'FR_vFR_0_1'] = 0
main_df_statistika.loc[main_df_statistika['group'] == 1, 'FR_vFR_0_1'] = 1


# budu počítat nezávisle na seeg, tedy pro všechny dohrmady
target = ['resection_0_1', 'FR_vFR_uFR_0_0_1', 'FR_vFR_uFR_0_1_1', 'FR_vFR_0_1']
methods = ['pse', 'fac', 'pac', 'shannon_entropy', 'sample_entropy0', 'sample_entropy1']  # , 'shannon_entropy0']

vysledky_testu = pd.DataFrame(columns=['Seeg', 'Parametr', 'Target', 'statistic', 'p_hodnota', 'shapiro_array1', 'shapiro_array2'])

seegs = 0




i = 0
for tar in target:
    plt.figure(figsize=(6,8))


    if tar == 'resection_0_1':
        tar_title = 'Resekce'
    elif tar == "FR_vFR_uFR_0_0_1":
        tar_title = '(FR + vFR) vs. uFR'
    elif tar == "FR_vFR_uFR_0_1_1":
        tar_title = 'FR vs. (vFR + uFR)'
    elif tar == "FR_vFR_0_1":
        tar_title = 'FR vs. vFR'


    plt.suptitle('Rozložení hodnot pro jednotlivé metody, \n Klasifikační skupina: ' + str(tar_title))
    sub = 1
    for met in methods:
        #tar = 'resection_0_1'
        #met = 'pac'

        # informace o resekci u phase amplitude frequency
        array1 = main_df_statistika[met].loc[main_df_statistika[tar] == 1]
        array2 = main_df_statistika[met].loc[main_df_statistika[tar] == 0]



        # testování normality
        shapiro_array1 = scipy.stats.shapiro(array1)
        shapiro_array2 = scipy.stats.shapiro(array2)


        # t-test
        vals = scipy.stats.ttest_ind(array1, array2) # t-test
        vals = scipy.stats.ranksums(array1, array2) # rank-sum test

        statistic = vals[0]
        p_hodnota = vals[1]

        vysledky_testu.loc[i] = [seegs, met, tar, statistic, p_hodnota, shapiro_array1, shapiro_array2]


        plt.subplot(3, 2, sub)
        sns.distplot(array1)
        sns.distplot(array2)
        #plt.title('Metoda ' + str(met) + ', p-hodnoty t-testu: \n ' + str('{0:1.2E}'.format(p_hodnota)))
        plt.title('p-hodnota: ' + str('{0:1.2E}'.format(p_hodnota)))

        #plt.tight_layout()


        i = i + 1
        sub = sub + 1
        #uložit někam hodnoty

    plt.tight_layout()
    plt.subplots_adjust(top=0.85) #, bottom=0.1)

    plt.savefig("Grafy_" +str(volba_df_vzorkovaci_frekvence) + "/rozlozeni_a_statisticky_test_metoda" +str(met)+ "target" + str(tar)+ ".png")

#eps = np.finfo(np.double).eps
#print('{0:1.2E}'.format(eps))

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


seegs = [15,32, 45, 48]


#i = 0
for s in seegs:
    for tar in target:
        for met in methods:
            #tar = 'resection_0_1'
            #met = 'pac'

            # informace o resekci u phase amplitude frequency
            array1 = main_df_statistika[met].loc[(main_df_statistika[tar] == 1) & (main_df_statistika['seeg'] == s)]
            array2 = main_df_statistika[met].loc[(main_df_statistika[tar] == 0) & (main_df_statistika['seeg'] == s)]
            # (df_AUC['Target'] == tar) & (df_AUC['Parametr'] == par)]


            # testování normality
            shapiro_array1 = scipy.stats.shapiro(array1)
            shapiro_array2 = scipy.stats.shapiro(array2)

            # t-test


            vals = scipy.stats.ttest_ind(array1, array2)

            statistic = vals[0]
            p_hodnota = vals[1]

            vysledky_testu.loc[i] = [s, met, tar, statistic, p_hodnota, shapiro_array1, shapiro_array2]

            i = i + 1
            sub = sub + 1


# uložit hodnoty
print(vysledky_testu)
vysledky_testu.to_csv("Dataframes_" +str(volba_df_vzorkovaci_frekvence) + "/00_p_hodnoty_ranksum.csv", sep='\t',encoding='utf-8')
