-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpresent_data.js
467 lines (375 loc) · 14.1 KB
/
present_data.js
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
// Modify SI units such that the param counts to be presented as e.g. 7B
fcopy = d3.format;
function myFormat(){
function_ret = fcopy.apply(d3, arguments)
return (function(args){return function (){
return args.apply(d3, arguments).replace(/G/, "B");
}})(function_ret)
}
d3.format = myFormat;
/* LEADERBOARD TABLE */
var leaderboard = d3.select("table.leaderboard")
var table_body = leaderboard.select("tbody");
function pad(text, length, end=false) {
if (text.length >= length)
return text
else if (end)
return text + " ".repeat(length - text.length)
else
return " ".repeat(length - text.length) + text
}
function format_accuracy(accuracy) {
if (accuracy.sem) {
return `${pad((accuracy.mean * 100).toFixed(1), 4)}% ±<small>${pad((accuracy.sem * 100).toFixed(1), 4)}%</small>`
} else {
return pad((accuracy.mean * 100).toFixed(1), 4) + "%" + (" ".repeat(3)) + `<small>${pad("", 4)}</small>`
}
}
function params_format(x, precision=null) {
if (x == "-") return x;
else if (x >= 1e12) return (x / 1e12).toFixed(precision !== null ? precision : 1) + "T";
else if (x >= 1e9) return (x / 1e9).toFixed(precision !== null ? precision : 1) + "b";
else if (x >= 1e6) return (x / 1e6).toFixed(precision !== null ? precision : 0) + "M";
else if (x >= 1e3) return (x / 1e6).toFixed(precision !== null ? precision : 0) + "K";
else return x;
}
function setup_table(data) {
data = data.sort((a, b) => b.accuracy.mean - a.accuracy.mean)
var row = table_body.selectAll("tr")
.data(data)
.enter()
.append("tr");
row.append("td").append("a").attr("href", (d) => d.model_url).text((d) => d.model_name);
row.append("td").text((d) => d.model_type);
row.append("td").text((d) => params_format(d.num_params));
row.append("td").text((d) => params_format(d.num_tokens, 0));
row.append("td").html((d) => format_accuracy(d.accuracy));
}
function sort_table(sort_key, to_ascending) {
table_body.selectAll("tr").sort(
(a, b) => {
let a_value, b_value
if (sort_key == "accuracy") {
a_value = a.accuracy.mean;
b_value = b.accuracy.mean;
} else {
if (a.model_name == "Random Baseline") return 1;
else if (b.model_name == "Random Baseline") return -1;
a_value = a[sort_key];
b_value = b[sort_key];
}
if (typeof a_value === 'string' || a_value instanceof String) a_value = a_value.toLowerCase();
if (typeof b_value === 'string' || b_value instanceof String) b_value = b_value.toLowerCase();
if (a_value == null) return 1;
else if (b_value == null) return -1;
else if (to_ascending) return d3.ascending(a_value, b_value);
else return d3.descending(a_value, b_value);
}
);
}
leaderboard.selectAll("th").on("click", (e) => {
var th = d3.select(e.target);
var to_ascending = !th.classed("th-sort-asc");
var sort_key = th.attr("data-sort-key")
console.log(sort_key, to_ascending);
leaderboard.selectAll("th").classed("th-sort-asc", false).classed("th-sort-desc", false);
th.classed("th-sort-asc", to_ascending).classed("th-sort-desc", !to_ascending);
sort_table(sort_key, to_ascending);
})
/* PERFROMANCE PLOT */
const width = 600;
const height = 300;
const marginTop = 20;
const marginRight = 20;
const marginBottom = 30;
const marginLeft = 40;
// Setup the static elements
var symbol = d3.symbol();
const svg = d3.selectAll("svg.result-plot").attr("viewBox","0 0 " + width + " " + height);
// Set up the axis
const x = d3.scaleLog().range([marginLeft, width - marginRight]);
const y = d3.scaleLinear().range([height - marginBottom, marginTop]);
const group_x = svg.append("g")
.attr("transform", `translate(0,${height - marginBottom})`)
.attr("opacity", 0.7);
const xAxis = d3.axisBottom(x).tickSizeOuter(0);
const group_y = svg.append("g")
.attr("transform", `translate(${marginLeft},0)`)
.attr("opacity", 0.7)
const yAxis = d3.axisLeft(y);
const random_baseline = svg.append("g");
const line_plot = svg.append("g");
const scatter_plot = svg.append("g");
const scatter_plot_hightlight_layer = svg.append("g").attr("pointer-events", "none");
// Add the line for the random baseline
random_baseline
.append("line")
.attr("x1", marginLeft)
.attr("x2", width - marginRight)
.attr("y1", 0)
.attr("y2", 0)
.attr("fill", "none")
.attr("stroke", "gray")
.attr("stroke-dasharray", "1,1")
.attr("stroke-width", 1);
random_baseline.append("text")
.attr("x", width - marginRight)
.attr("y", -4)
.attr("fill", "gray")
.text("Random Baseline")
.attr("text-anchor", "end")
.attr("font-size", "10");
// Add a legend-group
const legend_dy = 15;
const legend = svg.append("g").attr("transform", `translate(${marginLeft + 20} ${marginTop + legend_dy/2})`);
function prepare_data(data) {
window.random_baseline_accuracy = data.filter((d) => d.model_name == "Random Baseline")[0].accuracy.mean;
window.datapoints = data.filter((d) => d.model_name != "Random Baseline");
model_families = {};
window.datapoints.forEach((d) => {
if (!(d.model_family in model_families)) {
model_families[d.model_family] = [];
}
model_families[d.model_family].push(d);
});
window.model_families = Object.values(model_families);
}
function sorted_datapoints(datapoints) {
var data_key = xAxisKeySelect.value;
datapoints = datapoints.slice(0);
datapoints.sort((a, b) => (a == b) ? (a.accuracy.mean - b.accuracy.mean) : (a[data_key] - b[data_key]));
return datapoints;
}
function update_axis() {
var data_key = xAxisKeySelect.value;
var data = window.datapoints;
let xValues = data.map((d) => d[data_key]).filter(x => !!x);
var xmin = Math.max(Math.min(...xValues), 1);
var xmax = Math.max(Math.max(...xValues), 10);
var ymax = Math.max(...data.map((d) => d.accuracy.mean));
var xmin_log = Math.log(xmin);
var xmax_log = Math.log(xmax);
x.domain([Math.exp(xmin_log - 0.1*(xmax_log - xmin_log)), Math.exp(xmax_log + 0.1*(xmax_log - xmin_log))]).range([marginLeft, width - marginRight]);
y.domain([0, ymax + 0.05]).range([height - marginBottom, marginTop]);
group_x.transition().duration(400).call(xAxis);
group_y.transition().duration(400).call(yAxis);
}
function update_plot() {
var data_key = xAxisKeySelect.value;
update_axis();
line_plot
.selectAll(".family-line")
.data(model_families)
.enter()
.append("path")
.attr("d", d3.line()
.x((d) => x(d[xAxisKeySelect.value]))
.y((d) => y(d.accuracy.mean))
);
/*.on("mouseover", (e, d, i) => mouseover_model_family(d[0].model_family))
.on("mouseout", (e, d, i) => mouseout_model_family(d[0].model_family));*/
// Add the error bars
datapoint_elements
.append("line")
.attr("x1", (d) => x(d[data_key]))
.attr("x2", (d) => x(d[data_key]))
var datapoint = datapoint_elements
.append("g")
.attr("transform", (d) => `translate(${x(d[data_key])}, ${y(d.accuracy.mean)}) scale(1, 1)`);
}
function setup_plot() {
var data_key = xAxisKeySelect.value;
var model_families = window.model_families;
update_axis();
// Add the legend
// Usually you have a color scale in your chart already
var family_color = d3.scaleOrdinal()
.domain(model_families.map(f => f[0].model_family))
.range(d3.schemeTableau10);
line_plot
.selectAll(".family-line")
.data(model_families)
.join(
function(enter) {
enter.append("path")
.attr("class", d => `family-line family-line-${d[0].model_family}`)
.attr("stroke", "gray")
.attr("fill", "none")
.attr("stroke-width", 1)
.attr("stroke-dasharray", "2,2")
.attr("d", family_d => (d3.line()
.defined((d) => !!d[data_key])
.x((d) => x(d[xAxisKeySelect.value]))
.y((d) => y(d.accuracy.mean))
)(sorted_datapoints(family_d)));
},
function(update) {
var line =
update
.transition().duration(200).attr("opacity", 0)
.transition().duration(0)
.attr("d", family_d => (d3.line()
.defined((d) => !!d[data_key])
.x((d) => x(d[xAxisKeySelect.value]))
.y((d) => y(d.accuracy.mean))
)(sorted_datapoints(family_d)))
.transition().duration(200).attr("opacity", 1)
}
)
function transform_string(d) {
if (d[data_key]) {
return `translate(${x(d[data_key])}, ${y(d.accuracy.mean)})`
} else {
return `translate(${width/2}, ${height-marginBottom})`
}
}
scatter_plot
.selectAll("g.datapoint")
.data(window.datapoints)
.join(
function (enter) {
var annotatedDatapoint = enter.append("g")
.attr("class", (d) => `datapoint datapoint-${d.model_family}`)
.on("mouseover", (e, d, i) => hightlight_model_family(d.model_family))
.on("mouseout", (e, d, i) => dehiglight_model_family(d.model_family))
.attr("opacity", (d) => (d[data_key]) ? 1.0 : 0.0)
.attr("transform", transform_string)
.attr("style", (d) => (d[data_key]) ? "" : "display: none");
// Add the error bars
annotatedDatapoint
.append("line")
.attr("class", "datapoint-errorbar")
.attr("x1", 0)
.attr("x2", 0)
.attr("y1", (d) => y(d.accuracy.mean + d.accuracy.sem) - y(d.accuracy.mean))
.attr("y2", (d) => y(d.accuracy.mean - d.accuracy.sem) - y(d.accuracy.mean))
.attr("stroke", (d) => family_color(d.model_family))
.attr("stroke-width", 2);
// Add the scatter plot symbols
annotatedDatapoint
.append("path")
.attr("class", "datapoint-symbol")
.attr("d", symbol.type((d) => {
if (d.model_type.startsWith("MLM")) {return d3.symbolSquare} else {return d3.symbolCircle}
}))
.attr("transform", (d) => `scale(0.7, 0.7)`)
.attr("fill", (d) => family_color(d.model_family))
.append("svg:title").text((d) => `${d.model_name}: ${d.accuracy.mean.toPrecision(3)}`);
},
function (update) {
update
.attr("style", (d) => (d[data_key]) ? "" : "display: none")
.transition()
.duration(400)
.attr("transform", transform_string)
.attr("opacity", (d) => (d[data_key]) ? 1.0 : 0.0);
}
)
scatter_plot_hightlight_layer
.selectAll("g.datapoint")
.data(window.datapoints)
.join(
function (enter) {
var annotatedDatapoint = enter.append("g")
.attr("class", (d) => `datapoint datapoint-${d.model_family}`)
.attr("opacity", 0.0)
.attr("transform", transform_string)
.attr("style", (d) => (d[data_key]) ? "" : "display: none");
// Add the scatter plot symbols
annotatedDatapoint
.append("path")
.attr("class", "datapoint-symbol")
.attr("d", symbol.type((d) => {
if (d.model_type.startsWith("MLM")) {return d3.symbolSquare} else {return d3.symbolCircle}
}))
.attr("transform", (d) => `scale(0.7, 0.7)`)
.attr("fill", (d) => family_color(d.model_family))
// Add the model names
annotatedDatapoint
.append("text")
.attr("class", "datapoint-annotation")
.text((d) => d.model_name)
.attr("transform", "rotate(20)")
.attr("dx", 8)
.attr("dy", 4)
.attr("font-size", 10);
},
function (update) {
update.transition().duration(400)
.attr("transform", transform_string)
.attr("style", (d) => (d[data_key]) ? "" : "display: none");
}
)
random_baseline
.attr("transform", `translate(0, ${y(random_baseline_accuracy)})`);
// Add one dot in the legend for each name.
var legend_element = legend.selectAll("g.legend-element")
.data(model_families)
.enter()
.append("g")
.attr("class", (d, i) => `legend-element legend-element-${d[0].model_family}`)
.attr("transform", (d, i) => `translate (0, ${i * legend_dy})`)
.on("mouseover", (e, d, i) => hightlight_model_family(d[0].model_family))
.on("mouseout", (e, d, i) => dehiglight_model_family(d[0].model_family));
legend_element
.append("path")
.attr("d", symbol.type((d) => {
if (d[0].model_type.startsWith("MLM")) {return d3.symbolSquare} else {return d3.symbolCircle}
}))
.attr("transform", "scale(0.7, 0.7)")
.style("fill", function(d, i){ return family_color(d[0].model_family)})
// Add one dot in the legend for each name.
legend_element
.append("text")
.attr("x", 10)
.attr("y", 3)
.attr("opacity", 0.7)
.text(function(d){ return d[0].model_family})
.attr("text-anchor", "start")
.attr("font-size", "10");
}
/* Animate the plot on selecting a different value */
xAxisKeySelect.addEventListener("change", function(event) {
event.preventDefault();
setup_plot();
return false;
});
/* Animate hightlighting of model families */
function hightlight_model_family(model_family) {
console.log(model_family)
line_plot.select(`.family-line-${model_family}`)
.transition().duration(200)
.attr("stroke-width", 2);
// Hightlight all scatter plots
var dp = scatter_plot_hightlight_layer.selectAll(`.datapoint-${model_family}`)
dp.attr("visible", "visible")
.transition()
.duration(100)
.attr("opacity", 1.0);
dp.selectAll("path.datapoint-symbol")
.transition()
.duration(100)
.attr("transform", "scale(1, 1)");
}
function dehiglight_model_family(model_family) {
line_plot.select(`.family-line-${model_family}`)
.transition().duration(200)
.attr("stroke-width", 1);
var dp = scatter_plot_hightlight_layer.selectAll(`.datapoint-${model_family}`);
dp.transition()
.duration(200)
.attr("opacity", 0.0)
.transition()
.attr("visible", "none");
dp.selectAll("path.datapoint-symbol")
.transition()
.duration(200)
.attr("transform", "scale(0.7, 0.7)");
}
/* DATA LOADING */
// Load the data and add lines and dots
d3.json("results.json").then((data) => {
prepare_data(data);
setup_plot();
setup_table(data);
});