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

from funkce2 import *
from sklearn import metrics
from lib.utilities import load_or_get, save_data, load_data

"""--------------------
POPIS:
vytvoření jedné ROC pro všechny vybrané pacienty z normalizovaných hodnot
-------------------------------------------------------------------------------------"""
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

#---Důležité---
##--pro výpočet normalizované křivky u lék.ozn.záznamů/resekované tkáně nutno zvolit file_name = ("fpr_resected" + '-'.join(str(pat) for pat in patients) + str(database)) a upravit název grafu
#----------



# ----Vytvoření názvu/legendy grafu
databases = ['LL_alg_Fnusa', 'Matt_alg_Fnusa', 'hilbert_alg_v2']
nazev_grafu = []

if database == databases[0]:  # volba 0, 1, 2
    nazev_grafu = 'algoritmus zalozeny na vypoctu delky cary'
elif database == databases[1]:
    nazev_grafu = 'algoritmus zalozeny na vypoctu frekvencni homogenity'
elif database == databases[2]:
    nazev_grafu = 'algoritmus zalozeny na vypoctu Hilbertovy transformace'


#------vykreslení hotnot z .npy

database = databases[0]
file_name = ("fpr_resected" + '-'.join(str(pat) for pat in patients) + str(database))
data_ll = load_data(file_name)
ydata_ll = data_ll['tpr']
xdata_ll = data_ll['fpr']

database = databases[1]
file_name = ("fpr_resected" + '-'.join(str(pat) for pat in patients) + str(database))
data_mat = load_data(file_name)
ydata_mat = data_mat['tpr']
xdata_mat = data_mat['fpr']

database = databases[2]
file_name = ("fpr_resected" + '-'.join(str(pat) for pat in patients) + str(database))
data_hilbert = load_data(file_name)
ydata_hilbert = data_hilbert['tpr']
xdata_hilbert = data_hilbert['fpr']


#-------vykresleni
# nazev_grafu1 = 'algoritmus zalozeny na vypoctu delky cary'
# nazev_grafu2 = 'algoritmus zalozeny na vypoctu frekvencni homogenity'
# nazev_grafu3 = 'algoritmus zalozeny na vypoctu Hilbertovy transformace'


AUC = dict()
AUC[0] = round(metrics.auc(xdata_ll, ydata_ll), 3)
AUC[1] = round(metrics.auc(xdata_mat, ydata_mat), 3)
AUC[2] = round(metrics.auc(xdata_hilbert, ydata_hilbert), 3)

#---------vykreslení s AUC
# create some fictive access data by hour

# let us make a simple graph
fig = plt.figure(figsize=[7,5])
ax = plt.subplot()
l = ax.fill_between(xdata_ll, ydata_ll)
l2 = ax.fill_between(xdata_mat, ydata_mat) #---potom sem dát třeba ll
l3 = ax.fill_between(xdata_hilbert, ydata_hilbert)


# set the basic properties
ax.set_xlabel('Senzitivita (False positive rate)')
ax.set_ylabel('Specificita (True positive rate)')
ax.set_title('Normalizovana ROC pro resekovanou tkan, pacienti '+str(patients)+'')

# set the limits
ax.set_xlim(0, 1)
ax.set_ylim(0, 1)

# set the grid on
ax.grid('on')

# change the fill into a blueish color with opacity .3
l.set_facecolors([[.9,.1,.1,.1]]) #--poslední parametr - míra neprůhlednosti
l2.set_facecolors([[.1,.6,.3,.1]])
l3.set_facecolors([[.1,.1,.6,.1]])

# change the edge color (bluish and transparentish) and thickness

l.set_edgecolors([[.9, .1, .2, .3]]) #barva ohraničení
l.set_linewidths([3])
l2.set_edgecolors([[.1, .9, .2, .3]]) #barva ohraničení
l2.set_linewidths([3])
l3.set_edgecolors([[0, 0, .9, .3]]) #barva ohraničení
l3.set_linewidths([3])


# add more ticks
#ax.set_xticks(np.arange(25))
# remove tick marks
ax.xaxis.set_tick_params(size=0)
ax.yaxis.set_tick_params(size=0)

# change the color of the top and right spines to opaque gray
ax.spines['right'].set_color((.8,.8,.8))
ax.spines['top'].set_color((.8,.8,.8))

# tweak the axis labels
xlab = ax.xaxis.get_label()
ylab = ax.yaxis.get_label()

plt.gca().set_facecolor('w'); #bílé pozadí

xlab.set_style('italic')
xlab.set_size(10)
ylab.set_style('italic')
ylab.set_size(10)

# tweak the title
ttl = ax.title
ttl.set_weight('bold')


import matplotlib.pyplot as plt
import numpy as np

t1 = xdata_ll
s1 = ydata_ll
t2 = xdata_mat
s2 = ydata_mat
t3 = xdata_hilbert
s3 = ydata_hilbert

seeg=2
#plt.plot(t1, s1, 'r', t2, s2, 'g', t3, s3, 'b')

plt.plot(t1, s1, label="delky cary, AUC: " +str(AUC[0])+ "", color='r')
plt.plot(t2, s2, label="frekvencni homogenity, AUC: " +str(AUC[1])+ "", color = 'g')
plt.plot(t3, s3, label="Hilbertovy transformace, AUC: " +str(AUC[2])+ "", color = 'b')

plt.xlabel('Senzitivita (False positive rate)')
plt.ylabel('Specificita (True positive rate)')
#plt.title('Normalizovana ROC pro pacienty '+str(patients)+', ')

plt.grid(True)
legend =plt.legend(loc=4, title="Algoritmus zalozeny na vypoctu: ", fancybox=True)
legend.get_frame().set_facecolor('r')
plt.rcParams['axes.facecolor'] = 'w'
plt.savefig("test.png")

