anselp commited on
Commit
5cb00fa
·
verified ·
1 Parent(s): 0e52835

Upload dipromats_evaluation_v2.py

Browse files
Files changed (1) hide show
  1. dipromats_evaluation_v2.py +273 -0
dipromats_evaluation_v2.py ADDED
@@ -0,0 +1,273 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import json
3
+ import numpy as np
4
+ import warnings
5
+ # Suprimir SettingWithCopyWarning
6
+ warnings.simplefilter(action='ignore', category=pd.errors.SettingWithCopyWarning)
7
+
8
+
9
+ def evaluate_results(lang, gold, test):
10
+ def normalize_labels(df):
11
+ # Define a function that checks if each narrative is present and assigns "yes" or "no"
12
+ def convert_narratives(row):
13
+ country_code = row['country'][:2].upper() # Get the country code ('RU', 'CH', etc.)
14
+ narratives = row['narratives'] # List of narratives for that row
15
+
16
+ # For each N1 to N6, check if it appears in the list of narratives
17
+ for i in range(1, 7):
18
+ narrative_code = f"{country_code}{i}"
19
+ row[f"N{i}"] = 'yes' if narrative_code in narratives else 'no'
20
+ return row
21
+ # Apply the function to each row of the DataFrame
22
+ data = df.apply(convert_narratives, axis=1)
23
+ # Drop the original 'narratives' column if no longer needed
24
+ data.drop(columns=['narratives', 'tweet_id'], inplace=True)
25
+ return data
26
+ def get_gold_lists_for_evaluation(gold_list, test_list):
27
+ gold_strict=[]
28
+ gold_lenient=[]
29
+ for i in range(0,6):
30
+ g=gold_list[i]
31
+ t=test_list[i]
32
+ g = 1 if g == 'yes' else 2 if g == 'no' else g
33
+ t = 1 if t == 'yes' else 2 if t == 'no' else t
34
+ if g==t:
35
+ gold_strict.append(g)
36
+ gold_lenient.append(g)
37
+ elif g!=t:
38
+ if g in [2, 1]:
39
+ gold_strict.append(g)
40
+ gold_lenient.append(g)
41
+ else:
42
+ gold_strict.append(2)
43
+ gold_lenient.append(t)
44
+ return gold_strict, gold_lenient
45
+ def gen_dic(lang):
46
+ narratives_list=['CH1', 'CH2', 'CH3', 'CH4', 'CH5', 'CH6', 'CH_micro', 'RU1', 'RU2', 'RU3', 'RU4', 'RU5', 'RU6', 'RU_micro', 'EU1', 'EU2', 'EU3', 'EU4', 'EU5', 'EU6', 'EU_micro', 'US1', 'US2', 'US3', 'US4', 'US5', 'US6', 'US_micro']
47
+ countries_dic={'China':'CH', 'Russia':'RU', 'EU':'EU', 'USA':'US'}
48
+ dic = {}
49
+ dic[lang] = {}
50
+ for ev in ['strict', 'lenient']:
51
+ if ev not in dic[lang]:
52
+ dic[lang][ev] = {}
53
+ for narr in narratives_list:
54
+ dic[lang][ev][narr] = {'scores': {'precision': 0., 'recall': 0., 'f1-score': 0.}, 'raw_data': []}
55
+
56
+ for code in countries_dic.values():
57
+ dic[lang][ev][f'{code}_micro'] = {'scores': {'precision': 0., 'recall': 0., 'f1-score': 0}, 'raw_data': []}
58
+
59
+ dic[lang][ev]['micro'] = {'scores': {'precision': 0., 'recall': 0., 'f1-score': 0}, 'raw_data': []}
60
+ return dic
61
+ def convert_labels(values):
62
+ return np.array([
63
+ [1 if v == 'yes' else 2 if v == 'no' else 3 for v in row]
64
+ for row in values
65
+ ])
66
+ def convert_floats(dic):
67
+ for key, value in dic.items():
68
+ if isinstance(value, np.float64):
69
+ dic[key] = float(value)
70
+ elif isinstance(value, dict): # If the value is another dictionary, apply recursion
71
+ convert_floats(value)
72
+ elif isinstance(value, list): # If the value is a list, convert individual elements
73
+ dic[key] = [float(v) if isinstance(v, np.float64) else v for v in value]
74
+ dic=gen_dic(lang)
75
+ countries_dic={'China':'CH', 'Russia':'RU', 'EU':'EU', 'USA':'US'}
76
+ cols=[f'N{i}' for i in range(1,7)]
77
+
78
+ df_gold=pd.DataFrame(gold)
79
+ df_gold.drop_duplicates(subset=['id', 'lang'], keep='last', inplace=True)
80
+ df=df_gold[df_gold['lang']==lang]
81
+ df.reset_index(inplace=True, drop=True)
82
+
83
+ df_test=pd.DataFrame(test)
84
+ df_test=normalize_labels(df_test)
85
+ df_test.drop_duplicates(subset=['id', 'language'], keep='last', inplace=True)
86
+ df_test.reset_index(inplace=True, drop=True)
87
+
88
+ df_strict=df.copy()
89
+ df_lenient=df.copy()
90
+ for i in range(len(df)):
91
+ lang=df['lang'].iloc[i]
92
+ id=df['id'].iloc[i]
93
+ gold_values=df[cols].iloc[i].values
94
+ dft=df_test[(df_test['language']==lang) & (df_test['id']==id)]
95
+
96
+ test_values=dft[cols].iloc[0].values
97
+ df_strict.loc[i, cols], df_lenient.loc[i, cols]=get_gold_lists_for_evaluation(gold_values, test_values)
98
+
99
+ countries=['China', 'Russia', 'EU', 'USA']
100
+
101
+ df_lang=df[(df['lang']==lang)]
102
+ df_test_lang=df_test[(df_test['language']==lang)]
103
+ df_strict_lang=df_strict[df_strict['lang']==lang]
104
+ df_lenient_lang=df_lenient[df_lenient['lang']==lang]
105
+ #F1 per narrative
106
+ for country in countries:
107
+ df_dup_t=df[(df['country']==country) & (df['lang']==lang)]
108
+ df_strict_t=df_strict_lang[df_strict_lang['country']==country]
109
+ df_lenient_t=df_lenient_lang[df_lenient_lang['country']==country]
110
+ dft=df_test_lang[(df_test_lang['country']==country)]
111
+ real_strict=[]
112
+ real_lenient=[]
113
+ real=[]
114
+ pred=[]
115
+ for i in range(len(df_strict_t)):
116
+ id=df_strict_t['id'].iloc[i]
117
+ dft2=dft[dft['id']==id]
118
+ if len(dft2)!=0:
119
+ real_strict.append(df_strict_t[cols].iloc[i].values)
120
+ real_lenient.append(df_lenient_t[cols].iloc[i].values)
121
+ pred.append(dft2[cols].iloc[0].values)
122
+ real.append(df_dup_t[df_dup_t['id']==id][cols].iloc[0].values)
123
+ real_strict=np.array(real_strict)
124
+ real_lenient=np.array(real_lenient)
125
+
126
+ real = convert_labels(real)
127
+ pred = convert_labels(pred)
128
+
129
+ for i in range(0, 6):
130
+ raw_matrix = np.zeros((2, 3), dtype=int) # 2 filas (pred), 3 columnas (real)
131
+ pred_options = [1, 2] # 1 -> 'yes', 2 -> 'no'
132
+ real_options = [1, 3, 2] # 1
133
+ p=pred[:,i]
134
+ r=real[:,i]
135
+ for p, r in zip(p, r):
136
+ pred_index = pred_options.index(p)
137
+ real_index = real_options.index(r)
138
+ raw_matrix[pred_index, real_index] += 1
139
+ tp=raw_matrix[0,0]
140
+ yl=raw_matrix[0,1]
141
+ fp=raw_matrix[0,2]
142
+ fn=raw_matrix[1,0]
143
+ nl=raw_matrix[1,1]
144
+ tn=raw_matrix[1,2]
145
+ dic[lang]['lenient'][f'{countries_dic[country]}{i+1}']['raw_data']=raw_matrix.tolist()
146
+ precision=(tp+yl)/(tp+yl+fp) if (tp+yl+fp)!=0 else 0
147
+ recall=(tp+yl)/(tp+fn+yl) if (tp+fn+yl)!=0 else 0
148
+ dic[lang]['lenient'][f'{countries_dic[country]}{i+1}']['scores']['precision']=precision
149
+ dic[lang]['lenient'][f'{countries_dic[country]}{i+1}']['scores']['recall']=recall
150
+ dic[lang]['lenient'][f'{countries_dic[country]}{i+1}']['scores']['f1-score']=(2*precision*recall)/(precision+recall) if (precision+recall)!=0 else 0
151
+ dic[lang]['strict'][f'{countries_dic[country]}{i+1}']['raw_data']=raw_matrix.tolist()
152
+ precision=tp/(tp+fp+yl) if (tp+fp+yl)!=0 else 0
153
+ recall=tp/(tp+fn) if (tp+fn)!=0 else 0
154
+ dic[lang]['strict'][f'{countries_dic[country]}{i+1}']['scores']['precision']=precision
155
+ dic[lang]['strict'][f'{countries_dic[country]}{i+1}']['scores']['recall']=recall
156
+ dic[lang]['strict'][f'{countries_dic[country]}{i+1}']['scores']['f1-score']=(2*precision*recall)/(precision+recall) if (precision+recall)!=0 else 0
157
+
158
+ #F1 Micro
159
+ real_strict=[]
160
+ real_lenient=[]
161
+ pred=[]
162
+ not_match=[]
163
+ real=[]
164
+ for i in range(len(df_lang)):
165
+ id=df_lang['id'].iloc[i]
166
+ dft=df_test_lang[df_test_lang['id']==id][cols]
167
+ if len(dft)!=0:
168
+ real_strict.extend(df_strict_lang[cols].iloc[i].values)
169
+ real_lenient.extend(df_strict_lang[cols].iloc[i].values)
170
+ pred.extend(df_test_lang[df_test_lang['id']==id][cols].iloc[0].values)
171
+ real.extend(df_lang[df_lang['id']==id][cols].iloc[0].values)
172
+ else:
173
+ not_match.append(id)
174
+
175
+ real = convert_labels([real])[0]
176
+ pred = convert_labels([pred])[0]
177
+ raw_matrix=np.zeros((2,3), dtype=int)
178
+ pred_options = [1, 2] # 1 -> 'yes', 2 -> 'no'
179
+ real_options = [1, 3, 2] # 1
180
+ raw_matrix = np.zeros((2, 3), dtype=int)
181
+ for p, r in zip(pred, real):
182
+ pred_index = pred_options.index(p)
183
+ real_index = real_options.index(r)
184
+ raw_matrix[pred_index, real_index] += 1
185
+ tp=raw_matrix[0,0]
186
+ yl=raw_matrix[0,1]
187
+ fp=raw_matrix[0,2]
188
+ fn=raw_matrix[1,0]
189
+ nl=raw_matrix[1,1]
190
+ tn=raw_matrix[1,2]
191
+ dic[lang]['lenient']['micro']['raw_data']=raw_matrix.tolist()
192
+ precision=(tp+yl)/(tp+yl+fp) if (tp+yl+fp)!=0 else 0
193
+ recall=(tp+yl)/(tp+fn+yl) if (tp+fn+yl)!=0 else 0
194
+ dic[lang]['lenient']['micro']['scores']['precision']=precision
195
+ dic[lang]['lenient']['micro']['scores']['recall']=recall
196
+ dic[lang]['lenient']['micro']['scores']['f1-score']=(2*precision*recall)/(precision+recall) if (precision+recall)!=0 else 0
197
+ dic[lang]['strict']['micro']['raw_data']=raw_matrix.tolist()
198
+ precision=tp/(tp+fp+yl) if (tp+yl+fp)!=0 else 0
199
+ recall=tp/(tp+fn) if (tp+fn)!=0 else 0
200
+ dic[lang]['strict']['micro']['scores']['precision']=precision
201
+ dic[lang]['strict']['micro']['scores']['recall']=recall
202
+ dic[lang]['strict']['micro']['scores']['f1-score']=(2*precision*recall)/(precision+recall) if (precision+recall)!=0 else 0
203
+
204
+ #Micro-Countries
205
+ for country in countries_dic.values():
206
+ raw_matrix = np.sum([np.array(dic[f'{lang}']['strict'][f'{country}{i}']['raw_data']) for i in range(1, 7)], axis=0)
207
+ tp=raw_matrix[0,0]
208
+ yl=raw_matrix[0,1]
209
+ fp=raw_matrix[0,2]
210
+ fn=raw_matrix[1,0]
211
+ nl=raw_matrix[1,1]
212
+ tn=raw_matrix[1,2]
213
+ precision=(tp+yl)/(tp+yl+fp) if (tp+yl+fp)!=0 else 0
214
+ recall=(tp+yl)/(tp+fn+yl) if (tp+fn+yl)!=0 else 0
215
+ dic[lang]['lenient'][f'{country}_micro']['scores']['precision']=precision
216
+ dic[lang]['lenient'][f'{country}_micro']['scores']['recall']=recall
217
+ dic[lang]['lenient'][f'{country}_micro']['scores']['f1-score']=(2*precision*recall)/(precision+recall) if (precision+recall)!=0 else 0
218
+ dic[lang]['lenient'][f'{country}_micro']['raw_data']=raw_matrix.tolist()
219
+ precision=tp/(tp+fp+yl) if (tp+yl+fp)!=0 else 0
220
+ recall=tp/(tp+fn) if (tp+fn)!=0 else 0
221
+ dic[lang]['strict'][f'{country}_micro']['scores']['precision']=precision
222
+ dic[lang]['strict'][f'{country}_micro']['scores']['recall']=recall
223
+ dic[lang]['strict'][f'{country}_micro']['scores']['f1-score']=(2*precision*recall)/(precision+recall) if (precision+recall)!=0 else 0
224
+ dic[lang]['strict'][f'{country}_micro']['raw_data']=raw_matrix.tolist()
225
+
226
+ convert_floats(dic[lang])
227
+
228
+ return dic[lang]
229
+
230
+
231
+
232
+
233
+
234
+ """
235
+ strict
236
+ narrative_country (e.g. CH1)
237
+ scores
238
+ precision
239
+ recall
240
+ f1-score
241
+ raw_data
242
+ country_micro (e.g. CH_micro)
243
+ scores
244
+ precision
245
+ recall
246
+ f1-score
247
+ raw_data
248
+ micro (global micro)
249
+ scores
250
+ precision
251
+ recall
252
+ f1-score
253
+ raw_data
254
+
255
+ lenient
256
+ narrative_country (e.g. CH1)
257
+ scores
258
+ precision
259
+ recall
260
+ f1-score
261
+ raw_data
262
+ country_micro (e.g. CH_micro)
263
+ scores
264
+ precision
265
+ recall
266
+ f1-score
267
+ raw_data
268
+ micro (global micro)
269
+ scores
270
+ precision
271
+ recall
272
+ f1-score
273
+ raw_data"""