-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathexperiment.js
More file actions
673 lines (540 loc) · 20.1 KB
/
experiment.js
File metadata and controls
673 lines (540 loc) · 20.1 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
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
/* ************************************ */
/* Define Helper Functions */
/* ************************************ */
//Functions added for in-person sessions
function genITIs() {
mean_iti = 0.5 //mean and standard deviation of 0.5 secs
min_thresh = 0
max_thresh = 4
lambda = 1/mean_iti
iti_array = []
for (i=0; i < exp_len + 3*numTestBlocks ; i++) { //add 3 ITIs per test block to make sure there are enough
curr_iti = - Math.log(Math.random()) / lambda;
while (curr_iti > max_thresh || curr_iti < min_thresh) {
curr_iti = - Math.log(Math.random()) / lambda;
}
iti_array.push(curr_iti*1000) //convert ITIs from seconds to milliseconds
}
return(iti_array)
}
function getITI_stim() { //added for fMRI compatibility
var currITI = ITIs_stim.shift()
return currITI
}
function getITI_resp() { //added for fMRI compatibility
var currITI = ITIs_resp.shift()
return currITI
}
//feedback functions added for in-person version
var getRefreshFeedback = function() {
return '<div class = bigbox><div class = picture_box><p class = instruct-text><font color="white">' + refresh_feedback_text + '</font></p></div></div>'
}
var getRefreshTrialID = function() {
return refresh_trial_id
}
var getRefreshFeedbackTiming = function() {
return refresh_feedback_timing
}
var getRefreshResponseEnds = function() {
return refresh_response_ends
}
//
function getDisplayElement() {
$('<div class = display_stage_background></div>').appendTo('body')
return $('<div class = display_stage></div>').appendTo('body')
}
function addID() {
jsPsych.data.addDataToLastTrial({exp_id: 'n_back_single_task_network__fmri'})
}
function assessPerformance() {
var experiment_data = jsPsych.data.getTrialsOfType('poldrack-single-stim')
var missed_count = 0
var trial_count = 0
var rt_array = []
var rt = 0
var correct = 0
//record choices participants made
var choice_counts = {}
choice_counts[-1] = 0
choice_counts[71] = 0
choice_counts[89] = 0
for (var k = 0; k < possible_responses.length; k++) {
choice_counts[possible_responses[k][1]] = 0
}
for (var i = 0; i < experiment_data.length; i++) {
if (experiment_data[i].trial_id == 'test_trial'){
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)
}
if (key == experiment_data[i].correct_response){
correct += 1
}
}
}
//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
var accuracy = correct / trial_count
credit_var = (missed_percent < 0.25 && avg_rt > 200 && responses_ok && accuracy > 0.60)
jsPsych.data.addDataToLastTrial({final_credit_var: credit_var,
final_missed_percent: missed_percent,
final_avg_rt: avg_rt,
final_responses_ok: responses_ok,
final_accuracy: accuracy})
}
var getResponse = function() {
return correct_response
}
var getFeedback = function() {
return '<div class = bigbox><div class = picture_box><p class = instruct-text><font color="white">' + feedback_text + '</font></p></div></div>'
}
var randomDraw = function(lst) {
var index = Math.floor(Math.random() * (lst.length))
return lst[index]
};
var createTrialTypes = function(numTrialsPerBlock, delay){
first_stims = []
for (var i = 0; i < 3; i++){
if (i < delay){
n_back_condition = 'N/A'
} else {
n_back_condition = n_back_conditions[Math.floor(Math.random() * 5)]
}
probe = randomDraw(letters)
correct_response = possible_responses[1][1]
if (n_back_condition == 'match'){
correct_response = possible_responses[0][1]
probe = randomDraw([first_stims[i - delay].probe.toUpperCase(), first_stims[i - delay].probe.toLowerCase()])
} else if (n_back_condition == "mismatch"){
probe = randomDraw('bBdDgGtTvV'.split("").filter(function(y) {return $.inArray(y, [first_stims[i - delay].probe.toLowerCase(), first_stims[i - delay].probe.toUpperCase()]) == -1}))
correct_response = possible_responses[1][1]
}
first_stim = {
n_back_condition: n_back_condition,
probe: probe,
correct_response: correct_response,
delay: delay
}
first_stims.push(first_stim)
}
stims = []
for(var numIterations = 0; numIterations < numTrialsPerBlock/n_back_conditions.length; numIterations++){
for (var numNBackConds = 0; numNBackConds < n_back_conditions.length; numNBackConds++){
n_back_condition = n_back_conditions[numNBackConds]
stim = {
n_back_condition: n_back_condition
}
stims.push(stim)
}
}
stims = jsPsych.randomization.repeat(stims,1)
stims = first_stims.concat(stims)
stim_len = stims.length
new_stims = []
for (i = 0; i < stim_len; i++){
if (i < 3){
stim = stims.shift()
n_back_condition = stim.n_back_condition
probe = stim.probe
correct_response = stim.correct_response
delay = stim.delay
} else {
stim = stims.shift()
n_back_condition = stim.n_back_condition
if (n_back_condition == "match"){
probe = randomDraw([new_stims[i - delay].probe.toUpperCase(), new_stims[i - delay].probe.toLowerCase()])
correct_response = possible_responses[0][1]
} else if (n_back_condition == "mismatch"){
probe = randomDraw('bBdDgGtTvV'.split("").filter(function(y) {return $.inArray(y, [new_stims[i - delay].probe.toLowerCase(), new_stims[i - delay].probe.toUpperCase()]) == -1}))
correct_response = possible_responses[1][1]
}
}
stim = {
n_back_condition: n_back_condition,
probe: probe,
correct_response: correct_response,
delay: delay
}
new_stims.push(stim)
}
return new_stims
}
var getStim = function(){
stim = stims.shift()
n_back_condition = stim.n_back_condition
probe = stim.probe
correct_response = stim.correct_response
delay = stim.delay
if (probe == probe.toUpperCase()) {
letter_case = 'uppercase'
} else if (probe == probe.toLowerCase()) {
letter_case = 'lowercase'
}
return task_boards[0]+ preFileType + letter_case + '_' + probe.toUpperCase() + fileTypePNG + task_boards[1]
}
var getResponse = function(){
return correct_response
}
var appendData = function(){
curr_trial = jsPsych.progress().current_trial_global
trial_id = jsPsych.data.getDataByTrialIndex(curr_trial).trial_id
current_trial+=1
if (trial_id == 'practice_trial'){
current_block = refreshCount
} else if (trial_id == 'test_trial'){
current_block = testCount
}
jsPsych.data.addDataToLastTrial({
n_back_condition: n_back_condition,
probe: probe,
correct_response: correct_response,
delay: delay,
current_trial: current_trial,
current_block: current_block,
letter_case: letter_case
})
if (jsPsych.data.getDataByTrialIndex(curr_trial).key_press == correct_response){
jsPsych.data.addDataToLastTrial({
correct_trial: 1,
})
} else if (jsPsych.data.getDataByTrialIndex(curr_trial).key_press != correct_response){
jsPsych.data.addDataToLastTrial({
correct_trial: 0,
})
}
}
/* ************************************ */
/* Define Experimental Variables */
/* ************************************ */
// generic task variables
var sumInstructTime = 0 //ms
var instructTimeThresh = 0 ///in seconds
var credit_var = 0
var practice_len = 10 // must be divisible by 5
var exp_len = 220 //150 // must be divisible by 5 --9:30
var numTrialsPerBlock = 55 // must be divisible by 5 and we need to have a multiple of 2 blocks (2,4,6) in order to have equal delays across blocks
var numTestBlocks = exp_len / numTrialsPerBlock //should be divisble by 2 ^^
var accuracy_thresh = 0.75
var rt_thresh = 1000
var missed_thresh = 0.10
var delays = jsPsych.randomization.repeat([1, 2], numTestBlocks / 2) //jsPsych.randomization.repeat([1, 2, 3], numTestBlocks / 3)
var delay = 2 //1
var pathSource = "/static/experiments/n_back_single_task_network__fmri/images/"
var fileTypePNG = ".png'></img>"
var preFileType = "<img class = center src='/static/experiments/n_back_single_task_network__fmri/images/"
var n_back_conditions = jsPsych.randomization.repeat(['mismatch','mismatch','match','mismatch','mismatch'],1)
var possible_responses = [['index finger', 89],['middle finger', 71]]
var letters = 'bBdDgGtTvV'.split("")
var prompt_text_list = '<ul style="text-align:left;">'+
'<li>Match the current letter to the letter that appeared some number of trials ago</li>' +
'<li>If they match, press your '+possible_responses[0][0]+'</li>' +
'<li>If they mismatch, press your '+possible_responses[1][0]+'</li>' +
'</ul>'
var prompt_text = '<div class = prompt_box>'+
'<p class = center-block-text style = "font-size:16px; line-height:80%%;">Match the current letter to the letter that appeared 1 trial ago</p>' +
'<p class = center-block-text style = "font-size:16px; line-height:80%%;">If they match, press your '+possible_responses[0][0]+'</p>' +
'<p class = center-block-text style = "font-size:16px; line-height:80%%;">If they mismatch, press your '+possible_responses[1][0]+'</p>' +
'</div>'
var current_trial = 0
var current_block = 0
//PRE LOAD IMAGES HERE
var pathSource = "/static/experiments/n_back_single_task_network__fmri/images/"
var lettersPreload = ['B','D','G','T','V']
var casePreload = ['lowercase','uppercase']
var images = []
for(i = 0; i < lettersPreload.length; i++){
for(x = 0; x < casePreload.length; x++){
images.push(pathSource + casePreload[x] + '_' + lettersPreload[i] + '.png')
}
}
jsPsych.pluginAPI.preloadImages(images);
//ADDED FOR SCANNING
//fmri variables
var ITIs_stim = []
var ITIs_resp = []
//refresh feedback variables
var refresh_feedback_text = '<div class = instructbox>'+
'<p class = instruct-text>In this task, you will see a letter on every trial.</p>'+
'<p class = instruct-text>You will be asked to match the current letter to the letter that appeared either 1, 2, or 3 trials ago depending on the delay given to you for that block.</p>'+
'<p class = instruct-text><strong>Press your '+possible_responses[0][0]+' if the letters match, and your '+possible_responses[1][0]+' if they mismatch.</strong></p>'+
'<p class = instruct-text>Your delay (the number of trials ago which you must match the current letter to) will change from block to block. You will be given the delay at the start of every block of trials.</p>'+
'<p class = instruct-text>Capitalization does not matter, so "T" matches with "t".</p> '+
'<p class = instruct-text><strong><i>Your delay for this upcoming practice round is 1</i>.</strong></p> '+
'<p class = instruct-text>During practice, you will see a reminder of the rules. <i> This will be removed for the test</i>. </p>'+
'<p class = instruct-text>To let the experimenters know when you are ready to begin, please press any button. </p>'+
'</div>'
var refresh_trial_id = "instructions"
var refresh_feedback_timing = -1
var refresh_response_ends = true
/* ************************************ */
/* Define Game Boards */
/* ************************************ */
var task_boards = ['<div class = bigbox><div class = centerbox><div class = gng_number><div class = cue-text>','</div></div></div></div>']
var stims = createTrialTypes(practice_len, delay)
/* ************************************ */
/* Set up jsPsych blocks */
/* ************************************ */
var end_block = {
type: 'poldrack-text',
data: {
trial_id: "end"
},
timing_response: 10000,
text: '<div class = centerbox><p class = center-block-text>Thanks for completing this task!</p></div>',
cont_key: [32],
timing_post_trial: 0,
on_finish: function(){
assessPerformance()
}
};
var refresh_feedback_block = {
type: 'poldrack-single-stim',
stimulus: getRefreshFeedback,
data: {
trial_id: getRefreshTrialID
},
choices: [32],
timing_post_trial: 0,
is_html: true,
timing_response: getRefreshFeedbackTiming, //10 seconds for feedback
timing_stim: getRefreshFeedbackTiming,
response_ends_trial: getRefreshResponseEnds,
on_finish: function() {
refresh_trial_id = "practice-no-stop-feedback"
refresh_feedback_timing = 10000
refresh_response_ends = false
if (ITIs_stim.length===0) { //if ITIs haven't been generated, generate them!
ITIs_stim = genITIs()
ITIs_resp = ITIs_stim.slice(0) //make a copy of ITIs so that timing_stimulus & timing_response are the same
}
}
};
var refresh_fixation_block = {
type: 'poldrack-single-stim',
stimulus: '<div class = centerbox><div class = fixation>+</div></div>',
is_html: true,
choices: 'none',
data: {
trial_id: "practice_fixation"
},
timing_response: 500, //500
timing_post_trial: 0,
prompt: prompt_text_list
}
var ITI_fixation_block = {
type: 'poldrack-single-stim',
stimulus: '<div class = centerbox><div class = fixation>+</div></div>',
is_html: true,
choices: 'none',
data: {
trial_id: "fixation",
},
timing_post_trial: 0,
timing_stim: getITI_stim, //500
timing_response: getITI_resp //500
};
var feedback_text =
'Welcome to the experiment. This experiment will take around 5 minutes. Press <i>enter</i> to begin.'
var feedback_block = {
type: 'poldrack-single-stim',
data: {
trial_id: "practice-no-stop-feedback"
},
choices: 'none',
stimulus: getFeedback,
timing_post_trial: 0,
is_html: true,
timing_response: 10000,
response_ends_trial: false,
};
/* ************************************ */
/* Set up timeline blocks */
/* ************************************ */
//refresh trials for in scanner practice
var refreshTrials = []
refreshTrials.push(refresh_feedback_block)
for (i = 0; i < practice_len + 2; i++) { //was changed from + 3 as delays went from 1:3 to 1:2
var refresh_block = {
type: 'poldrack-categorize',
stimulus: getStim,
is_html: true,
choices: [possible_responses[0][1],possible_responses[1][1]],
key_answer: getResponse,
data: {
trial_id: "practice_trial"
},
correct_text: '<div class = upperbox><div class = center-text>Correct!</font></div></div>',// + prompt_text_list,
incorrect_text: '<div class = upperbox><div class = center-text>Incorrect</font></div></div>',// + prompt_text_list,
timeout_message: '<div class = upperbox><div class = center-text>Respond Faster!</font></div></div>' + prompt_text_list,
timing_stim: 1000, //1000
timing_response: 2000, //2000
timing_feedback_duration: 500,
show_stim_with_feedback: false,
timing_post_trial: 0,
on_finish: appendData,
prompt: prompt_text_list,
fixation_default: true
}
refreshTrials.push(refresh_fixation_block)
refreshTrials.push(refresh_block)
}
var refreshCount = 0
var refreshNode = {
timeline: refreshTrials,
loop_function: function(data) {
var sum_rt = 0
var sum_responses = 0
var correct = 0
var total_trials = 0
var mismatch_press = 0
for (var i = 0; i < data.length; i++){
if (data[i].trial_id == "practice_trial"){
total_trials+=1
if (data[i].rt != -1){
sum_rt += data[i].rt
sum_responses += 1
if (data[i].key_press == data[i].correct_response){
correct += 1
}
}
if (data[i].key_press == possible_responses[1][1]){
mismatch_press += 1
}
}
}
var accuracy = correct / total_trials
var missed_responses = (total_trials - sum_responses) / total_trials
var ave_rt = sum_rt / sum_responses
var mismatch_press_percentage = mismatch_press / total_trials
refresh_feedback_text = "<br><p class = instruct-text>Please take this time to read your feedback and to take a short break." // Press enter to continue"
if (accuracy < accuracy_thresh){
if (missed_responses > missed_thresh){
refresh_feedback_text +=
'</p><p class = instruct-text>You have not been responding to some trials. Please respond on every trial that requires a response.'
}
if (ave_rt > rt_thresh){
refresh_feedback_text +=
'</p><p class = instruct-text>You have been responding too slowly.'
}
if (mismatch_press_percentage >= 0.90){
refresh_feedback_text +=
'</p><p class = instruct-text>Please do not simply press your "'+possible_responses[1][0]+'" to every stimulus. Please try to identify the matches and press your "'+possible_responses[0][0]+'" when they occur.'
}
}
refresh_feedback_text +='</p><p class = instruct-text>Done with this practice. The test session will begin shortly.'
delay = delays.pop()
stims = createTrialTypes(numTrialsPerBlock, delay)
feedback_text = "Your delay for this block is "+delay+". Please match the current letter to the letter that appeared "+delay+" trial(s) ago."
return false
}
}
var testTrials = []
testTrials.push(feedback_block)
for (i = 0; i < numTrialsPerBlock + 2; i++) { //was changed from + 3 as delays went from 1:3 to 1:2
var test_block = {
type: 'poldrack-single-stim',
stimulus: getStim,
is_html: true,
data: {
"trial_id": "test_trial",
},
choices: [possible_responses[0][1],possible_responses[1][1]],
timing_stim: 1000, //1000
timing_response: 2000, //2000
timing_post_trial: 0,
response_ends_trial: false,
on_finish: appendData,
fixation_default: true
}
testTrials.push(ITI_fixation_block)
testTrials.push(test_block)
}
var testCount = 0
var testNode = {
timeline: testTrials,
loop_function: function(data) {
testCount += 1
current_trial = 0
var sum_rt = 0
var sum_responses = 0
var correct = 0
var total_trials = 0
var mismatch_press = 0
for (var i = 0; i < data.length; i++){
if (data[i].trial_id == "test_trial"){
total_trials+=1
if (data[i].rt != -1){
sum_rt += data[i].rt
sum_responses += 1
if (data[i].key_press == data[i].correct_response){
correct += 1
}
}
if (data[i].key_press == possible_responses[1][1]){
mismatch_press += 1
}
}
}
var accuracy = correct / total_trials
var missed_responses = (total_trials - sum_responses) / total_trials
var ave_rt = sum_rt / sum_responses
var mismatch_press_percentage = mismatch_press / total_trials
feedback_text = "<br><p class = instruct-text>Please take this time to read your feedback and to take a short break!"
feedback_text += "</p><p class = instruct-text>You have completed: "+testCount+" out of "+numTestBlocks+" blocks of trials."
if (accuracy < accuracy_thresh){
feedback_text +=
'</p><p class = instruct-text>Your accuracy is too low. Remember: <br>' + prompt_text_list
}
if (missed_responses > missed_thresh){
feedback_text +=
'</p><p class = instruct-text>You have not been responding to some trials. Please respond on every trial that requires a response.'
}
if (ave_rt > rt_thresh){
feedback_text +=
'</p><p class = instruct-text>You have been responding too slowly.'
}
if (mismatch_press_percentage >= 0.90){
feedback_text +=
'</p><p class = instruct-text>Please do not simply press your "'+possible_responses[1][0]+'" to every stimulus. Please try to identify the matches and press your "'+possible_responses[0][0]+'" when they occur.'
}
if (testCount == numTestBlocks){
feedback_text +=
'</p><p class = instruct-text>Done with this test.<br> If you have been completing tasks continuously for an hour or more, please take a 15-minute break before starting again.'
return false
} else {
delay = delays.pop()
stims = createTrialTypes(numTrialsPerBlock, delay)
feedback_text += "</p><p class = instruct-text><i>For the next round of trials, your delay is "+delay+"</i>."
return true
}
}
}
/* ************************************ */
/* Set up Experiment */
/* ************************************ */
var n_back_single_task_network__fmri_experiment = []
test_keys(n_back_single_task_network__fmri_experiment, [possible_responses[0][1],possible_responses[1][1]])
//in scanner practice
n_back_single_task_network__fmri_experiment.push(refreshNode);
n_back_single_task_network__fmri_experiment.push(refresh_feedback_block);
//in scanner test
cni_bore_setup(n_back_single_task_network__fmri_experiment)
n_back_single_task_network__fmri_experiment.push(testNode);
n_back_single_task_network__fmri_experiment.push(feedback_block);
n_back_single_task_network__fmri_experiment.push(end_block);