forked from HazyResearch/fonduer-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhardware_utils.py
204 lines (175 loc) · 6.46 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import codecs
import csv
from builtins import range
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 = 0
FALSE = 1
TRUE = 2
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
def load_hardware_labels(
session, candidate_classes, filename, attrib, annotator_name="gold"
):
"""Bulk insert hardware GoldLabels.
:param session: The database session to use.
:param candidate_classes: Which candidate_classes to load labels for.
:param filename: Path to the CSV file containing gold labels.
:param attrib: Which attributes to load labels for (e.g. "stg_temp_max").
"""
# Check that candidate_classes is iterable
candidate_classes = (
candidate_classes
if isinstance(candidate_classes, (list, tuple))
else [candidate_classes]
)
ak = session.query(GoldLabelKey).filter(GoldLabelKey.name == annotator_name).first()
# Add the gold key
if ak is None:
ak = GoldLabelKey(name=annotator_name)
session.add(ak)
session.commit()
# Bulk insert candidate labels
candidates = []
for candidate_class in candidate_classes:
candidates.extend(session.query(candidate_class).all())
gold_dict = get_gold_dict(filename, attribute=attrib)
cand_total = len(candidates)
print("Loading {} candidate labels".format(cand_total))
labels = 0
cands = []
values = []
for i, c in enumerate(tqdm(candidates)):
doc = (c[0].span.sentence.document.name).upper()
part = (c[0].span.get_span()).upper()
val = ("".join(c[1].span.get_span().split())).upper()
label = session.query(GoldLabel).filter(GoldLabel.candidate == c).first()
if label is None:
if (doc, part, val) in gold_dict:
values.append(TRUE)
else:
values.append(FALSE)
cands.append(c)
labels += 1
# Only insert the labels which were not already present
session.bulk_insert_mappings(
GoldLabel,
[
{"candidate_id": cand.id, "keys": [annotator_name], "values": [val]}
for (cand, val) in zip(cands, values)
],
)
session.commit()
print("GoldLabels created: {}".format(labels))
def entity_confusion_matrix(pred, gold):
if not isinstance(pred, set):
pred = set(pred)
if not isinstance(gold, set):
gold = set(gold)
TP = pred.intersection(gold)
FP = pred.difference(gold)
FN = gold.difference(pred)
return (TP, FP, FN)
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("Gold File: {}\n Attribute: {}".format(gold_file, 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].span.get_span()
doc = c[0].span.sentence.document.name.upper()
if attribute:
val = c[1].span.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) = entity_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("Corpus Precision {:.3}".format(prec))
print("Corpus Recall {:.3}".format(rec))
print("Corpus F1 {:.3}".format(f1))
print("----------------------------------------")
print("TP: {} | FP: {} | FN: {}".format(TP, FP, 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].span.sentence.document.name.upper()]
+ [c[i].span.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