-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPsychoPyFunctions.py
242 lines (187 loc) · 6.13 KB
/
PsychoPyFunctions.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
import os
import pylink
import sys
from psychopy import visual, logging, core, event
import rtPupil_config
import numpy as np
def set_up_directories( behavioral_folder, eyelink_folder):
"""
Set up directories to save real-time data. If directories do not exist, make them.
Parameters
----------
behavioral_folder : str
directory where to save behavioral log files
eyelink_folder : str
directory where to save EyeLink EDF files
"""
# Check for the behavioral data directory, otherwise make it
if not os.path.isdir(behavioral_folder):
os.makedirs(behavioral_folder) # If this fails (e.g. permissions) we will get error
# Check for the EyeLink data directory, otherwise make it
if not os.path.isdir(eyelink_folder):
os.makedirs(eyelink_folder) # If this fails (e.g. permissions) we will get error
# Specify code directory
_thisDir = os.path.dirname(os.path.abspath(__file__))
os.chdir(_thisDir)
def clear_screen(win):
""" Clear window """
win.fillColor = rtPupil_config.bg_color
win.flip()
def terminate_task(win):
"""
Terminate the task gracefully and retrieve the EDF data file
Parameters
----------
win : PsychoPy Screen
Screen where experiment takes place
"""
el_tracker = pylink.getEYELINK()
if el_tracker.isConnected():
error = el_tracker.isRecording()
if error == pylink.TRIAL_OK:
# Stop EyeLink recording
abort_trial(win)
el_tracker.setOfflineMode()
el_tracker.sendCommand('clear_screen 0')
pylink.msecDelay(500)
el_tracker.closeDataFile()
el_tracker.sendMessage('End EyeLink Recording')
el_tracker.close()
win.close()
core.quit()
sys.exit()
def abort_trial(win):
"""
Ends recording abruptly
Parameters
----------
win : PsychoPy Screen
Screen where experiment takes place
"""
el_tracker = pylink.getEYELINK()
# Stop EyeLink recording
if el_tracker.isRecording():
pylink.pumpDelay(100)
el_tracker.stopRecording()
clear_screen(win)
bgcolor_RGB = rtPupil_config.bg_color
el_tracker.sendMessage('!V CLEAR %d %d %d' % bgcolor_RGB)
el_tracker.sendMessage('TRIAL_RESULT %d' % pylink.TRIAL_ERROR)
return pylink.TRIAL_ERROR
def end_experiment(win) -> None:
"""
End experiment
Parameters
----------
win : PsychoPy Screen
Screen where experiment takes place
"""
el_tracker = pylink.getEYELINK()
# Log
logging.log(level=logging.EXP,msg='*** END EXPERIMENT ***')
el_tracker.sendMessage('*** END EXPERIMENT ***')
# Stop EyeLink recording
pylink.pumpDelay(100)
el_tracker.stopRecording()
# Terminate task
terminate_task(win)
core.wait(2)
win.close()
def quit_task(win) -> None:
"""
Quit task based off of key presses
Parameters
----------
win : PsychoPy Screen
Screen where experiment takes place
"""
# Record key presses
allKeys = event.getKeys(['p','escape'])
# Check keypresses
if allKeys != None:
# Loop over key presses
for thisKey in allKeys:
# If target key found, end experiment
if thisKey in ['p', 'escape']:
end_experiment(win)
def block_trigger(win):
"""
Display block trigger screen
Parameters
----------
win : PsychoPy Screen
Screen where experiment takes place
"""
el_tracker = pylink.getEYELINK()
# Log
logging.log(level=logging.EXP,msg='Waiting for block trigger')
el_tracker.sendMessage('Waiting for block trigger')
scn_width, scn_height = win.size
# On-screen text
onscreen_instructions = visual.TextStim(win, text='Waiting to start. Standby...', color = rtPupil_config.text_color, wrapWidth = scn_width/2)
onscreen_instructions.draw()
win.flip()
# Wait for trigger key press
key = event.waitKeys(keyList=['5','t','escape', 'p'])
# If pressed escape key, quit task
if np.in1d(key,['escape','p']):
end_experiment(win)
# Log
logging.log(level=logging.EXP,msg='Block trigger received')
el_tracker.sendMessage('Block trigger received')
def instruction_continue(win):
"""
Continue/proceed from instruction screen
Parameters
----------
win : PsychoPy Screen
Screen where experiment takes place
"""
# Wait for key press
key = event.waitKeys(keyList=['space', 'escape', 'p'])
# End experiment
if np.in1d(key,['escape','p']):
end_experiment(win)
def instructions_screens(win, instruction:str):
"""
Function presents all the instructions needed for the task
Parameters
----------
win : PsychoPy Screen
Screen where experiment takes place
instruction : str
instruction to be presented
"""
scn_width, scn_height = win.size
# Setup text
task_instructions = visual.TextStim(win, instruction, color = rtPupil_config.text_color, wrapWidth = scn_width/2, units = 'cm')
# Clear window
clear_screen(win)
# Draw text screen
task_instructions.draw()
win.flip()
# Continue or quit from instructions
instruction_continue(win)
def general_instruction_screens(win, fixation):
"""
Present the task instructions
Parameters
----------
win : PsychoPy Screen
Screen where experiment takes place
fixation : PsychoPy TextStim object
fixation cross to be presented
"""
# Log
logging.log(level=logging.EXP,msg='General task instructions')
# Define instructions text
instructions = "Please fixate on the fixation point at all times."
# Setup instructions
task_instructions = visual.TextStim(win, text='', color=rtPupil_config.text_color, pos=[0, 5], units='cm')
# Draw the text
task_instructions.setText(instructions)
task_instructions.draw()
fixation.draw()
win.flip()
# Continue or quit from instructions
instruction_continue(win)