# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 2
#     language: python
#     name: python2
# ---

from __future__ import division
# %load_ext autoreload
# %autoreload 2
from django.conf import settings
import cPickle as pickle
import csv
import os
import sys
import numpy as np
import pandas as pd
sys.path.append('c:\dev\opinion\opinion\python\opinion')
import utils.fileutils as fileutils
import utils.metrics as metrics
import nltk

rootdir = r"C:\dev\opinion\papers\sentiplus"
indir = os.path.join(rootdir, "Hybrid")

# +
# Hot Encode DSOSE values and then Change the previous Base file into the new one
#infile = os.path.join(indir, "DSOSE_ResultsConsolidated_RF.xls")
infile = os.path.join(indir, "ConsolidatedWithBestTrainedClfsFromThreeSETools.xls")

df = pd.read_excel(infile, "Sheet1", encoding="ISO-8859-1")
dsoseHotEncodes = []
for index, row in df.iterrows():
    dsose = row["DSOSE"]
    if dsose == "p":
        dsoseHotEncodes.append(1)
    elif dsose == "n":
        dsoseHotEncodes.append(-1)
    else:
        dsoseHotEncodes.append(0)
df["DSOSE_HotEncoded"] = (pd.Series(dsoseHotEncodes)).values
df.to_excel(infile, index=False)
# -





# +
def computePerformanceOverallOfLearner(infile, learnerCol, filenames):
    #infile = os.path.join(indir, "ResultsConsolidated_"+algo+".xls")
    df = fileutils.readExcel(infile, "Sheet1", encoding="ISO-8859-1")
    exps = []
    gots = []
    labels = set()
    for index, row in df.iterrows():
        fname = row["File"]
        fname = fname.split("_")[0]
        if fname not in filenames:
                #print fname, " not in filenmaes"
                #return
                continue
        else:
                exp = row["ManualLabel"]
                got = row[learnerCol]
                labels.add(exp)
                exps.append(exp)
                gots.append(got)
    computer = metrics.PerformanceMultiClass(exps, gots, labels = list(labels))
    for label in labels:
            pr = computer.precision(label)
            re = computer.recall(label)
            f1 = 2*pr*re/(pr+re)
            print "Label = %s. Precision = %.3f. Recall = %.3f. F1 = %.3f"%(label, pr, re, f1)
    f1_macro = computer.f1_macro_average()
    pr_macro = computer.precision_macro_average()
    rec_macro = computer.recall_macro_average()
    f1_micro, _, _ = computer.compute_micro_average()
    print "F1 Macro = %.3f. Micro = %.3f"%(f1_macro, f1_micro)
    print "Macro Precision = %.3f. Recall = %.3f"%(pr_macro, rec_macro)
    print "-------------------------------"

def computePerformancOfLearner(infile, learnerCol, filenames):
    #infile = os.path.join(indir, "ResultsConsolidated_"+algo+".xls")
    df = fileutils.readExcel(infile, "Sheet1", encoding="ISO-8859-1")
    exps = dict()
    gots = dict()
    labels = dict()
    for index, row in df.iterrows():
        fname = row["File"]
        fname = fname.split("_")[0]
        if fname not in filenames:
                #print fname, " not in filenmaes"
                continue
        else:
                if fname not in exps:
                    exps[fname] = []
                    gots[fname] = []
                    labels[fname] = set()
        exp = row["ManualLabel"]
        got = row[learnerCol]
        labels[fname].add(exp)
        exps[fname].append(exp)
        gots[fname].append(got)
    for fname in filenames:
        computer = metrics.PerformanceMultiClass(exps[fname], gots[fname], labels = list(labels[fname]))
        for label in labels[fname]:
                pr = computer.precision(label)
                re = computer.recall(label)
                f1 = 2*pr*re/(pr+re)
                print "File %s. Label = %s. Precision = %.2f. Recall = %.2f. F1 = %.2f"%(fname, label, pr, re, f1)
        f1_macro = computer.f1_macro_average()
        f1_micro, _, _ = computer.compute_micro_average()
        print "File = %s. F1 Macro = %.2f. Micro = %.2f"%(fname, f1_macro, f1_micro)
        print "-------------------------------"



# -

filenames = ["DatasetLinJIRA", "BenchmarkUddinSO", "DatasetLinAppReviews", 
                     "DatasetLinSO", "DatasetSenti4SDSO", "OrtuJIRA"]

infile = os.path.join(indir, "ConsolidatedWithBestTrainedClfsFromThreeSETools.xls")
learnerCol = "DsoLabelFullTextW2V"
print learnerCol, ": Overall Performance"
print "-"*80
computePerformanceOverallOfLearner(infile, learnerCol, filenames)    
print learnerCol, ": By File Performance"
print "-"*80
computePerformancOfLearner(infile, learnerCol, filenames)

infile = os.path.join(indir, "ConsolidatedWithBestTrainedClfsFromThreeSETools.xls")
learnerCol = "DsoLabelFullText"
print learnerCol, ": Overall Performance"
print "-"*80
computePerformanceOverallOfLearner(infile, learnerCol, filenames)    
print learnerCol, ": By File Performance"
print "-"*80
computePerformancOfLearner(infile, learnerCol, filenames)

infile = os.path.join(indir, "ConsolidatedWithBestTrainedClfsFromThreeSETools.xls")
learnerCol = "Senti4SD"
print learnerCol, ": Overall Performance"
print "-"*80
computePerformanceOverallOfLearner(infile, learnerCol, filenames)    
print learnerCol, ": By File Performance"
print "-"*80
computePerformancOfLearner(infile, learnerCol, filenames)

infile = os.path.join(indir, "ConsolidatedWithBestTrainedClfsFromThreeSETools.xls")
learnerCol = "SentiCR"
print learnerCol, ": Overall Performance"
print "-"*80
computePerformanceOverallOfLearner(infile, learnerCol, filenames)    
print learnerCol, ": By File Performance"
print "-"*80
computePerformancOfLearner(infile, learnerCol, filenames)

infile = os.path.join(indir, "ConsolidatedWithBestTrainedClfsFromThreeSETools.xls")
learnerCol = "SentistrengthSE"
print learnerCol, ": Overall Performance"
print "-"*80
computePerformanceOverallOfLearner(infile, learnerCol, filenames)    
print learnerCol, ": By File Performance"
print "-"*80
computePerformancOfLearner(infile, learnerCol, filenames)



StarcoderdataJupyterScriptsDedupFiltered