# -*- coding: utf-8 -*-
"""
Created on Tue May 30 13:13:44 2017
@author: larakamal

total evaluation evalutes the sorted documents 
using precision and NDCG
change the directory of the sorted documents from lines 79-87
change the directory of the precision and NDCG graphs from 
line 345 and 352
"""
from math import log10
import csv 
import numpy as np
import pandas as pd

def getNDCG(list, k):
    #convert to double 
   dcg = float(getDCG(list,k))
   idcg = float(getIDCG(list,k))
   ndcg = 0.0
   if (idcg > 0.0):
       ndcg = dcg/idcg
   return ndcg 


def getPrecision(list, k):
    size = len(list)
    if (size == 0 or k == 0):
        return 0.0
        
    if(k > size):
        k = size
    rel_doc_num = getRelevantDocNum(list,k)
    #convert to double 
    precision = float(float(rel_doc_num)/float(k))
    return precision 

      
def getRelevantDocNum(list,k):
    size = len(list)
    if (size == 0 or k == 0):
        return 0
    if (k > size):
        k = size
    rel_num = 0
    for i in range(k):
       if list[i] > 5:
           rel_num = rel_num + 1
    return rel_num 


def getDCG(list,k):
    size = len(list)
    if (size == 0 or k == 0):
        return 0.0
    if (k > size):
        k = size
    #convert to double
    dcg = list[0]
    dcg = float(dcg)

    for i in range(1,k):
        rel = list[i]
        pos = i+1
        rel_log = log10(pos)/log10(2)
        rel_log = float(rel_log)
        dcg = dcg + (rel/rel_log)
    return dcg 


def getIDCG(list, k):
    # sort list
    sortedList = list
    sortedList = sorted(sortedList, key=int, reverse=True)
    idcg = getDCG(sortedList, k)
    return float(idcg) 

def evaluate(algorithm):
    path = "data/results/test/" + algorithm + "/"   

    #change directory of the ranked documents
    dataframe1 = pd.read_csv(path + "gravity_sorted.csv")
    dataframe2 = pd.read_csv(path + "ocean pressure_sorted.csv")
    dataframe3 = pd.read_csv(path + "ocean temperature_sorted.csv")
    dataframe4 = pd.read_csv(path + "ocean wind_sorted.csv")
    dataframe5 = pd.read_csv(path + "pathfinder_sorted.csv")
    dataframe6 = pd.read_csv(path + "quikscat_sorted.csv")
    dataframe7 = pd.read_csv(path + "radar_sorted.csv")
    dataframe8 = pd.read_csv(path + "saline density_sorted.csv")
    dataframe9 = pd.read_csv(path + "sea ice_sorted.csv")
    
    
    label1 = dataframe1.ix[:,10:11]
    label2 = dataframe2.ix[:,10:11]
    label3 = dataframe3.ix[:,10:11]
    label4 = dataframe4.ix[:,10:11]
    label5 = dataframe5.ix[:,10:11]
    label6 = dataframe6.ix[:,10:11]
    label7 = dataframe7.ix[:,10:11]
    label8 = dataframe8.ix[:,10:11]
    label9 = dataframe9.ix[:,10:11]
    
    temp_list1 = label1['label'].tolist()
    temp_list2 = label2['label'].tolist()
    temp_list3 = label3['label'].tolist()
    temp_list4 = label4['label'].tolist()
    temp_list5 = label5['label'].tolist()
    temp_list6 = label6['label'].tolist()
    temp_list7 = label7['label'].tolist()
    temp_list8 = label8['label'].tolist()
    temp_list9 = label9['label'].tolist()
    
    label_list1 = [];
    label_list2 = [];
    label_list3 = [];
    label_list4 = [];
    label_list5 = [];
    label_list6 = [];
    label_list7 = [];
    label_list8 = [];
    label_list9 = [];
    
    
    for i in range(len(temp_list1)):
        if temp_list1[i] == 'Excellent':
            label_list1.append(7) 
        elif temp_list1[i] == 'Very good':
            label_list1.append(6) 
        elif temp_list1[i] == 'Good':
            label_list1.append(5) 
        elif temp_list1[i] == 'Ok':
            label_list1.append(4) 
        elif temp_list1[i] == 'Bad':
            label_list1.append(3) 
        elif temp_list1[i] == 'Very bad':
            label_list1.append(2) 
        elif temp_list1[i] == 'Terrible':
            label_list1.append(1) 
        else:
            label_list1.append(0)
            
    for i in range(len(temp_list2)):
        if temp_list2[i] == 'Excellent':
            label_list2.append(7) 
        elif temp_list2[i] == 'Very good':
            label_list2.append(6) 
        elif temp_list2[i] == 'Good':
            label_list2.append(5) 
        elif temp_list2[i] == 'Ok':
            label_list2.append(4) 
        elif temp_list2[i] == 'Bad':
            label_list2.append(3) 
        elif temp_list2[i] == 'Very bad':
            label_list2.append(2) 
        elif temp_list2[i] == 'Terrible':
            label_list2.append(1) 
        else:
            label_list2.append(0)
    
    for i in range(len(temp_list3)):
        if temp_list3[i] == 'Excellent':
            label_list3.append(7) 
        elif temp_list3[i] == 'Very good':
            label_list3.append(6) 
        elif temp_list3[i] == 'Good':
            label_list3.append(5) 
        elif temp_list3[i] == 'Ok':
            label_list3.append(4) 
        elif temp_list3[i] == 'Bad':
            label_list3.append(3) 
        elif temp_list3[i] == 'Very bad':
            label_list3.append(2) 
        elif temp_list3[i] == 'Terrible':
            label_list3.append(1) 
        else:
            label_list3.append(0)
            
            
    for i in range(len(temp_list4)):
        if temp_list4[i] == 'Excellent':
            label_list4.append(7) 
        elif temp_list4[i] == 'Very good':
            label_list4.append(6) 
        elif temp_list4[i] == 'Good':
            label_list4.append(5) 
        elif temp_list4[i] == 'Ok':
            label_list4.append(4) 
        elif temp_list4[i] == 'Bad':
            label_list4.append(3) 
        elif temp_list4[i] == 'Very bad':
            label_list4.append(2) 
        elif temp_list4[i] == 'Terrible':
            label_list4.append(1) 
        else:
            label_list4.append(0)
            
    for i in range(len(temp_list5)):
        if temp_list5[i] == 'Excellent':
            label_list5.append(7) 
        elif temp_list5[i] == 'Very good':
            label_list5.append(6) 
        elif temp_list5[i] == 'Good':
            label_list5.append(5) 
        elif temp_list5[i] == 'Ok':
            label_list5.append(4) 
        elif temp_list5[i] == 'Bad':
            label_list5.append(3) 
        elif temp_list5[i] == 'Very bad':
            label_list5.append(2) 
        elif temp_list5[i] == 'Terrible':
            label_list5.append(1) 
        else:
            label_list5.append(0)
            
            
    for i in range(len(temp_list6)):
        if temp_list6[i] == 'Excellent':
            label_list6.append(7) 
        elif temp_list6[i] == 'Very good':
            label_list6.append(6) 
        elif temp_list6[i] == 'Good':