#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-

from funkce2 import *

#--- zobrazení celého DataFrame
pd.set_option('display.height', 1000)
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)

"""--------------------
POPIS:
provedení t-testů pro lékařské označení, resekovanou tkáň i konektivitu
-------------------------------------------------------------------------------------"""
databases = [ 'LL_alg_Fnusa', 'Matt_alg_Fnusa', 'hilbert_alg_v2']

#--------vstupní parametry:
#patients = range(22, 26)
patients = [5, 7, 15, 18, 32]
database = databases[2] #volba 0, 1, 2
#----------

df5 = get_data(database, patients[0], patients[0], '*')
df7 = get_data(database, patients[1], patients[1], '*')
df15 = get_data(database, patients[2], patients[2], '*')
df18 = get_data(database, patients[3], patients[3], '*')
df32 = get_data(database, patients[4], patients[4], '*')

df_count5 = count_hfo_events(df5)
df_count7 = count_hfo_events(df7)
df_count15 = count_hfo_events(df15)
df_count18 = count_hfo_events(df18)
df_count32 = count_hfo_events(df32)


#------sjednocení dataframe

df_count = [df_count5, df_count7, df_count15, df_count18, df_count32]
df_count = pd.concat(df_count)

import pickle
df_count.to_pickle(
    'C:/Users/admin/ownCloud/BakalarskaPrace/cache/count_hfo_events_005,015,018,032_' + database + '.pkl')


#----obsahuje nuly
# SOZ_count = df_count.SOZ_count
# NON_SOZ_count = df_count.NON_SOZ_count
# IZ_count = df_count.IZ_count

#----nebo jinak
SOZ_count = df_count.loc[df_count['SOZ_count'] != 0]
NON_SOZ_count = df_count.loc[df_count['NON_SOZ_count'] != 0]
IZ_count = df_count.loc[df_count['IZ_count'] != 0]



#---testování p hodnoty
from scipy.stats import ttest_ind
p1 = ttest_ind(SOZ_count.SOZ_count,NON_SOZ_count.NON_SOZ_count)[1]

#---testování p hodnoty
p2 = ttest_ind(SOZ_count.SOZ_count,IZ_count.IZ_count)[1]

#---testování p hodnoty
p3 = ttest_ind(IZ_count.IZ_count,NON_SOZ_count.NON_SOZ_count)[1]


'''--------výpočet pro resekovanou tkáň----------'''
#ll_df = get_data(database, seeg, i, columns)

#--- funkce pro součet detekcí v jednotlivých kanálech
count_df_soz_info5 = count_hfo_events_resected(df5)
count_df_soz_info7 = count_hfo_events_resected(df7)
count_df_soz_info15 = count_hfo_events_resected(df15)
count_df_soz_info18 = count_hfo_events_resected(df18)
count_df_soz_info32 = count_hfo_events_resected(df32)


count_df_soz_info = [count_df_soz_info5, count_df_soz_info7, count_df_soz_info15, count_df_soz_info18, count_df_soz_info32]
count_df_soz_info = pd.concat(count_df_soz_info)


#---uložení dat
count_df_soz_info.to_pickle(
    'C:/Users/admin/ownCloud/BakalarskaPrace/cache/count_hfo_events_new_005,015,018,032_' + database + '_resected.pkl')

#count_df_soz_info.soz_info.loc = 'SOZ'
#

database=databases[1]
#----načíst --- vypočítal p hodnotu pro LL a Mata, Hilbert v jiném formátu
bu = pd.read_pickle('C:/Users/admin/ownCloud/BakalarskaPrace/cache/count_hfo_events_005,015,018,032_' + database + '_resected.pkl')
bu.reset_index(inplace=True)
SOZ_data = bu[bu.soz_info == 'SOZ'].resection.values
NON_SOZ_data = bu[bu.soz_info == 'NON_SOZ'].resection.values

from scipy.stats import ttest_ind

#---testování p hodnoty
p4 = ttest_ind(NON_SOZ_data,SOZ_data)[1]


#------testování p-hodnoty pro hilberta
database=databases[2]

#hfo_events1 = count_hfo_events(ll_df)
#hfo_events_resected = count_hfo_events_resected(ll_df)
bu=count_df_soz_info


bu.reset_index(inplace=True)
SOZ_data = bu[bu.resection == 'resected'].soz_info.values
NON_SOZ_data = bu[bu.resection == 'non_resected'].soz_info.values

from scipy.stats import ttest_ind

#---testování p hodnoty
p5 = ttest_ind(NON_SOZ_data,SOZ_data)[1]

''''---------výpočet pro konektivitu'''''

with open('C:/Users/admin/ownCloud/BakalarskaPrace/cache/connectivity_wm_seeg_005_007_015_018_032.pkl', 'rb') as f:
   data= pickle.load(f)

data6 = data.groupby(by=('low_fc', 'soz_info')).agg("mean")

from scipy.stats import ttest_ind

p_SOZ_SOZ = dict()
p_SOZ_BRIDGE = dict()
p_SOZ_NON = dict()
p_BRIDGE_BRIDGE = dict()
p_BRIDGE_NON = dict()
p_NON_NON = dict()

low_fcs = data.low_fc.unique()

for low_fc in low_fcs:
    SOZ_low_fc = data.loc[(data.low_fc == low_fc) & (data.soz_info == 'SOZ'),'lin_corr_median'] #kde soz info1 = SOZ,
    NON_SOZ_low_fc = data.loc[(data.low_fc == low_fc) & (data.soz_info == 'NON_SOZ'), 'lin_corr_median']  # kde soz info1 = SOZ,
    BRIDGE_low_fc = data.loc[(data.low_fc == low_fc) & (data.soz_info == 'BRIDGE'), 'lin_corr_median']  # kde soz info1 = SOZ,

    #---výpočty p-hodnot
    p_SOZ_BRIDGE[low_fc] = ttest_ind(SOZ_low_fc, BRIDGE_low_fc)[1]
    p_SOZ_NON[low_fc] = ttest_ind(SOZ_low_fc, NON_SOZ_low_fc)[1]
    p_BRIDGE_NON[low_fc] = ttest_ind(BRIDGE_low_fc, NON_SOZ_low_fc)[1]

# low_fc = 1
# SOZ_low_fc[low_fc] = data.loc[data['low_fc'] == 1 & data['high_fc'] == 4 & data['soz_info'] == 'SOZ']


p6 = ttest_ind(NON_SOZ_data,SOZ_data)[1]

#---agregace dat podle kanálů
#data6 = data.groupby(by=('soz_info', 'channel_name_1')).agg("count")



