-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvaluationScript.py
65 lines (53 loc) · 1.79 KB
/
EvaluationScript.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'''
CS 584: Applied BioNLP
Assignment 1: Evaluation script
The file simply loads a submission file and a gold standard and
computes the recall, precision and F-score for the system
@author Abeed Sarker
email: [email protected]
Created: 09/14/2020
'''
import pandas as pd
from collections import defaultdict
def load_labels(f_path):
'''
Loads the labels
:param f_path:
:return:
'''
labeled_df = pd.read_excel(f_path)
labeled_dict = defaultdict(list)
for index,row in labeled_df.iterrows():
id_ = row['ID']
if not pd.isna(row['Symptom CUIs']) and not pd.isna(row['Negation Flag']):
cuis = row['Symptom CUIs'].split('$$$')[1:-1]
neg_flags = row['Negation Flag'].split('$$$')[1:-1]
for cui,neg_flag in zip(cuis,neg_flags):
labeled_dict[id_].append(cui + '-' + str(neg_flag))
return labeled_dict
#gold_standard_dict = load_labels('./assignmentGoldStandardSet2.xlsx')
#submission_dict = load_labels('./AssignmentSampleSubmission.xlsx')
gold_standard_dict = load_labels('./s11.xlsx')
submission_dict = load_labels('./results.xlsx')
tp = 0
tn = 0
fp = 0
fn = 0
for k,v in gold_standard_dict.items():
for c in v:
try:
if c in submission_dict[k]:
tp+=1
else:
fn+=1
except KeyError:#if the key is not found in the submission file, each is considered
#to be a false negative..
fn+=1
for c2 in submission_dict[k]:
if not c2 in gold_standard_dict[k]:
fp+=1
print('True Positives:',tp, 'False Positives: ', fp, 'False Negatives:', fn)
recall = tp/(tp+fn)
precision = tp/(tp+fp)
f1 = (2*recall*precision)/(recall+precision)
print('Recall: ',recall,'\nPrecision:',precision,'\nF1-Score:',f1)