-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexperiment.js
490 lines (441 loc) · 15.6 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
/* ***************************************** */
/* Define helper functions */
/* ***************************************** */
function evalAttentionChecks() {
var check_percent = 1
if (run_attention_checks) {
var attention_check_trials = jsPsych.data.getTrialsOfType('attention-check')
var checks_passed = 0
for (var i = 0; i < attention_check_trials.length; i++) {
if (attention_check_trials[i].correct === true) {
checks_passed += 1
}
}
check_percent = checks_passed / attention_check_trials.length
}
return check_percent
}
var getInstructFeedback = function() {
return '<div class = centerbox><p class = center-block-text>' + feedback_instruct_text +
'</p></div>'
}
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('poldrack-single-stim')
experiment_data = experiment_data.concat(jsPsych.data.getTrialsOfType('poldrack-categorize'))
var missed_count = 0
var trial_count = 0
var rt_array = []
var rt = 0
//record choices participants made
var choice_counts = {}
choice_counts[-1] = 0
for (var k = 0; k < choices.length; k++) {
choice_counts[choices[k]] = 0
}
for (var i = 0; i < experiment_data.length; i++) {
if (experiment_data[i].possible_responses != 'none') {
trial_count += 1
rt = experiment_data[i].rt
key = experiment_data[i].key_press
choice_counts[key] += 1
if (rt == -1) {
missed_count += 1
} else {
rt_array.push(rt)
}
}
}
//calculate average rt
var avg_rt = -1
if (rt_array.length !== 0) {
avg_rt = math.median(rt_array)
}
//calculate whether response distribution is okay
var responses_ok = true
Object.keys(choice_counts).forEach(function(key, index) {
if (choice_counts[key] > trial_count * 0.85) {
responses_ok = false
}
})
var missed_percent = missed_count/trial_count
credit_var = (missed_percent < 0.4 && avg_rt > 200 && responses_ok)
jsPsych.data.addDataToLastTrial({"credit_var": credit_var})
}
var getStim = function() {
stim = firstPhaseStimsComplete.image.pop()
curr_data = firstPhaseStimsComplete.data.pop()
return stim;
}
var getData = function() {
return curr_data
}
var getSecondPhaseStim = function() {
stim = secondPhaseStimsComplete.image.pop()
curr_data = secondPhaseStimsComplete.data.pop()
return stim;
}
var getResponse = function() {
return answers.pop()
}
var genResponses = function(stimuli) {
var answers_80_20 = jsPsych.randomization.repeat([37, 37, 37, 37, 37, 37, 37, 37, 39, 39],
eachComboNum / 10);
var answers_20_80 = jsPsych.randomization.repeat([39, 39, 39, 39, 39, 39, 39, 39, 37, 37],
eachComboNum / 10);
var answers_70_30 = jsPsych.randomization.repeat([37, 37, 37, 37, 37, 37, 37, 39, 39, 39],
eachComboNum / 10);
var answers_30_70 = jsPsych.randomization.repeat([39, 39, 39, 39, 39, 39, 39, 37, 37, 37],
eachComboNum / 10);
var answers_60_40 = jsPsych.randomization.repeat([37, 37, 37, 37, 37, 37, 39, 39, 39, 39],
eachComboNum / 10);
var answers_40_60 = jsPsych.randomization.repeat([39, 39, 39, 39, 39, 39, 37, 37, 37, 37],
eachComboNum / 10);
var count1 = 0;
var count2 = 0;
var count3 = 0;
var count4 = 0;
var count5 = 0;
var count6 = 0;
var answers = [];
for (var i = 0; i < FP_trials; i++) {
if (stimuli.data[i].condition === '80_20') {
answers.push(answers_80_20[count1]);
count1 = count1 + 1;
} else if (stimuli.data[i].condition === '20_80') {
answers.push(answers_20_80[count2]);
count2 = count2 + 1;
} else if (stimuli.data[i].condition === '70_30') {
answers.push(answers_70_30[count3]);
count3 = count3 + 1;
} else if (stimuli.data[i].condition === '30_70') {
answers.push(answers_30_70[count4]);
count4 = count4 + 1;
} else if (stimuli.data[i].condition === '60_40') {
answers.push(answers_60_40[count5]);
count5 = count5 + 1;
} else {
answers.push(answers_40_60[count6]);
count6 = count6 + 1;
}
}
return answers;
};
/*************************************************************************/
/* DEFINE EXPERIMENTAL VARIABLES */
/*************************************************************************/
// generic task variables
var run_attention_checks = false
var attention_check_thresh = 0.45
var sumInstructTime = 0 //ms
var instructTimeThresh = 0 ///in seconds
var credit_var = true
// task specific variables
var choices = [37, 39]
var curr_data = []
var stim = ''
var current_trial = 0
/* SPECIFY HOW MANY TRIALS YOU WANT FOR FIRST PHASE, and SECOND PHASE. FP=first(must be divisible by 60), SP=second(must be divisible by 22) */
var FP_trials = 60;
var SP_trials = 90;
var eachComboNum = FP_trials / 6; /* don't change this line */
var eachComboNumSP = SP_trials / 30; /* don't change this line */
/* THIS IS TO RANDOMIZE STIMS */
var stimArray = ["images/1.png",
"images/2.png",
"images/3.png",
"images/4.png",
"images/5.png",
"images/6.png"
];
jsPsych.pluginAPI.preloadImages(stimArray)
var randomStimArray = jsPsych.randomization.repeat(stimArray, 1);
var stims = [['80', randomStimArray[0]],
['20', randomStimArray[1]],
['70', randomStimArray[2]],
['30', randomStimArray[3]],
['60', randomStimArray[4]],
['40', randomStimArray[5]]]
firstPhaseStims = []
/* THIS IS FOR FIRST PHASE STIMS, randomized and counterbalanced*/
for (var i = 0; i<3; i++) {
var order1_stim = {}
order1_stim.image = "<div class = decision-left><img src='" + stims[i*2][1] +
"'></img></div><div class = decision-right><img src='" + stims[i*2+1][1] + "'></img></div>"
order1_stim.data = {
trial_id: 'stim',
exp_stage: 'training',
condition: stims[i*2][0] + '_' + stims[i*2+1][0],
optimal_response: 37
}
var order2_stim = {}
order2_stim.image = "<div class = decision-left><img src='" + stims[i*2+1][1] +
"'></img></div><div class = decision-right><img src='" + stims[i*2][1] + "'></img></div>"
order2_stim.data = {
trial_id: 'stim',
exp_stage: 'training',
condition: stims[i*2+1][0] + '_' + stims[i*2][0],
optimal_response: 39
}
firstPhaseStims.push(order1_stim)
firstPhaseStims.push(order2_stim)
}
var firstPhaseStimsComplete = jsPsych.randomization.repeat(firstPhaseStims, eachComboNum, true);
var answers = genResponses(firstPhaseStimsComplete)
var curr_data = ''
/*THIS IS FOR SECOND PHASE STIMS, randomized and counterbalanced*/
secondPhaseStims = []
for (var i = 0; i<5; i++) {
for (var j = i+1; j < 6; j++) {
var order1_stim = {}
order1_stim.image = "<div class = decision-left><img src='" + stims[i][1] +
"'></img></div><div class = decision-right><img src='" + stims[j][1] + "'></img></div>"
var optimal_response1 = choices[stims[i][0] < stims[j][0] ? 1 : 0]
order1_stim.data = {
trial_id: 'stim',
exp_stage: 'test',
condition: stims[i][0] + '_' + stims[j][0],
optimal_response: optimal_response1
}
var order2_stim = {}
order2_stim.image = "<div class = decision-left><img src='" + stims[j][1] +
"'></img></div><div class = decision-right><img src='" + stims[i][1] + "'></img></div>"
var optimal_response2 = choices[stims[i][0] > stims[j][0] ? 1 : 0]
order2_stim.data = {
trial_id: 'stim',
exp_stage: 'test',
condition: stims[j][0] + '_' + stims[i][0],
optimal_response: optimal_response2
}
secondPhaseStims.push(order1_stim)
secondPhaseStims.push(order2_stim)
}
}
var secondPhaseStimsComplete = jsPsych.randomization.repeat(secondPhaseStims, eachComboNumSP, true);
/* This is to end the training while loop, if the subject has reached 6 training blocks */
var training_count = 0;
/* ************************************ */
/* Set up jsPsych blocks */
/* ************************************ */
// Set up attention check node
var attention_check_block = {
type: 'attention-check',
data: {
trial_id: "attention_check"
},
timing_response: 180000,
response_ends_trial: true,
timing_post_trial: 200
}
var attention_node = {
timeline: [attention_check_block],
conditional_function: function() {
return run_attention_checks
}
}
//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 feedback_instruct_text =
'Welcome to the experiment. This experiment will take about 14 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 = centerbox><p class = block-text>This experiment is composed of two phases. During the first phase, you will be presented with one of three pairs of abstract shapes. For each pair, you must choose one of the shapes by pressing either the <strong>left</strong> or <strong>right arrow key</strong> to correspond with the left or right image respectively.</p><p class = block-text>Each shape has a different chance of being correct. Your task is to maximize your points by selecting the shape with the higher probability of being correct on each trial.</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 FP_block = {
type: 'poldrack-text',
data: {
trial_id: "first_phase_intro"
},
timing_response: 180000,
text: '<div class = centerbox><p class = center-block-text> We will now begin Phase 1. Press <strong>enter</strong> to begin. </p></div>',
cont_key: [13],
timing_post_trial: 1000
};
training_trials = []
for (i = 0; i < 60; i++) {
var training_block = {
type: 'poldrack-categorize',
stimulus: getStim,
key_answer: getResponse,
choices: choices,
correct_text: '<div class = bottombox><div style="color:green"; class = center-text>Correct!</div></div>',
incorrect_text: '<div class = bottombox><div style="color:red"; class = center-text>Incorrect</div></div>',
timeout_message: '<div class = bottombox><div class = center-text>no response detected</div></div>',
timing_stim: 4000,
timing_response: 4000,
timing_feedback_duration: 750,
response_ends_trial: true,
timing_post_trial: 500,
is_html: true,
data: getData,
on_finish: function(data) {
choice = choices.indexOf(data.key_press)
stims = data.condition.split('_')
chosen_stim = stims[choice]
correct = false
if (data.key_press == data.optimal_response){
correct = true
}
jsPsych.data.addDataToLastTrial({
'feedback': data.correct,
'correct': correct,
'stim_chosen': chosen_stim,
'trial_num': current_trial
})
current_trial += 1
}
};
training_trials.push(training_block)
}
var performance_criteria = {
timeline: training_trials,
loop_function: function(data) {
var ab_total_correct = 0;
var cd_total_correct = 0;
var ef_total_correct = 0;
var ab_cum_trials = 0;
var cd_cum_trials = 0;
var ef_cum_trials = 0;
for (var i = 0; i < data.length; i++) {
if (data[i].condition == "80_20" || data[i].condition == "20_80" ) {
ab_cum_trials = ab_cum_trials + 1;
if (data[i].key_press === data[i].optimal_response) {
ab_total_correct = ab_total_correct + 1;
}
} else if (data[i].condition == "70_30" || data[i].condition == "30_70") {
cd_cum_trials = cd_cum_trials + 1;
if (data[i].key_press === data[i].optimal_response) {
cd_total_correct = cd_total_correct + 1;
}
} else if (data[i].condition == "60_40" || data[i].condition == "40_60") {
ef_cum_trials = ef_cum_trials + 1;
if (data[i].key_press === data[i].optimal_response) {
ef_total_correct = ef_total_correct + 1;
}
}
}
var ab_percent = ab_total_correct / ab_cum_trials
var cd_percent = cd_total_correct / cd_cum_trials
var ef_percent = ef_total_correct / ef_cum_trials
training_count = training_count + 1;
if ((ab_percent > 0.7 && cd_percent > 0.65 && ef_percent > 0.5 && training_count > 3) || (
training_count == 8)) {
return false
} else {
firstPhaseStimsComplete = jsPsych.randomization.repeat(firstPhaseStims, eachComboNum, true);
answers = genResponses(firstPhaseStimsComplete)
return true
}
}
};
var SP_block = {
type: 'poldrack-text',
timing_response: 180000,
data: {
trial_id: "second_phase_intro"
},
text: '<div class = centerbox><p class = block-text>We will now begin Phase 2.</p><p class = block-text> For this phase, you must again choose between pairs of shapes. Press the <strong>right</strong> arrow key to choose the image on the right, and the <strong>left</strong> arrow key to choose the image on the left.</p><p class = block-text>In this phase there will be no visual feedback, but your are still earning points. Your task is still to choose the shape that has the higher probability of being correct to maximize your points. If you are not sure how to respond, use your gut instinct.</p><p class = block-text> Press <strong> Enter </strong> when you are ready.</p></div>',
cont_key: [13],
on_finish: function() {
current_trial = 0
}
};
var second_phase_trials = {
type: 'poldrack-single-stim',
stimulus: getSecondPhaseStim,
is_html: true,
data: getData,
choices: choices,
timing_stim: 2500,
timing_response: 2500,
timing_post_trial: 500,
on_finish: function(data) {
choice = choices.indexOf(data.key_press)
stims = data.condition.split('_')
chosen_stim = stims[choice]
correct = false
if (data.key_press == data.optimal_response){
correct = true
}
jsPsych.data.addDataToLastTrial({
'correct': correct,
'stim_chosen': chosen_stim,
'trial_num': current_trial
})
current_trial += 1
}
};
var end_block = {
type: 'poldrack-text',
data: {
trial_id: "end",
exp_id: 'probabilistic_selection'
},
timing_response: 180000,
text: '<div class = centerbox><p class = center-block-text>Finished with this task!</p><p class = center-block-text>Press <strong>enter</strong> to continue.</p></div>',
cont_key: [13],
on_finish: assessPerformance
};
/* create experiment definition array */
var probabilistic_selection_experiment = [];
probabilistic_selection_experiment.push(instruction_node);
probabilistic_selection_experiment.push(FP_block);
probabilistic_selection_experiment.push(performance_criteria);
probabilistic_selection_experiment.push(attention_node);
probabilistic_selection_experiment.push(SP_block);
for(var i = 0; i<SP_trials; i++){
probabilistic_selection_experiment.push(second_phase_trials);
}
probabilistic_selection_experiment.push(attention_node);
probabilistic_selection_experiment.push(post_task_block)
probabilistic_selection_experiment.push(end_block);