-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_metrics.py
238 lines (183 loc) · 8.16 KB
/
get_metrics.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
"""
Generate metrics given cleaned team data.
Usage:
get_metrics (-i <input> | --input=<input>)
[-o <output> | --output=<output>]
get_metrics (-h | --help)
Options:
-i <input> --input=<input>
The cleaned team data in tsv format
-o <output> --output=<output>
The output directory to store metrics.tsv file
-h --help
Show this screen.
"""
import pandas as pd
from sklearn.metrics import cohen_kappa_score as ck
from sklearn.metrics import f1_score as f1
import itertools as it
from docopt import docopt
arguments = docopt(__doc__)
BIAS_VALUES = [
'Biased against Palestine',
'Biased against Israel',
'Biased against both Palestine and Israel',
'Biased against others',
'Unbiased',
'Unclear',
'Not Applicable',
]
def get_slice_by(df, s_type, batch, team, task, annotator = None):
# ignore annotator filter if no annotator is given
annotator_filter = (df.sheet_type == annotator) if annotator else True
data_slice = df[(df.type == s_type) & (df.batch == batch) & (df.team_name == team) & (df.task == task) & (annotator_filter)].reset_index(drop=True).copy()
return data_slice.sort_values(['batch', 'source_language', 'id'])
def get_annotator_details(pair):
if 'sheet_type' in pair[0]:
return pair[0]['sheet_type'], pair[1]['sheet_type']
else:
return None, None
def replace_labels(series, from_labels, to_label):
for f_label in from_labels:
series.replace(f_label, to_label, inplace=True)
return series
def to_bias_grp_else_series(series):
x = replace_labels(series.copy(), ['Unclear', 'Not Applicable'], 'Un/NA')
return replace_labels(x.copy(), BIAS_VALUES[:4], 'Biased')
def get_cohens_kappa(series_1, series_2):
return ck(series_1, series_2)
def get_accuracy(series_1, series_2):
return series_1.eq(series_2).sum() / series_1.shape[0]
def get_accuracy_else(series_1, series_2):
x = replace_labels(series_1.copy(), ['Unclear', 'Not Applicable'], 'Un/NA')
y = replace_labels(series_2.copy(), ['Unclear', 'Not Applicable'], 'Un/NA')
return get_accuracy(x, y)
def get_accuracy_bias_grp(series_1, series_2):
x = to_bias_grp_else_series(series_1)
y = to_bias_grp_else_series(series_2)
return get_accuracy(x, y)
def get_fscore(series_1, series_2):
return f1(series_1, series_2, average='macro')
def get_fscore_bias(series_1, series_2):
x = to_bias_grp_else_series(series_1)
y = to_bias_grp_else_series(series_2)
return f1(x, y, average='micro', labels=['Biased'])
def get_fscore_prop(series_1, series_2):
x = replace_labels(series_1.copy(), ['Unclear', 'Not Applicable'], 'Un/NA')
y = replace_labels(series_2.copy(), ['Unclear', 'Not Applicable'], 'Un/NA')
return f1(x, y, average='micro', labels=['Propaganda'])
def get_pair_bias_score(df, s_type, pair):
annot_1, annot_2 = get_annotator_details(pair)
team_1_data = get_slice_by(df, s_type, pair[0]['batch'], pair[0]['team_name'], 'Bias', annot_1)
team_2_data = get_slice_by(df, s_type, pair[1]['batch'], pair[1]['team_name'], 'Bias', annot_2)
if team_1_data.shape[0] == 0 or team_2_data.shape[0] == 0:
return {}
# get cohens kappa
b_co_ka = get_cohens_kappa(team_1_data.label, team_2_data.label)
# get bias accuracy - 1) exact 2) else 3) bias_grp + else
b_accuracy = get_accuracy(team_1_data.label, team_2_data.label)
# b_accuracy_else = get_accuracy_else(team_1_data.label, team_2_data.label)
# b_accuracy_bias_grp_else = get_accuracy_bias_grp(team_1_data.label, team_2_data.label)
# fscores
b_fscore = get_fscore(team_1_data.label, team_2_data.label)
b_only_fscore = get_fscore_bias(team_1_data.label, team_2_data.label)
return {
'bias_cohen_kappa': b_co_ka,
'bias_accuracy': b_accuracy,
'bias_macro_f1': b_fscore,
'bias*_f1': b_only_fscore
}
def get_pair_prop_score(df, s_type, pair):
annot_1, annot_2 = get_annotator_details(pair)
team_1_data = get_slice_by(df, s_type, pair[0]['batch'], pair[0]['team_name'], 'Propaganda', annot_1)
team_2_data = get_slice_by(df, s_type, pair[1]['batch'], pair[1]['team_name'], 'Propaganda', annot_2)
if team_1_data.shape[0] == 0 or team_2_data.shape[0] == 0:
return {}
# get cohens kappa
p_co_ka = get_cohens_kappa(team_1_data.label, team_2_data.label)
# get bias accuracy - 1) exact 2) else 3) bias_grp + else
p_accuracy = get_accuracy(team_1_data.label, team_2_data.label)
# fscores
p_fscore = get_fscore(team_1_data.label, team_2_data.label)
p_only_fscore = get_fscore_prop(team_1_data.label, team_2_data.label)
return {
'propaganda_cohen_kappa': p_co_ka,
'propaganda_accuracy': p_accuracy,
'propaganda_macro_f1': p_fscore,
'propaganda_f1': p_only_fscore
}
def get_team_main_metrics(df):
# get unique team-batch pairs from MAIN
unique_team_batch = df[df.type == 'MAIN'][['team_name', 'batch']].drop_duplicates()
# unique pairs across teams
pairs = it.permutations(unique_team_batch.to_dict('records'), 2)
pair_scores = []
for pair in pairs:
if pair[0]['batch'] != pair[1]['batch']:
continue
print(pair)
s_type = 'MAIN'
pair_details = {
's_type': s_type,
'batch': pair[0]['batch'],
'team_1': pair[0]['team_name'],
'team_2': pair[1]['team_name']
}
bias_scores = get_pair_bias_score(df, s_type, pair)
prop_scores = get_pair_prop_score(df, s_type, pair)
pair_scores.append({**pair_details, **bias_scores, **prop_scores})
pair_df = pd.DataFrame(pair_scores)
pair_df['metrics'] = 'across_team'
team_avgs = pair_df.groupby(['s_type', 'team_1']).mean(numeric_only=True).reset_index()
team_avgs['metrics'] = 'across_team_avg'
return pd.concat([pair_df, team_avgs])
def get_team_iaa_metrics(df):
unique_team_batch = df[df['type'] == 'IAA'][['team_name', 'batch', 'sheet_type']].drop_duplicates()
# unique pairs across teams
pairs = it.permutations(unique_team_batch.to_dict('records'), 2)
pair_scores = []
for pair in pairs:
if pair[0]['batch'] != pair[1]['batch']:
continue
print(pair)
if pair[0]['team_name'] != pair[1]['team_name']:
metric = 'across_team'
else:
metric = 'within_team'
s_type = 'IAA'
pair_details = {
's_type': s_type,
'metrics': metric,
'batch': pair[0]['batch'],
'team_1': pair[0]['team_name'],
'annot_1': pair[0]['sheet_type'],
'team_2': pair[1]['team_name'],
'annot_2': pair[1]['sheet_type']
}
bias_scores = get_pair_bias_score(df, s_type, pair)
prop_scores = get_pair_prop_score(df, s_type, pair)
pair_scores.append({**pair_details, **bias_scores, **prop_scores})
return pd.DataFrame(pair_scores)
def get_within_team_avgs(iaa_pair_df):
in_team_df = iaa_pair_df[iaa_pair_df.team_2 == iaa_pair_df.team_1]
in_team_avgs = in_team_df.groupby(['s_type', 'team_1']).mean(numeric_only=True).reset_index()
in_team_avgs['metrics'] = 'within_team_avg'
return in_team_avgs
def get_across_team_avgs(iaa_pair_df):
across_team_df = iaa_pair_df[iaa_pair_df.team_2 != iaa_pair_df.team_1]
across_team_avgs = across_team_df.groupby(['s_type', 'team_1']).mean(numeric_only=True).reset_index()
across_team_avgs['metrics'] = 'across_team_avg'
return across_team_avgs
if __name__ == '__main__':
df = pd.read_csv(arguments['--input'], sep='\t')
# team metrics, MAIN
team_df = get_team_main_metrics(df)
# annotator metricS, IAA
iaa_pair_df = get_team_iaa_metrics(df)
# team averages
in_team_avgs = get_within_team_avgs(iaa_pair_df)
across_team_avgs = get_across_team_avgs(iaa_pair_df)
# combining above metrics
annotator_df = pd.concat([iaa_pair_df, in_team_avgs, across_team_avgs])
annotator_df = pd.concat([team_df, annotator_df]).reset_index(drop=True)
annotator_df.to_csv(f"{arguments['--output']}/metrics.tsv", sep='\t', index=False)