-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference.py
380 lines (322 loc) · 13.8 KB
/
inference.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
from train_utils import initiate_tokenizer
import pandas as pd
import json
import lexical_baseline
def read_settings(filepath):
with open(filepath) as json_file:
settings = json.load(json_file)
tokenizer = initiate_tokenizer(settings)
testfile_name = settings['metadata_testfile']['original_filename']
return settings, tokenizer, testfile_name
def count_gold_events(tokens, gold):
"""
Counts amount of tokens annotated with I-event in a given part of the data
"""
events=0
zipped = zip(tokens, gold)
for t, g in zipped:
tok_lab = zip(t[1:-1], g)
for tok, lab in tok_lab:
if lab == 'I-event':
events+=1
return events
def calculate_score_event_class(predictions, gold):
tn=0
tp=0
fn=0
fp=0
zipped = zip(predictions, gold)
for p, g in zipped:
p_g = zip(p, g)
for pred, gold in p_g:
if pred == gold and pred == 'O':
tn+=1
if pred == gold and pred == 'I-event':
tp+=1
if pred != gold and gold == 'I-event':
fn+=1
if pred != gold and gold == "O":
fp+=1
# allow for no predictions
try:
precision = tp / (tp+fp)
except ZeroDivisionError:
precision = 0
try:
recall = tp / (tp + fn)
except ZeroDivisionError:
recall = 0
try:
f1 = 2 * (precision * recall) / (precision + recall)
except ZeroDivisionError:
f1 = 0
return(precision, recall, f1)
def calculate_score_no_class(predictions, gold):
tn=0
tp=0
fn=0
fp=0
zipped = zip(predictions, gold)
for p, g in zipped:
p_g = zip(p, g)
for pred, gold in p_g:
if pred == gold and pred == 'O':
tp+=1
if pred == gold and pred == 'I-event':
tn+=1
if pred != gold and gold == 'I-event':
fp+=1
if pred != gold and gold == "O":
fn+=1
#precision = tp / (tp+fp)
#recall = tp / (tp+fn)
#f1 = 2 * (precision * recall) / (precision + recall)
# allow for no predictions
try:
precision = tp / (tp+fp)
except ZeroDivisionError:
precision = 0
try:
recall = tp / (tp+fn)
except ZeroDivisionError:
recall = 0
try:
f1 = 2 * (precision * recall) / (precision + recall)
except ZeroDivisionError:
f1 = 0
return(precision, recall, f1)
def calculate_macro_avg(precision1, precision2, recall1, recall2):
mavg_p = (precision1+precision2)/2
mavg_r = (recall1+recall2)/2
try:
mavg_f1 = 2 * (( ((precision1+precision2)/2) * ((recall1+recall2)/2) ) / ( ((precision1+precision2)/2) + ((recall1+recall2)/2) ) )
except ZeroDivisionError:
mavg_f1 = 0
return(mavg_p, mavg_r, mavg_f1)
def delete_multiple_at_indices(lst, indices):
"""
This function is used by token_level_per_sentence, which sends indices of any labels that are not linked to
any first subtoken of a complete token. This function deletes those labels so that we get matching
lists of tokens and labels.
"""
# Sort indices in descending order
new_list = []
for i in range(0, len(lst)):
if i in indices:
continue
else:
new_list.append(lst[i])
return(new_list)
def token_level_per_sentence(sentence, predictions, gold, tokenizername): ###Chatgpt was used for this
"""
According to the tokenizer used, this function joins subtokens back together in the original tokens
as extracted from Inception and saves the label of the first subtoken of each token.
This happens on sentence level, i.e., each time one Inception text region is inputted to the function.
"""
joined_tokens = []
current_token = ""
to_delete = []
if tokenizername.startswith("emanjavacas") and not tokenizername.startswith('emanjavacas/MacBERTh'):
for i, tok in enumerate(sentence):
if tok.startswith('##'):
current_token += tok[2:] # Append without '##'
to_delete.append(i)
else:
if current_token: # If there's a current token being built, add it
joined_tokens.append(current_token)
current_token = "" # Reset for the next token
current_token = tok # Start a new current token with the new token
# If there's a current token left to add, append it
if current_token:
joined_tokens.append(current_token)# + current_token[2:])
if tokenizername.startswith('emanjavacas/MacBERTh'):
for i, tok in enumerate(sentence):
if tok.startswith('##'):
current_token += tok[2:] # Append without '##'
to_delete.append(i)
else:
if current_token: # If there's a current token being built, add it
joined_tokens.append(current_token)
current_token = "" # Reset for the next token
current_token = tok # Start a new current token with the new token
# If there's a current token left to add, append it
if current_token:
joined_tokens.append(current_token)# + current_token[2:])
if '[SEP]' in joined_tokens:
joined_tokens.remove('[SEP]') # filter out [SEP]
if tokenizername.startswith("GroNLP") or tokenizername.startswith("bert-base-multilingual-cased") or tokenizername.startswith('google-bert'):
for i, tok in enumerate(sentence):
if tok.startswith('##'):
current_token += tok[2:] # Append without '##'
to_delete.append(i)
else:
if current_token: # If there's a current token being built, add it
joined_tokens.append(current_token)
current_token = "" # Reset for the next token
current_token = tok # Start a new current token with the new token
# If there's a current token left to add, append it
if current_token:
joined_tokens.append(current_token)
new_preds = delete_multiple_at_indices(predictions, to_delete)
new_gold = delete_multiple_at_indices(gold, to_delete)
if tokenizername.startswith("globert"): ### this code is not used at the moment
# Result containers
joined_tokens = []
new_preds = []
new_gold = []
# read in reformatted vocab
with open('globertokenizer/tokenizer_from_scratch/reformatted/vocab.json') as infile:
data = infile.read()
#vocab_dict = ast.literal_eval(data)
# Temporary variables for the current word being built
current_token = ""
current_label = None
current_gold = None
for i, (subtoken, label, gold) in enumerate(zip(sentence, predictions, gold)):
for i, tok in enumerate(sentence):
#for key, value in vocab_dict.items():
if '_'+tok in data:
# If we already have a word being built, save it
if current_token:
joined_tokens.append(current_token)
new_preds.append(current_label)
new_gold.append(current_gold)
# Start a new word
current_token = subtoken #.lstrip('▁') # Remove leading '▁' for the word
current_label = label
current_gold = gold
else:
# Append the subtoken to the current word
current_token += subtoken
# Handle the last token (flush remaining word)
if i == len(sentence) - 1:
joined_tokens.append(current_token)
new_preds.append(current_label)
new_gold.append(current_gold)
if tokenizername.startswith("FacebookAI/xlm"):
# Result containers
joined_tokens = []
new_preds = []
new_gold = []
# Temporary variables for the current word being built
current_token = ""
current_label = None
current_gold = None
for i, (subtoken, label, gold) in enumerate(zip(sentence, predictions, gold)):
if subtoken.startswith('▁'): # new word detected
# If we already have a word being built, save it
if current_token:
joined_tokens.append(current_token)
new_preds.append(current_label)
new_gold.append(current_gold)
# Start a new word
current_token = subtoken.lstrip('▁') # Remove leading '▁' for the word
current_label = label
current_gold = gold
else:
# Append the subtoken to the current word
current_token += subtoken
# Handle the last token (flush remaining word)
if i == len(sentence) - 1:
joined_tokens.append(current_token)
new_preds.append(current_label)
new_gold.append(current_gold)
if tokenizername.startswith("pdelobelle") or tokenizername.startswith("FacebookAI/roberta"):
# clean up encoding errors
new_subtokens = []
for subtoken in sentence:
if subtoken == 'ĠâĢĶ':
new_subtokens.append('Ġ—')
if subtoken == 'âĢĶ':
new_subtokens.append('—')
if subtoken == 'ĠÆĴ':
new_subtokens.append('Ġƒ')
if subtoken == 'ÆĴ':
new_subtokens.append('ƒ')
if subtoken == 'ĠâĢŀ':
new_subtokens.append('Ġ„')
if subtoken == 'âĢŀ':
new_subtokens.append('„')
if subtoken == 'Ġ½':
new_subtokens.append('Ġ½')
if subtoken == '½':
new_subtokens.append('½')
if subtoken != 'ĠâĢĶ' and subtoken != 'ĠÆĴ' and subtoken != 'ĠâĢŀ' and subtoken != 'Ġý' and subtoken != 'âĢĶ' and subtoken != 'ÆĴ' and subtoken != 'âĢŀ' and subtoken != 'ý':
new_subtokens.append(subtoken)
joined_tokens = []
new_preds = []
new_gold = []
# Temporary variables for the current word being built
current_token = ""
current_label = None
current_gold = None
for i, (subtoken, label, gold) in enumerate(zip(new_subtokens, predictions, gold)):
print(subtoken, label, gold)
if subtoken.startswith('Ġ'): # new word detected
# If we already have a word being built, save it
if current_token:
joined_tokens.append(current_token)
new_preds.append(current_label)
new_gold.append(current_gold)
# Start a new word
current_token = subtoken.lstrip('Ġ') # Remove leading '▁' for the word
current_label = label
current_gold = gold
else:
# Append the subtoken to the current word
current_token += subtoken
# Handle the last token (flush remaining word)
if i == len(sentence) - 1:
joined_tokens.append(current_token)
new_preds.append(current_label)
new_gold.append(current_gold)
return joined_tokens, new_preds, new_gold
def interpolate(subtokens, predictions, gold, tokenizername):
interpolated_tokens = []
interpolated_predictions = []
interpolated_gold = []
for t, p, g in zip(subtokens, predictions, gold):
#print(t, p, g)
joined_tokens, joined_preds, joined_gold = token_level_per_sentence(t[1:-1], p, g, tokenizername)
interpolated_tokens.append(joined_tokens)
interpolated_predictions.append(joined_preds)
interpolated_gold.append(joined_gold)
return interpolated_tokens, interpolated_predictions, interpolated_gold
def write_preds_to_file(output_dir, tokens, predictions, gold, settings):
df = pd.DataFrame() #create dataframe
#assign columns
df['token'] = tokens
df['prediction'] = predictions
df['gold'] = gold
lex_tokens = lexical_baseline.parse_lexicon("lexicon_v4.csv") #latest version of the lexicon, also the one released
lexical_base = lexical_baseline.label_with_lexicon(lex_tokens, tokens)
df['lexical'] = lexical_base # add lexical baseline predictions to csv with predicted labels per token
#write to file with filename corresponding to model and test file used
try:
df.to_csv(output_dir+'/predictions_'+str(settings['metadata_testfile']['inv_nr'])+'_'+str(settings['metadata_testfile']['year'])+'_'+settings['model'].split('/')[1]+'_'+str(settings['seed'])+'.csv', sep='\t', index=False)
except IndexError:
df.to_csv(output_dir + '/predictions_' + str(settings['metadata_testfile']['inv_nr']) + '_' + str(
settings['metadata_testfile']['year']) + '_' + settings['model'] + '_' + str(
settings['seed']) + '.csv', sep='\t', index=False)
def get_datastats(tokens, predictions, gold):
tokens = [item for row in tokens for item in row]
predictions = [item for row in predictions for item in row]
gold = [item for row in gold for item in row]
eventcount_g = 0
for label in gold:
if label == 'I-event':
eventcount_g += 1
token_count = len(tokens)
eventcount_p = 0
for label in predictions:
if label == 'I-event':
eventcount_p += 1
try:
gold_density = ((eventcount_g / token_count) * 100)
except ZeroDivisionError:
gold_density = 0
return token_count, eventcount_g, eventcount_p, gold_density
def main():
print("This file should not be run independently ")
if __name__ == "__main__":
main()