-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexperiment.js
604 lines (576 loc) · 17.3 KB
/
experiment.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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/* ************************************ */
/* Define helper functions */
/* ************************************ */
function assessPerformance() {
/* Function to calculate the "credit_var", which is a boolean used to
credit individual experiments in expfactory.
*/
var experiment_data = jsPsych.data.getTrialsOfType('single-stim-button');
var missed_count = 0;
var trial_count = 0;
var rt_array = [];
var rt = 0;
var avg_rt = -1;
//record choices participants made
for (var i = 0; i < experiment_data.length; i++) {
trial_count += 1
rt = experiment_data[i].rt
if (rt == -1) {
missed_count += 1
} else {
rt_array.push(rt)
}
}
//calculate average rt
if (rt_array.length !== 0) {
avg_rt = math.median(rt_array)
} else {
avg_rt = -1
}
credit_var = (avg_rt > 100)
jsPsych.data.addDataToLastTrial({"credit_var": credit_var})
}
var getStim = function() {
var ref_board = makeBoard('your_board', curr_placement, 'ref')
var target_board = makeBoard('peg_board', problems[problem_i])
var canvas = '<div class = tol_canvas><div class="tol_vertical_line"></div></div>'
var hold_box;
if (held_ball !== 0) {
ball = colors[held_ball - 1]
hold_box = '<div class = tol_hand_box><div class = "tol_hand_ball tol_' + ball +
'"><div class = tol_ball_label>' + ball[0] +
'</div></div></div><div class = tol_hand_label><strong>Ball in Hand</strong></div>'
} else {
hold_box =
'<div class = tol_hand_box></div><div class = tol_hand_label><strong>Ball in Hand</strong></div>'
}
return canvas + ref_board + target_board + hold_box
}
var getPractice = function() {
var ref_board = makeBoard('your_board', curr_placement, 'ref')
var target_board = makeBoard('peg_board', example_problem3)
var canvas = '<div class = tol_canvas><div class="tol_vertical_line"></div></div>'
var hold_box;
if (held_ball !== 0) {
ball = colors[held_ball - 1]
hold_box = '<div class = tol_hand_box><div class = "tol_hand_ball tol_' + ball +
'"><div class = tol_ball_label>' + ball[0] +
'</div></div></div><div class = tol_hand_label><strong>Ball in Hand</strong></div>'
} else {
hold_box =
'<div class = tol_hand_box></div><div class = tol_hand_label><strong>Ball in Hand</strong></div>'
}
return canvas + ref_board + target_board + hold_box
}
var getFB = function() {
var data = jsPsych.data.getLastTrialData()
var target = data.target
var isequal = true
correct = false
for (var i = 0; i < target.length; i++) {
isequal = arraysEqual(target[i], data.current_position[i])
if (isequal === false) {
break;
}
}
var feedback;
if (isequal === true) {
feedback = "You got it!"
correct = true
} else {
feedback = "Didn't get that one."
}
var ref_board = makeBoard('your_board', curr_placement)
var target_board = makeBoard('peg_board', target)
var canvas = '<div class = tol_canvas><div class="tol_vertical_line"></div></div>'
var feedback_box = '<div class = tol_feedbackbox><p class = center-text>' + feedback +
'</p></div>'
return canvas + ref_board + target_board + feedback_box
}
var getTime = function() {
if ((time_per_trial - time_elapsed) > 0) {
return time_per_trial - time_elapsed
} else {
return 1
}
}
var getText = function() {
return '<div class = centerbox><p class = center-block-text>About to start problem ' + (problem_i + 2) +
'. Press <strong>enter</strong> to begin.</p></div>'
}
var pegClick = function(peg_id) {
var choice = Number(peg_id.slice(-1)) - 1
var peg = curr_placement[choice]
var ball_location = 0
if (held_ball === 0) {
for (var i = peg.length - 1; i >= 0; i--) {
if (peg[i] !== 0) {
held_ball = peg[i]
peg[i] = 0
num_moves += 1
break;
}
}
} else {
var open_spot = peg.indexOf(0)
if (open_spot != -1) {
peg[open_spot] = held_ball
held_ball = 0
}
}
}
var makeBoard = function(container, ball_placement, board_type) {
var board = '<div class = tol_' + container + '><div class = tol_base></div>'
if (container == 'your_board') {
board += '<div class = tol_board_label><strong>Your Board</strong></div>'
} else {
board += '<div class = tol_board_label><strong>Target Board</strong></div>'
}
for (var p = 0; p < 3; p++) {
board += '<div id = tol_peg_' + (p + 1) + '><div class = tol_peg></div></div>' //place peg
//place balls
if (board_type == 'ref') {
if (ball_placement[p][0] === 0 & held_ball === 0) {
board += '<div id = tol_peg_' + (p + 1) + ' onclick = "pegClick(this.id)">'
} else if (ball_placement[p].slice(-1)[0] !== 0 & held_ball !== 0) {
board += '<div id = tol_peg_' + (p + 1) + ' onclick = "pegClick(this.id)">'
} else {
board += '<div class = special id = tol_peg_' + (p + 1) + ' onclick = "pegClick(this.id)">'
}
} else {
board += '<div id = tol_peg_' + (p + 1) + ' >'
}
var peg = ball_placement[p]
for (var b = 0; b < peg.length; b++) {
if (peg[b] !== 0) {
ball = colors[peg[b] - 1]
board += '<div class = "tol_ball tol_' + ball + '"><div class = tol_ball_label>' + ball[0] +
'</div></div>'
}
}
board += '</div>'
}
board += '</div>'
return board
}
var arraysEqual = function(arr1, arr2) {
if (arr1.length !== arr2.length)
return false;
for (var i = arr1.length; i--;) {
if (arr1[i] !== arr2[i])
return false;
}
return true;
}
var getInstructFeedback = function() {
return '<div class = centerbox><p class = center-block-text>' + feedback_instruct_text +
'</p></div>'
}
/* ************************************ */
/* Define experimental variables */
/* ************************************ */
// generic task variables
var sumInstructTime = 0 //ms
var instructTimeThresh = 0 ///in seconds
var credit_var = true
// task specific variables
var correct = false
var exp_stage = 'practice'
var colors = ['Green', 'Red', 'Blue']
var problem_i = 0
var time_per_trial = 20000 //time per trial in seconds
var time_elapsed = 0 //tracks time for a problem
var num_moves = 0 //tracks number of moves for a problem
/*keeps track of peg board (where balls are). Lowest ball is the first value for each peg.
So the initial_placement has the 1st ball and 2nd ball on the first peg and the third ball on the 2nd peg.
*/
// make Your board
var curr_placement = [
[1, 2, 0],
[3, 0],
[0]
]
var example_problem1 = [
[1, 2, 0],
[0, 0],
[3]
]
var example_problem2 = [
[1, 0, 0],
[3, 0],
[2]
]
var example_problem3 = [
[1, 0, 0],
[3, 2],
[0]
]
var ref_board = makeBoard('your_board', curr_placement)
var problems = [
[
[0, 0, 0],
[3, 1],
[2]
],
[
[1, 0, 0],
[2, 0],
[3]
],
[
[1, 3, 0],
[2, 0],
[0]
],
[
[1, 0, 0],
[2, 3],
[0]
],
[
[2, 1, 0],
[3, 0],
[0]
],
[
[3, 0, 0],
[2, 1],
[0]
],
[
[2, 3, 0],
[0, 0],
[1]
],
[
[0, 0, 0],
[2, 3],
[1]
],
[
[2, 1, 3],
[0, 0],
[0]
],
[
[2, 3, 1],
[0, 0],
[0]
],
[
[3, 1, 0],
[2, 0],
[0]
],
[
[3, 0, 0],
[2, 0],
[1]
]
]
var answers = [2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5]
var held_ball = 0
/* ************************************ */
/* Set up jsPsych blocks */
/* ************************************ */
//Set up post task questionnaire
var post_task_block = {
type: 'survey-text',
data: {
trial_id: "post task questions"
},
questions: ['<p class = center-block-text style = "font-size: 20px">Please summarize what you were asked to do in this task.</p>',
'<p class = center-block-text style = "font-size: 20px">Do you have any comments about this task?</p>'],
rows: [15, 15],
columns: [60,60]
};
/* define static blocks */
var end_block = {
type: 'poldrack-text',
data: {
trial_id: "end",
exp_id: 'tower_of_london'
},
timing_response: 180000,
text: '<div class = centerbox><p class = center-block-text>Thanks for completing this task!</p><p class = center-block-text>Press <strong>enter</strong> to continue.</p></div>',
cont_key: [13],
timing_post_trial: 0,
on_finish: assessPerformance
};
var feedback_instruct_text =
'Welcome to the experiment. This experiment will take about 5 minutes. Press <strong>enter</strong> to begin.'
var feedback_instruct_block = {
type: 'poldrack-text',
data: {
trial_id: "instruction"
},
cont_key: [13],
text: getInstructFeedback,
timing_post_trial: 0,
timing_response: 180000
};
/// This ensures that the subject does not read through the instructions too quickly. If they do it too quickly, then we will go over the loop again.
var instructions_block = {
type: 'poldrack-instructions',
data: {
trial_id: "instruction"
},
pages: [
'<div class = tol_topbox><p class = block-text>During this task, two boards will be presented at a time. The boards will be of colored balls arranged on pegs like this:</p></div>' +
ref_board + makeBoard('peg_board', example_problem1) +
'<div class = tol_bottombox><p class = block-text>Imagine that these balls have holes through them and the pegs are going through the holes. Notice that the first peg can hold three balls, the second peg can hold two balls, and the third peg can hold one ball.</p></div>',
'<div class = tol_topbox><p class = block-text>Your task will be to make the arrangements of balls in your board look like the arrangements of balls in the target board in the fewest possible moves.</p></div>' +
ref_board + makeBoard('peg_board', example_problem1) +
'<div class = tol_bottombox><p class = block-text>The balls in the target board are fixed in place, but the balls in your board are movable. You have to move them to make your board look like the target board. Sometime you will have to move a ball to a different peg in order to get to the ball below it. During this task it is important that you remember, you want the <strong>fewest possible moves</strong> that are required to make your board look like the target board. You will have 20 seconds to make your decision.</p></div>',
'<div class = tol_topbox><p class = block-text>Here is an example. Notice that the balls in your board are in a different arrangement than in the target board. If we move the red ball from the first peg in your board to the third peg then it would look like the target board.</p></div>' +
ref_board + makeBoard('peg_board', example_problem2) + '<div class = tol_bottombox></div>',
"<div class = centerbox><p class = block-text>During the test you will move the balls on your board by clicking on the pegs. When you click on a peg, the top ball will move into a box called 'your hand'. When you click on another peg, the ball in 'your hand' will move to the top of that peg.</p><p class = block-text>If you try to select a peg with no balls or try to place a ball on a full peg, nothing will happen. If you successfully make your board look like the target board, the trial will end and you will move to the next problem.</p><p class = block-text>We will start with an easy example so that you can learn the controls.</p></div>"
],
allow_keys: false,
show_clickable_nav: true,
timing_post_trial: 1000
};
var instruction_node = {
timeline: [feedback_instruct_block, instructions_block],
/* This function defines stopping criteria */
loop_function: function(data) {
for (i = 0; i < data.length; i++) {
if ((data[i].trial_type == 'poldrack-instructions') && (data[i].rt != -1)) {
rt = data[i].rt
sumInstructTime = sumInstructTime + rt
}
}
if (sumInstructTime <= instructTimeThresh * 1000) {
feedback_instruct_text =
'Read through instructions too quickly. Please take your time and make sure you understand the instructions. Press <strong>enter</strong> to continue.'
return true
} else if (sumInstructTime > instructTimeThresh * 1000) {
feedback_instruct_text =
'Done with instructions. Press <strong>enter</strong> to continue.'
return false
}
}
}
var start_test_block = {
type: 'poldrack-text',
data: {
trial_id: "instruction"
},
timing_response: 180000,
text: '<div class = centerbox><p class = block-text>We will now start Problem 1. There will be ' +
problems.length + ' problems to complete. Press <strong>enter</strong> to begin.</p></div>',
cont_key: [13],
timing_post_trial: 1000,
on_finish: function() {
exp_stage = 'test'
held_ball = 0
time_elapsed = 0
num_moves = 0;
curr_placement = [
[1, 2, 0],
[3, 0],
[0]
]
}
};
var advance_problem_block = {
type: 'poldrack-text',
data: {
trial_id: "advance",
exp_stage: 'test'
},
timing_response: 180000,
text: getText,
cont_key: [13],
on_finish: function() {
held_ball = 0
time_elapsed = 0
problem_i += 1;
num_moves = 0;
curr_placement = [
[1, 2, 0],
[3, 0],
[0]
]
}
}
var practice_tohand = {
type: 'single-stim-button',
stimulus: getPractice,
button_class: 'special',
is_html: true,
data: {
trial_id: "to_hand",
exp_stage: 'practice'
},
timing_stim: getTime,
timing_response: getTime,
timing_post_trial: 0,
on_finish: function(data) {
if (data.mouse_click != -1) {
time_elapsed += data.rt
} else {
time_elapsed += getTime()
}
jsPsych.data.addDataToLastTrial({
'current_position': jQuery.extend(true, [], curr_placement),
'num_moves_made': num_moves,
'target': example_problem3,
'min_moves': 1,
'problem_id': 'practice'
})
}
}
var practice_toboard = {
type: 'single-stim-button',
stimulus: getPractice,
button_class: 'special',
is_html: true,
data: {
trial_id: "to_board",
exp_stage: 'practice'
},
timing_stim: getTime,
timing_response: getTime,
timing_post_trial: 0,
on_finish: function(data) {
if (data.mouse_click != -1) {
time_elapsed += data.rt
} else {
time_elapsed += getTime()
}
jsPsych.data.addDataToLastTrial({
'current_position': jQuery.extend(true, [], curr_placement),
'num_moves_made': num_moves,
'target': example_problem3,
'min_moves': 1,
'problem_id': 'practice'
})
}
}
var test_tohand = {
type: 'single-stim-button',
stimulus: getStim,
button_class: 'special',
is_html: true,
data: {
trial_id: "to_hand",
exp_stage: 'test'
},
timing_stim: getTime,
timing_response: getTime,
timing_post_trial: 0,
on_finish: function(data) {
if (data.mouse_click != -1) {
time_elapsed += data.rt
} else {
time_elapsed += getTime()
}
jsPsych.data.addDataToLastTrial({
'current_position': jQuery.extend(true, [], curr_placement),
'num_moves_made': num_moves,
'target': problems[problem_i],
'min_moves': answers[problem_i],
'problem_id': problem_i
})
}
}
var test_toboard = {
type: 'single-stim-button',
stimulus: getStim,
button_class: 'special',
is_html: true,
data: {
trial_id: "to_board",
exp_stage: 'test'
},
timing_stim: getTime,
timing_response: getTime,
timing_post_trial: 0,
on_finish: function(data) {
if (data.mouse_click != -1) {
time_elapsed += data.rt
} else {
time_elapsed += getTime()
}
jsPsych.data.addDataToLastTrial({
'current_position': jQuery.extend(true, [], curr_placement),
'num_moves_made': num_moves,
'target': problems[problem_i],
'min_moves': answers[problem_i],
'problem_id': problem_i
})
}
}
var feedback_block = {
type: 'poldrack-single-stim',
stimulus: getFB,
choices: 'none',
is_html: true,
data: {
trial_id: 'feedback'
},
timing_stim: 2000,
timing_response: 2000,
timing_post_trial: 500,
on_finish: function() {
jsPsych.data.addDataToLastTrial({
'exp_stage': exp_stage,
'problem_time': time_elapsed,
'correct': correct
})
},
}
var practice_node = {
timeline: [practice_tohand, practice_toboard],
loop_function: function(data) {
if (time_elapsed >= time_per_trial) {
return false
}
data = data[1]
var target = data.target
var isequal = true
for (var i = 0; i < target.length; i++) {
isequal = arraysEqual(target[i], data.current_position[i])
if (isequal === false) {
break;
}
}
return !isequal
},
timing_post_trial: 1000
}
var problem_node = {
timeline: [test_tohand, test_toboard],
loop_function: function(data) {
if (time_elapsed >= time_per_trial) {
return false
}
data = data[1]
var target = data.target
var isequal = true
for (var i = 0; i < target.length; i++) {
isequal = arraysEqual(target[i], data.current_position[i])
if (isequal === false) {
break;
}
}
return !isequal
},
timing_post_trial: 1000
}
/* create experiment definition array */
var tower_of_london_experiment = [];
tower_of_london_experiment.push(instruction_node);
tower_of_london_experiment.push(practice_node);
tower_of_london_experiment.push(feedback_block)
tower_of_london_experiment.push(start_test_block);
for (var i = 0; i < problems.length; i++) {
tower_of_london_experiment.push(problem_node);
tower_of_london_experiment.push(feedback_block)
if (i != problems.length-1) {
tower_of_london_experiment.push(advance_problem_block)
}
}
tower_of_london_experiment.push(post_task_block)
tower_of_london_experiment.push(end_block);