-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordExperiment.html
431 lines (393 loc) · 15.6 KB
/
wordExperiment.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
<!DOCTYPE html>
<html>
<head>
<title>My experiment</title>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="https://unpkg.com/jspsych@6.2.0"></script>
<script src="plugins/jspsych-html-keyboard-response.js"></script>
<link href="https://unpkg.com/jspsych@6.2.0/css/jspsych.css" rel="stylesheet" type="text/css">
<script src="plugins/jspsych-fullscreen.js"></script>
<script src="plugins/jspsych-image-keyboard-response.js"></script>
<script src="plugins/jspsych-html-slider-response.js"></script>
<script src="plugins/jspsych-html-button-response.js"></script>
<script src="plugins/jspsych-survey-text.js"></script>
<script src="plugins/jspsych-instructions.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
.graybox {
border: 2px solid white;
background: white;
padding-top: 20px;
padding-bottom: 20px;
padding-left: 50px;
padding-right: 50px;
width: 80%;
margin: 0 auto;
margin-bottom: 20px;
font-family: sans-serif;
font-size: smaller;
text-align: left;
}
* {
box-sizing: border-box;
}
.box {
float: left;
width: 50%;
padding: 20px;
outline-style: solid;
outline-color: rgb(172, 186, 228);
margin-left: 32vw;
outline-width: 3px;
}
h1 {
text-align: center;
text-transform: uppercase;
font-size: 60px;
}
p {
font-size: 22px;
}
psmol {
font-size: 18px;
}
h2 {
text-align: center;
text-transform: uppercase;
}
table,
td {
padding: 10px
}
.clearfix::after {
content: "";
clear: both;
display: table;
}
</style>
</head>
<body>
<div id="choice" class="clearfix">
<div class="box" style="background-color:#ffffff; width: 30vw">
<h2>Yes ↑ </h2>
</div>
<div class="box" style="background-color:#ffffff; width: 30vw">
<h2>No ↓</h2>
</div>
</div>
<div id="choiceup" class="graybox">
<div class="box" style="background-color: rgb(172, 186, 228); width: 30vw">
<h2>Yes ↑ </h2>
</div>
<div class="box" style="background-color:#ffffff; width: 30vw">
<h2>No ↓</h2>
</div>
</div>
<div id="choicedown" class="clearfix">
<div class="box" style="background-color:#ffffff; width: 30vw">
<h2>Yes ↑ </h2>
</div>
<div class="box" style="background-color:rgb(172, 186, 228); width: 30vw">
<h2>No ↓</h2>
</div>
</div>
</body>
<script type=module>
var saveLocally = 0; // 0: Nothing. 1: Download CSV file
var displayData = 0; // 0: Nothing. 1: Display data on browser
var launchOnline = 1; // 0: Nothing. 1: Save to database, consent form, completion code generation, dummy variables created for all 3 IDs
var tableName = 'My experiment';
var welcome = {
type: "html-keyboard-response",
stimulus: "Welcome to the experiment. Press any key to begin."
};
var instructions = {
type: 'instructions',
pages: [
'<p>In this experiment, you will be shown 31 words, one after another.</p> ' +
'<p>Try your best to remember these words.</p> ' +
'<p>Press the right arrow key to continue.</p> ',
'<p>You will then be shown more words and asked whether they appeared in the original group.</p>',
"<p>On every trial, there is a 50/50 chance of the words being old or new.</p>",
"<p>Then rate your confidence in your choice on a scale from <strong> not at all confident (press 1)</strong> to <strong>completely confident (press 4)</strong>.</p>" +
"<p>Press the right arrow key to start seeing the words.</p>"
],
show_clickable_nav: true,
}
import word_set from "./word_set.json" assert { type: 'json' };
function getRndInteger(min, max, mode) { //get random integer between min and max
if (mode == 0) {
return Math.floor(Math.random() * 2) === 0 ? min : max; // Only min or max
} else if (mode == 1) {
return Math.floor(Math.random() * (max - min + 1)) + min; // anything in between
}
}
function generateImage(word_set, ind, i) {
var pick = ind[i];
var randWordInd = getRndInteger(1, 2, 0);
var word = word_set[pick].word1;
if (randWordInd == 1) {
word = word_set[pick].word2;
}
return word;
}
function generateStimuli(num) {
var stimuli = [];
// Generate 'num' sets of stimuli
for (let i = 0; i < num; i++) {
// Randomize the order of indices for the word set
var shuffledIndices = d3.shuffle(d3.range(word_set.length));
// Iterate over the shuffled indices, generate an image for each index and add it to the stimuli array
shuffledIndices.forEach((index) => {
// Generate an image for each index and add it to the stimuli array
stimuli.push(generateImage(word_set, shuffledIndices, index));
});
}
return stimuli;
}
function generateEncodingTrial(word) {
return {
type: 'html-keyboard-response',
stimulus: '<span style="font-size: 96px;">' + word + '</span>',
data: {
word: word
},
choices: jsPsych.NO_KEYS,
trial_duration: 2500
}
}
function generateTestTrial(word) {
var judgementStim = $("#choice").html();
var wordTest = {
type: 'html-keyboard-response',
stimulus: "<p> Was</p>" + '<span style="font-size: 96px;">' + word + '</span>' + "<p>in the original group of words?</p>" + judgementStim,
choices: ['uparrow', 'downarrow'],
on_finish: function (data) {
// Score the response as correct or incorrect.
if (jsPsych.pluginAPI.compareKeys(data.key_press, 'downarrow')) {
data.side = true;
} else {
data.side = false;
}
}
};
return {
timeline: [
wordTest,
]
}
}
function generateFeedback(word) {
var choiceup = $("#choiceup").html();
var choicedown = $("#choicedown").html();
var feedback = {
type: "html-keyboard-response",
choices: "NO_KEYS",
stimulus: function () {
// The feedback stimulus is a dynamic parameter because we can't know in advance whether
// the stimulus should be 'correct' or 'incorrect'.
// Instead, this function will check the accuracy of the last response and use that information to set
// the stimulus value on each trial.
var last_trial = jsPsych.data.get().last(1).values()[0].side;
if (last_trial) {
return "<p> Was</p>" + '<span style="font-size: 96px;">' + word + '</span>' + "<p>in the original group of words?</p>" + choicedown;
} else {
return "<p> Was</p>" + '<span style="font-size: 96px;">' + word + '</span>' + "<p>in the original group of words?</p>" + choiceup;
}
},
trial_duration: 400,
};
return {
timeline: [
feedback,
]
}
}
var confidence = {
type: 'html-keyboard-response',
stimulus: '<p>How confident do you feel in your choice?</p>' +
'<table id="myTable", padding: 15px >' +
'<tr>' +
'<td>1</td>' +
'<td>2</td>' +
'<td>3</td>' +
'<td>4</td>' +
'</tr>' +
'<tr>' +
'<td> Not at all confident </td>' +
'<td> Slighly confident </td>' +
'<td> Fairly confident </td>' +
'<td> Completely confident </td>' +
'</tr>' +
'</table>',
on_finish: function (data) {
// Score the response as correct or incorrect.
data.confidence = parseInt(data.key_press);
},
choices: ['1', '2', '3', '4'],
}
var feedbackConfidence = {
type: "html-keyboard-response",
choices: "NO_KEYS",
stimulus: function () {
// Get the response from the previous trial
var jspsychdata = jsPsych.data.get();
console.log(jspsychdata);
var last_trial = jspsychdata.last(1).values()[0].confidence;
console.log(jspsychdata.last(1).values());
// Provide feedback based on the response
if (last_trial == 49) {
return '<p>How confident do you feel in your choice?</p>' +
'<table id="myTable" style="padding: 15px;">' +
'<tr>' +
'<td><strong>1</strong></td>' +
'<td>2</td>' +
'<td>3</td>' +
'<td>4</td>' +
'</tr>' +
'<tr>' +
'<td><strong>Not at all confident</strong></td>' +
'<td>Slightly confident</td>' +
'<td>Fairly confident</td>' +
'<td>Completely confident</td>' +
'</tr>' +
'</table>';
} else if (last_trial === 50) {
return '<p>How confident do you feel in your choice?</p>' +
'<table id="myTable" style="padding: 15px;">' +
'<tr>' +
'<td>1</td>' +
'<td><strong>2</strong></td>' +
'<td>3</td>' +
'<td>4</td>' +
'</tr>' +
'<tr>' +
'<td>Not at all confident</td>' +
'<td><strong>Slightly confident</strong></td>' +
'<td>Fairly confident</td>' +
'<td>Completely confident</td>' +
'</tr>' +
'</table>';
} else if (last_trial === 51) {
return '<p>How confident do you feel in your choice?</p>' +
'<table id="myTable" style="padding: 15px;">' +
'<tr>' +
'<td>1</td>' +
'<td>2</td>' +
'<td><strong>3</strong></td>' +
'<td>4</td>' +
'</tr>' +
'<tr>' +
'<td>Not at all confident</td>' +
'<td>Slightly confident</td>' +
'<td><strong>Fairly confident</strong></td>' +
'<td>Completely confident</td>' +
'</tr>' +
'</table>';
} else if (last_trial === 52) {
return '<p>How confident do you feel in your choice?</p>' +
'<table id="myTable" style="padding: 15px;">' +
'<tr>' +
'<td>1</td>' +
'<td>2</td>' +
'<td>3</td>' +
'<td><strong>4</strong></td>' +
'</tr>' +
'<tr>' +
'<td>Not at all confident</td>' +
'<td>Slightly confident</td>' +
'<td>Fairly confident</td>' +
'<td><strong>Completely confident</strong></td>' +
'</tr>' +
'</table>';
}
},
trial_duration: 200,
}
var startTest = {
type: 'instructions',
pages: [
'<p>That was all the words!</p> ' +
'<p>Now you will see more words and asked whether they appeared in the original group.</p>' +
"<p>On every trial, there is a 50/50 chance of the words being old or new.</p>",
"<p>Then rate your confidence in your choice on a scale from <strong> not at all confident (press 1)</strong> to <strong>completely confident (press 4)</strong>.</p>" +
'<p>Press the right arrow key to start seeing the words.</p> ',
],
show_clickable_nav: true,
}
function makeFinal() {
var finalp1 =
"<p style='font-size: 40px'>You're all done!</p>" +
"<p style='font-size: 40px'>Press SUBMIT to ensure you're data is collected.</p>" +
"<p style='font-size: 30px'> Your completion code is C1676TSP </p>";
var final = {
type: 'instructions',
pages: [
finalp1
],
on_finish: function () {
//Set the variable to true so that we submit the HIT
allPhasesCompleted = true;
},
button_label_next: "SUBMIT",
show_clickable_nav: true,
}
return {
timeline: [
final,
]
}
}
var ready = {
type: "html-keyboard-response",
stimulus: '<p style="font-size: 48px;">+</p>',
choices: "NO_KEYS",
trial_duration: 600,
};
// generate stimuli for training
var trainStimuli = generateStimuli(1);
var timeline = [];
timeline.push(welcome);
timeline.push(instructions);
for (let i = 0; i < trainStimuli.length; i++) {
timeline.push(generateEncodingTrial(trainStimuli[i]));
}
timeline.push(startTest); // add start test instructions to timeline
// generate stimuli for test
var testStimuli = generateStimuli(1)
for (let i = 0; i < testStimuli.length; i++) {
timeline.push(generateTestTrial(testStimuli[i]));
timeline.push(generateFeedback(testStimuli[i]));
timeline.push(confidence);
timeline.push(feedbackConfidence);
//timeline.push(ready);
}
jsPsych.init({
timeline: timeline,
on_start: function () {
console.log(timeline)
},
on_finish: function () {
if (saveLocally == true) {
jsPsych.data.get().localSave('csv', 'testSave.csv'); //Save the data locally in a .csv file
}
if (displayData == true) {
jsPsych.data.displayData();
}
if (launchOnline == true) {
save_data(tableName, jsPsych.data.get().csv());
}
}
})
timeline.push(makeFinal());
// get csv representation of data and log to console
function save_data(name, data) {
var xhr = new XMLHttpRequest();
xhr.open('POST', 'write_data.php'); // 'write_data.php' is the path to the php file described above.
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({ filedata: data }));
}
</script>
</html>
</html>