-
Notifications
You must be signed in to change notification settings - Fork 9
/
draw_illustrative_results.py
357 lines (324 loc) · 12.6 KB
/
draw_illustrative_results.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
import json
import os
import plotly.graph_objects as go
import numpy as np
from plotly.subplots import make_subplots
import plotly.io as pio
pio.kaleido.scope.mathjax = None
model2name = {
'bert_large':'BERT-Large',
'pubmed_bert': 'PubMedBERT',
'bart_large':'BART-Large',
'flan_t5_large': 'Flan-T5-Large',
'bloom_1b7': 'BLOOM-1b7',
'flan_t5_xl': 'Flan-T5-XL',
'bloom_3b': 'BLOOM-3b',
'llama_7b':'LLaMA-7B',
'gpt3':'GPT-3',
'chatgpt': 'GPT-3.5',
'gpt4': 'GPT-4',
'umls_flan_t5_large':'Flan-T5-Large*',
'umls_flan_t5_xl': 'Flan-T5-XL*',
'schemaorg_flan_t5_large': 'Flan-T5-Large*',
'schemaorg_flan_t5_xl': 'Flan-T5-XL*',
'geonames_flan_t5_large': 'Flan-T5-Large*',
'geonames_flan_t5_xl': 'Flan-T5-XL*',
'wn18rr_flan_t5_large':'Flan-T5-Large*',
'wn18rr_flan_t5_xl':'Flan-T5-XL*',
}
llm_no = 13
dir2name = {
'wn18rr': 'WordNet',
'geonames': 'GeoNames',
'nci': 'NCI',
'snomedct_us': 'SNOMEDCT',
'medcin':'Medcin',
'umls': 'UMLS',
'schema': 'schema.org'
}
medical_dir2name = {
'nci': 'NCI',
'snomedct_us': 'SNOMEDCT',
'medcin':'Medcin',
'umls': 'UMLS',
}
tasks = ['A', 'B', 'C']
task_templates_name = {
"A": [f'template-{str(index)}' for index in range(1, 9)],
"B": [f'-{str(index)}-' for index in range(0, 8)],
"C": [""]
}
def read_json(path: str):
"""
Reads the ``json`` file of the given ``input_path``.
:param input_path: Path to the json file
:return: A loaded json object.
"""
with open(path, encoding="utf-8") as f:
json_data = json.load(f)
return json_data
def rounding(score):
return score
report_catalog = {}
for task in tasks:
report_catalog[task] = {}
root_task_dir = f"Task{task}/results/"
for dataset in dir2name.keys():
if dataset in os.listdir(root_task_dir):
report_catalog[task][dataset] = {}
# print(dir2name[dataset])
dataset_dir_path = os.path.join(root_task_dir, dataset)
for model_output_dir in os.listdir(dataset_dir_path):
model_output_dir_path = os.path.join(dataset_dir_path, model_output_dir)
if os.path.isdir(model_output_dir_path) and not '.ipynb_checkpoints' in model_output_dir_path:
prefix = f"report-{model_output_dir}"
results = []
for template in task_templates_name[task]:
for file in os.listdir(model_output_dir_path):
if file.startswith(prefix) and template in file:
json_file = read_json(path=os.path.join(model_output_dir_path, file))
if task == 'A':
score = json_file['results']['MAP@1'] * 100
results.append(score)
elif task == 'B':
score = json_file['results']['clf-report-dict']['macro avg']['f1-score'] * 100
results.append(score)
elif task == 'C':
score = json_file['results']['clf-report']['macro avg']['f1-score'] * 100
results.append(score)
break
report_catalog[task][dataset][model_output_dir] = results
markers_dict = {
"WordNet":"square",
"UMLS": "star",
"GeoNames": "circle",
"schema.org": "diamond-tall",
"NCI": "star",
"SNOMEDCT": "star",
"Medcin":"star"
}
colors_dict = {
"WordNet":"orange",
"UMLS": "blue",
"GeoNames": "green",
"schema.org": "red",
"NCI": "blue",
"SNOMEDCT": "#1f77b4",
"Medcin":"#17becf"
}
color_a_box = 'pink'
color_b_box = 'powderblue'
color_all_box = 'powderblue'
activate_all = True
fig = make_subplots(rows=1, cols=2,
shared_yaxes=True,
horizontal_spacing = 0.025,
column_widths=[0.85, 0.15],
subplot_titles=("Zero-Shot Testing", "Finetuned"))
categories = list(model2name.values())[:llm_no]
# ----------------------------------------------------------------------------------------------------------------------------------------------
# Task A
# ----------------------------------------------------------------------------------------------------------------------------------------------
#####################################################################################################################
task = 'A'
df_file_name = 'task_a_radar.csv'
results_matrix = []
colors = ["orange", "gray", "purple", "blue", "green"]
dataset_names = []
for index, dataset in enumerate(report_catalog[task].keys()):
dataset_names.append(dir2name[dataset])
results = []
for model in model2name.keys():
for report, results_score in report_catalog[task][dataset].items():
if model == report:
if model == 'pubmed_bert' and dataset in ['umls', 'nci', 'medcin', 'snomedct_us']:
results.append(rounding(max(results_score)))
elif model != 'pubmed_bert':
results.append(rounding(max(results_score)))
if model == 'pubmed_bert' and dataset not in ['umls', 'nci', 'medcin', 'snomedct_us']:
results.append(0)
results_matrix.append(results)
for i in range(0, 5):
dot_opacity = np.ones(len(results_matrix[i]))
if list(report_catalog[task].keys())[i] in ['umls', 'nci', 'medcin', 'snomedct_us']:
dot_opacity[1] = 1
else:
dot_opacity[1] = 0
fig.add_trace(go.Scatter(
x=categories[:-2],
y=results_matrix[i][:-2],
name="Task A: " + dataset_names[i],
line_color=colors_dict[dataset_names[i]],
mode='markers',
marker=dict(
size=12, symbol=markers_dict[dataset_names[i]] + "-open", opacity=dot_opacity
)
), row=1, col=1)
fig.add_trace(go.Scatter(
x=categories[-2:],
y=results_matrix[i][-2:],
name="Task A: " + dataset_names[i],
line_color=colors_dict[dataset_names[i]],
mode='markers',
marker=dict(
size=12, symbol=markers_dict[dataset_names[i]] + "-open"
), showlegend=False
), row=1, col=2)
results_matrix_all = results_matrix
# if not activate_all:
# results_matrix = np.array(results_matrix_all)
# for i in range(0, results_matrix.shape[1]):
# print(categories[i], results_matrix[:,i])
# fig.add_trace(go.Box(
# y=[score for score in list(results_matrix[:,i]) if score != 0],
# name=categories[i],
# line_color=color_a_box,
# showlegend=False
# ))
# ----------------------------------------------------------------------------------------------------------------------------------------------
# Task B
# ----------------------------------------------------------------------------------------------------------------------------------------------
#####################################################################################################################
task = 'B'
df_file_name = 'task_a_radar.csv'
results_matrix = []
colors = ["gray", "blue", "red"]
dataset_names = []
for index, dataset in enumerate(report_catalog[task].keys()):
dataset_names.append(dir2name[dataset])
results = []
for model in model2name.keys():
for report, results_score in report_catalog[task][dataset].items():
if model == report:
if model == 'pubmed_bert' and dataset in ['umls', 'nci', 'medcin', 'snomedct_us']:
results.append(rounding(max(results_score)))
elif model != 'pubmed_bert':
results.append(rounding(max(results_score)))
if model == 'pubmed_bert' and dataset not in ['umls', 'nci', 'medcin', 'snomedct_us']:
results.append(0)
results_matrix.append(results)
for i in range(0, 3):
dot_opacity = np.ones(len(results_matrix[i]))
if list(report_catalog[task].keys())[i] in ['umls', 'nci', 'medcin', 'snomedct_us']:
dot_opacity[1] = 1
else:
dot_opacity[1] = 0
fig.add_trace(go.Scatter(
x=categories[:-2],
y=results_matrix[i][:-2],
name="Task B: " + dataset_names[i],
line_color=colors_dict[dataset_names[i]],
mode='markers',
marker=dict(
size=12, symbol=markers_dict[dataset_names[i]], opacity=dot_opacity
)
), row=1, col=1)
fig.add_trace(go.Scatter(
x=categories[-2:],
y=results_matrix[i][-2:],
name="Task B: " + dataset_names[i],
line_color=colors_dict[dataset_names[i]],
mode='markers',
marker=dict(
size=12, symbol=markers_dict[dataset_names[i]]
), showlegend=False
), row=1, col=2)
results_matrix_all += results_matrix
# if not activate_all:
# results_matrix = np.array(results_matrix)
# for i in range(0, results_matrix.shape[1]):
# # print(categories[i], results_matrix[:,i])
# fig.add_trace(go.Box(
# y=[score for score in list(results_matrix[:,i]) if score != 0],
# name=categories[i],
# line_color=color_b_box,
# showlegend=False
# ))
# ----------------------------------------------------------------------------------------------------------------------------------------------
# Task C
# ----------------------------------------------------------------------------------------------------------------------------------------------
#####################################################################################################################
task = 'C'
df_file_name = 'task_a_radar.csv'
results_matrix = []
colors = ["blue"]
dataset_names = []
for index, dataset in enumerate(report_catalog[task].keys()):
dataset_names.append(dir2name[dataset])
results = []
for model in model2name.keys():
for report, results_score in report_catalog[task][dataset].items():
if model == report:
results.append(rounding(max(results_score)))
results_matrix.append(results)
for i in range(0, 1):
dot_opacity = np.ones(len(results_matrix[i]))
if list(report_catalog[task].keys())[i] in ['umls', 'nci', 'medcin', 'snomedct_us']:
dot_opacity[1] = 1
else:
dot_opacity[1] = 0
fig.add_trace(go.Scatter(
x=categories[:-2],
y=results_matrix[i][:-2],
name="Task C: " + dataset_names[i],
line_color=colors_dict[dataset_names[i]],
mode='markers',
marker=dict(
size=5, symbol=markers_dict[dataset_names[i]] + "-open-dot", opacity=dot_opacity
)
), row=1, col=1)
fig.add_trace(go.Scatter(
x=categories[-2:],
y=results_matrix[i][-2:],
name="Task C: " + dataset_names[i],
line_color=colors_dict[dataset_names[i]],
mode='markers',
marker=dict(
size=5, symbol=markers_dict[dataset_names[i]] + "-open-dot"
), showlegend=False
), row=1, col=2)
results_matrix_all += results_matrix
if activate_all:
results_matrix = np.array(results_matrix_all)
for i in range(0, results_matrix.shape[1]):
print(categories[i], results_matrix[:, i])
if categories[i] == "PubMedBERT":
continue
if categories[i] not in ['Flan-T5-Large*', 'Flan-T5-XL*']:
fig.add_trace(go.Box(
y=[score for score in list(results_matrix[:, i]) if score != 0],
name=categories[i],
line_color=color_b_box,
showlegend=False
), row=1, col=1)
else:
fig.add_trace(go.Box(
y=[score for score in list(results_matrix[:, i]) if score != 0],
name=categories[i],
line_color=color_b_box,
showlegend=False
), row=1, col=2)
#####################################################################################################################
width = 1280
height = 400
fig.update_layout(title=None,
# scattermode="group",
width=width,
height=height,
legend=dict(
orientation="h",
y=-0.15,
x=0,
yanchor='bottom',
xanchor="left"
),
margin=dict(l=20, r=20, t=20, b=20)
)
fig.update_yaxes(tick0=0, dtick=10)
fig.show()
fig.write_image("images/results-figure.pdf",
width=width,
height=height)
fig.write_image("images/results-figure.jpeg",
width=width,
height=height)