-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattribute_amnesia_task.js
More file actions
348 lines (289 loc) · 10.2 KB
/
attribute_amnesia_task.js
File metadata and controls
348 lines (289 loc) · 10.2 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
/**
*
* attribute_amnesia_task.js
* William Xiang Quan Ngiam
*
* demo of the attribute amnesia task
*
* started coding: 3/6/25
**/
/* Initialize jsPsych */
var jsPsych = initJsPsych({
});
timeline = [];
// DEFINE EXPERIMENT VARIABLES
const n_trials = 16; // Number of trials in the experiment
const n_items = 4; // Number of items in the screen
const canvas_width = 1000; // Width of the canvas
const canvas_height = 600; // Height of the canvas
// EXPERIMENT STIMULI
// Define an array of letters and an array of numbers
var letters = ['A', 'B', 'C', 'D', 'F', 'H', 'J', 'K', 'L', 'N', 'P', 'R', 'T', 'V', 'X', 'Y']; // Based on Chen and Wyble (2016)
const numbers = ['2', '3', '4', '5', '6', '7', '8', '9'] // Based on Chen and Wyble (2016)
const colors = ['red', 'orange', 'blue', 'magenta']
// Set item locations
const item_locations = [
[canvas_width / 2 - 200, canvas_height / 2 - 200],
[canvas_width / 2 + 200, canvas_height / 2 - 200],
[canvas_width / 2 - 200, canvas_height / 2 + 200],
[canvas_width / 2 + 200, canvas_height / 2 + 200]
]
// Set response cue locations
const cue_locations = [
[canvas_width / 2 - 150, canvas_height / 2],
[canvas_width / 2 - 50, canvas_height / 2],
[canvas_width / 2 + 50, canvas_height / 2],
[canvas_width / 2 + 150, canvas_height / 2]
]
// BUILD EXPERIMENT VARIABLES
// BUILD EXPERIMENT
/* force fullscreen */
var enter_fullscreen = {
type: jsPsychFullscreen,
fullscreen_mode: true
}
timeline.push(enter_fullscreen);
/* define welcome message trial */
var welcome = {
type: jsPsychHtmlButtonResponse,
stimulus: `
<p>Welcome to the demo.</p>
<br>
`,
choices: [">>"],
button_html: ['<button class="jspsych-btn">%choice%</button>']
};
timeline.push(welcome);
/* show task instructions */
var instructions_1 = {
type: jsPsychHtmlButtonResponse,
stimulus: `
<p>On each trial, you will see three coloured numbers and one coloured letter.</p>
<p>Your goal is to remember the colour of the <b>letter</b>.</p>
`,
choices: ['>>'],
button_html: ['<button class="jspsych-btn">%choice%</button>']
};
timeline.push(instructions_1);
var instructions_2 = {
type: jsPsychHtmlButtonResponse,
stimulus: `
<p>When prompted for a response, press the corresponding number to indicate the colour of the letter.</p>
<p>Press the button below when you are ready.</p>
`,
choices: ['>>'],
button_html: ['<button class="jspsych-btn">%choice%</button>']
};
timeline.push(instructions_2);
// TRIAL LOOP
for (var t = 0; t < n_trials; t++) {
// Set up trial
var trial_time = function () {
dur = jsPsych.randomization.randomInt(800, 1800)
return dur
}
var trial_start = {
type: jsPsychCanvasKeyboardResponse,
canvas_size: [canvas_height, canvas_width],
stimulus: draw_fixation,
choices: "NO_KEYS",
trial_duration: trial_time
}
function draw_fixation(c) {
var ctx = c.getContext("2d")
// Add fixation cross
ctx.font = "50px Arial"
ctx.fillStyle = 'black'
ctx.fillText('+', canvas_width / 2, canvas_height / 2)
for (var i = 0; i < n_items; i++) {
ctx.beginPath();
ctx.arc(item_locations[i][0]+20, item_locations[i][1]-20, 20, 0, 2 * Math.PI);
ctx.stroke();
}
}
timeline.push(trial_start)
var trial_stim = {
type: jsPsychCanvasKeyboardResponse,
canvas_size: [canvas_height, canvas_width],
stimulus: draw_trial,
choices: "NO_KEYS",
trial_duration: 250,
on_finish: function (data) {
if (jsPsych.pluginAPI.compareKeys(data.response, null)) {
data.trial_letter = trial_letter
} else (
jsPsych.endCurrentTimeline()
)
}
}
function draw_trial(c) {
trial_letter = jsPsych.randomization.sampleWithoutReplacement(letters, 1);
var trial_numbers = jsPsych.randomization.sampleWithoutReplacement(numbers, 3);
var trial_stim = trial_letter.concat(trial_numbers)
var shuffled_stim = jsPsych.randomization.repeat(trial_stim, 1);
var shuffle_colors = jsPsych.randomization.repeat(colors, 1)
var ctx = c.getContext("2d");
ctx.font = "50px Arial"
for (var i = 0; i < n_items; i++) {
ctx.fillStyle = shuffle_colors[i]
ctx.fillText(shuffled_stim[i], item_locations[i][0], item_locations[i][1])
}
// Add fixation cross
ctx.fillStyle = 'black'
ctx.fillText('+', canvas_width / 2, canvas_height / 2)
}
timeline.push(trial_stim);
var trial_mask = {
type: jsPsychCanvasKeyboardResponse,
canvas_size: [canvas_height, canvas_width],
stimulus: draw_mask,
choices: "NO_KEYS",
trial_duration: 100
}
function draw_mask(c) {
var ctx = c.getContext("2d");
ctx.font = "50px Arial"
for (var i = 0; i < n_items; i++) {
ctx.fillStyle = "black"
ctx.fillText("@", item_locations[i][0], item_locations[i][1])
ctx.fillStyle = "red";
ctx.fillRect(item_locations[i][0], item_locations[i][1] - 50, 10, 50);
ctx.fillStyle = "orange";
ctx.fillRect(item_locations[i][0], item_locations[i][1] - 50, 50, 10);
ctx.fillStyle = "blue";
ctx.fillRect(item_locations[i][0] + 50, item_locations[i][1] - 50, 10, 50);
ctx.fillStyle = "yellow";
ctx.fillRect(item_locations[i][0], item_locations[i][1], 50, 10);
}
}
timeline.push(trial_mask);
var blank = {
type: jsPsychHtmlKeyboardResponse,
stimulus: '',
choices: "NO_KEYS",
trial_duration: 400,
on_finish: function (data) {
var trial_letter = []
trial_letter = jsPsych.data.getLastTrialData().values()[0].trial_letter
data.trial_letter = trial_letter
},
}
timeline.push(blank)
var response = {
type: jsPsychCanvasKeyboardResponse,
canvas_size: [canvas_height, canvas_width],
stimulus: draw_response,
choices: ['1', '2', '3', '4'],
prompt: "Which colour was the letter?"
}
function draw_response(c) {
var ctx = c.getContext("2d")
// Red square
ctx.fillStyle = "red";
ctx.fillRect(cue_locations[0][0], cue_locations[0][1], 50, 50)
ctx.font = "40px Arial"
ctx.fillStyle = "white"
ctx.fillText("1", cue_locations[0][0] + 15, cue_locations[0][1] + 40)
// Yellow square
ctx.fillStyle = "orange";
ctx.fillRect(cue_locations[1][0], cue_locations[1][1], 50, 50)
ctx.fillStyle = "white"
ctx.fillText("2", cue_locations[1][0] + 15, cue_locations[1][1] + 40)
// Blue Square
ctx.fillStyle = "blue";
ctx.fillRect(cue_locations[2][0], cue_locations[2][1], 50, 50)
ctx.fillStyle = "white";
ctx.fillText("3", cue_locations[2][0] + 15, cue_locations[2][1] + 40)
// Magenta Square
ctx.fillStyle = "magenta";
ctx.fillRect(cue_locations[3][0], cue_locations[3][1], 50, 50)
ctx.fillStyle = "white";
ctx.fillText("4", cue_locations[3][0] + 15, cue_locations[3][1] + 40)
}
if (t != n_trials - 1) {
timeline.push(response)
}
var ITI = {
type: jsPsychHtmlKeyboardResponse,
stimulus: '',
choices: "NO_KEYS",
trial_duration: 400,
on_finish: function (data) {
var trial_letter = []
trial_letter = jsPsych.data.getLastTrialData().values()[0].trial_letter
data.trial_letter = trial_letter
},
}
timeline.push(ITI)
// SURPRISE TRIAL!
var surprise_trial = {
type: jsPsychCanvasKeyboardResponse,
canvas_size: [canvas_height, canvas_width],
stimulus: draw_surprise,
prompt: "This is a surprise memory test! Here, we test the identity of the target letter. Press a corresponding number to indicate the 'identity' of the target letter.",
choices: ['5', '6', '7', '8'],
on_finish: function (data) {
response = Number(data.response)+4
correct = correct_letter == shuffled_surprise_letters[response - 1]
data.correct = correct
}
}
function draw_surprise(c) {
correct_letter = trial_letter
let remaining_letters = letters.filter(x => !correct_letter.includes(x))
other_letters = jsPsych.randomization.sampleWithoutReplacement(remaining_letters, 3)
surprise_letters = correct_letter.concat(other_letters)
shuffled_surprise_letters = jsPsych.randomization.repeat(surprise_letters, 1)
var ctx = c.getContext("2d")
for (var i = 0; i < 4; i++) {
ctx.font = "40px Arial"
ctx.fillStyle = "black";
ctx.fillText(shuffled_surprise_letters[i], canvas_width / 2 + ((i - 1.5) * 100), canvas_height / 2)
ctx.fillText(i + 5, canvas_width / 2 + ((i - 1.5) * 100), canvas_height / 2 + 100)
}
}
if (t == n_trials - 1) {
timeline.push(surprise_trial)
timeline.push(response)
}
}
var surprise_feedback = {
type: jsPsychHtmlButtonResponse,
stimulus: write_feedback,
choices: ['>>'],
button_html: ['<button class="jspsych-btn">%choice%</button>']
}
function write_feedback() {
if (correct) {
return "You got that right! You did not experience attribute amnesia."
} else {
return "You got that wrong! You experienced attribute amnesia!"
}
}
timeline.push(surprise_feedback)
/* upload data */
//var save_data = {
// type: jsPsychPipe,
// action: "save",
// experiment_id: expID,
// filename: fileID + `.csv`,
// wait_message: "<p>Saving data. Please do not close this page.</p>",
// data_string: ()=>jsPsych.data.get().csv()
//};
//
//timeline.push(save_data)
/* end of experiment */
var end_experiment = {
type: jsPsychHtmlButtonResponse,
stimulus: '<p>Thank you for trying the demo!</p>',
choices: ["FINISH"],
button_html: ['<button class="jspsych-btn">%choice%</button>']
}
timeline.push(end_experiment)
var close_fullscreen = {
type: jsPsychFullscreen,
fullscreen_mode: false
}
timeline.push(close_fullscreen)
// Run the Task
jsPsych.run(timeline)