-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsequential_faster_loading_funcs.js
270 lines (233 loc) · 8.64 KB
/
sequential_faster_loading_funcs.js
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
/* UsefulFunctions
------------------------------------------ */
function getRandomElement (list){
return list[Math.floor(Math.random()*list.length)];
}
function getRandomInt(min, max) {
return Math.floor(Math.random()*(max - min + 1) + min);
}
// function shuffle(a) {
// for (let i = a.length - 1; i > 0; i--) {
// const j = Math.floor(Math.random() * (i + 1));
// [a[i], a[j]] = [a[j], a[i]];
// }
// return a;
// }
function getRandom(arr, n) {
var result = new Array(n),
len = arr.length,
taken = new Array(len);
if (n > len)
throw new RangeError("getRandom: more elements taken than available");
while (n--) {
var x = Math.floor(Math.random() * len);
result[n] = arr[x in taken ? taken[x] : x];
taken[x] = --len in taken ? taken[len] : len;
}
return result;
}
function getFixationTime (){ //get randomized time of fixation by randomly choosing from 0.4, 0.5 and 0.6s
fixationTime = getRandomElement([400, 500, 600]);
return fixationTime;
}
function make_stim(stim_array,face){
var html_stim_array = []
for (p of stim_array){
html_stim_array.push("<div class='"+face+"' style='background-position:" + (stim_array[p]*100) + "% 0%'></div>")
}
html_stim_array.join();
return html_stim_array
}
function array_length(){
var length = getRandomInt(1,12)
return length
}
/* SAVE FUNC
------------------------------------------ */
const cognitoIdentityPool = "us-east-1:0f699842-4091-432f-8b93-a2d4b7bb5f20";
const DATA_BUCKET = "task-data-raw";
const DIRECTORY = "stim-sim-faces-production";
function saveDataToS3() {
id = faceData.subject_id
d = {
"curTime": jsPsych.startTime(),
"userAgent": navigator.userAgent,
"windowWidth": window.innerWidth,
"windowHeight": window.innerHeight,
"screenWidth": screen.width,
"screenHeight": screen.height,
"totalTime": jsPsych.totalTime(),
"interactionData": jsPsych.data.getInteractionData().json(),
"trialStruct": jsPsych.data.get().json(),
};
d = JSON.stringify(d);
AWS.config.update({
region: "us-east-1",
credentials: new AWS.CognitoIdentityCredentials({
IdentityPoolId: cognitoIdentityPool
}),
});
const filename = `${DIRECTORY}/${id}.json`;
const bucket = new AWS.S3({
params: { Bucket: DATA_BUCKET },
apiVersion: "2006-03-01",
})
const objParams = {
Key: filename,
Body: d
}
bucket.putObject(objParams, function(err, data) {
if (err) {
console.log("Error: ", err.message);
} else {
console.log("Data: ", data);
}
});
}
/* SCALE SHENANIGANS
------------------------------------------ */
function getScale (){ //generate the rating scale depending on the person and valence randomly chosen in faceArray
var scale = [];
if (faceData.valence == "Happy"){
var add_me = 50;
}
else{
var add_me = 100;
}
for(i = 1; i < 51; i++){
scale.push('img/'+faceData.person+(add_me+i) + '.jpg')}
return scale;
}
function morphedScale (){ //generate the rating scale depending on the person and valence randomly chosen in faceArray
// defining a few helper functions
function nrange(size, startAt = 0) {
return [...Array(size).keys()].map(i => i + startAt);
};
function fillArray(value, len) {
var arr = [];
for (var i = 0; i < len; i++) {
arr.push(value);
}
return arr;
};
function generateSlices(vWidth, nSlices){
var step = vWidth*0.6/(nSlices-2);
var stepArray = fillArray(step,nSlices-2)
stepArray.unshift(0.2*vWidth)
stepArray.push(0.2*vWidth)
var bounds = [];
for (var i = 0; i < nSlices; i++) {
if (i==0){
bounds.push([(i*stepArray[i]), (i*stepArray[i])+stepArray[i]])
} else if (i>0 && i!=nSlices-1) {
bounds.push([(i*stepArray[i]+0.2*vWidth), (i*stepArray[i])+stepArray[i]+0.2*vWidth])
} else{
bounds.push([(vWidth-0.2*vWidth), vWidth])
}
};
return bounds;
};
// start trial timer
var startTime = (new Date()).getTime();
// get trial data
var trialData = jsPsych.currentTrial();
// remove the picture scales and the slider
$('.jspsych-image-slider-response_noButton-container').remove();
$('img').remove();
var imgScale = getScale();
// drop the first element of the img scale
//imgScale = imgScale.slice(1,imgScale.length) // why?????
// derive the letter of the image filenames
var imgBase = imgScale[0].split('img/')[1].split('.jpg')[0].replace(/\d+/g, '');
// split the number of the image filenames off from the rest of it
var nScale = [];
for (var i = 0; i < imgScale.length-1; i++) {
var n = imgScale[i].split('img/')[1].split('.jpg')[0].split(imgBase)[1];
nScale.push(n);
};
// calculate the element width, and slice it up into sections
var vWidth = $(document).width();
var nSlices = nScale.length;
var slices = generateSlices(vWidth, nSlices);
// setting up iniital vertical line to start the mousemove functionality
var vHeight = $(document).height()-8;
var lineSlice = vWidth / 10;
var vertLine = `<div style="border-left:black;border-style:solid;margin-left:${lineSlice}px; height:${vHeight}px;width:0px;position:absolute;" id="vertLine"></div>`;
var linePrompt = `<div id="linePrompt"><div style="font-size:50px;position:absolute;margin-left:${lineSlice*1.3}px;margin-top:${vHeight/2}px"></div><div style="position:absolute;margin-left:${lineSlice*1.2}px;margin-top:${vHeight/2}px;z-index:5;">Move mouse left of the line to begin</div></div>`
$(".jspsych-content-wrapper").prepend(vertLine);
$(".jspsych-content-wrapper").prepend(linePrompt);
// hide prompt until the trial is begun
$('#jspsych-content > p').css("visibility", "hidden");
// initialize the central image with the most neutral one (i.e. from
// the middle of the scale) and set the image to be blurred
var initialImage = imgScale[0] ; //imgScale[Math.floor(imgScale.length/2)];
$('#jspsych-image-slider-response_noButton-stimulus').append(`<img src="${initialImage}" style="filter:blur(4px);visibility:hidden;" id="changeable-image"/>`);
// workaround with a global variable
window.__imageMouseOver = {
lineSlice: lineSlice,
slices: slices,
nScale: nScale,
imgBase: imgBase,
startTime: startTime,
trialData: trialData,
sliceSelected: 0,
};
var __listenerBools = {};
// define mousemove event listener that changes image
function changeImg(event){
var mouseX = Math.floor(event.pageX);
for (var i = 0; i < __imageMouseOver.slices.length; i++) {
// if mouse X position is within the bounds of the X axis slice,
// change the image to one that is indexed to that slice
if (mouseX >= __imageMouseOver.slices[i][0] && mouseX <= __imageMouseOver.slices[i][1]) {
// capture which slice is selected
__imageMouseOver.sliceSelected = i;
// update img src to the picture that corresponds to that slice
$('#changeable-image').attr('src', `img/${__imageMouseOver.imgBase}${__imageMouseOver.nScale[i]}.jpg`);
}
};
};
// define the click listener that ends trial
function clickHandler(event){
if (__listenerBools['mousemove']) {
// derive trial data
var trialData = __imageMouseOver.trialData;
var end_time = (new Date()).getTime();
var rt = end_time - __imageMouseOver.startTime;
trialData['rt'] = rt;
trialData['stimulus_duration'] = rt;
trialData['trial_duration'] = rt;
trialData['imageSelected'] = `${__imageMouseOver.imgBase}${__imageMouseOver.nScale[__imageMouseOver.sliceSelected]}.jpg`
trialData['indexSelected'] = __imageMouseOver.sliceSelected;
// turn off listeners
$(document).off('mousemove');
$(document).off('click');
// clean up variable namespaces
delete window.__imageMouseOver
delete __listenerBools;
// finish the trial with trial data
jsPsych.finishTrial(trialData);
};
};
function verticalLineInit(event){
var mouseX = Math.floor(event.pageX);
if (mouseX <= __imageMouseOver.lineSlice) {
$("#vertLine").remove();
$("#linePrompt").remove();
$("#jspsych-image-slider-response_noButton-stimulus > img").css({
"filter":"blur(0px)",
"visibility": "visible",
});
$('#jspsych-content > p').css("visibility", "visible");
__listenerBools['mousemove'] = true;
// turn off THIS mouse move listener
$(document).off("mousemove");
// turn on the mouse move listener that changes the image
$(document).mousemove(changeImg);
// add mouse click listener
$(document).on('click', clickHandler);
};
};
// turn on the vertical line mouse move listener
$(document).mousemove(verticalLineInit);
}