forked from marsja/psychomotor_vigilance_task
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPVT.py
828 lines (725 loc) · 34.9 KB
/
PVT.py
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This experiment was created using PsychoPy3 Experiment Builder (v2021.2.3),
on Mon Nov 27 10:53:43 2023
If you publish work using this script the most relevant publication is:
Peirce J, Gray JR, Simpson S, MacAskill M, Höchenberger R, Sogo H, Kastman E, Lindeløv JK. (2019)
PsychoPy2: Experiments in behavior made easy Behav Res 51: 195.
https://doi.org/10.3758/s13428-018-01193-y
Edited by Ingram B.T. Dec 2023
"""
from __future__ import absolute_import, division
from psychopy import locale_setup
from psychopy import prefs
from psychopy import sound, gui, visual, core, data, event, logging, clock, colors
from psychopy.constants import (NOT_STARTED, STARTED, PLAYING, PAUSED,
STOPPED, FINISHED, PRESSED, RELEASED, FOREVER)
import numpy as np # whole numpy lib is available, prepend 'np.'
from numpy import (sin, cos, tan, log, log10, pi, average,
sqrt, std, deg2rad, rad2deg, linspace, asarray)
from numpy.random import random, randint, normal, shuffle, choice as randchoice
import os # handy system and path functions
import sys # to get file system encoding
from psychopy.hardware import keyboard
import json
from datetime import datetime
# Ensure that relative paths start from the same directory as this script
_thisDir = os.path.dirname(os.path.abspath(__file__))
os.chdir(_thisDir)
# Store info about the experiment session
psychopyVersion = '2021.2.3'
expName = 'PVT' # from the Builder filename that created this script
expInfo = {'participant': '', 'session': ''}
dlg = gui.DlgFromDict(dictionary=expInfo, sortKeys=False, title=expName)
if dlg.OK == False:
core.quit() # user pressed cancel
expInfo['date'] = data.getDateStr() # add a simple timestamp
expInfo['expName'] = expName
expInfo['psychopyVersion'] = psychopyVersion
# Data file name stem = absolute path + name; later add .psyexp, .csv, .log, etc
subject_directory = os.path.abspath(os.path.join(_thisDir, "../../")) + os.sep + f'results/sub-{expInfo["participant"]}/ses-{expInfo["session"]}/beh/'
if not os.path.exists(subject_directory):
os.makedirs(subject_directory)
filename = f'sub-{expInfo["participant"]}_ses-{expInfo["session"]}_task-pvt_beh'
output_path = f'{subject_directory}{filename}'
# Create JSON Metadata file
# datetime object containing current date and time
now = datetime.now()
print("now =", now)
# dd/mm/YY H:M:S
dt_string = now.strftime("%Y-%m-%dT%H-%M-%S")
metadata = {'TaskName' : 'Psychomotor Vigilance Task',
'DateTime' : dt_string}
# Serializing json
json_object = json.dumps(metadata, indent=4)
# Writing to sample.json
with open(output_path+'.json', "w") as outfile:
outfile.write(json_object)
# An ExperimentHandler isn't essential but helps with data saving
thisExp = data.ExperimentHandler(name=expName, version='',
extraInfo=expInfo, runtimeInfo=None,
originPath='/Users/u1984094/Documents/submarine/psychomotor_vigilance_task/PVT_lastrun.py',
savePickle=True, saveWideText=True,
dataFileName=output_path)
# save a log file for detail verbose info
logFile = logging.LogFile(output_path + '.log', level=logging.EXP)
logging.console.setLevel(logging.WARNING) # this outputs to the screen, not a file
endExpNow = False # flag for 'escape' or other condition => quit the exp
frameTolerance = 0.001 # how close to onset before 'same' frame
# Start Code - component code to be run after the window creation
# Setup the Window
win = visual.Window(
size=[1536, 864], fullscr=True, screen=0,
winType='pyglet', allowGUI=False, allowStencil=False,
monitor='testMonitor', color='black', colorSpace='rgb',
blendMode='avg', useFBO=True,
units='height')
# store frame rate of monitor if we can measure it
expInfo['frameRate'] = win.getActualFrameRate()
if expInfo['frameRate'] != None:
frameDur = 1.0 / round(expInfo['frameRate'])
else:
frameDur = 1.0 / 60.0 # could not measure, so guess
# Setup eyetracking
ioDevice = ioConfig = ioSession = ioServer = eyetracker = None
# create a default keyboard (e.g. to check for escape)
defaultKeyboard = keyboard.Keyboard()
# Initialize components for Routine "Instructions"
InstructionsClock = core.Clock()
InstructionsText = visual.TextStim(win=win, name='InstructionsText',
text='Welcome!\n\n\nIn this task, you are to press the SPACEBAR as quick as possible after a red counter appears\non screen.\n\n\nStart the task by pressing the SPACEBAR.',
font='Open Sans',
pos=(0, 0), height=0.05, wrapWidth=None, ori=0.0,
color='white', colorSpace='rgb', opacity=None,
languageStyle='LTR',
depth=0.0);
InstrucKey = keyboard.Keyboard()
# Initialize components for Routine "ISI"
ISIClock = core.Clock()
# All the durations are in seconds
# Random ISI between 2 and 10.
minISI = 2
maxISI = 10
# Task duration (10 mins, 600 seconds).
length_of_task = 600
# Feedback duration
feed = 0.5
# A timer
timing = core.Clock()
# Loading the beep sound
#warning_beep = sound.Sound('beep.wav') # THIS BEEP IS THE ISSUE
ISI_text = visual.TextStim(win=win, name='ISI_text',
text=None,
font='Open Sans',
pos=(0, 0), height=0.05, wrapWidth=None, ori=0.0,
color='white', colorSpace='rgb', opacity=None,
languageStyle='LTR',
depth=-1.0);
dontrespond = keyboard.Keyboard()
# Initialize components for Routine "Target"
TargetClock = core.Clock()
Targetstim = visual.TextStim(win=win, name='Targetstim',
text='',
font='Open Sans',
pos=(0, 0), height=0.05, wrapWidth=None, ori=0.0,
color='red', colorSpace='rgb', opacity=None,
languageStyle='LTR',
depth=-1.0);
Response = keyboard.Keyboard()
# Initialize components for Routine "Feedback"
FeedbackClock = core.Clock()
Feedback_text = visual.TextStim(win=win, name='Feedback_text',
text='',
font='Open Sans',
pos=(0, 0), height=0.05, wrapWidth=None, ori=0.0,
color='red', colorSpace='rgb', opacity=None,
languageStyle='LTR',
depth=0.0);
# Initialize components for Routine "End_task"
End_taskClock = core.Clock()
# Initialize components for Routine "The_end"
The_endClock = core.Clock()
Goodbye = visual.TextStim(win=win, name='Goodbye',
text='Thanks you for your participation\n\nPress the SPACE key to end the task',
font='Open Sans',
pos=(0, 0), height=0.05, wrapWidth=None, ori=0.0,
color='white', colorSpace='rgb', opacity=None,
languageStyle='LTR',
depth=0.0);
GoodbyeEnd = keyboard.Keyboard()
# Create some handy timers
globalClock = core.Clock() # to track the time since experiment started
routineTimer = core.CountdownTimer() # to track time remaining of each (non-slip) routine
# ------Prepare to start Routine "Instructions"-------
continueRoutine = True
# update component parameters for each repeat
InstrucKey.keys = []
InstrucKey.rt = []
_InstrucKey_allKeys = []
# keep track of which components have finished
InstructionsComponents = [InstructionsText, InstrucKey]
for thisComponent in InstructionsComponents:
thisComponent.tStart = None
thisComponent.tStop = None
thisComponent.tStartRefresh = None
thisComponent.tStopRefresh = None
if hasattr(thisComponent, 'status'):
thisComponent.status = NOT_STARTED
# reset timers
t = 0
_timeToFirstFrame = win.getFutureFlipTime(clock="now")
InstructionsClock.reset(-_timeToFirstFrame) # t0 is time of first possible flip
frameN = -1
# -------Run Routine "Instructions"-------
win.setMouseVisible(False)
while continueRoutine:
# get current time
t = InstructionsClock.getTime()
tThisFlip = win.getFutureFlipTime(clock=InstructionsClock)
tThisFlipGlobal = win.getFutureFlipTime(clock=None)
frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
# update/draw components on each frame
# *InstructionsText* updates
if InstructionsText.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
# keep track of start time/frame for later
InstructionsText.frameNStart = frameN # exact frame index
InstructionsText.tStart = t # local t and not account for scr refresh
InstructionsText.tStartRefresh = tThisFlipGlobal # on global time
win.timeOnFlip(InstructionsText, 'tStartRefresh') # time at next scr refresh
InstructionsText.setAutoDraw(True)
# *InstrucKey* updates
waitOnFlip = False
if InstrucKey.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
# keep track of start time/frame for later
InstrucKey.frameNStart = frameN # exact frame index
InstrucKey.tStart = t # local t and not account for scr refresh
InstrucKey.tStartRefresh = tThisFlipGlobal # on global time
win.timeOnFlip(InstrucKey, 'tStartRefresh') # time at next scr refresh
InstrucKey.status = STARTED
# keyboard checking is just starting
waitOnFlip = True
win.callOnFlip(InstrucKey.clock.reset) # t=0 on next screen flip
win.callOnFlip(InstrucKey.clearEvents, eventType='keyboard') # clear events on next screen flip
if InstrucKey.status == STARTED and not waitOnFlip:
theseKeys = InstrucKey.getKeys(keyList=['space'], waitRelease=False)
_InstrucKey_allKeys.extend(theseKeys)
if len(_InstrucKey_allKeys):
InstrucKey.keys = _InstrucKey_allKeys[-1].name # just the last key pressed
InstrucKey.rt = _InstrucKey_allKeys[-1].rt
# a response ends the routine
continueRoutine = False
# check for quit (typically the Esc key)
if endExpNow or defaultKeyboard.getKeys(keyList=["escape"]):
core.quit()
# check if all components have finished
if not continueRoutine: # a component has requested a forced-end of Routine
break
continueRoutine = False # will revert to True if at least one component still running
for thisComponent in InstructionsComponents:
if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
continueRoutine = True
break # at least one component has not yet finished
# refresh the screen
if continueRoutine: # don't flip if this routine is over or we'll get a blank screen
win.flip()
# -------Ending Routine "Instructions"-------
for thisComponent in InstructionsComponents:
if hasattr(thisComponent, "setAutoDraw"):
thisComponent.setAutoDraw(False)
thisExp.addData('InstructionsText.started', InstructionsText.tStartRefresh)
thisExp.addData('InstructionsText.stopped', InstructionsText.tStopRefresh)
# check responses
if InstrucKey.keys in ['', [], None]: # No response was made
InstrucKey.keys = None
thisExp.addData('InstrucKey.keys',InstrucKey.keys)
if InstrucKey.keys != None: # we had a response
thisExp.addData('InstrucKey.rt', InstrucKey.rt)
thisExp.addData('InstrucKey.started', InstrucKey.tStartRefresh)
thisExp.addData('InstrucKey.stopped', InstrucKey.tStopRefresh)
thisExp.nextEntry()
# the Routine "Instructions" was not non-slip safe, so reset the non-slip timer
routineTimer.reset()
# set up handler to look after randomisation of conditions etc
PVT_Trials = data.TrialHandler(nReps=120.0, method='random',
extraInfo=expInfo, originPath=-1,
trialList=[None],
seed=None, name='PVT_Trials')
thisExp.addLoop(PVT_Trials) # add the loop to the experiment
thisPVT_Trial = PVT_Trials.trialList[0] # so we can initialise stimuli with some values
# abbreviate parameter names if possible (e.g. rgb = thisPVT_Trial.rgb)
if thisPVT_Trial != None:
for paramName in thisPVT_Trial:
exec('{} = thisPVT_Trial[paramName]'.format(paramName))
for thisPVT_Trial in PVT_Trials:
currentLoop = PVT_Trials
# abbreviate parameter names if possible (e.g. rgb = thisPVT_Trial.rgb)
if thisPVT_Trial != None:
for paramName in thisPVT_Trial:
exec('{} = thisPVT_Trial[paramName]'.format(paramName))
# ------Prepare to start Routine "ISI"-------
continueRoutine = True
# update component parameters for each repeat
# ISI is then set each routine
randISI = random() * (maxISI - minISI) + minISI
# If it is the first trial
if PVT_Trials.thisN == 0:
overall_timer = core.Clock()
realISI = 0
if PVT_Trials.thisN > 0:
# We count the duration of the feedback as part of the ISI
realISI = feed
# A message when participant miss
message = 'You did not hit the button!'
# Adding the ISI so it is saved in the datafile
thisExp.addData('ISI', randISI)
dontrespond.keys = []
dontrespond.rt = []
_dontrespond_allKeys = []
# keep track of which components have finished
ISIComponents = [ISI_text, dontrespond]
for thisComponent in ISIComponents:
thisComponent.tStart = None
thisComponent.tStop = None
thisComponent.tStartRefresh = None
thisComponent.tStopRefresh = None
if hasattr(thisComponent, 'status'):
thisComponent.status = NOT_STARTED
# reset timers
t = 0
_timeToFirstFrame = win.getFutureFlipTime(clock="now")
ISIClock.reset(-_timeToFirstFrame) # t0 is time of first possible flip
frameN = -1
# -------Run Routine "ISI"-------
win.setMouseVisible(False)
while continueRoutine:
# get current time
t = ISIClock.getTime()
tThisFlip = win.getFutureFlipTime(clock=ISIClock)
tThisFlipGlobal = win.getFutureFlipTime(clock=None)
frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
# update/draw components on each frame
keys = dontrespond.getKeys(keyList=['space'], waitRelease=False)
keys = [key.name for key in keys]
# Append True to list if a key is pressed, clear list if not
if "space" in keys:
message = "Too soon!"
continueRoutine = False
# *ISI_text* updates
if ISI_text.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
# keep track of start time/frame for later
ISI_text.frameNStart = frameN # exact frame index
ISI_text.tStart = t # local t and not account for scr refresh
ISI_text.tStartRefresh = tThisFlipGlobal # on global time
win.timeOnFlip(ISI_text, 'tStartRefresh') # time at next scr refresh
ISI_text.setAutoDraw(True)
if ISI_text.status == STARTED:
# is it time to stop? (based on global clock, using actual start)
if tThisFlipGlobal > ISI_text.tStartRefresh + randISI-realISI-frameTolerance:
# keep track of stop time/frame for later
ISI_text.tStop = t # not accounting for scr refresh
ISI_text.frameNStop = frameN # exact frame index
win.timeOnFlip(ISI_text, 'tStopRefresh') # time at next scr refresh
ISI_text.setAutoDraw(False)
# *dontrespond* updates
waitOnFlip = False
if dontrespond.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
# keep track of start time/frame for later
dontrespond.frameNStart = frameN # exact frame index
dontrespond.tStart = t # local t and not account for scr refresh
dontrespond.tStartRefresh = tThisFlipGlobal # on global time
win.timeOnFlip(dontrespond, 'tStartRefresh') # time at next scr refresh
dontrespond.status = STARTED
# keyboard checking is just starting
waitOnFlip = True
win.callOnFlip(dontrespond.clock.reset) # t=0 on next screen flip
win.callOnFlip(dontrespond.clearEvents, eventType='keyboard') # clear events on next screen flip
if dontrespond.status == STARTED:
# is it time to stop? (based on global clock, using actual start)
if tThisFlipGlobal > dontrespond.tStartRefresh + randISI-1-frameTolerance:
# keep track of stop time/frame for later
dontrespond.tStop = t # not accounting for scr refresh
dontrespond.frameNStop = frameN # exact frame index
win.timeOnFlip(dontrespond, 'tStopRefresh') # time at next scr refresh
dontrespond.status = FINISHED
if dontrespond.status == STARTED and not waitOnFlip:
theseKeys = dontrespond.getKeys(keyList=['space'], waitRelease=False)
_dontrespond_allKeys.extend(theseKeys)
if len(_dontrespond_allKeys):
dontrespond.keys = _dontrespond_allKeys[-1].name # just the last key pressed
dontrespond.rt = _dontrespond_allKeys[-1].rt
# a response ends the routine
continueRoutine = False
# check for quit (typically the Esc key)
if endExpNow or defaultKeyboard.getKeys(keyList=["escape"]):
core.quit()
# check if all components have finished
if not continueRoutine: # a component has requested a forced-end of Routine
break
continueRoutine = False # will revert to True if at least one component still running
for thisComponent in ISIComponents:
if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
continueRoutine = True
break # at least one component has not yet finished
# refresh the screen
if continueRoutine: # don't flip if this routine is over or we'll get a blank screen
win.flip()
# -------Ending Routine "ISI"-------
for thisComponent in ISIComponents:
if hasattr(thisComponent, "setAutoDraw"):
thisComponent.setAutoDraw(False)
PVT_Trials.addData('ISI_text.started', ISI_text.tStartRefresh)
PVT_Trials.addData('ISI_text.stopped', ISI_text.tStopRefresh)
# check responses
if dontrespond.keys in ['', [], None]: # No response was made
dontrespond.keys = None
PVT_Trials.addData('dontrespond.keys',dontrespond.keys)
if dontrespond.keys != None: # we had a response
PVT_Trials.addData('dontrespond.rt', dontrespond.rt)
PVT_Trials.addData('dontrespond.started', dontrespond.tStartRefresh)
PVT_Trials.addData('dontrespond.stopped', dontrespond.tStopRefresh)
# the Routine "ISI" was not non-slip safe, so reset the non-slip timer
routineTimer.reset()
# ------Prepare to start Routine "Target"-------
continueRoutine = True
routineTimer.add(30.000000)
# update component parameters for each repeat
# Reset the timer
timing.reset()
# Check for response
if message == 'Too soon!':
# Adding 0 to Accuracy and missing to RTms
thisExp.addData('Accuracy', 0)
thisExp.addData('RTms', np.NAN)
# End the Routine to continue next trial
continueRoutine = False
Response.keys = []
Response.rt = []
_Response_allKeys = []
# keep track of which components have finished
TargetComponents = [Targetstim, Response]
for thisComponent in TargetComponents:
thisComponent.tStart = None
thisComponent.tStop = None
thisComponent.tStartRefresh = None
thisComponent.tStopRefresh = None
if hasattr(thisComponent, 'status'):
thisComponent.status = NOT_STARTED
# reset timers
t = 0
_timeToFirstFrame = win.getFutureFlipTime(clock="now")
TargetClock.reset(-_timeToFirstFrame) # t0 is time of first possible flip
frameN = -1
# -------Run Routine "Target"-------
while continueRoutine and routineTimer.getTime() > 0:
# get current time
t = TargetClock.getTime()
tThisFlip = win.getFutureFlipTime(clock=TargetClock)
tThisFlipGlobal = win.getFutureFlipTime(clock=None)
frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
# update/draw components on each frame
# counter in seconds
time = int(round(timing.getTime(), 3) * 1000)
# *Targetstim* updates
if Targetstim.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
# keep track of start time/frame for later
Targetstim.frameNStart = frameN # exact frame index
Targetstim.tStart = t # local t and not account for scr refresh
Targetstim.tStartRefresh = tThisFlipGlobal # on global time
win.timeOnFlip(Targetstim, 'tStartRefresh') # time at next scr refresh
Targetstim.setAutoDraw(True)
if Targetstim.status == STARTED:
# is it time to stop? (based on global clock, using actual start)
if tThisFlipGlobal > Targetstim.tStartRefresh + 30-frameTolerance:
# keep track of stop time/frame for later
Targetstim.tStop = t # not accounting for scr refresh
Targetstim.frameNStop = frameN # exact frame index
win.timeOnFlip(Targetstim, 'tStopRefresh') # time at next scr refresh
Targetstim.setAutoDraw(False)
if Targetstim.status == STARTED: # only update if drawing
Targetstim.setText(time, log=False)
# *Response* updates
waitOnFlip = False
if Response.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
# keep track of start time/frame for later
Response.frameNStart = frameN # exact frame index
Response.tStart = t # local t and not account for scr refresh
Response.tStartRefresh = tThisFlipGlobal # on global time
win.timeOnFlip(Response, 'tStartRefresh') # time at next scr refresh
Response.status = STARTED
# keyboard checking is just starting
waitOnFlip = True
win.callOnFlip(Response.clock.reset) # t=0 on next screen flip
win.callOnFlip(Response.clearEvents, eventType='keyboard') # clear events on next screen flip
if Response.status == STARTED:
# is it time to stop? (based on global clock, using actual start)
if tThisFlipGlobal > Response.tStartRefresh + 30-frameTolerance:
# keep track of stop time/frame for later
Response.tStop = t # not accounting for scr refresh
Response.frameNStop = frameN # exact frame index
win.timeOnFlip(Response, 'tStopRefresh') # time at next scr refresh
Response.status = FINISHED
if Response.status == STARTED and not waitOnFlip:
theseKeys = Response.getKeys(keyList=['space'], waitRelease=False)
_Response_allKeys.extend(theseKeys)
if len(_Response_allKeys):
Response.keys = _Response_allKeys[-1].name # just the last key pressed
Response.rt = _Response_allKeys[-1].rt
# a response ends the routine
continueRoutine = False
# check for quit (typically the Esc key)
if endExpNow or defaultKeyboard.getKeys(keyList=["escape"]):
core.quit()
# check if all components have finished
if not continueRoutine: # a component has requested a forced-end of Routine
break
continueRoutine = False # will revert to True if at least one component still running
for thisComponent in TargetComponents:
if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
continueRoutine = True
break # at least one component has not yet finished
# refresh the screen
if continueRoutine: # don't flip if this routine is over or we'll get a blank screen
win.flip()
# -------Ending Routine "Target"-------
for thisComponent in TargetComponents:
if hasattr(thisComponent, "setAutoDraw"):
thisComponent.setAutoDraw(False)
if type(Response.rt) is float:
message = str(round(Response.rt * 1000))
thisExp.addData('Accuracy', 1)
thisExp.addData('RTms', Response.rt * 1000)
# PsychoPy is not running the trial for more than 29.991...
if timing.getTime() >= 29.99:
message = 'No response!'
warning_beep.play()
Response.rt = timing.getTime()
thisExp.addData('RTms', np.NAN)
thisExp.addData('Accuracy', 0)
continueRoutine = False
PVT_Trials.addData('Targetstim.started', Targetstim.tStartRefresh)
PVT_Trials.addData('Targetstim.stopped', Targetstim.tStopRefresh)
# check responses
if Response.keys in ['', [], None]: # No response was made
Response.keys = None
PVT_Trials.addData('Response.keys',Response.keys)
if Response.keys != None: # we had a response
PVT_Trials.addData('Response.rt', Response.rt)
PVT_Trials.addData('Response.started', Response.tStartRefresh)
PVT_Trials.addData('Response.stopped', Response.tStopRefresh)
# ------Prepare to start Routine "Feedback"-------
continueRoutine = True
# update component parameters for each repeat
Feedback_text.setText(message)
# keep track of which components have finished
FeedbackComponents = [Feedback_text]
for thisComponent in FeedbackComponents:
thisComponent.tStart = None
thisComponent.tStop = None
thisComponent.tStartRefresh = None
thisComponent.tStopRefresh = None
if hasattr(thisComponent, 'status'):
thisComponent.status = NOT_STARTED
# reset timers
t = 0
_timeToFirstFrame = win.getFutureFlipTime(clock="now")
FeedbackClock.reset(-_timeToFirstFrame) # t0 is time of first possible flip
frameN = -1
# -------Run Routine "Feedback"-------
while continueRoutine:
# get current time
t = FeedbackClock.getTime()
tThisFlip = win.getFutureFlipTime(clock=FeedbackClock)
tThisFlipGlobal = win.getFutureFlipTime(clock=None)
frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
# update/draw components on each frame
# *Feedback_text* updates
if Feedback_text.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
# keep track of start time/frame for later
Feedback_text.frameNStart = frameN # exact frame index
Feedback_text.tStart = t # local t and not account for scr refresh
Feedback_text.tStartRefresh = tThisFlipGlobal # on global time
win.timeOnFlip(Feedback_text, 'tStartRefresh') # time at next scr refresh
Feedback_text.setAutoDraw(True)
if Feedback_text.status == STARTED:
# is it time to stop? (based on global clock, using actual start)
if tThisFlipGlobal > Feedback_text.tStartRefresh + feed-frameTolerance:
# keep track of stop time/frame for later
Feedback_text.tStop = t # not accounting for scr refresh
Feedback_text.frameNStop = frameN # exact frame index
win.timeOnFlip(Feedback_text, 'tStopRefresh') # time at next scr refresh
Feedback_text.setAutoDraw(False)
# check for quit (typically the Esc key)
if endExpNow or defaultKeyboard.getKeys(keyList=["escape"]):
core.quit()
# check if all components have finished
if not continueRoutine: # a component has requested a forced-end of Routine
break
continueRoutine = False # will revert to True if at least one component still running
for thisComponent in FeedbackComponents:
if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
continueRoutine = True
break # at least one component has not yet finished
# refresh the screen
if continueRoutine: # don't flip if this routine is over or we'll get a blank screen
win.flip()
# -------Ending Routine "Feedback"-------
for thisComponent in FeedbackComponents:
if hasattr(thisComponent, "setAutoDraw"):
thisComponent.setAutoDraw(False)
PVT_Trials.addData('Feedback_text.started', Feedback_text.tStartRefresh)
PVT_Trials.addData('Feedback_text.stopped', Feedback_text.tStopRefresh)
# the Routine "Feedback" was not non-slip safe, so reset the non-slip timer
routineTimer.reset()
# ------Prepare to start Routine "End_task"-------
continueRoutine = True
# update component parameters for each repeat
# keep track of which components have finished
End_taskComponents = []
for thisComponent in End_taskComponents:
thisComponent.tStart = None
thisComponent.tStop = None
thisComponent.tStartRefresh = None
thisComponent.tStopRefresh = None
if hasattr(thisComponent, 'status'):
thisComponent.status = NOT_STARTED
# reset timers
t = 0
_timeToFirstFrame = win.getFutureFlipTime(clock="now")
End_taskClock.reset(-_timeToFirstFrame) # t0 is time of first possible flip
frameN = -1
# -------Run Routine "End_task"-------
while continueRoutine:
# get current time
t = End_taskClock.getTime()
tThisFlip = win.getFutureFlipTime(clock=End_taskClock)
tThisFlipGlobal = win.getFutureFlipTime(clock=None)
frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
# update/draw components on each frame
# check for quit (typically the Esc key)
if endExpNow or defaultKeyboard.getKeys(keyList=["escape"]):
core.quit()
# check if all components have finished
if not continueRoutine: # a component has requested a forced-end of Routine
break
continueRoutine = False # will revert to True if at least one component still running
for thisComponent in End_taskComponents:
if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
continueRoutine = True
break # at least one component has not yet finished
# refresh the screen
if continueRoutine: # don't flip if this routine is over or we'll get a blank screen
win.flip()
# -------Ending Routine "End_task"-------
for thisComponent in End_taskComponents:
if hasattr(thisComponent, "setAutoDraw"):
thisComponent.setAutoDraw(False)
# Get the time in the task
time_in_task = overall_timer.getTime()
# If time_in_task corresponds to the duration we set previously we end te task
if time_in_task >= length_of_task:
continueRoutine = False
PVT_Trials.finished = True
# the Routine "End_task" was not non-slip safe, so reset the non-slip timer
routineTimer.reset()
thisExp.nextEntry()
# completed 120.0 repeats of 'PVT_Trials'
# ------Prepare to start Routine "The_end"-------
continueRoutine = True
# update component parameters for each repeat
GoodbyeEnd.keys = []
GoodbyeEnd.rt = []
_GoodbyeEnd_allKeys = []
# keep track of which components have finished
The_endComponents = [Goodbye, GoodbyeEnd]
for thisComponent in The_endComponents:
thisComponent.tStart = None
thisComponent.tStop = None
thisComponent.tStartRefresh = None
thisComponent.tStopRefresh = None
if hasattr(thisComponent, 'status'):
thisComponent.status = NOT_STARTED
# reset timers
t = 0
_timeToFirstFrame = win.getFutureFlipTime(clock="now")
The_endClock.reset(-_timeToFirstFrame) # t0 is time of first possible flip
frameN = -1
# -------Run Routine "The_end"-------
while continueRoutine:
# get current time
t = The_endClock.getTime()
tThisFlip = win.getFutureFlipTime(clock=The_endClock)
tThisFlipGlobal = win.getFutureFlipTime(clock=None)
frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
# update/draw components on each frame
# *Goodbye* updates
if Goodbye.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
# keep track of start time/frame for later
Goodbye.frameNStart = frameN # exact frame index
Goodbye.tStart = t # local t and not account for scr refresh
Goodbye.tStartRefresh = tThisFlipGlobal # on global time
win.timeOnFlip(Goodbye, 'tStartRefresh') # time at next scr refresh
Goodbye.setAutoDraw(True)
# *GoodbyeEnd* updates
waitOnFlip = False
if GoodbyeEnd.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
# keep track of start time/frame for later
GoodbyeEnd.frameNStart = frameN # exact frame index
GoodbyeEnd.tStart = t # local t and not account for scr refresh
GoodbyeEnd.tStartRefresh = tThisFlipGlobal # on global time
win.timeOnFlip(GoodbyeEnd, 'tStartRefresh') # time at next scr refresh
GoodbyeEnd.status = STARTED
# keyboard checking is just starting
waitOnFlip = True
win.callOnFlip(GoodbyeEnd.clock.reset) # t=0 on next screen flip
win.callOnFlip(GoodbyeEnd.clearEvents, eventType='keyboard') # clear events on next screen flip
if GoodbyeEnd.status == STARTED and not waitOnFlip:
theseKeys = GoodbyeEnd.getKeys(keyList=['space'], waitRelease=False)
_GoodbyeEnd_allKeys.extend(theseKeys)
if len(_GoodbyeEnd_allKeys):
GoodbyeEnd.keys = _GoodbyeEnd_allKeys[-1].name # just the last key pressed
GoodbyeEnd.rt = _GoodbyeEnd_allKeys[-1].rt
# a response ends the routine
continueRoutine = False
# check for quit (typically the Esc key)
if endExpNow or defaultKeyboard.getKeys(keyList=["escape"]):
core.quit()
# check if all components have finished
if not continueRoutine: # a component has requested a forced-end of Routine
break
continueRoutine = False # will revert to True if at least one component still running
for thisComponent in The_endComponents:
if hasattr(thisComponent, "status") and thisComponent.status != FINISHED:
continueRoutine = True
break # at least one component has not yet finished
# refresh the screen
if continueRoutine: # don't flip if this routine is over or we'll get a blank screen
win.flip()
# -------Ending Routine "The_end"-------
for thisComponent in The_endComponents:
if hasattr(thisComponent, "setAutoDraw"):
thisComponent.setAutoDraw(False)
thisExp.addData('Goodbye.started', Goodbye.tStartRefresh)
thisExp.addData('Goodbye.stopped', Goodbye.tStopRefresh)
# check responses
if GoodbyeEnd.keys in ['', [], None]: # No response was made
GoodbyeEnd.keys = None
thisExp.addData('GoodbyeEnd.keys',GoodbyeEnd.keys)
if GoodbyeEnd.keys != None: # we had a response
thisExp.addData('GoodbyeEnd.rt', GoodbyeEnd.rt)
thisExp.addData('GoodbyeEnd.started', GoodbyeEnd.tStartRefresh)
thisExp.addData('GoodbyeEnd.stopped', GoodbyeEnd.tStopRefresh)
thisExp.nextEntry()
# the Routine "The_end" was not non-slip safe, so reset the non-slip timer
routineTimer.reset()
# Flip one final time so any remaining win.callOnFlip()
# and win.timeOnFlip() tasks get executed before quitting
win.flip()
# these shouldn't be strictly necessary (should auto-save)
thisExp.saveAsWideText(output_path+'.tsv', delim='\t')
thisExp.saveAsPickle(output_path)
logging.flush()
# make sure everything is closed down
thisExp.abort() # or data files will save again on exit
win.close()
core.quit()