generated from Synteraction-Lab/TemplateRepository
-
Notifications
You must be signed in to change notification settings - Fork 1
/
task_vigilance.py
422 lines (374 loc) · 18 KB
/
task_vigilance.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This experiment was created using PsychoPy3 Experiment Builder (v2020.2.10),
on Wed 27 Jan 2021 03:28:06 PM +08
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
"""
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
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
import os # handy system and path functions
import sys # to get file system encoding
from psychopy.hardware import keyboard
import trigger_notification_illustrations
import stimuli_generation
# 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 = '2020.2.10'
expName = 'task_vigilance' # from the Builder filename that created this script
expInfo = {'participant': '', 'session': '01'}
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
filename = _thisDir + os.sep + u'data/%s/%s_%s_%s_%s' % (expInfo['participant'], expInfo['participant'], expInfo['session'], expName, expInfo['date'])
# An ExperimentHandler isn't essential but helps with data saving
thisExp = data.ExperimentHandler(name=expName, version='',
extraInfo=expInfo, runtimeInfo=None,
originPath='/home/nj/Documents/Codes/Python/TriggerNotificationPython/psychopy/illustrations_vigilance2.py',
savePickle=True, saveWideText=True,
dataFileName=filename)
# save a log file for detail verbose info
logFile = logging.LogFile(filename+'.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=[3840, 1200], fullscr=True, screen=1,
winType='pyglet', allowGUI=False, allowStencil=False,
monitor='testMonitor', color=[0,0,0], 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
# create a default keyboard (e.g. to check for escape)
defaultKeyboard = keyboard.Keyboard()
# Initialize components for Routine "start"
startClock = core.Clock()
stimuli_generation.generate_new_stimuli_csv('stimuli/stimuli_data2.csv')
txt_start = visual.TextStim(win=win, name='txt_start',
text='Starting ...',
font='Arial',
pos=(0, 0), height=0.1, wrapWidth=None, ori=0,
color='white', colorSpace='rgb', opacity=1,
languageStyle='LTR',
depth=-1.0);
# Initialize components for Routine "task"
taskClock = core.Clock()
timing_info = ''
image_mapping = {'1': 'img/76x76.png', '2':'img/100x100.png', '3':'img/100x76.png', '4':'img/76x100.png'}
mouse = event.Mouse(win=win)
x, y = [None, None]
mouse.mouseClock = core.Clock()
im = visual.ImageStim(
win=win,
name='im', units='cm',
image='sin', mask=None,
ori=0, pos=(0, 0), size=(3, 3),
color=[1,1,1], colorSpace='rgb', opacity=1,
flipHoriz=False, flipVert=False,
texRes=128, interpolate=True, depth=-2.0)
# Initialize components for Routine "end"
endClock = core.Clock()
txt_end = visual.TextStim(win=win, name='txt_end',
text='Stopping ...',
font='Arial',
pos=(0, 0), height=0.1, wrapWidth=None, ori=0,
color='white', colorSpace='rgb', opacity=1,
languageStyle='LTR',
depth=0.0);
# 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 "start"-------
continueRoutine = True
routineTimer.add(2.000000)
# update component parameters for each repeat
trigger_notification_illustrations.trigger_notification_randomly_threaded(expInfo['participant'], expInfo['session'], globalClock)
# keep track of which components have finished
startComponents = [txt_start]
for thisComponent in startComponents:
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")
startClock.reset(-_timeToFirstFrame) # t0 is time of first possible flip
frameN = -1
# -------Run Routine "start"-------
while continueRoutine and routineTimer.getTime() > 0:
# get current time
t = startClock.getTime()
tThisFlip = win.getFutureFlipTime(clock=startClock)
tThisFlipGlobal = win.getFutureFlipTime(clock=None)
frameN = frameN + 1 # number of completed frames (so 0 is the first frame)
# update/draw components on each frame
# *txt_start* updates
if txt_start.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
# keep track of start time/frame for later
txt_start.frameNStart = frameN # exact frame index
txt_start.tStart = t # local t and not account for scr refresh
txt_start.tStartRefresh = tThisFlipGlobal # on global time
win.timeOnFlip(txt_start, 'tStartRefresh') # time at next scr refresh
txt_start.setAutoDraw(True)
if txt_start.status == STARTED:
# is it time to stop? (based on global clock, using actual start)
if tThisFlipGlobal > txt_start.tStartRefresh + 2-frameTolerance:
# keep track of stop time/frame for later
txt_start.tStop = t # not accounting for scr refresh
txt_start.frameNStop = frameN # exact frame index
win.timeOnFlip(txt_start, 'tStopRefresh') # time at next scr refresh
txt_start.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 startComponents:
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 "start"-------
for thisComponent in startComponents:
if hasattr(thisComponent, "setAutoDraw"):
thisComponent.setAutoDraw(False)
trigger_notification_illustrations.log_timing_threaded(expInfo['participant'], expInfo['session'], -1, globalClock.getTime(), win.getFutureFlipTime(clock=None))
thisExp.addData('txt_start.started', txt_start.tStartRefresh)
thisExp.addData('txt_start.stopped', txt_start.tStopRefresh)
# set up handler to look after randomisation of conditions etc
trials = data.TrialHandler(nReps=1, method='sequential',
extraInfo=expInfo, originPath=-1,
trialList=data.importConditions('stimuli/stimuli_data2.csv'),
seed=None, name='trials')
thisExp.addLoop(trials) # add the loop to the experiment
thisTrial = trials.trialList[0] # so we can initialise stimuli with some values
# abbreviate parameter names if possible (e.g. rgb = thisTrial.rgb)
if thisTrial != None:
for paramName in thisTrial:
exec('{} = thisTrial[paramName]'.format(paramName))
for thisTrial in trials:
currentLoop = trials
# abbreviate parameter names if possible (e.g. rgb = thisTrial.rgb)
if thisTrial != None:
for paramName in thisTrial:
exec('{} = thisTrial[paramName]'.format(paramName))
# ------Prepare to start Routine "task"-------
continueRoutine = True
routineTimer.add(0.625000)
# update component parameters for each repeat
timing_info = timing_info + '{},{},{}\n'.format(trials.thisRepN, globalClock.getTime(), win.getFutureFlipTime(clock=None))
# setup some python lists for storing info about the mouse
mouse.x = []
mouse.y = []
mouse.leftButton = []
mouse.midButton = []
mouse.rightButton = []
mouse.time = []
gotValidClick = False # until a click is received
im.setImage(image_mapping[str(image_id)])
# keep track of which components have finished
taskComponents = [mouse, im]
for thisComponent in 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")
taskClock.reset(-_timeToFirstFrame) # t0 is time of first possible flip
frameN = -1
# -------Run Routine "task"-------
while continueRoutine and routineTimer.getTime() > 0:
# get current time
t = taskClock.getTime()
tThisFlip = win.getFutureFlipTime(clock=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
# *mouse* updates
if mouse.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
# keep track of start time/frame for later
mouse.frameNStart = frameN # exact frame index
mouse.tStart = t # local t and not account for scr refresh
mouse.tStartRefresh = tThisFlipGlobal # on global time
win.timeOnFlip(mouse, 'tStartRefresh') # time at next scr refresh
mouse.status = STARTED
prevButtonState = mouse.getPressed() # if button is down already this ISN'T a new click
if mouse.status == STARTED:
# is it time to stop? (based on global clock, using actual start)
if tThisFlipGlobal > mouse.tStartRefresh + .625-frameTolerance:
# keep track of stop time/frame for later
mouse.tStop = t # not accounting for scr refresh
mouse.frameNStop = frameN # exact frame index
win.timeOnFlip(mouse, 'tStopRefresh') # time at next scr refresh
mouse.status = FINISHED
if mouse.status == STARTED: # only update if started and not finished!
buttons = mouse.getPressed()
if buttons != prevButtonState: # button state changed?
prevButtonState = buttons
if sum(buttons) > 0: # state changed to a new click
x, y = mouse.getPos()
mouse.x.append(x)
mouse.y.append(y)
buttons = mouse.getPressed()
mouse.leftButton.append(buttons[0])
mouse.midButton.append(buttons[1])
mouse.rightButton.append(buttons[2])
mouse.time.append(globalClock.getTime())
# *im* updates
if im.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
# keep track of start time/frame for later
im.frameNStart = frameN # exact frame index
im.tStart = t # local t and not account for scr refresh
im.tStartRefresh = tThisFlipGlobal # on global time
win.timeOnFlip(im, 'tStartRefresh') # time at next scr refresh
im.setAutoDraw(True)
if im.status == STARTED:
# is it time to stop? (based on global clock, using actual start)
if tThisFlipGlobal > im.tStartRefresh + 0.625-frameTolerance:
# keep track of stop time/frame for later
im.tStop = t # not accounting for scr refresh
im.frameNStop = frameN # exact frame index
win.timeOnFlip(im, 'tStopRefresh') # time at next scr refresh
im.setAutoDraw(False)
# check for quit (typically the Esc key)
if endExpNow or defaultKeyboard.getKeys(keyList=["escape"]):
trigger_notification_illustrations.cancel_notification_trigger()
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 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 "task"-------
for thisComponent in taskComponents:
if hasattr(thisComponent, "setAutoDraw"):
thisComponent.setAutoDraw(False)
# store data for trials (TrialHandler)
trials.addData('mouse.x', mouse.x)
trials.addData('mouse.y', mouse.y)
trials.addData('mouse.leftButton', mouse.leftButton)
trials.addData('mouse.midButton', mouse.midButton)
trials.addData('mouse.rightButton', mouse.rightButton)
trials.addData('mouse.time', mouse.time)
trials.addData('mouse.started', mouse.tStartRefresh)
trials.addData('mouse.stopped', mouse.tStopRefresh)
trials.addData('im.started', im.tStartRefresh)
trials.addData('im.stopped', im.tStopRefresh)
thisExp.nextEntry()
# completed 1 repeats of 'trials'
# ------Prepare to start Routine "end"-------
continueRoutine = True
routineTimer.add(2.000000)
# update component parameters for each repeat
# keep track of which components have finished
endComponents = [txt_end]
for thisComponent in 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")
endClock.reset(-_timeToFirstFrame) # t0 is time of first possible flip
frameN = -1
# -------Run Routine "end"-------
while continueRoutine and routineTimer.getTime() > 0:
# get current time
t = endClock.getTime()
tThisFlip = win.getFutureFlipTime(clock=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
# *txt_end* updates
if txt_end.status == NOT_STARTED and tThisFlip >= 0.0-frameTolerance:
# keep track of start time/frame for later
txt_end.frameNStart = frameN # exact frame index
txt_end.tStart = t # local t and not account for scr refresh
txt_end.tStartRefresh = tThisFlipGlobal # on global time
win.timeOnFlip(txt_end, 'tStartRefresh') # time at next scr refresh
txt_end.setAutoDraw(True)
if txt_end.status == STARTED:
# is it time to stop? (based on global clock, using actual start)
if tThisFlipGlobal > txt_end.tStartRefresh + 2-frameTolerance:
# keep track of stop time/frame for later
txt_end.tStop = t # not accounting for scr refresh
txt_end.frameNStop = frameN # exact frame index
win.timeOnFlip(txt_end, 'tStopRefresh') # time at next scr refresh
txt_end.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 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 "end"-------
for thisComponent in endComponents:
if hasattr(thisComponent, "setAutoDraw"):
thisComponent.setAutoDraw(False)
thisExp.addData('txt_end.started', txt_end.tStartRefresh)
thisExp.addData('txt_end.stopped', txt_end.tStopRefresh)
trigger_notification_illustrations.cancel_notification_trigger()
trigger_notification_illustrations.log_all_timing(expInfo['participant'], expInfo['session'], timing_info)
# 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(filename+'.csv', delim='auto')
thisExp.saveAsPickle(filename)
logging.flush()
# make sure everything is closed down
thisExp.abort() # or data files will save again on exit
win.close()
core.quit()