forked from msaddler/jspsych_tutorial_960
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
executable file
·362 lines (316 loc) · 13.7 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<title>Jordan & Nakul's 9.60 Final Project</title>
<meta http-equiv="pragma" content="no-cache">
<link rel="icon" href="data:;base64,iVBORw0KGgo=">
<script src="./jspsych/dist/jspsych.js"></script>
<link href="./jspsych/dist/jspsych.css" rel="stylesheet" type="text/css" />
<link href="override.css" rel="stylesheet" type="text/css" />
<script src="./jspsych/dist/plugin-browser-check.js"></script>
<script src="./jspsych/dist/plugin-preload.js"></script>
<script src="./jspsych/dist/plugin-html-button-response.js"></script>
<script src="./jspsych/dist/plugin-image-button-response.js"></script>
<script src="./jspsych/dist/plugin-survey-text.js"></script>
<script src="./js/webgazer/webgazer.js"></script>
<script src="./jspsych/dist/extension-webgazer.js"></script>
<!-- <script src="https://unpkg.com/@jspsych/[email protected]"></script> -->
<script src='https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js'></script>
<script src="./jspsych/dist/plugin-webgazer-init-camera.js"></script>
<script src="./jspsych/dist/plugin-webgazer-validate.js"></script>
<script src="./jspsych/dist/plugin-webgazer-calibrate.js"></script>
<script src="util.js"></script>
</head>
<body></body>
<script>
hash = function(val){
var hash = 0;
for (var i = 0; i < val.length; i++) {
var code = val.charCodeAt(i);
hash = ((hash<<5)-hash)+code;
hash = hash & hash; // Convert to 32bit integer
}
return hash;
}
const NUM_TRIALS = 20;
// Initialize the jsPsych object (possibly with arguments: https://www.jspsych.org/7.3/reference/jspsych/)
var jsPsych = initJsPsych({
override_safe_mode: true,
show_progress_bar: true,
extensions: [
{
type: jsPsychExtensionWebgazer
}
]
});
var timeline = [];
var post_validation_timeline = [];
var timeline_variables_for_trials = [];
var image_nums = arange(1, 298, 1)
var images_to_preload = []
for (var i = 0; i < NUM_TRIALS; i++) {
var pair = jsPsych.randomization.sampleWithoutReplacement(image_nums, 2)
var factor = {
left_image: `./images/MIT_BENCHMARK_IMAGES/i${pair[0]}.jpg`,
right_image: `./images/MIT_BENCHMARK_IMAGES/i${pair[1]}.jpg`
}
images_to_preload.push(factor['left_image'])
images_to_preload.push(factor['right_image'])
timeline_variables_for_trials.push(factor)
}
console.log(timeline_variables_for_trials)
console.log(images_to_preload)
var timeline_for_trials = [
{
type: jsPsychHtmlButtonResponse,
stimulus: function() {
var left = jsPsych.timelineVariable('left_image')
var right = jsPsych.timelineVariable('right_image')
return `
<div class="left_image">
<img height="300" width="300" src=${left}/>
</div>
<div class="right_image">
<img height="300" width="300" src=${right}/>
</div>
`
},
trial_duration: 15000,
choices: ['Image 1', 'Image 2'],
css_classes: ['separate_images'],
data: {
left_image: jsPsych.timelineVariable('left_image'),
right_image: jsPsych.timelineVariable('right_image')
},
extensions: [
{
type: jsPsychExtensionWebgazer,
params: {targets: ['.left_image', '.right_image', '#jspsych-html-button-response-button-0', '#jspsych-html-button-response-button-1']}
}
]
}
];
// console.log(jsPsych.timelineVariable('left_image'))
// console.log(timeline_for_trials)
var kerb = ''
var url_experiment_dir = 'https://wilke18.scripts.mit.edu/960_final_proj/';
var url_write_data_php = url_experiment_dir + 'write_data.php';
var output_filename = ''
var preload = {
type: jsPsychPreload,
images: images_to_preload
};
timeline.push(preload);
var start_page = {
type: jsPsychSurveyText,
questions: [
{prompt: 'What is your kerb?', required: true, name: 'kerb'}
],
preamble: `
<p>Thank you for helping us with our experiment!</p>
<p>We are trying to investigate what humans are interested in and how well computer models can identify and explain what we might find interesting.</p>
<p>This experiment requires you be on Google Chrome on your computer. We have found that other browsers are unable to support some of the tech.</p>
<p>For posterity, here is our codebase and feel free to reach out with questions: https://github.com/wilke0818/jspsych_tutorial_960</p>
`,
choices: ['Got it'],
on_finish: function() {
kerb = jsPsych.data.getLastTrialData().trials[0].response.kerb
console.log(kerb)
var subject_id = hash(kerb).toString(16);
// console.log(`subject_id from URL: ${subject_id}`);
// if (!subject_id) {
// subject_id = jsPsych.randomization.randomID(kerb.length);
console.log(`subject_id randomly assigned: ${subject_id}`);
// }
output_filename = `data/${subject_id}.json`;
console.log(`Data will be saved to: ${url_experiment_dir + output_filename}`);
return;
}
}
var browser = {
type: jsPsychBrowserCheck,
inclusion_function: (data) => {
return 'chrome' == data.browser && data.mobile === false;
},
minimum_height: 800,
minimum_width: 1400,
exclusion_message: (data) => {
if(data.mobile){
return '<p>You must use a desktop/laptop computer to participate in this experiment.</p>';
} else if(data.browser !== 'chrome'){
return '<p>You must use Chrome as your browser to complete this experiment.</p>'
}
},
window_resize_message: `<p>Your browser window is too small to complete this experiment. Please maximize the size of your browser window.
If your browser window is already maximized, you will not be able to complete this experiment.</p>
<p>The minimum window width is <span id="browser-check-min-width"></span> px.</p>
<p>Your current window width is <span id="browser-check-actual-width"></span> px.</p>
<p>The minimum window height is <span id="browser-check-min-height"></span> px.</p>
<p>Your current window height is <span id="browser-check-actual-height"></span> px.</p>
<p>Try resizing your window or zooming out.</p>`
};
timeline.push(start_page)
timeline.push(browser)
var camera_instructions = {
type: jsPsychHtmlButtonResponse,
stimulus: `
<p>In order to participate you must allow the experiment to use your camera.</p>
<p>You will be prompted to do this on the next screen.</p>
<p>If you do not wish to allow use of your camera, you cannot participate in this experiment.<p>
<p>It may take up to 30 seconds for the camera to initialize after you give permission.</p>
<p>Note, though your camera is being used, no video is being recorded. We are using an eye tracker to better compare human and machines. </p>
`,
choices: ['Got it'],
}
timeline.push(camera_instructions)
var init_camera_trial = {
type: jsPsychWebgazerInitCamera
}
timeline.push(init_camera_trial)
var calibration_instructions = {
type: jsPsychHtmlButtonResponse,
stimulus: `
<p>Now you'll calibrate the eye tracking, so that the software can use the image of your eyes to predict where you are looking.</p>
<p>You'll see a series of dots appear on the screen. Look at each dot and click on it.</p>
`,
choices: ['Got it'],
}
timeline.push(calibration_instructions)
var calibration_trial = {
type: jsPsychWebgazerCalibrate,
calibration_points: [[15,15], [50, 15], [85,15],
[15,50], [50, 50], [85,50],
[15,85], [50, 85], [85,85]],
calibration_mode: 'click',
repetitions_per_point: 2
}
timeline.push(calibration_trial)
var validation_instructions = {
type: jsPsychHtmlButtonResponse,
stimulus: `
<p>Now we'll measure the accuracy of the calibration.</p>
<p>Look at each dot as it appears on the screen.</p>
<p style="font-weight: bold;">You do not need to click on the dots this time.</p>
`,
choices: ['Got it'],
post_trial_gap: 1000
}
timeline.push(validation_instructions)
var validation = {
type: jsPsychWebgazerValidate,
validation_points: [
[25,25],[75,25],[50,50],[25,75],[75,75]
],
roi_radius: 225,
time_to_saccade: 1000,
validation_duration: 2500,
// show_validation_data: true,
data: {
task: 'validate'
}
}
timeline.push(validation)
var recalibrate_instructions = {
type: jsPsychHtmlButtonResponse,
stimulus: `
<p>The accuracy of the calibration is a little lower than we'd like.</p>
<p>Let's try calibrating one more time.</p>
<p>On the next screen, look at the dots and click on them.<p>
`,
choices: ['OK'],
}
var recalibrate = {
timeline: [recalibrate_instructions, calibration_trial, validation_instructions, validation],
conditional_function: function(){
var validation_datas = jsPsych.data.get().filter({task: 'validate'}).values();
var validation_data = validation_datas[validation_datas.length-1]
console.log(jsPsych.data.get().filter({task: 'validate'}).values());
console.log(validation_data);
const average = array => array.reduce((a, b) => a + b) / array.length;
console.log(average(validation_data.percent_in_roi))
return validation_data.percent_in_roi.some(function(x){
var minimum_percent_acceptable = 40;
return x < minimum_percent_acceptable;
}) || average(validation_data.percent_in_roi) < 70;
},
data: {
phase: 'recalibration'
}
}
timeline.push(recalibrate)
// timeline.push(recalibrate)
var calibration_done = {
type: jsPsychHtmlButtonResponse,
stimulus: `
<p>Great, we're done with calibration!</p>
<p>On the next page we'll start the experiment. You'll see ${NUM_TRIALS} pairs of images and for each pair choose the photo you find more interesting.</p>
<p>There are no right or wrong answers. Each pair will be up for 15 seconds but take as little or as much time as you need.</p>
`,
choices: ['Begin']
}
var failed_validation = {
type: jsPsychHtmlButtonResponse, // This trial will use the "html-button-response" plugin
stimulus: [
'<h2>Unfortunately we couldn\'t get our gaze detection working well enough to run the experiment</h2>' +
'<p>Feel free to retry the experiment in a place with possibly better lighting.</p>' +
'<p>We appreciate your efforts! Thanks for trying!</p>'
],
choices: ['End experiment'],
on_finish: function(data){
jsPsych.endExperiment();
}
};
var final_check = {
timeline: [failed_validation],
conditional_function: function(){
var validation_datas = jsPsych.data.get().filter({task: 'validate'}).values();
var validation_data = validation_datas[validation_datas.length-1]
console.log(jsPsych.data.get().filter({task: 'validate'}).values());
console.log(validation_data);
return validation_data.percent_in_roi.some(function(x){
var minimum_percent_acceptable = 40;
return x < minimum_percent_acceptable;
});
},
data: {
phase: 'recalibration'
}
}
timeline.push(final_check);
timeline.push(calibration_done)
var trials = {
timeline: timeline_for_trials,
timeline_variables: timeline_variables_for_trials,
// randomize_order: true,
};
timeline.push(trials);
var last_page = {
type: jsPsychHtmlButtonResponse, // This trial will use the "html-button-response" plugin
stimulus: [
'<h2>Press the button below to end experiment.</h2>' +
'<p>Thank you for participating!</p>' +
'<p>Note your camera might claim to still be on which seems to be a bug of the eyetracker we\'re using. Once you end the experiment exiting out of the tab should fix this.'
],
on_load: function() {
jsPsych.extensions.webgazer.pause();
write_data_to_server();
},
choices: ['End experiment'],
};
timeline.push(last_page);
// The `write_data_to_server` function requires two global variables to be defined:
// `url_write_data_php` (web location of the write_data.php script) and `output_filename`
// var url_experiment_dir = 'https://wilke18.scripts.mit.edu/jspsych_tutorial_960/';
// var url_write_data_php = url_experiment_dir + 'write_data.php';
// var subject_id = kerb;
// console.log(`subject_id from URL: ${subject_id}`);
// if (!subject_id) {
// subject_id = jsPsych.randomization.randomID(10);
// console.log(`subject_id randomly assigned: ${subject_id}`);
// }
// var output_filename = `data/${subject_id}.json`;
// console.log(`Data will be saved to: ${url_experiment_dir + output_filename}`);
// // Run the timeline
jsPsych.run(timeline);
</script>
</html>