-
Notifications
You must be signed in to change notification settings - Fork 0
/
subtask_1_2a.py
246 lines (217 loc) · 8.82 KB
/
subtask_1_2a.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
import pdb
import json
import logging.handlers
import argparse
import os
import numpy as np
from sklearn.metrics import f1_score
from sklearn.preprocessing import MultiLabelBinarizer
from networkx import DiGraph, relabel_nodes, all_pairs_shortest_path_length
from sklearn_hierarchical_classification.constants import ROOT
from sklearn_hierarchical_classification.metrics import h_fbeta_score, h_recall_score, h_precision_score, \
fill_ancestors, multi_labeled
import sys
sys.path.append('.')
KEYS = ['id','labels']
logger = logging.getLogger("subtask_1_2a_scorer")
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.setLevel(logging.INFO)
#logging.basicConfig(format='%(levelname)s : %(message)s', level=logging.INFO)
G = DiGraph()
G.add_edge(ROOT, "Logos")
G.add_edge("Logos", "Repetition")
G.add_edge("Logos", "Obfuscation, Intentional vagueness, Confusion")
G.add_edge("Logos", "Reasoning")
G.add_edge("Logos", "Justification")
G.add_edge('Justification', "Slogans")
G.add_edge('Justification', "Bandwagon")
G.add_edge('Justification', "Appeal to authority")
G.add_edge('Justification', "Flag-waving")
G.add_edge('Justification', "Appeal to fear/prejudice")
G.add_edge('Reasoning', "Simplification")
G.add_edge('Simplification', "Causal Oversimplification")
G.add_edge('Simplification', "Black-and-white Fallacy/Dictatorship")
G.add_edge('Simplification', "Thought-terminating cliché")
G.add_edge('Reasoning', "Distraction")
G.add_edge('Distraction', "Misrepresentation of Someone's Position (Straw Man)")
G.add_edge('Distraction', "Presenting Irrelevant Data (Red Herring)")
G.add_edge('Distraction', "Whataboutism")
G.add_edge(ROOT, "Ethos")
G.add_edge('Ethos', "Appeal to authority")
G.add_edge('Ethos', "Glittering generalities (Virtue)")
G.add_edge('Ethos', "Bandwagon")
G.add_edge('Ethos', "Ad Hominem")
G.add_edge('Ethos', "Transfer")
G.add_edge('Ad Hominem', "Doubt")
G.add_edge('Ad Hominem', "Name calling/Labeling")
G.add_edge('Ad Hominem', "Smears")
G.add_edge('Ad Hominem', "Reductio ad hitlerum")
G.add_edge('Ad Hominem', "Whataboutism")
G.add_edge(ROOT, "Pathos")
G.add_edge('Pathos', "Exaggeration/Minimisation")
G.add_edge('Pathos', "Loaded Language")
G.add_edge('Pathos', "Appeal to (Strong) Emotions")
G.add_edge('Pathos', "Appeal to fear/prejudice")
G.add_edge('Pathos', "Flag-waving")
G.add_edge('Pathos', "Transfer")
def get_all_classes_from_graph(graph):
return [
node
for node in graph.nodes
if node != ROOT
]
def _h_fbeta_score(y_true, y_pred, class_hierarchy, beta=1., root=ROOT):
hP = _h_precision_score(y_true, y_pred, class_hierarchy, root=root)
hR = _h_recall_score(y_true, y_pred, class_hierarchy, root=root)
return (1. + beta ** 2.) * hP * hR / (beta ** 2. * hP + hR)
def _fill_ancestors(y, graph, root, copy=True):
y_ = y.copy() if copy else y
paths = all_pairs_shortest_path_length(graph.reverse(copy=False))
for target, distances in paths:
if target == root:
continue
ix_rows = np.where(y[:, target] > 0)[0]
ancestors = list(filter(lambda x: x != ROOT,distances.keys()))
y_[tuple(np.meshgrid(ix_rows, ancestors))] = 1
graph.reverse(copy=False)
return y_
def _h_recall_score(y_true, y_pred, class_hierarchy, root=ROOT):
y_true_ = _fill_ancestors(y_true, graph=class_hierarchy, root=root)
y_pred_ = _fill_ancestors(y_pred, graph=class_hierarchy, root=root)
ix = np.where((y_true_ != 0) & (y_pred_ != 0))
true_positives = len(ix[0])
all_positives = np.count_nonzero(y_true_)
return true_positives / all_positives
def _h_precision_score(y_true, y_pred, class_hierarchy, root=ROOT):
y_true_ = _fill_ancestors(y_true, graph=class_hierarchy, root=root)
y_pred_ = _fill_ancestors(y_pred, graph=class_hierarchy, root=root)
ix = np.where((y_true_ != 0) & (y_pred_ != 0))
true_positives = len(ix[0])
all_results = np.count_nonzero(y_pred_)
return true_positives / all_results
def read_classes(file_path):
CLASSES = []
with open(file_path) as f:
for label in f.readlines():
label = label.strip()
if label:
CLASSES.append(label)
return CLASSES
def check_format(file_path):
_classes = get_all_classes_from_graph(G)
if not os.path.exists(file_path):
logging.error("File doesnt exists: {}".format(file_path))
return False
submmission = ''
try:
with open(file_path, encoding='utf-8') as p:
submission = json.load(p)
except:
logging.error("File is not a valid json file: {}".format(file_path))
return False
for i, obj in enumerate(submission):
for key in KEYS:
if key not in obj:
logging.error("Missing entry in {}:{}".format(file_path, i))
return False
for label in list(obj['labels']):
if label not in _classes:
print(label)
logging.error("Unknown Label in {}:{}".format(file_path, i))
return False
return True
def _read_gold_and_pred(pred_fpath, gold_fpath):
"""
Read gold and predicted data.
:param pred_fpath: a json file with predictions,
:param gold_fpath: the original annotated gold file.
:return: {id:pred_labels} dict; {id:gold_labels} dict
"""
gold_labels = {}
with open(gold_fpath, encoding='utf-8') as gold_f:
gold = json.load(gold_f)
for obj in gold:
gold_labels[obj['id']] = obj['labels']
pred_labels = {}
with open(pred_fpath, encoding='utf-8') as pred_f:
pred = json.load(pred_f)
for obj in pred:
pred_labels[obj['id']] = obj['labels']
if set(gold_labels.keys()) != set(pred_labels.keys()):
logger.error('There are either missing or added examples to the prediction file. Make sure you only have the gold examples in the prediction file.')
raise ValueError('There are either missing or added examples to the prediction file. Make sure you only have the gold examples in the prediction file.')
return pred_labels, gold_labels
def evaluate_h(pred_fpath, gold_fpath):
pred_file = "./final.txt"
#gold_file = args.gold_file_path
#gold_file = "./data/task1_validation.json"
#gold_file="/home/lidailin/bert_semeval/test_task1_dev_with_labels.json"
gold_file="/home/lidailin/bert_semeval/data/new_task1_validation1.json"
pred_labels, gold_labels = _read_gold_and_pred(pred_file, gold_file)
gold = []
pred = []
for id in gold_labels:
gold.append(gold_labels[id])
pred.append(pred_labels[id])
with multi_labeled(gold, pred, G) as (gold_, pred_, graph_):
return _h_precision_score(gold_, pred_,graph_), _h_recall_score(gold_, pred_,graph_), _h_fbeta_score(gold_, pred_,graph_)
def validate_files(pred_files, gold_files):
if not check_format(pred_file):
logger.error('Bad format for pred file {}. Cannot score.'.format(pred_file))
return False
return True
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
"--gold_file_path",
'-g',
type=str,
required=True,
help="Paths to the file with gold annotations."
)
parser.add_argument(
"--pred_file_path",
'-p',
type=str,
required=True,
help="Path to the file with predictions"
)
parser.add_argument(
"--log_to_file",
"-l",
action='store_true',
default=False,
help="Set flag if you want to log the execution file. The log will be appended to <pred_file>.log"
)
args = parser.parse_args()
#pred_file = args.pred_file_path
pred_file = "./test2.txt"
#gold_file = args.gold_file_path
gold_file = "./data/task1_validation.json"
if args.log_to_file:
output_log_file = pred_file + ".log"
logger.info("Logging execution to file " + output_log_file)
fileLogger = logging.FileHandler(output_log_file)
fileLogger.setLevel(logging.DEBUG)
fileLogger.setFormatter(formatter)
logger.addHandler(fileLogger)
logger.setLevel(logging.DEBUG)
if args.log_to_file:
logger.info('Reading gold file')
else:
logger.info("Reading gold predictions from file {}".format(args.gold_file_path))
if args.log_to_file:
logger.info('Reading predictions file')
else:
logger.info('Reading predictions file {}'.format(args.pred_file_path))
if validate_files(pred_file, gold_file):
logger.info('Prediction file format is correct')
prec_h, rec_h, f1_h = evaluate_h(pred_file, gold_file)
logger.info("f1_h={:.5f}\tprec_h={:.5f}\trec_h={:.5f}".format(f1_h, prec_h, rec_h))
if args.log_to_file:
print("{}\t{}\t{}".format(f1_h, prec_h, rec_h))
else:
print("f1_h={:.5f}\tprec_h={:.5f}\trec_h={:.5f}".format(f1_h, prec_h, rec_h))