-
Notifications
You must be signed in to change notification settings - Fork 0
/
odd_ball.html
511 lines (467 loc) · 19.2 KB
/
odd_ball.html
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
<!DOCTYPE html>
<html>
<head>
<title>Odd Ball Task</title>
<script src="jspsych-master/jspsych.js"></script>
<script src="jspsych-master/plugins/jspsych-html-keyboard-response.js"></script>
<script src="jspsych-master/plugins/jspsych-image-keyboard-response.js"></script>
<link rel="stylesheet" href="jspsych-master/css/jspsych.css"></link>
<style>
div.center {
position: absolute;
width:100vw;
left: 0;
}
.oddball-stim {
display: inline-block;
padding: 10px;
box-shadow: 0 4px 2px 0 rgba(0,0,0,0.5);
border: 1px solid #ccc;
margin: 0px 10px;
background-color: white;
}
</style>
</head>
<body></body>
<script>
//CONSTANTS
var stim_width_height = 200;
var stim_padding = 40;
var min_dot_density = 100;
var max_dot_density = 2000;
var min_line_density = 30;
var max_line_density = 550;
//STAIRCASE
var rel_be_distance = 0.2;
var rel_wi_distance = 0.2;
var irr_distance = 0.2;
var three = 0;
// adjusts the particular distance for every three correct responses
function adjust_distance(distance, correct) {
if(correct) {three += 1;}
else {distance *= 1.05;}
if(three % 3 == 0) {distance *= 0.95;}
return distance;
}
//PARTICIPANT
// determines which dimension is relevant throughout the experiment
var relevant_dimension = jsPsych.randomization.sampleWithoutReplacement(['dots', 'lines'], 1) [0];
// create timeline
var timeline = [];
//INSTRUCTIONS
var welcome = {
type: "html-keyboard-response",
stimulus: "Welcome to the experiment. Press any key to begin."
};
timeline.push(welcome)
var instructions = {
type: "html-keyboard-response",
choices: '1',
stimulus: "In this first task, four sunbursts will appear in the center of the screen. <p> \
<p> If the <strong>first</strong> star from the left is the odd one out, press 1. </p> \
<p> If the <strong>second</strong> star from the left is the odd one out, press 2. </p> \
<p>If the <strong>third</strong> star from the left is the odd one out, press 3. </p> \
<p> If the <strong>fourth</strong> star from the left is the odd one out, press 4. </p> \
<p><div style='float: left;'><img src='jspsych-master/img/example_ob.png'></img></p> \
<p class='small'> ex. <strong>Press 1</strong> to continue</p></div>",
post_trial_gap: 250,
};
timeline.push(instructions)
var instructions_pt2 = {
type: "html-keyboard-response",
stimulus: "<p> On each trial you will have up to 4 seconds to enter your response. </p> \
<p>After that, it will move on to the next trial, so please do your best to respond within that time! </p> \
<p>After you respond, you will receive feedback as to whether your response was correct, </p> \
<p>indicated by a green box (for correct) and red box (for incorrect). </p> \
<p>Press any key to continue. </p>",
post_trial_gap: 250,
};
timeline.push(instructions_pt2)
//RANDOMIZERS
// determines which sunburst will be the odd-ball
function randomize_correct() {
return jsPsych.randomization.sampleWithoutReplacement(['1', '2', '3', '4'], 1)[0];
}
// randomly samples a particular sunburst from the scene
function randomize_sunburst() {
return jsPsych.randomization.sampleWithoutReplacement([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16], 1)[0];
}
/* randomly chooses which pair on the relevant-between dimension to present in the odd_ball task */
function randomize_be_pair() {
return jsPsych.randomization.sampleWithoutReplacement([1, 2, 3, 4], 1)[0];
}
/* randomly chooses which pair on the relevant-between dimension to present in the odd_ball task */
function randomize_wi_pair() {
return jsPsych.randomization.sampleWithoutReplacement([5, 6, 7, 8, 9, 10, 11, 12], 1)[0];
}
/* randomly chooses which pair on the relevant-between dimension to present in the odd_ball task */
function randomize_irr_pair() {
return jsPsych.randomization.sampleWithoutReplacement([13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24], 1)[0];
}
/* randomly generates true or false */
function boolean() {
var boolean = jsPsych.randomization.sampleWithoutReplacement([true, false], 1)[0];
return boolean;
}
//HELPERS
function small_sunburst(pair) {
if (pair == 1) {sunburst = 8;}
else if (pair == 2) {sunburst = 7;}
else if (pair == 3) {sunburst = 6;}
else if (pair == 4) {sunburst = 5;}
else if (pair == 5) {sunburst = 4;}
else if (pair == 6) {sunburst = 3;}
else if (pair == 7) {sunburst = 2;}
else if (pair == 8) {sunburst = 1;}
else if (pair == 9) {sunburst = 12;}
else if (pair == 10) {sunburst = 11;}
else if (pair == 11) {sunburst = 10;}
else if (pair == 12) {sunburst = 9;}
else if (pair == 13) {sunburst = 3;}
else if (pair == 14) {sunburst = 2;}
else if (pair == 15) {sunburst = 1;}
else if (pair == 16) {sunburst = 7;}
else if (pair == 17) {sunburst = 6;}
else if (pair == 18) {sunburst = 5;}
else if (pair == 19) {sunburst = 11;}
else if (pair == 20) {sunburst = 10;}
else if (pair == 21) {sunburst = 9;}
else if (pair == 22) {sunburst = 15;}
else if (pair == 23) {sunburst = 14;}
else {sunburst = 13;}
return sunburst;
}
function big_sunburst(pair) {
if (pair == '1') {sunburst = 12;}
else if (pair == '2') {sunburst = 11;}
else if (pair == '3') {sunburst = 10;}
else if (pair == '4') {sunburst = 9;}
else if (pair == '5') {sunburst = 8;}
else if (pair == '6') {sunburst = 7;}
else if (pair == '7') {sunburst = 6;}
else if (pair == '8') {sunburst = 5;}
else if (pair == '9') {sunburst = 16;}
else if (pair == '10') {sunburst = 15;}
else if (pair == '11') {sunburst = 14;}
else if (pair == '12') {sunburst = 13;}
else if (pair == '13') {sunburst = 4;}
else if (pair == '14') {sunburst = 3;}
else if (pair == '15') {sunburst = 2;}
else if (pair == '16') {sunburst = 8;}
else if (pair == '17') {sunburst = 7;}
else if (pair == '18') {sunburst = 6;}
else if (pair == '19') {sunburst = 12;}
else if (pair == '20') {sunburst = 11;}
else if (pair == '21') {sunburst = 10;}
else if (pair == '22') {sunburst = 16;}
else if (pair == '23') {sunburst = 15;}
else {sunburst = 14;}
return sunburst;
}
function x_coordinate(sunburst) {
var x_dim;
if (sunburst <= 4) {x_dim = 0;}
else if (sunburst >= 5 && sunburst <= 8) {x_dim = 1;}
else if (sunburst >= 9 && sunburst <= 12) {x_dim = 2;}
else {x_dim = 3;}
return x_dim;
}
function y_coordinate(sunburst) {
var y_dim;
if (sunburst % 4 == 1) {y_dim = 0;}
else if (sunburst % 4 == 2) {y_dim = 1;}
else if (sunburst % 4 == 3) {y_dim = 2;}
else {y_dim = 3;}
return y_dim;
}
function rel_dim_val(x, be, wi){
var x_dim;
if(x == 0){
x_dim = 0.5 - (be/2) - wi;
} else if(x == 1) {
x_dim = 0.5 - (be/2);
} else if(x == 2) {
x_dim = 0.5 + (be/2);
} else {
x_dim = 0.5 + (be/2) + wi;
}
return x_dim;
}
function irr_dim_val(y, irr) {
var y_dim;
if(y == 0){
y_dim = 0.5 - (irr/2) - irr;
} else if(y == 1) {
y_dim = 0.5 - (irr/2);
} else if(y == 2) {
y_dim = 0.5 + (irr/2);
} else {
y_dim = 0.5 + (irr/2) + irr;
}
return y_dim;
}
function calculate_sunburst_values(sunburst) {
var dimension = relevant_dimension;
var xCoordinate = x_coordinate(sunburst);
var yCoordinate = y_coordinate(sunburst);
if (dimension == 'dots') {
var x_dim = rel_dim_val(xCoordinate, rel_be_distance, rel_wi_distance);
var y_dim = irr_dim_val(yCoordinate, irr_distance);
return generate_stim(x_dim, y_dim);
} else {
var x_dim = irr_dim_val(yCoordinate, irr_distance);
var y_dim = rel_dim_val(xCoordinate, rel_be_distance, rel_wi_distance);
return generate_stim(x_dim, y_dim);
}
}
function calculate_pair_values(pair, correct, rel_be_distance, rel_wi_distance, irr_distance) {
var bool = boolean();
var rel_be_distance = rel_be_distance;
var rel_wi_distance = rel_wi_distance;
var irr_distance = irr_distance;
if (bool) {
var same_ball = small_sunburst(pair);
var sx_coord = x_coordinate(same_ball);
var sy_coord = y_coordinate(same_ball);
var odd_ball = big_sunburst(pair);
var ox_coord = x_coordinate(odd_ball);
var oy_coord = y_coordinate(odd_ball);
} else {
var odd_ball = small_sunburst(pair);
var ox_coord = x_coordinate(odd_ball);
var oy_coord = y_coordinate(odd_ball);
var same_ball = big_sunburst(pair);
var sx_coord = x_coordinate(same_ball);
var sy_coord = y_coordinate(same_ball);
}
if (relevant_dimension == 'dots') {
var ox_dim = rel_dim_val(ox_coord, rel_be_distance, rel_wi_distance);
var oy_dim = irr_dim_val(oy_coord, irr_distance);
var sx_dim = rel_dim_val(sx_coord, rel_be_distance, rel_wi_distance);
var sy_dim = irr_dim_val(sy_coord, irr_distance);
} else {
var ox_dim = irr_dim_val(ox_coord, irr_distance);
var oy_dim = rel_dim_val(oy_coord, rel_be_distance, rel_wi_distance);
var sx_dim = irr_dim_val(sx_coord, irr_distance);
var sy_dim = rel_dim_val(sy_coord, rel_be_distance, rel_wi_distance);
}
var set = generate_set(sx_dim, sy_dim, ox_dim, oy_dim, correct)
return set;
}
// depicts the set of stimuli in the scene
function generate_set(same_dot, same_line, odd_dot, odd_line, correct) {
var html = '<div style="position: relative; width:'+((stim_width_height+stim_padding)*4 + stim_padding)+'px; height:'+(stim_width_height+stim_padding)+'px;">';
if (correct == '1'){
html += '<div class="oddball-stim ex1">'+generate_stim(odd_dot, odd_line)+'</div>'+
'<div class="oddball-stim ex2">'+generate_stim(same_dot, same_line)+'</div>'+
'<div class="oddball-stim ex3">'+generate_stim(same_dot, same_line)+'</div>'+
'<div class="oddball-stim ex4">'+generate_stim(same_dot, same_line)+'</div>';
} else if (correct == '2') {
html += '<div class="oddball-stim ex1">'+generate_stim(same_dot, same_line)+'</div>'+
'<div class="oddball-stim ex2">'+generate_stim(odd_dot, odd_line)+'</div>'+
'<div class="oddball-stim ex3">'+generate_stim(same_dot, same_line)+'</div>'+
'<div class="oddball-stim ex4">'+generate_stim(same_dot, same_line)+'</div>';
} else if (correct == '3') {
html += '<div class="oddball-stim ex1">'+generate_stim(same_dot, same_line)+'</div>'+
'<div class="oddball-stim ex2">'+generate_stim(same_dot, same_line)+'</div>'+
'<div class="oddball-stim ex3">'+generate_stim(odd_dot, odd_line)+'</div>'+
'<div class="oddball-stim ex4">'+generate_stim(same_dot, same_line)+'</div>';
} else {
html += '<div class="oddball-stim ex1">'+generate_stim(same_dot, same_line)+'</div>'+
'<div class="oddball-stim ex2">'+generate_stim(same_dot, same_line)+'</div>'+
'<div class="oddball-stim ex3">'+generate_stim(same_dot, same_line)+'</div>'+
'<div class="oddball-stim ex4">'+generate_stim(odd_dot, odd_line)+'</div>';
}
html += '</div>';
return html;
}
// displays individual sunburst
function generate_stim(dot_dim, line_dim) {
// params
var w = stim_width_height;
var r = stim_width_height/4;
var x = w / 2;
var y = w / 2;
var circle_r = stim_width_height / 200;
var minLineLength = 10;
var dot_density = min_dot_density + (max_dot_density-min_dot_density)*dot_dim;
var line_density = min_line_density + (max_line_density-min_line_density)*line_dim;
// svg string
var str = '<svg xmlns="http://www.w3.org/2000/svg" width="' + w + '" height="' + w + '">'
//Code for drawing the lines that come out of the circle.
//Each line starts in the center of the circle and is drawn outward
for (var i = 0; i < line_density; i++) {
var angle = Math.random() * Math.PI / 2;
var lineLength = r + minLineLength + (r - minLineLength) * Math.random();
var x2 = Math.cos(angle) * lineLength;
var y2 = Math.sin(angle) * lineLength;
if (Math.random() < 0.5) {
x2 = -x2;
}
if (Math.random() < 0.5) {
y2 = -y2;
}
x2 = x + x2;
y2 = y + y2;
str += '<line x1="' + x + '" y1="' + y + '" x2="' + x2 + '" y2="' + y2 + '" style="stroke:rgb(65,105,225); stroke-width:1;" />';
}
// draw circle
str += '<circle cx="' + x + '" cy="' + y + '" r="' + r + '" style="stroke:rgb(65,105,225); stroke-width:2; fill: rgb(255,255,255);"/>'
//Code for creating and dispersing the dots
for (var j = 0; j < dot_density; j++) {
do {
var x2 = Math.random() * 2 * r + x - r;
var y2 = Math.random() * 2 * r + y - r;
} while ((Math.pow(x - x2, 2) + Math.pow(y - y2, 2)) > Math.pow(r - circle_r - 2, 2));
str += '<circle cx="' + x2 + '" cy="' + y2 + '" r="' + circle_r + '" style="fill:rgb(65,105,225);" />';
}
str += "</svg>";
return str;
}
// determines if the last answer was correct
function last_correct() {
return jsPsych.data.get().last(1).values()[0].correct;
}
function category_feedback() {
var correct_response = jsPsych.data.get().last(1).values()[0].correct_response;
if (correct_response == 'T') {var feedback = 'Tig'}
else {var feedback = 'Fep'}
return feedback;
}
function too_slow() {
return jsPsych.randomization.sampleWithoutReplacement(['Snooze ya lose!', 'Oops!', 'Cmon Grandma!'], 1)[0]
}
//TIMELINES
// universal pause for each trial
var pause = {
type: 'html-keyboard-response',
stimulus: function () {
if (jsPsych.data.get().last(2).values()[0].key_press != null) {return ''}
else {return "<p>Still with us? Press any key to continue. </p>"}
},
on_start: function () {
if (jsPsych.data.get().last(2).values()[0].correct_response == true) {
document.querySelector("progress").innerHTML += progress_bar();
}
},
choices: function () {
if (jsPsych.data.get().last(2).values()[0].key_press != null) {return jsPsych.NO_KEYS}
},
trial_duration: function () {
if (jsPsych.data.get().last(2).values()[0].key_press != null) {return 500}
},
data: {test_part: 'pause'}
}
// oddball task trial
var test = {
type: "html-keyboard-response",
stimulus: '',
choices: ['1', '2', '3', '4'],
trial_duration: 4000,
timeline_variables: [
{ pair_type: 'be' },
{ pair_type: 'wi' },
{ pair_type: 'wi' },
{ pair_type: 'irr' },
{ pair_type: 'irr' },
{ pair_type: 'irr' }
],
sample: {
type: 'without-replacement',
size: 6,
},
randomize_order: true,
on_start: function(trial) {
var correct = randomize_correct();
if (jsPsych.timelineVariable('pair_type') == 'be') {
var pair = randomize_be_pair();
} else if (jsPsych.timelineVariable('pair_type') == 'wi') {
var pair = randomize_wi_pair();
} else {
var pair = randomize_irr_pair();
}
var question = '<div class="center"><p>Choose the odd-ball</p></div>';
var set = calculate_pair_values(pair, correct, rel_be_distance, rel_wi_distance, irr_distance);
var stimulus = set + question;
trial.stimulus = stimulus;
trial.data = {
test_part: 'test',
correct_response: correct,
stimulus: set,
}
},
on_finish: function(data) {
data.correct = data.key_press == jsPsych.pluginAPI.convertKeyCharacterToKeyCode(data.correct_response);
if(jsPsych.timelineVariable('pair_type') == 'be'){
rel_be_distance = adjust_distance(rel_be_distance, data.correct);
} else if(jsPsych.timelineVariable('pair_type') == 'wi'){
rel_wi_distance = adjust_distance(rel_wi_distance, data.correct);
} else {
irr_distance = adjust_distance(irr_distance, data.correct);
}
},
}
// feedback for oddball trials
var feedback = {
type: "html-keyboard-response",
stimulus: function() {
var lastCorrect = last_correct();
var html = "<style>";
var stimulus = jsPsych.data.get().last(1).values()[0].stimulus;
var correct_response = jsPsych.data.get().last(1).values()[0].correct_response;
var actualResponse = jsPsych.data.get().last(1).values()[0].key_press - 48;
html += ".ex"+correct_response+" { border: 11px solid limegreen; padding: 0;} ";
if(lastCorrect == true) {
html += "</style>"
html += "<p style='position:absolute; width: 100%; left: 0;'>Correct!</p>"
} else if (jsPsych.data.get().last(1).values()[0].key_press == null) {
html += ".ex"+actualResponse+" { border: 11px solid red; padding: 0;} ";
html += "</style>"
html += jsPsych.data.get().last(1).values()[0].stimulus +
"<p style='position:absolute; width: 100%; left: 0;'>" + too_slow();
return html;
} else {
html += ".ex"+actualResponse+" { border: 11px solid red; padding: 0;} ";
html += "</style>"
html += "<p style='position:absolute; width: 100%; left: 0;'>Incorrect!</p>"
}
stimulus += html;
return stimulus;
},
choices: function () {
if (jsPsych.data.get().last(1).values()[0].key_press != null) {return jsPsych.NO_KEYS}
},
trial_duration: function () {
if (jsPsych.data.get().last(1).values()[0].key_press != null) {return 500}
},
}
var test_procedure = {
timeline: [pause, test, feedback],
//timeline_variables: test_stimuli,
repetitions: 20,
randomize_order: true
}
timeline.push(test_procedure)
// define debrief
var debrief_block = {
type: "html-keyboard-response",
stimulus: function() {
var trials = jsPsych.data.get().filter({test_part: 'test'});
var correct_trials = trials.filter({correct: true});
var accuracy = Math.round(correct_trials.count() / trials.count() * 100);
var rt = Math.round(correct_trials.select('rt').mean());
return "<p>You responded correctly on "+accuracy+"% of the trials.</p>"+
"<p>Your average response time was "+rt+"ms.</p>"+
"<p>Press any key to complete the experiment. Thank you!</p>";
}
};
timeline.push(debrief_block)
// start the experiment
jsPsych.init({
timeline: timeline,
on_finish: function() {
console.log(jsPsych.data.get().ignore('stimulus').json());
}
});
</script>
</html>