-
Notifications
You must be signed in to change notification settings - Fork 0
/
experiment.html
441 lines (382 loc) · 16.7 KB
/
experiment.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Buchstabenpaare</title>
<script src="jspsych-6.2.0/jspsych.js"></script>
<!-- CSS -->
<link href="jspsych-6.2.0/css/jspsych.css" rel="stylesheet" type="text/css">
<!-- PLugins -->
<script src="jspsych-6.2.0/plugins/jspsych-html-keyboard-response.js"></script>
<script src="jspsych-6.2.0/plugins/jspsych-survey-html-form.js"></script>
<script src="jspsych-6.2.0/plugins/jspsych-instructions.js"></script>
<script src="jspsych-6.2.0/plugins/jspsych-fullscreen.js"></script>
<!--Include jQuery library-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<!--load custom helper scripts-->
<script src="helper.js"></script>
<script src="csv-formatter.js"></script> <!--JSON2CSV-->
<!--CSS-->
<link href="styles.css" rel="stylesheet" type="text/css">
</head>
<body id="experiment-body"></body>
<script>
let SUBJECT_ID, SUPERVISOR_ID;
const ALPHABET = [...'ABCDEFGHIJKLMNOPQRSTUVWXYZ']
const PHP_SCRIPT_FILE = 'write_data.php' // SAVING DATA PHP-SCRIPT
const CUE = "A"
const TARGET_KEY = "f"
const PROBE = "X"
const NON_TARGET_KEY = "j"
const EXCLUDED_CUE = [CUE, PROBE, "K", "Y"]
const EXCLUDED_PROBE = [CUE, PROBE, "K", "Y"] // alles, außer Y und K, A und X
const NON_CUE = ALPHABET.filter((c) => !EXCLUDED_CUE.includes(c))
const NON_PROBE = ALPHABET.filter((c) => !EXCLUDED_PROBE.includes(c))
const SHORT_CONDITION_TIME = 1000
const LONG_CONDITION_TIME = 5000
// delay between A-X 1000ms X-A 1000ms
// delay between A-X 5000ms X-A 1000ms
const MAX_ANSWER_TIME = 1500
const STIMULUS_PRESENTATION_TIME = 500
const DISTRACTION_INTERFERENCE = false // this is not yet implemented
const DELAY = SHORT_CONDITION_TIME // in seconds, long delay: 5(!)-10s
const CUE_PROBE_FONTSIZE = "60px"
const PENALITY_FONTSIZE = "60px"
const FEEDBACK_FONTSIZE = "60px"
const VISUAL_FEEDBACK_COLOR = "#BFBFBF"
const BACKGROUND_COLOR = "#7f7f7f"//"#686868"
const NO_ANSWER_GIVEN = "-"
const TRIALS_PER_BLOCK = 10 // * 2 => Per Condition
const TRIALS_PER_PRACTICE = 10 // Per Condition
const EXPERIMENT_STRUCTURE = {A: SHORT_CONDITION_TIME, B: LONG_CONDITION_TIME} // delays given
const FULLSCREEN_START_MESSAGE = (
`
<p style="font-size:36px;"><strong>Herzlich willkommen zur Buchstabenaufgabe</strong></p>
<p style="font-size:26px; line-height: 34px;">Drücken Sie auf "Weiter", um in den Vollbildmodus<br>
zu wechseln und mit dem Experiment zu beginnen.</p>
`
)
const INSTRUCTION_SLIDE = (
`
Im Folgenden werden Ihnen Buchstaben präsentiert.<br>
Wenn auf ein <strong>A</strong> als nächster Buchstabe ein <strong>X</strong> folgt,<br>
drücken Sie bitte die Taste <strong>f</strong>, ansonsten die Tasten <strong>j</strong>.<br>
Sie haben 1,5 Sekunden Zeit für Ihre Antwort.
`
)
// IMAGES
const IMG_FOLDER = "./img/"
const WELCOME_MSG = IMG_FOLDER + "01welcome.png"
const INSTRUCTIONS_1 = IMG_FOLDER + "02instructions.png"
const INSTRUCTIONS_2 = IMG_FOLDER + "03instructions.png" // 04 not needed anymore
const POST_PRACTICE_A = IMG_FOLDER + "04post-practice-A.png"
const START_EXPERIMENT_A = IMG_FOLDER + "05start-exp-A.png"
const START_PRACTICE_B = IMG_FOLDER + "06start-practice-B.png" // post first exp
const START_EXPERIMENT_B = IMG_FOLDER + "07start-exp-B.png"
const FINISHED_MSG = IMG_FOLDER + "08finished.png"
const ALL_IMAGES = [
WELCOME_MSG, INSTRUCTIONS_1, INSTRUCTIONS_2, POST_PRACTICE_A, START_PRACTICE_B,
START_EXPERIMENT_A, START_EXPERIMENT_B, FINISHED_MSG
]
// both are incremented before storing => starting at 1
let trialIndex = 0 // continous
let trialBlock = 0 // starts after practice at 1
let lastTrialSet = "" // used to track when block number has changed
const INTERTRIAL_DELAY = 1000
// UTILITY FUNCS
function getRandomElement(array) {
return jsPsych.randomization.sampleWithoutReplacement(array, 1)[0]
}
function changeBackgroundColor(colorString) {
$("#jspsych-target").css("background-color", colorString)
$("#experiment-body").css("background-color", colorString)
}
function generateTrialList(n) {
const AMOUNT_TARGET = n * 0.7 // AX
const AMOUNT_NON_CUE_PROBE = n * 0.1 // BX
const AMOUNT_CUE_NON_PROBE = n * 0.1 // AY
const AMOUNT_NON_CUE_NON_PROBE = n * 0.1 // BY
const STANDARD_ARRAY = jsPsych.randomization.repeat(
["AX", "BX", "AY", "BY"],
[AMOUNT_TARGET, AMOUNT_NON_CUE_PROBE, AMOUNT_NON_CUE_PROBE, AMOUNT_NON_CUE_NON_PROBE],
false) // generate the conditions with standard values
return STANDARD_ARRAY.map((condition) => {
const RANDOM_NON_CUE = getRandomElement(NON_CUE)
const RANDOM_NON_PROBE = getRandomElement(NON_PROBE)
return { // turn standard array with mapping into destandardized array (random non cues/probes)
cue: condition[0]
.replace("B", RANDOM_NON_CUE),
probe: condition[1]
.replace("Y", RANDOM_NON_PROBE),
condition,
isTarget: condition === "AX",
}
})
}
function generateTimelineVariables(n, delay, isPractice=false, currentSet=1) {
if (n % 10 != 0) {
console.log("Warning, not a multiple of ten, filling up to multiple of ten")
n += 10 - (n % 10) // fill up to ten
}
const TRIAL_LIST = generateTrialList(n)
return TRIAL_LIST.map((condition) => {
return {
cue: condition.cue,
probe: condition.probe,
condition: condition.condition,
isTarget: condition.isTarget,
isPractice,
currentSet: isPractice ? "-" : currentSet, // keep track of current block
delay
}
})
}
function answeredLastTrial() {
const LAST_DATA = jsPsych.data.get().last().values()[0]
return LAST_DATA.rt != null
}
function answeredLastTrialCorrect() {
return jsPsych.data.get().filter({relevant: true}).last(1).values()[0].correct
}
function getDelayTrial(delayTime) {
return {
type: "html-keyboard-response",
stimulus: "",
choices: jsPsych.NO_KEYS,
trial_duration: delayTime,
}
}
// DATA SAVING FUNCS
function processData(data) {
const formattedData = []
const date = getDate("-");
for (let trialData of data) {
formattedData.push({
subcode: SUBJECT_ID,
supervisor: SUPERVISOR_ID,
date,
time: trialData.time,
timeelap: trialData.time_elapsed,
phase: trialData.phase,
trialNr: trialData.trialIndex,
trialBlock: trialData.trialBlock,
isTarget: trialData.isTarget,
condition: trialData.condition,
delay: trialData.delay,
cue: trialData.cue,
probe: trialData.probe,
answer: trialData.answer,
correct: trialData.correct,
rt: roundTo(trialData.rt, 2),
})
}
return formattedData
}
function saveData(name, data){
let xhr = new XMLHttpRequest();
xhr.open('POST', PHP_SCRIPT_FILE); // PHP_SCRIPT_FILE
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({filename: name, filedata: data}));
console.log("saved data to: data/" + name);
return true;
}
const finishUp = () => {
const fileName = `AXCPT_${getDate("")}_${getTime("")}_${SUBJECT_ID}`
const relevantData = jsPsych.data.get().filter({relevant: true}).values()
saveData(fileName, JSON2CSV(processData(relevantData)))
}
// TRIALS
const ID_INPUT = {
type: 'survey-html-form',
html: /*html*/`
<p>Eingabemaske:</p>
<div class="survey-form">
<p style="margin: 0">Probanden ID:</p> <input name="subjectid" type="text" />
<p style="margin: 0">Versuchsleiter ID:</p><input name="supervisorid" type="text" />
</div>
`,
button_label: 'Weiter',
on_finish: (data) => {
const parsed = JSON.parse(data.responses);
SUBJECT_ID = parsed.subjectid;
SUPERVISOR_ID = parsed.supervisorid;
}
};
const ENTER_FULLSCREEN = {
type: "fullscreen",
message: FULLSCREEN_START_MESSAGE,
button_label: "Weiter",
fullscreen_mode: true,
delay_after: 0,
}
const EXIT_FULLSCREEN = {
type: "fullscreen",
fullscreen_mode: false, // false will cause to leave fullscreen
delay_after: 0,
on_start: () => {
showCursor()
},
}
const AFTER_PRACTICE = {
type: "instructions",
pages: [
"Die Übung ist nun vorbei.<br>Wenn Sie eine Eingabe getätigt haben,<br>erhälten Sie visuelles Feedback in Form eines Flackerns.",
"Drücken Sie auf \"Weiter\", um mit dem Experiment zu beginnen.",
],
show_clickable_nav: true,
button_label_next: "Weiter",
allow_backward: false,
on_start: () => {
showCursor()
},
on_finish: () => {
hideCursor()
}
}
const CUE_PRESENTATION = {
type: "html-keyboard-response",
stimulus: () => {
return `<span style="font-size:${CUE_PROBE_FONTSIZE}">${jsPsych.timelineVariable("cue", true)}</span>`
},
trial_duration: STIMULUS_PRESENTATION_TIME,
choices: jsPsych.NO_KEYS,
}
const PROBE_PRESENTATION = {
type: "html-keyboard-response",
stimulus: () => {
return `<span style="font-size:${CUE_PROBE_FONTSIZE}">${jsPsych.timelineVariable("probe", true)}</span>`
},
trial_duration: MAX_ANSWER_TIME,
response_ends_trial: false,
choices: [TARGET_KEY, NON_TARGET_KEY],
on_finish: (data) => {
data.phase = jsPsych.timelineVariable("isPractice", true) ? "practice" : "experiment"
data.time = getTime()
data.trialIndex = ++trialIndex // count continuously
// reset trial Block?
const currentTrialSet = jsPsych.timelineVariable("currentSet", true)
if (currentTrialSet !== lastTrialSet) {
trialBlock = 0
lastTrialSet = currentTrialSet // update
}
data.trialBlock = ++trialBlock // count each block, reset in AFTER_PRACTICE
data.trialSet = currentTrialSet
data.cue = jsPsych.timelineVariable("cue", true)
data.probe = jsPsych.timelineVariable("probe", true)
data.condition = jsPsych.timelineVariable("condition", true)
data.isTarget = jsPsych.timelineVariable("isTarget", true)
if (data.rt !== null) {
const answer = jsPsych.pluginAPI.convertKeyCodeToKeyCharacter(data.key_press)
data.answer = answer
const correctAnswer = data.isTarget ? TARGET_KEY : NON_TARGET_KEY
data.correct = answer === correctAnswer
} else {
// TODO: should we do this? data.rt = MAX_ANSWER_TIME // time took to answer
data.answer = NO_ANSWER_GIVEN
data.correct = NO_ANSWER_GIVEN
}
data.relevant = true // to filter for these trials
data.delay = jsPsych.timelineVariable("delay", true);
}
}
const PENALITY_AND_VISUAL = {
type: "html-keyboard-response",
stimulus: () => {
return answeredLastTrial() ? "" : `<span style="font-size:${PENALITY_FONTSIZE}">ZU LANGSAM!</span>`
},
on_start: (data) => {
const hasAnswered = answeredLastTrial()
data.trial_duration = hasAnswered ? 1 : 2000 // 10 => 1
if (hasAnswered) {
// changeBackgroundColor(VISUAL_FEEDBACK_COLOR)
}
},
trial_duration: 1,
choices: jsPsych.NO_KEYS,
on_finish: () => {
changeBackgroundColor(BACKGROUND_COLOR)
}
}
const FEEDBACK = { // used for practicing
type: "html-keyboard-response",
stimulus: () => {
if (!answeredLastTrial()) {
return `<span style="font-size:${PENALITY_FONTSIZE}">ZU LANGSAM!</span>`
} else if (answeredLastTrialCorrect()) {
return `<span style="font-size:${PENALITY_FONTSIZE};color: lightgreen">RICHTIG</span>`
} else {
return `<span style="font-size:${PENALITY_FONTSIZE};color: darkred">FALSCH</span>`
}
},
trial_duration: 1000,
choices: jsPsych.NO_KEYS,
}
const END = {
type: "html-keyboard-response",
stimulus: `<div class="imgbox">
<img class="center-fit" src='${FINISHED_MSG}'>
</div>`,
reponse_ends_trial: false,
on_start: () => {
finishUp() // save data
}
}
// STRUCTURE
function generateFullExperimentTrials(trialAmount, delayCondition=SHORT_CONDITION_TIME, isPractice=false) {
const AFTER_PROBE_TRIAL = isPractice ? FEEDBACK : PENALITY_AND_VISUAL
return {
timeline: [CUE_PRESENTATION, getDelayTrial(delayCondition), PROBE_PRESENTATION, AFTER_PROBE_TRIAL, getDelayTrial(INTERTRIAL_DELAY)],
timeline_variables: generateTimelineVariables(trialAmount, delayCondition, isPractice=isPractice),
}
}
// generate trials according to structure of experiment
const A_1 = generateFullExperimentTrials(TRIALS_PER_BLOCK, EXPERIMENT_STRUCTURE.A, isPractice=false)
const A_2 = generateFullExperimentTrials(TRIALS_PER_BLOCK, EXPERIMENT_STRUCTURE.A, isPractice=false)
const B_1 = generateFullExperimentTrials(TRIALS_PER_BLOCK, EXPERIMENT_STRUCTURE.B, isPractice=false)
const B_2 = generateFullExperimentTrials(TRIALS_PER_BLOCK, EXPERIMENT_STRUCTURE.B, isPractice=false)
// insert: 2 practices, pauses (updates)
const A_PRACTICE = generateFullExperimentTrials(TRIALS_PER_PRACTICE, EXPERIMENT_STRUCTURE.A, isPractice=true)
const B_PRACTICE = generateFullExperimentTrials(TRIALS_PER_PRACTICE, EXPERIMENT_STRUCTURE.B, isPractice=true)
// placeholder
function generatePagesFromImages(images) {
let _images = images
if (typeof images === "string") {
_images = [images]
}
return _images.map(img => (`<div class="imgbox">
<img class="center-fit" src='${img}'>
</div>`))
}
function generateInstructionTrials(pages) {
return {
type: "instructions",
pages: pages,
show_clickable_nav: false,
// button_label_next: "Weiter",
key_forward: "space",
allow_backward: false,
post_trial_gap: 1000, // delay
on_start: () => {
showCursor()
},
on_finish: () => {
hideCursor()
}
}
}
function getPauseTrial(completedParts) {
return generateInstructionTrials([`<progress value="${completedParts}" max="4">70 %</progress><br><br>Wie wäre es mit einer kleinen Pause?<br>Drücken Sie die <em>Leertaste</em>, um fortzufahren.`])
}
const INSTRUCTIONS = generateInstructionTrials(generatePagesFromImages( [INSTRUCTIONS_1, INSTRUCTIONS_2]))
const AFTER_PRACTICE_A = generateInstructionTrials(generatePagesFromImages( [POST_PRACTICE_A, START_EXPERIMENT_A]))
const INSTRUCTIONS_B = generateInstructionTrials(generatePagesFromImages( START_PRACTICE_B))
const AFTER_PRACTICE_B = generateInstructionTrials(generatePagesFromImages( START_EXPERIMENT_B))
const TIMELINE_ALL_TRIALS = [A_PRACTICE, AFTER_PRACTICE_A, A_1, getPauseTrial(1), A_2, getPauseTrial(2), INSTRUCTIONS_B, B_PRACTICE, AFTER_PRACTICE_B, B_1, getPauseTrial(3), B_2]
jsPsych.init({
timeline: [ID_INPUT, ENTER_FULLSCREEN, INSTRUCTIONS, ...TIMELINE_ALL_TRIALS, EXIT_FULLSCREEN, END],
preload_images: ALL_IMAGES,
})
</script>
</html>