Commit 778d2d87 authored by opitz's avatar opitz
Browse files

added missing file for evaluating the ensemble test predictions

parent 10aeda40
Loading
Loading
Loading
Loading
+122 −0
Original line number Diff line number Diff line
import sys
import os
import numpy as np
import ast
from sklearn.metrics import f1_score,confusion_matrix,precision_score,recall_score
from scipy.stats import pearsonr
from eval_helpers import evaluate,extensive_evaluate,evaluate_regression,extensive_evaluate_regression


##script for evaluating final ensemble votings

S1=["changes_possession" ,"volition" ,"stationary" ,"location_of_event" ,"existed_before" ,"awareness" ,"exists_as_physical" ,"sentient" ,"existed_after" ,"existed_during" ,"makes_physical_contact" ,"destroyed" ,"change_of_location" ,"created" ,"instigation" ,"manipulated_by_another" ,"change_of_state", "predicate_changed_argument"]

S2=[
"awareness",
                "change_of_location",
                "change_of_possession",
                "change_of_state",
                "change_of_state_continuous",
                "existed_after",
                "existed_before",
                "existed_during",
                "instigation",
                "partitive",
                "sentient",
                "volition",
                "was_for_benefit",
                "was_used"]
S1=list(sorted(S1))
print(S1)



### these parameters must be set ###

#voter model ids
runids=list(range(0,50))

#are we doing regression or multi-label?
REGR = False

#SPR1:18 classes, SPR2:14
NCLASSES = 18




res={}
full = []

if NCLASSES == 14:
    S1=S2


def maxto1(x):
    #converts soft vote to hard vote
    for i in range(x.shape[0]):
        a=np.argmax(x[i])
        b=np.argmin(x[i])
        x[i][a] = 1
        x[i][b] = 0
    return None

def tolikert(x):
    #crops relu predictions to likert scale
    new=np.zeros(x.shape)
    for i in range(x.shape[0]):
        for j in range(x.shape[1]):
            if x[i][j] < 1:
                new[i][j] = 1
            elif x[i][j] > 5:
                new[i][j]=5
            else:
                new[i][j] = x[i][j]
    return new

N=0

for fi in os.listdir("bertbaselogs"):
    with open("bertbaselogs/"+fi,"r") as f:
        if int(fi.split("-")[0]) in runids:
            string=f.read()
            
            
            #get gold labels once from log file
            if N == 0:
                gold = [np.array(x) for x in ast.literal_eval(string.split("gold: ")[1].split("\n")[0])][:NCLASSES]
            

            #get test predictions from log file
            preds = [np.array(x) for x in ast.literal_eval(string.split("predictions: ")[1].split("\n")[0])][:NCLASSES]
            
            #probabilities to hard
            if not REGR:
                [maxto1(x) for x in preds]
            
            
            #relu scores to likert scale with simple crop 1 = scores < 1 ; 5 = scores > 5
            if REGR:
                preds=[tolikert(p) for p in preds]
            
            #if this is the first voter, intitialize committee predictions
            if not full:
                full = preds
            
            #else aggregate
            else:
                full = [full[i]+preds[i] for i in range(len(preds))]
            N+=1
            print(N,"voters loaded")

#print final results
if REGR:
    ext=extensive_evaluate_regression(gold,full,NCLASSES)
    print(list(zip(S1,ext[2].tolist())))
    print(evaluate_regression(gold,[tolikert(x/N) for x in full],NCLASSES))
else:
    ext=extensive_evaluate(gold,full,NCLASSES)
    print(list(zip(S1,ext[2].tolist())))
    print("mean F1","macro F1","micro F1")
    print(evaluate(gold,full,NCLASSES))