-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathhardware_utils.py
148 lines (126 loc) · 4.61 KB
/
hardware_utils.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import codecs
import csv
from builtins import range
from fonduer.candidates.models import Candidate
from fonduer.learning.utils import confusion_matrix
from fonduer.supervision.models import GoldLabel, GoldLabelKey
try:
from IPython import get_ipython
if "IPKernelApp" not in get_ipython().config:
raise ImportError("console")
except (AttributeError, ImportError):
from tqdm import tqdm
else:
from tqdm import tqdm_notebook as tqdm
# Define labels
ABSTAIN = -1
FALSE = 0
TRUE = 1
def get_gold_dict(
filename, doc_on=True, part_on=True, val_on=True, attribute=None, docs=None
):
with codecs.open(filename, encoding="utf-8") as csvfile:
gold_reader = csv.reader(csvfile)
gold_dict = set()
for row in gold_reader:
(doc, part, attr, val) = row
if docs is None or doc.upper() in docs:
if attribute and attr != attribute:
continue
if val == TRUE:
continue
else:
key = []
if doc_on:
key.append(doc.upper())
if part_on:
key.append(part.upper())
if val_on:
key.append(val.upper())
gold_dict.add(tuple(key))
return gold_dict
gold_dict = get_gold_dict(
"data/hardware_tutorial_gold.csv", attribute="stg_temp_max"
)
def gold(c: Candidate) -> int:
doc = (c[0].context.sentence.document.name).upper()
part = (c[0].context.get_span()).upper()
val = ("".join(c[1].context.get_span().split())).upper()
if (doc, part, val) in gold_dict:
return TRUE
else:
return FALSE
def entity_level_f1(
candidates, gold_file, attribute=None, corpus=None, parts_by_doc=None
):
"""Checks entity-level recall of candidates compared to gold.
Turns a CandidateSet into a normal set of entity-level tuples
(doc, part, [attribute_value])
then compares this to the entity-level tuples found in the gold.
Example Usage:
from hardware_utils import entity_level_total_recall
candidates = # CandidateSet of all candidates you want to consider
gold_file = 'tutorials/tables/data/hardware/hardware_gold.csv'
entity_level_total_recall(candidates, gold_file, 'stg_temp_min')
"""
docs = [(doc.name).upper() for doc in corpus] if corpus else None
val_on = attribute is not None
gold_set = get_gold_dict(
gold_file,
docs=docs,
doc_on=True,
part_on=True,
val_on=val_on,
attribute=attribute,
)
if len(gold_set) == 0:
print(f"Gold File: {gold_file}\n Attribute: {attribute}")
print("Gold set is empty.")
return
# Turn CandidateSet into set of tuples
print("Preparing candidates...")
entities = set()
for i, c in enumerate(tqdm(candidates)):
part = c[0].context.get_span()
doc = c[0].context.sentence.document.name.upper()
if attribute:
val = c[1].context.get_span()
for p in get_implied_parts(part, doc, parts_by_doc):
if attribute:
entities.add((doc, p, val))
else:
entities.add((doc, p))
(TP_set, FP_set, FN_set) = confusion_matrix(entities, gold_set)
TP = len(TP_set)
FP = len(FP_set)
FN = len(FN_set)
prec = TP / (TP + FP) if TP + FP > 0 else float("nan")
rec = TP / (TP + FN) if TP + FN > 0 else float("nan")
f1 = 2 * (prec * rec) / (prec + rec) if prec + rec > 0 else float("nan")
print("========================================")
print("Scoring on Entity-Level Gold Data")
print("========================================")
print(f"Corpus Precision {prec:.3}")
print(f"Corpus Recall {rec:.3}")
print(f"Corpus F1 {f1:.3}")
print("----------------------------------------")
print(f"TP: {TP} | FP: {FP} | FN: {FN}")
print("========================================\n")
return [sorted(list(x)) for x in [TP_set, FP_set, FN_set]]
def get_implied_parts(part, doc, parts_by_doc):
yield part
if parts_by_doc:
for p in parts_by_doc[doc]:
if p.startswith(part) and len(part) >= 4:
yield p
def entity_to_candidates(entity, candidate_subset):
matches = []
for c in candidate_subset:
c_entity = tuple(
[c[0].context.sentence.document.name.upper()]
+ [c[i].context.get_span().upper() for i in range(len(c))]
)
c_entity = tuple([str(x) for x in c_entity])
if c_entity == entity:
matches.append(c)
return matches