-
Notifications
You must be signed in to change notification settings - Fork 5
/
task_report.py
466 lines (407 loc) · 14.2 KB
/
task_report.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
import argparse
import base64
import matplotlib.pyplot as plt
import pandas as pd
import plotly.graph_objects as go
import seaborn as sns
from cik_benchmark import ALL_TASKS
from cik_benchmark.base import ALLOWED_SKILLS
from datetime import datetime
from io import BytesIO
CONTEXT_PRETTY_NAMES = {
"c_h": "History",
"c_f": "Future",
"c_i": "Intemporal",
"c_cov": "Covariates",
"c_causal": "Causal",
}
def _figure_to_html(figure):
"""
Convert a matplotlib figure to an HTML string
"""
# Save the figure to a byte array
figure_bytes = BytesIO()
figure.savefig(figure_bytes, format="png")
plt.close(figure)
figure_bytes.seek(0)
# Convert byte array to base64 string
figure_base64 = base64.b64encode(figure_bytes.read()).decode("utf-8")
# Render as it would be in an HTML file
figure_html = f'<img src="data:image/png;base64,{figure_base64}" class="img-fluid" alt="Figure"/>'
return figure_html
def get_task_context_info(tasks):
"""
Get a DataFrame with information about which tasks use which context flags
"""
context_sources = CONTEXT_PRETTY_NAMES.keys()
task_info = []
for task_cls in tasks:
tmp = {}
tmp["task"] = task_cls.__name__
tmp.update(
{source: source in task_cls._context_sources for source in context_sources}
)
task_info.append(tmp)
return (
pd.DataFrame(task_info).set_index("task").rename(columns=CONTEXT_PRETTY_NAMES)
)
def get_task_skill_info(tasks, omit=[]):
"""
Get a DataFrame with information about which tasks evaluate which skill
"""
task_info = []
for task_cls in tasks:
tmp = {}
tmp["task"] = task_cls.__name__
tmp.update(
{
skill.capitalize(): skill in task_cls._skills
for skill in ALLOWED_SKILLS
if skill not in omit
}
)
task_info.append(tmp)
return pd.DataFrame(task_info).set_index("task")
def list_tasks_per_column(task_info):
"""
Produce an HTML list where each column is a heading and the tasks
with a True value for that column are listed under it as a bullet list
"""
# Initialize the HTML string
html = ""
# Loop over the context types
for col in task_info.columns:
# Add the context type as a heading
html += f"<h3 class='text-primary'>{col}</h3>\n"
# Get the tasks using the context type
tasks = task_info[task_info[col]].index
if len(tasks) == 0:
html += "<p>No tasks</p>\n"
else:
# Add the tasks as a bullet list
html += "<ul class='list-group'>\n"
for task in tasks:
html += f"<li class='list-group-item'><a href='./{task}.html'>{task}</a></li>\n"
html += "</ul>\n"
return html
def create_task_summary_page(tasks):
"""
Create a summary page for the benchmark tasks
"""
summary = """
<!DOCTYPE html>
<html>
<head>
<title>Task - {task_name}</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
body {{
background-color: #f8f9fa;
}}
.container {{
margin-top: 20px;
}}
.section {{
background-color: #ffffff;
border: 1px solid #dee2e6;
border-radius: 0.25rem;
padding: 20px;
margin-bottom: 20px;
}}
.section h2 {{
border-bottom: 2px solid #007bff;
padding-bottom: 10px;
}}
.list-group-item {{
background-color: #f8f9fa;
}}
.back-button {{
margin-bottom: 20px;
}}
</style>
</head>
<body>
<div class="container">
<div class="section">
<h1 class="text-center text-primary">{task_name}</h1>
</div>
<!-- Back Button -->
<button class="btn btn-secondary back-button" onclick="history.back()">Go Back</button>
<!-- Content -->
{seeds}
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
"""
context_source_mapping = {
"c_h": "Historical information",
"c_f": "Future information",
"c_i": "Intemporal information",
"c_cov": "Covariate information",
"c_causal": "Causal information",
}
skill_name_map = {
"instruction following": "Instruction Following",
"retrieval: context": "Retrieval: Context",
"retrieval: memory": "Retrieval: Memory",
"reasoning: deduction": "Reasoning: Deduction",
"reasoning: analogy": "Reasoning: Analogy",
"reasoning: math": "Reasoning: Math",
"reasoning: causal": "Reasoning: Causal",
}
for task_cls in tasks:
task_name = task_cls.__name__
seeds = ""
for seed in range(1, 6):
task = task_cls(seed=seed)
context_sources = list(
map(lambda x: context_source_mapping.get(x), task._context_sources)
)
capabilities = list(map(lambda x: skill_name_map.get(x), task._skills[2:]))
if task.background:
task.background = task.background.replace("\n", "<br />")
if task.constraints:
task.constraints = task.constraints.replace("\n", "<br />")
if task.scenario:
task.scenario = task.scenario.replace("\n", "<br />")
seeds += f"""
<div class="section">
<h2 class="text-center text-primary">Seed {seed}</h2>
"""
if task.background:
seeds += f"""
<p class="text-center">{task.background}</p>
"""
if task.scenario:
seeds += f"""
<p class="text-center">{task.scenario}</p>
"""
if task.constraints:
seeds += f"""
<p class="text-center"><strong>Constraints:</strong> {task.constraints}</p>
<br>
"""
seeds += f"""
<p class="text-center"><strong>Types of context:</strong> {context_sources}</p>
<p class="text-center"><strong>Capabilities:</strong> {capabilities}</p>
<br>
<p class="text-center">
{_figure_to_html(task.plot())}
</p>
</div>
"""
with open(f"{task_name}.html", "w") as f:
f.write(summary.format(task_name=task_name, seeds=seeds))
def plot_task_heatmap(task_info, plot_topic="Context Type"):
"""
Render the task info matrix as a heatmap using Plotly.
"""
# Convert boolean values to integers
task_info_int = task_info.astype(int)
# Check if all values are zero
if (task_info_int.values == 0).all():
colorscale = [[0, "white"], [1, "white"]]
else:
colorscale = [[0, "white"], [1, "red"]]
# Calculate the height of the plot
# Set a base height and add extra height per row
base_height = 400
row_height = 20 # Adjust this value as needed
plot_height = base_height + row_height * len(task_info_int.index)
# Create a heatmap using Plotly
fig = go.Figure(
data=go.Heatmap(
z=task_info_int.values,
x=task_info.columns,
y=task_info.index,
colorscale=colorscale,
showscale=False,
)
)
fig.update_layout(
title=f"{plot_topic} by Task",
xaxis_title=plot_topic,
yaxis_title="Tasks",
xaxis=dict(tickangle=-90),
template="plotly_white",
margin=dict(l=200, r=20, t=50, b=50), # Adjust margins if needed
height=plot_height, # Set the calculated height
)
fig.update_xaxes(showgrid=False)
fig.update_yaxes(showgrid=False)
return fig.to_html(full_html=False, include_plotlyjs="cdn")
def plot_task_barchart(task_info, plot_topic="Context Type"):
"""
Plot the number of tasks using each context type using Plotly.
"""
# Count the number of tasks using each context type
context_counts = task_info.sum()
# Create a bar plot using Plotly
fig = go.Figure(
data=[
go.Bar(x=context_counts.index, y=context_counts.values, marker_color="blue")
]
)
fig.update_layout(
title=f"Number of Tasks by {plot_topic}",
xaxis_title=plot_topic,
yaxis_title="Number of Tasks",
template="plotly_white",
)
return fig.to_html(full_html=False, include_plotlyjs="cdn")
if __name__ == "__main__":
tasks = ALL_TASKS
task_context_info = get_task_context_info(tasks)
task_skill_info = get_task_skill_info(
tasks, omit=["forecasting", "natural language processing"]
)
generation_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Stats by context source
task_by_context_bar = plot_task_barchart(
task_context_info, plot_topic="Context Type"
)
task_context_heatmap = plot_task_heatmap(
task_context_info, plot_topic="Context Type"
)
task_by_context_list = list_tasks_per_column(task_context_info)
# # Stats by skill
task_by_skill_bar = plot_task_barchart(task_skill_info, plot_topic="Skill")
task_skill_heatmap = plot_task_heatmap(task_skill_info, plot_topic="Skill")
task_by_skill_list = list_tasks_per_column(task_skill_info)
create_task_summary_page(tasks)
report = f"""
<!DOCTYPE html>
<html>
<head>
<title>Benchmark Task Overview</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://unpkg.com/gridjs/dist/theme/mermaid.min.css" rel="stylesheet" />
<style>
body {{
background-color: #f8f9fa;
}}
.container {{
margin-top: 20px;
}}
.section {{
background-color: #ffffff;
border: 1px solid #dee2e6;
border-radius: 0.25rem;
padding: 20px;
margin-bottom: 20px;
}}
.subsection {{
background-color: #f1f1f1;
border: 1px solid #dee2e6;
border-radius: 0.25rem;
padding: 15px;
margin-bottom: 15px;
}}
.section h2 {{
border-bottom: 2px solid #007bff;
padding-bottom: 10px;
cursor: pointer;
position: relative;
}}
.section h2::after {{
content: "\\25BC"; /* Downward pointing caret */
font-size: 1rem;
position: absolute;
right: 10px;
top: 10px;
transition: transform 0.3s ease;
}}
.collapse.show + h2::after {{
transform: rotate(-180deg); /* Rotate caret icon */
}}
.subsection h3 {{
border-bottom: 1px solid #007bff;
padding-bottom: 5px;
cursor: pointer;
}}
.list-group-item {{
background-color: #f8f9fa;
}}
</style>
</head>
<body>
<div class="container">
<div class="section">
<h1 class="text-center text-primary">Context is Key: A Benchmark for Forecasting with Essential Textual Information</h1>
</div>
<div class="section">
<h2 class="text-primary" data-bs-toggle="collapse" data-bs-target="#visualizations-section">Visualizations</h2>
<div class="collapse" id="visualizations-section">
<!-- Context Type Plots -->
<div class="subsection">
<h3 class="text-secondary">Tasks by Context Type</h3>
<div class="mb-4">
{task_by_context_bar}
</div>
<div class="mb-4">
{task_context_heatmap}
</div>
</div>
<!-- Skill Type Plots -->
<div class="subsection">
<h3 class="text-secondary">Tasks by Skill Type</h3>
<div class="mb-4">
{task_by_skill_bar}
</div>
<div class="mb-4">
{task_skill_heatmap}
</div>
</div>
</div>
</div>
<div class="section">
<h2 class="text-primary" data-bs-toggle="collapse" data-bs-target="#tasks-by-context-section">Tasks by Context Type</h2>
<div class="collapse" id="tasks-by-context-section">
<div class="mb-4">
There is a total of {len(task_context_info)} tasks in the benchmark.
</div>
{task_by_context_list}
</div>
</div>
<div class="section">
<h2 class="text-primary" data-bs-toggle="collapse" data-bs-target="#tasks-by-skill-section">Tasks by Skill</h2>
<div class="collapse" id="tasks-by-skill-section">
<div class="mb-4">
There is a total of {len(task_skill_info)} tasks in the benchmark.
</div>
{task_by_skill_list}
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<!-- Tables -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.3.0/papaparse.min.js"></script>
<script src="https://unpkg.com/gridjs/dist/gridjs.umd.js"></script>
<script>
Papa.parse("https://raw.githubusercontent.com/anon-forecast/benchmark_report_dev/main/results.csv", {{
download: true,
complete: function(results) {{
new gridjs.Grid({{
columns: results.data[0].map(header => ({{
name: header,
}})),
data: results.data.slice(1), // Skip the header row
search: true,
sort: true,
pagination: {{
enabled: true,
limit: 10
}},
fixedHeader: true,
autoWidth: true,
resizable: true
}}).render(document.getElementById("result-table"));
}}
}});
</script>
</body>
</html>
"""
with open(f"index.html", "w") as f:
f.write(report)