-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added code to github
- Loading branch information
Showing
9 changed files
with
2,053 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
from __future__ import print_function, division | ||
|
||
class Entity(object): | ||
|
||
def __init__(self, type, id, string, spans, text_id, doctimerel=None, etree=None): | ||
self.type = type | ||
self.id = id | ||
self.string = string | ||
self.spans = spans | ||
self.text_id = text_id | ||
self.doctimerel = doctimerel | ||
self.phi = {} | ||
self.phi_v = None | ||
self.tokens = None | ||
self.paragraph = None | ||
self.xmltree = etree | ||
self.embedding = None | ||
self.next_event = None | ||
self.next_entity = None | ||
self.attributes = {} | ||
|
||
def __str__(self): | ||
return str(self.string) | ||
|
||
def __hash__(self): | ||
return hash(self.id) | ||
|
||
def __eq__(self, other): | ||
return self.id == other.id | ||
|
||
def __ne__(self, other): | ||
return not(self == other) | ||
|
||
def type(self): | ||
return self.type | ||
|
||
def ID(self): | ||
return self.id | ||
|
||
def get_tokens(self): | ||
return self.tokens | ||
|
||
def text_id(self): | ||
return self.text_id | ||
|
||
def get_doctimerel(self): | ||
return self.doctimerel | ||
|
||
def get_span(self): | ||
return self.spans[0] | ||
|
||
def get_etree(self): | ||
return self.xmltree | ||
|
||
def get_doc_id(self): | ||
return self.id.split('@')[2] | ||
|
||
class TLink(object): | ||
|
||
def __init__(self, e1, e2, tlink=None): | ||
self.e1 = e1 | ||
self.e2 = e2 | ||
self.tlink = tlink | ||
self.phi = {} | ||
self.phi_v = None | ||
self.tokens_ib = None | ||
self.id = None | ||
|
||
def __str__(self): | ||
return str(self.e1) + '-' + str(self.e2) | ||
|
||
def ID(self): | ||
if not self.id: | ||
self.id = self.e1.ID() + '-' + self.e2.ID() | ||
return self.id | ||
|
||
def __hash__(self): | ||
return hash(self.id()) | ||
|
||
def __eq__(self, other): | ||
return self.ID() == other.ID() | ||
|
||
def __ne__(self, other): | ||
return not (self == other) | ||
|
||
def set_tokens_ib(self, tokens): | ||
self.tokens_ib = list(tokens) | ||
|
||
def get_tokens_ib(self): | ||
return self.tokens_ib | ||
|
||
def type(self): | ||
return self.e1.type + '-' + self.e2.type | ||
|
||
def get_tlink(self): | ||
return self.tlink | ||
|
||
def get_e1(self): | ||
return self.e1 | ||
|
||
def get_e2(self): | ||
return self.e2 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from __future__ import print_function, division | ||
from collections import Counter | ||
import pandas as pd | ||
|
||
class Evaluation: | ||
|
||
def __init__(self, Y_p, Y, name='', tasks='DCTR,TLINK'): | ||
self.name = name | ||
self.Y_p, self.Y = Y_p, Y | ||
print('\n---> EVALUATION:',self.name,'<---') | ||
if 'DCTR' in tasks.split(','): | ||
self.evaluate_e() | ||
if 'TLINK' in tasks.split(','): | ||
self.evaluate_ee() | ||
|
||
def pprint(self): | ||
return 'todo' | ||
|
||
def evaluate_e(self): | ||
print('\n*** Evaluating DOCTIMEREL ***') | ||
self.evaluate([yp[0] for yp in self.Y_p], [y[0] for y in self.Y]) | ||
|
||
def evaluate_ee(self): | ||
print('\n*** Evaluating TLINKS ***') | ||
self.evaluate([yp[1] for yp in self.Y_p], [y[1] for y in self.Y]) | ||
|
||
def evaluate(self, Yp, Y): # Internal evaluation, may not be the same as in Clinical TempEval (due to temporal closure and candidate generation)! | ||
Yp = [l for i in Yp for l in i] | ||
Y = [l for i in Y for l in i] | ||
labels = set(Y+Yp) | ||
print('Y:',set(Y),'Yp',set(Yp)) | ||
y_actu = pd.Series(Y, name='Actual') | ||
y_pred = pd.Series(Yp, name='Predicted') | ||
confusion = Counter(zip(Y,Yp)) | ||
df_confusion = pd.crosstab(y_actu, y_pred, rownames=['Actual'], colnames=['Predicted'], margins=True) | ||
print('==CONFUSION MATRIX==') | ||
print(df_confusion) | ||
print('==PER LABEL EVALUATION==') | ||
print(' P\t R\t F\t') | ||
s_TP, s_FP, s_FN = 0,0,0 | ||
for l in labels: | ||
TP = confusion[(l,l)] if (l,l) in confusion else 0 | ||
FP = sum([confusion[(i,l)] for i in labels if (i,l) in confusion and l!=i]) | ||
FN = sum([confusion[(l,i)] for i in labels if (l,i) in confusion and l!=i]) | ||
print('TP',TP,'FP',FP,'FN',FN) | ||
precision = float(TP) / (TP + FP + 0.000001) | ||
recall = float(TP) / (TP + FN + 0.000001) | ||
fmeasure = (2 * precision * recall) / (precision + recall + 0.000001) | ||
print(round(precision,4),'\t',round(recall,4),'\t',round(fmeasure,4),'\t',l) | ||
s_TP += TP | ||
s_FP += FP | ||
s_FN += FN | ||
s_prec = float(s_TP) / (s_TP + s_FP + 0.000001) | ||
s_recall = float(s_TP) / (s_TP + s_FN + 0.000001) | ||
s_fmeasure = (2 * s_prec * s_recall) / (s_prec + s_recall + 0.000001) | ||
print(round(s_prec,4),'\t',round(s_recall,4),'\t',round(s_fmeasure,4),'\t','**ALL**') | ||
|
||
|
||
|
||
|
Oops, something went wrong.