-
Notifications
You must be signed in to change notification settings - Fork 0
/
Keys.js
615 lines (544 loc) · 19.2 KB
/
Keys.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
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
/*************
* Keys Test *
*************/
import { core, data, sound, util, visual, hardware } from './lib/psychojs-2022.2.2.js';
const { PsychoJS } = core;
const { TrialHandler, MultiStairHandler } = data;
const { Scheduler } = util;
//some handy aliases as in the psychopy scripts;
const { abs, sin, cos, PI: pi, sqrt } = Math;
const { round } = util;
// store info about the experiment session:
let expName = 'Keys'; // from the Builder filename that created this script
let expInfo = {
'participant': `${util.pad(Number.parseFloat(util.randint(0, 999999)).toFixed(0), 6)}`,
'session': '001',
};
// Start code blocks for 'Before Experiment'
// init psychoJS:
const psychoJS = new PsychoJS({
debug: true
});
// open window:
psychoJS.openWindow({
fullscr: true,
color: new util.Color([(- 1.0), (- 1.0), (- 1.0)]),
units: 'norm',
waitBlanking: true
});
// schedule the experiment:
psychoJS.schedule(psychoJS.gui.DlgFromDict({
dictionary: expInfo,
title: expName
}));
const flowScheduler = new Scheduler(psychoJS);
const dialogCancelScheduler = new Scheduler(psychoJS);
psychoJS.scheduleCondition(function() { return (psychoJS.gui.dialogComponent.button === 'OK'); }, flowScheduler, dialogCancelScheduler);
// flowScheduler gets run if the participants presses OK
flowScheduler.add(updateInfo); // add timeStamp
flowScheduler.add(experimentInit);
const tr_loopLoopScheduler = new Scheduler(psychoJS);
flowScheduler.add(tr_loopLoopBegin(tr_loopLoopScheduler));
flowScheduler.add(tr_loopLoopScheduler);
flowScheduler.add(tr_loopLoopEnd);
flowScheduler.add(quitPsychoJS, '', true);
// quit if user presses Cancel in dialog box:
dialogCancelScheduler.add(quitPsychoJS, '', false);
psychoJS.start({
expName: expName,
expInfo: expInfo,
resources: [
]
});
psychoJS.experimentLogger.setLevel(core.Logger.ServerLevel.DEBUG);
var currentLoop;
var frameDur;
async function updateInfo() {
currentLoop = psychoJS.experiment; // right now there are no loops
expInfo['date'] = util.MonotonicClock.getDateStr(); // add a simple timestamp
expInfo['expName'] = expName;
expInfo['psychopyVersion'] = '2022.2.2';
expInfo['OS'] = window.navigator.platform;
psychoJS.experiment.dataFileName = (("." + "/") + `data/${expInfo["participant"]}_${expName}_${expInfo["date"]}`);
// store frame rate of monitor if we can measure it successfully
expInfo['frameRate'] = psychoJS.window.getActualFrameRate();
if (typeof expInfo['frameRate'] !== 'undefined')
frameDur = 1.0 / Math.round(expInfo['frameRate']);
else
frameDur = 1.0 / 60.0; // couldn't get a reliable measure so guess
// add info from the URL:
util.addInfoFromUrl(expInfo);
return Scheduler.Event.NEXT;
}
var EEGtrialClock;
var Prep;
var Stance;
var Swing;
var Hit;
var Miss;
var End;
var globalClock;
var routineTimer;
async function experimentInit() {
// Initialize components for Routine "EEGtrial"
EEGtrialClock = new util.Clock();
Prep = new visual.ButtonStim({
win: psychoJS.window,
name: 'Prep',
text: 'Prep',
fillColor: 'white',
borderColor: [(- 1.0), (- 1.0), (- 1.0)],
color: 'black',
colorSpace: 'rgb',
pos: [0, 0.6],
letterHeight: 0.05,
size: [1, 0.1]
});
Prep.clock = new util.Clock();
Stance = new visual.ButtonStim({
win: psychoJS.window,
name: 'Stance',
text: 'Stance',
fillColor: 'white',
borderColor: 'black',
color: 'black',
colorSpace: 'rgb',
pos: [0, 0.4],
letterHeight: 0.05,
size: [1, 0.1]
});
Stance.clock = new util.Clock();
Swing = new visual.ButtonStim({
win: psychoJS.window,
name: 'Swing',
text: 'Swing',
fillColor: 'white',
borderColor: 'black',
color: 'black',
colorSpace: 'rgb',
pos: [0, 0.2],
letterHeight: 0.05,
size: [1, 0.1]
});
Swing.clock = new util.Clock();
Hit = new visual.ButtonStim({
win: psychoJS.window,
name: 'Hit',
text: 'Hit',
fillColor: 'green',
borderColor: 'green',
color: 'white',
colorSpace: 'rgb',
pos: [0, (- 0.1)],
letterHeight: 0.05,
size: [1, 0.1]
});
Hit.clock = new util.Clock();
Miss = new visual.ButtonStim({
win: psychoJS.window,
name: 'Miss',
text: 'Miss',
fillColor: 'red',
borderColor: 'red',
color: 'white',
colorSpace: 'rgb',
pos: [0, (- 0.3)],
letterHeight: 0.05,
size: [1, 0.1]
});
Miss.clock = new util.Clock();
End = new visual.ButtonStim({
win: psychoJS.window,
name: 'End',
text: 'End',
fillColor: [0.0039, 0.0039, 0.0039],
borderColor: 'black',
color: 'white',
colorSpace: 'rgb',
pos: [0, (- 0.5)],
letterHeight: 0.05,
size: [1, 0.1]
});
End.clock = new util.Clock();
// Create some handy timers
globalClock = new util.Clock(); // to track the time since experiment started
routineTimer = new util.CountdownTimer(); // to track time remaining of each (non-slip) routine
return Scheduler.Event.NEXT;
}
var tr_loop;
function tr_loopLoopBegin(tr_loopLoopScheduler, snapshot) {
return async function() {
TrialHandler.fromSnapshot(snapshot); // update internal variables (.thisN etc) of the loop
// set up handler to look after randomisation of conditions etc
tr_loop = new TrialHandler({
psychoJS: psychoJS,
nReps: 40, method: TrialHandler.Method.SEQUENTIAL,
extraInfo: expInfo, originPath: undefined,
trialList: undefined,
seed: undefined, name: 'tr_loop'
});
psychoJS.experiment.addLoop(tr_loop); // add the loop to the experiment
currentLoop = tr_loop; // we're now the current loop
// Schedule all the trials in the trialList:
for (const thisTr_loop of tr_loop) {
snapshot = tr_loop.getSnapshot();
tr_loopLoopScheduler.add(importConditions(snapshot));
tr_loopLoopScheduler.add(EEGtrialRoutineBegin(snapshot));
tr_loopLoopScheduler.add(EEGtrialRoutineEachFrame());
tr_loopLoopScheduler.add(EEGtrialRoutineEnd(snapshot));
tr_loopLoopScheduler.add(tr_loopLoopEndIteration(tr_loopLoopScheduler, snapshot));
}
return Scheduler.Event.NEXT;
}
}
async function tr_loopLoopEnd() {
// terminate loop
psychoJS.experiment.removeLoop(tr_loop);
// update the current loop from the ExperimentHandler
if (psychoJS.experiment._unfinishedLoops.length>0)
currentLoop = psychoJS.experiment._unfinishedLoops.at(-1);
else
currentLoop = psychoJS.experiment; // so we use addData from the experiment
return Scheduler.Event.NEXT;
}
function tr_loopLoopEndIteration(scheduler, snapshot) {
// ------Prepare for next entry------
return async function () {
if (typeof snapshot !== 'undefined') {
// ------Check if user ended loop early------
if (snapshot.finished) {
// Check for and save orphaned data
if (psychoJS.experiment.isEntryEmpty()) {
psychoJS.experiment.nextEntry(snapshot);
}
scheduler.stop();
} else {
psychoJS.experiment.nextEntry(snapshot);
}
return Scheduler.Event.NEXT;
}
};
}
var t;
var frameN;
var continueRoutine;
var EEGtrialComponents;
function EEGtrialRoutineBegin(snapshot) {
return async function () {
TrialHandler.fromSnapshot(snapshot); // ensure that .thisN vals are up to date
//--- Prepare to start Routine 'EEGtrial' ---
t = 0;
EEGtrialClock.reset(); // clock
frameN = -1;
continueRoutine = true; // until we're told otherwise
// update component parameters for each repeat
// keep track of which components have finished
EEGtrialComponents = [];
EEGtrialComponents.push(Prep);
EEGtrialComponents.push(Stance);
EEGtrialComponents.push(Swing);
EEGtrialComponents.push(Hit);
EEGtrialComponents.push(Miss);
EEGtrialComponents.push(End);
for (const thisComponent of EEGtrialComponents)
if ('status' in thisComponent)
thisComponent.status = PsychoJS.Status.NOT_STARTED;
return Scheduler.Event.NEXT;
}
}
var endExpNow;
function EEGtrialRoutineEachFrame() {
return async function () {
//--- Loop for each frame of Routine 'EEGtrial' ---
// get current time
t = EEGtrialClock.getTime();
frameN = frameN + 1;// number of completed frames (so 0 is the first frame)
// update/draw components on each frame
// *Prep* updates
if (t >= 0 && Prep.status === PsychoJS.Status.NOT_STARTED) {
// keep track of start time/frame for later
Prep.tStart = t; // (not accounting for frame time here)
Prep.frameNStart = frameN; // exact frame index
Prep.setAutoDraw(true);
}
if (Prep.status === PsychoJS.Status.STARTED) {
// check whether Prep has been pressed
if (Prep.isClicked) {
if (!Prep.wasClicked) {
// store time of first click
Prep.timesOn.push(Prep.clock.getTime());
Prep.numClicks += 1;
// store time clicked until
Prep.timesOff.push(Prep.clock.getTime());
} else {
// update time clicked until;
Prep.timesOff[Prep.timesOff.length - 1] = Prep.clock.getTime();
}
if (!Prep.wasClicked) {
expInfo["PrepTime"] = core.getAbsTime();
}
// if Prep is still clicked next frame, it is not a new click
Prep.wasClicked = true;
} else {
// if Prep is clicked next frame, it is a new click
Prep.wasClicked = false;
}
} else {
// keep clock at 0 if Prep hasn't started / has finished
Prep.clock.reset();
// if Prep is clicked next frame, it is a new click
Prep.wasClicked = false;
}
// *Stance* updates
if (t >= 0 && Stance.status === PsychoJS.Status.NOT_STARTED) {
// keep track of start time/frame for later
Stance.tStart = t; // (not accounting for frame time here)
Stance.frameNStart = frameN; // exact frame index
Stance.setAutoDraw(true);
}
if (Stance.status === PsychoJS.Status.STARTED) {
// check whether Stance has been pressed
if (Stance.isClicked) {
if (!Stance.wasClicked) {
// store time of first click
Stance.timesOn.push(Stance.clock.getTime());
Stance.numClicks += 1;
// store time clicked until
Stance.timesOff.push(Stance.clock.getTime());
} else {
// update time clicked until;
Stance.timesOff[Stance.timesOff.length - 1] = Stance.clock.getTime();
}
if (!Stance.wasClicked) {
expInfo["StanceTime"] = core.getAbsTime();
}
// if Stance is still clicked next frame, it is not a new click
Stance.wasClicked = true;
} else {
// if Stance is clicked next frame, it is a new click
Stance.wasClicked = false;
}
} else {
// keep clock at 0 if Stance hasn't started / has finished
Stance.clock.reset();
// if Stance is clicked next frame, it is a new click
Stance.wasClicked = false;
}
// *Swing* updates
if (t >= 0 && Swing.status === PsychoJS.Status.NOT_STARTED) {
// keep track of start time/frame for later
Swing.tStart = t; // (not accounting for frame time here)
Swing.frameNStart = frameN; // exact frame index
Swing.setAutoDraw(true);
}
if (Swing.status === PsychoJS.Status.STARTED) {
// check whether Swing has been pressed
if (Swing.isClicked) {
if (!Swing.wasClicked) {
// store time of first click
Swing.timesOn.push(Swing.clock.getTime());
Swing.numClicks += 1;
// store time clicked until
Swing.timesOff.push(Swing.clock.getTime());
} else {
// update time clicked until;
Swing.timesOff[Swing.timesOff.length - 1] = Swing.clock.getTime();
}
if (!Swing.wasClicked) {
expInfo["SwingTime"] = core.getAbsTime();
}
// if Swing is still clicked next frame, it is not a new click
Swing.wasClicked = true;
} else {
// if Swing is clicked next frame, it is a new click
Swing.wasClicked = false;
}
} else {
// keep clock at 0 if Swing hasn't started / has finished
Swing.clock.reset();
// if Swing is clicked next frame, it is a new click
Swing.wasClicked = false;
}
// *Hit* updates
if (t >= 0 && Hit.status === PsychoJS.Status.NOT_STARTED) {
// keep track of start time/frame for later
Hit.tStart = t; // (not accounting for frame time here)
Hit.frameNStart = frameN; // exact frame index
Hit.setAutoDraw(true);
}
if (Hit.status === PsychoJS.Status.STARTED) {
// check whether Hit has been pressed
if (Hit.isClicked) {
if (!Hit.wasClicked) {
// store time of first click
Hit.timesOn.push(Hit.clock.getTime());
Hit.numClicks += 1;
// store time clicked until
Hit.timesOff.push(Hit.clock.getTime());
} else {
// update time clicked until;
Hit.timesOff[Hit.timesOff.length - 1] = Hit.clock.getTime();
}
if (!Hit.wasClicked) {
// end routine when Hit is clicked
continueRoutine = false;
expInfo["EvType"] = "Hit";
}
// if Hit is still clicked next frame, it is not a new click
Hit.wasClicked = true;
} else {
// if Hit is clicked next frame, it is a new click
Hit.wasClicked = false;
}
} else {
// keep clock at 0 if Hit hasn't started / has finished
Hit.clock.reset();
// if Hit is clicked next frame, it is a new click
Hit.wasClicked = false;
}
// *Miss* updates
if (t >= 0 && Miss.status === PsychoJS.Status.NOT_STARTED) {
// keep track of start time/frame for later
Miss.tStart = t; // (not accounting for frame time here)
Miss.frameNStart = frameN; // exact frame index
Miss.setAutoDraw(true);
}
if (Miss.status === PsychoJS.Status.STARTED) {
// check whether Miss has been pressed
if (Miss.isClicked) {
if (!Miss.wasClicked) {
// store time of first click
Miss.timesOn.push(Miss.clock.getTime());
Miss.numClicks += 1;
// store time clicked until
Miss.timesOff.push(Miss.clock.getTime());
} else {
// update time clicked until;
Miss.timesOff[Miss.timesOff.length - 1] = Miss.clock.getTime();
}
if (!Miss.wasClicked) {
// end routine when Miss is clicked
continueRoutine = false;
expInfo["EvType"] = "Miss";
}
// if Miss is still clicked next frame, it is not a new click
Miss.wasClicked = true;
} else {
// if Miss is clicked next frame, it is a new click
Miss.wasClicked = false;
}
} else {
// keep clock at 0 if Miss hasn't started / has finished
Miss.clock.reset();
// if Miss is clicked next frame, it is a new click
Miss.wasClicked = false;
}
// *End* updates
if (t >= 0 && End.status === PsychoJS.Status.NOT_STARTED) {
// keep track of start time/frame for later
End.tStart = t; // (not accounting for frame time here)
End.frameNStart = frameN; // exact frame index
End.setAutoDraw(true);
}
if (End.status === PsychoJS.Status.STARTED) {
// check whether End has been pressed
if (End.isClicked) {
if (!End.wasClicked) {
// store time of first click
End.timesOn.push(End.clock.getTime());
End.numClicks += 1;
// store time clicked until
End.timesOff.push(End.clock.getTime());
} else {
// update time clicked until;
End.timesOff[End.timesOff.length - 1] = End.clock.getTime();
}
if (!End.wasClicked) {
// end routine when End is clicked
continueRoutine = false;
endExpNow = true;
}
// if End is still clicked next frame, it is not a new click
End.wasClicked = true;
} else {
// if End is clicked next frame, it is a new click
End.wasClicked = false;
}
} else {
// keep clock at 0 if End hasn't started / has finished
End.clock.reset();
// if End is clicked next frame, it is a new click
End.wasClicked = false;
}
// check for quit (typically the Esc key)
if (psychoJS.experiment.experimentEnded || psychoJS.eventManager.getKeys({keyList:['escape']}).length > 0) {
return quitPsychoJS('The [Escape] key was pressed. Goodbye!', false);
}
// check if the Routine should terminate
if (!continueRoutine) { // a component has requested a forced-end of Routine
return Scheduler.Event.NEXT;
}
continueRoutine = false; // reverts to True if at least one component still running
for (const thisComponent of EEGtrialComponents)
if ('status' in thisComponent && thisComponent.status !== PsychoJS.Status.FINISHED) {
continueRoutine = true;
break;
}
// refresh the screen if continuing
if (continueRoutine) {
return Scheduler.Event.FLIP_REPEAT;
} else {
return Scheduler.Event.NEXT;
}
};
}
function EEGtrialRoutineEnd(snapshot) {
return async function () {
//--- Ending Routine 'EEGtrial' ---
for (const thisComponent of EEGtrialComponents) {
if (typeof thisComponent.setAutoDraw === 'function') {
thisComponent.setAutoDraw(false);
}
}
psychoJS.experiment.addData('Prep.numClicks', Prep.numClicks);
psychoJS.experiment.addData('Prep.timesOn', Prep.timesOn);
psychoJS.experiment.addData('Prep.timesOff', Prep.timesOff);
psychoJS.experiment.addData('Stance.numClicks', Stance.numClicks);
psychoJS.experiment.addData('Stance.timesOn', Stance.timesOn);
psychoJS.experiment.addData('Stance.timesOff', Stance.timesOff);
psychoJS.experiment.addData('Swing.numClicks', Swing.numClicks);
psychoJS.experiment.addData('Swing.timesOn', Swing.timesOn);
psychoJS.experiment.addData('Swing.timesOff', Swing.timesOff);
psychoJS.experiment.addData('Hit.numClicks', Hit.numClicks);
psychoJS.experiment.addData('Hit.timesOn', Hit.timesOn);
psychoJS.experiment.addData('Hit.timesOff', Hit.timesOff);
psychoJS.experiment.addData('Miss.numClicks', Miss.numClicks);
psychoJS.experiment.addData('Miss.timesOn', Miss.timesOn);
psychoJS.experiment.addData('Miss.timesOff', Miss.timesOff);
psychoJS.experiment.addData('End.numClicks', End.numClicks);
psychoJS.experiment.addData('End.timesOn', End.timesOn);
psychoJS.experiment.addData('End.timesOff', End.timesOff);
// the Routine "EEGtrial" was not non-slip safe, so reset the non-slip timer
routineTimer.reset();
// Routines running outside a loop should always advance the datafile row
if (currentLoop === psychoJS.experiment) {
psychoJS.experiment.nextEntry(snapshot);
}
return Scheduler.Event.NEXT;
}
}
function importConditions(currentLoop) {
return async function () {
psychoJS.importAttributes(currentLoop.getCurrentTrial());
return Scheduler.Event.NEXT;
};
}
async function quitPsychoJS(message, isCompleted) {
// Check for and save orphaned data
if (psychoJS.experiment.isEntryEmpty()) {
psychoJS.experiment.nextEntry();
}
psychoJS.window.close();
psychoJS.quit({message: message, isCompleted: isCompleted});
return Scheduler.Event.QUIT;
}