-
Notifications
You must be signed in to change notification settings - Fork 718
Expand file tree
/
Copy pathtesting_interface.js
More file actions
418 lines (360 loc) · 12.8 KB
/
Copy pathtesting_interface.js
File metadata and controls
418 lines (360 loc) · 12.8 KB
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
// Internal state.
var CURRENT_INPUT_GRID = new Grid(3, 3);
var CURRENT_OUTPUT_GRID = new Grid(3, 3);
var TEST_PAIRS = new Array();
var CURRENT_TEST_PAIR_INDEX = 0;
var COPY_PASTE_DATA = new Array();
// Cosmetic.
var EDITION_GRID_HEIGHT = 500;
var EDITION_GRID_WIDTH = 500;
var MAX_CELL_SIZE = 100;
var task_index = -1
var training_tasks;
function resetTask() {
CURRENT_INPUT_GRID = new Grid(3, 3);
TEST_PAIRS = new Array();
CURRENT_TEST_PAIR_INDEX = 0;
$('#task_preview').html('');
resetOutputGrid();
}
function refreshEditionGrid(jqGrid, dataGrid) {
fillJqGridWithData(jqGrid, dataGrid);
setUpEditionGridListeners(jqGrid);
fitCellsToContainer(jqGrid, dataGrid.height, dataGrid.width, EDITION_GRID_HEIGHT, EDITION_GRID_HEIGHT);
initializeSelectable();
}
function syncFromEditionGridToDataGrid() {
copyJqGridToDataGrid($('#output_grid .edition_grid'), CURRENT_OUTPUT_GRID);
}
function syncFromDataGridToEditionGrid() {
refreshEditionGrid($('#output_grid .edition_grid'), CURRENT_OUTPUT_GRID);
}
function getSelectedSymbol() {
selected = $('#symbol_picker .selected-symbol-preview')[0];
return $(selected).attr('symbol');
}
function setUpEditionGridListeners(jqGrid) {
jqGrid.find('.cell').click(function(event) {
cell = $(event.target);
symbol = getSelectedSymbol();
mode = $('input[name=tool_switching]:checked').val();
if (mode == 'floodfill') {
// If floodfill: fill all connected cells.
syncFromEditionGridToDataGrid();
grid = CURRENT_OUTPUT_GRID.grid;
floodfillFromLocation(grid, cell.attr('x'), cell.attr('y'), symbol);
syncFromDataGridToEditionGrid();
}
else if (mode == 'edit') {
// Else: fill just this cell.
setCellSymbol(cell, symbol);
}
});
}
function resizeOutputGrid() {
size = $('#output_grid_size').val();
size = parseSizeTuple(size);
height = size[0];
width = size[1];
jqGrid = $('#output_grid .edition_grid');
syncFromEditionGridToDataGrid();
dataGrid = JSON.parse(JSON.stringify(CURRENT_OUTPUT_GRID.grid));
CURRENT_OUTPUT_GRID = new Grid(height, width, dataGrid);
refreshEditionGrid(jqGrid, CURRENT_OUTPUT_GRID);
}
function resetOutputGrid() {
syncFromEditionGridToDataGrid();
CURRENT_OUTPUT_GRID = new Grid(3, 3);
syncFromDataGridToEditionGrid();
resizeOutputGrid();
}
function copyFromInput() {
syncFromEditionGridToDataGrid();
CURRENT_OUTPUT_GRID = convertSerializedGridToGridObject(CURRENT_INPUT_GRID.grid);
syncFromDataGridToEditionGrid();
$('#output_grid_size').val(CURRENT_OUTPUT_GRID.height + 'x' + CURRENT_OUTPUT_GRID.width);
}
function fillPairPreview(pairId, inputGrid, outputGrid) {
var pairSlot = $('#pair_preview_' + pairId);
if (!pairSlot.length) {
// Create HTML for pair.
pairSlot = $('<div id="pair_preview_' + pairId + '" class="pair_preview" index="' + pairId + '"></div>');
pairSlot.appendTo('#task_preview');
}
var jqInputGrid = pairSlot.find('.input_preview');
if (!jqInputGrid.length) {
jqInputGrid = $('<div class="input_preview"></div>');
jqInputGrid.appendTo(pairSlot);
}
var jqOutputGrid = pairSlot.find('.output_preview');
if (!jqOutputGrid.length) {
jqOutputGrid = $('<div class="output_preview"></div>');
jqOutputGrid.appendTo(pairSlot);
}
fillJqGridWithData(jqInputGrid, inputGrid);
fitCellsToContainer(jqInputGrid, inputGrid.height, inputGrid.width, 200, 200);
fillJqGridWithData(jqOutputGrid, outputGrid);
fitCellsToContainer(jqOutputGrid, outputGrid.height, outputGrid.width, 200, 200);
}
function loadJSONTask(train, test) {
resetTask();
$('#modal_bg').hide();
$('#error_display').hide();
$('#info_display').hide();
for (var i = 0; i < train.length; i++) {
pair = train[i];
values = pair['input'];
input_grid = convertSerializedGridToGridObject(values)
values = pair['output'];
output_grid = convertSerializedGridToGridObject(values)
fillPairPreview(i, input_grid, output_grid);
}
for (var i=0; i < test.length; i++) {
pair = test[i];
TEST_PAIRS.push(pair);
}
values = TEST_PAIRS[0]['input'];
CURRENT_INPUT_GRID = convertSerializedGridToGridObject(values)
fillTestInput(CURRENT_INPUT_GRID);
CURRENT_TEST_PAIR_INDEX = 0;
$('#current_test_input_id_display').html('1');
$('#total_test_input_count_display').html(test.length);
}
function display_task_name(task_name, task_index, number_of_tasks) {
big_space = ' '.repeat(4);
document.getElementById('task_name').innerHTML = (
'Task name:' + big_space + task_name + big_space + (
task_index===null ? '' :
( String(task_index) + ' out of ' + String(number_of_tasks) )
)
);
}
function loadTaskFromFile(e) {
var file = e.target.files[0];
if (!file) {
errorMsg('No file selected');
return;
}
var reader = new FileReader();
reader.onload = function(e) {
var contents = e.target.result;
try {
contents = JSON.parse(contents);
train = contents['train'];
test = contents['test'];
} catch (e) {
errorMsg('Bad file format');
return;
}
loadJSONTask(train, test);
$('#load_task_file_input')[0].value = "";
display_task_name(file.name, null, null);
};
reader.readAsText(file);
}
async function load_training_tasks()
{
const subset = "training";
const apiUrl = `https://api.github.com/repos/fchollet/ARC/contents/data/${subset}`;
try {
const response = await $.ajax({
url: apiUrl,
dataType: 'json'
});
training_tasks = response;
} catch (error) {
errorMsg('Error loading task list');
throw error;
}
}
function loadTask(index) {
const downloadUrlTemplate = "https://api.github.com/repos/fchollet/ARC/contents/data/%s";
task_index = index === null ? Math.floor(Math.random() * training_tasks.length) : index;
const task = training_tasks[task_index];
$.getJSON(task["download_url"], function(json) {
try {
train = json['train'];
test = json['test'];
} catch (e) {
errorMsg('Bad file format');
return;
}
loadJSONTask(train, test);
infoMsg(`Loaded task ${task.name}`);
display_task_name(task.name, task_index, training_tasks.length);
})
.error(function(){
errorMsg('Error loading task');
});
}
function randomTask() {
loadTask(null);
}
function loadTaskByIndex() {
const taskIndex = document.getElementById('task_index_input').value;
loadTask(parseInt(taskIndex));
}
function previousTask() {
loadTask((task_index - 1 + training_tasks.length) % training_tasks.length);
}
function nextTask() {
loadTask((task_index + 1) % training_tasks.length);
}
function nextTestInput() {
if (TEST_PAIRS.length <= CURRENT_TEST_PAIR_INDEX + 1) {
errorMsg('No next test input. Pick another file?')
return
}
CURRENT_TEST_PAIR_INDEX += 1;
values = TEST_PAIRS[CURRENT_TEST_PAIR_INDEX]['input'];
CURRENT_INPUT_GRID = convertSerializedGridToGridObject(values)
fillTestInput(CURRENT_INPUT_GRID);
$('#current_test_input_id_display').html(CURRENT_TEST_PAIR_INDEX + 1);
$('#total_test_input_count_display').html(test.length);
}
function submitSolution() {
syncFromEditionGridToDataGrid();
reference_output = TEST_PAIRS[CURRENT_TEST_PAIR_INDEX]['output'];
submitted_output = CURRENT_OUTPUT_GRID.grid;
if (reference_output.length != submitted_output.length) {
errorMsg('Wrong solution.');
return
}
for (var i = 0; i < reference_output.length; i++){
ref_row = reference_output[i];
for (var j = 0; j < ref_row.length; j++){
if (ref_row[j] != submitted_output[i][j]) {
errorMsg('Wrong solution.');
return
}
}
}
infoMsg('Correct solution!');
}
function fillTestInput(inputGrid) {
jqInputGrid = $('#evaluation_input');
fillJqGridWithData(jqInputGrid, inputGrid);
fitCellsToContainer(jqInputGrid, inputGrid.height, inputGrid.width, 400, 400);
}
function copyToOutput() {
syncFromEditionGridToDataGrid();
CURRENT_OUTPUT_GRID = convertSerializedGridToGridObject(CURRENT_INPUT_GRID.grid);
syncFromDataGridToEditionGrid();
$('#output_grid_size').val(CURRENT_OUTPUT_GRID.height + 'x' + CURRENT_OUTPUT_GRID.width);
}
function initializeSelectable() {
try {
$('.selectable_grid').selectable('destroy');
}
catch (e) {
}
toolMode = $('input[name=tool_switching]:checked').val();
if (toolMode == 'select') {
infoMsg('Select some cells and click on a color to fill in, or press C to copy');
$('.selectable_grid').selectable(
{
autoRefresh: false,
filter: '> .row > .cell',
start: function(event, ui) {
$('.ui-selected').each(function(i, e) {
$(e).removeClass('ui-selected');
});
}
}
);
}
}
// Initial event binding.
$(function () {
$('#symbol_picker').find('.symbol_preview').click(function(event) {
symbol_preview = $(event.target);
$('#symbol_picker').find('.symbol_preview').each(function(i, preview) {
$(preview).removeClass('selected-symbol-preview');
})
symbol_preview.addClass('selected-symbol-preview');
toolMode = $('input[name=tool_switching]:checked').val();
if (toolMode == 'select') {
$('.edition_grid').find('.ui-selected').each(function(i, cell) {
symbol = getSelectedSymbol();
setCellSymbol($(cell), symbol);
});
}
});
$('.edition_grid').each(function(i, jqGrid) {
setUpEditionGridListeners($(jqGrid));
});
$('.load_task').on('change', function(event) {
loadTaskFromFile(event);
});
$('.load_task').on('click', function(event) {
event.target.value = "";
});
$('input[type=radio][name=tool_switching]').change(function() {
initializeSelectable();
});
$('input[type=text][name=size]').on('keydown', function(event) {
if (event.keyCode == 13) {
resizeOutputGrid();
}
});
$('body').keydown(function(event) {
// Copy and paste functionality.
if (event.which == 67) {
// Press C
selected = $('.ui-selected');
if (selected.length == 0) {
return;
}
COPY_PASTE_DATA = [];
for (var i = 0; i < selected.length; i ++) {
x = parseInt($(selected[i]).attr('x'));
y = parseInt($(selected[i]).attr('y'));
symbol = parseInt($(selected[i]).attr('symbol'));
COPY_PASTE_DATA.push([x, y, symbol]);
}
infoMsg('Cells copied! Select a target cell and press V to paste at location.');
}
if (event.which == 86) {
// Press P
if (COPY_PASTE_DATA.length == 0) {
errorMsg('No data to paste.');
return;
}
selected = $('.edition_grid').find('.ui-selected');
if (selected.length == 0) {
errorMsg('Select a target cell on the output grid.');
return;
}
jqGrid = $(selected.parent().parent()[0]);
if (selected.length == 1) {
targetx = parseInt(selected.attr('x'));
targety = parseInt(selected.attr('y'));
xs = new Array();
ys = new Array();
symbols = new Array();
for (var i = 0; i < COPY_PASTE_DATA.length; i ++) {
xs.push(COPY_PASTE_DATA[i][0]);
ys.push(COPY_PASTE_DATA[i][1]);
symbols.push(COPY_PASTE_DATA[i][2]);
}
minx = Math.min(...xs);
miny = Math.min(...ys);
for (var i = 0; i < xs.length; i ++) {
x = xs[i];
y = ys[i];
symbol = symbols[i];
newx = x - minx + targetx;
newy = y - miny + targety;
res = jqGrid.find('[x="' + newx + '"][y="' + newy + '"] ');
if (res.length == 1) {
cell = $(res[0]);
setCellSymbol(cell, symbol);
}
}
} else {
errorMsg('Can only paste at a specific location; only select *one* cell as paste destination.');
}
}
});
load_training_tasks()
});