forked from fredcallaway/eyeplan-experiment
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrial.py
More file actions
303 lines (260 loc) · 9.11 KB
/
trial.py
File metadata and controls
303 lines (260 loc) · 9.11 KB
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
"""Utilities for defining experimental trials for eyetracking experiment."""
import os
import random
import time
# NOTE: These packages are lazily imported
from psychopy import core, visual, event
import config
import eyetracking
class Block:
"""For type annotations."""
class Trial:
"""For type annotations."""
class ImageTrial(Trial):
"""A class wrapper for a psychopy ImageStim.
Attributes:
win: The psychopy window to render the stimulus to.
image_path: The path to the image.
timed: Whether the trial ends on its own (T), or by button press (F).
"""
def __init__(
self,
win:visual.Window,
image_path,
timed=False):
self.win = win
self.timed = timed
# Create the image stimulus showing the controller
self.image_stim = visual.ImageStim(
win=self.win,
image=image_path,
pos=(0,0),
units='pix',
size=[1920,1080]
)
def draw(self) -> None:
"""Draws the text stimuli on the window."""
self.image_stim.draw()
def run(self):
"""Method for running the trial."""
if self.timed:
self.draw()
self.win.flip()
time.sleep(0.5)
return
while True:
self.draw()
self.win.flip()
keys = event.getKeys([config.YELLOW_BUTTON])
if config.YELLOW_BUTTON in keys:
return True # Continue with the experiment
event.clearEvents()
class VideoTrial(Trial):
"""A class wrapper for a psychopy MovieStim.
Attributes:
end_keys: Which keys end the trial.
win: The psychopy window to render the stimulus to.
path: The path to the video.
"""
def __init__(
self, path:str, end_keys:list[str], win:visual.Window):
self.path = path
self.name = path.split('/')[-1].split('.')[0]
self.end_keys = end_keys
self.video = None
self.response = None
self.rt = None
self.win = win
def _play_button(self, eyelink):
event.clearEvents()
eyelink.message('VIDEO_START')
self.video.play()
while True:
# Draw video frame
self.video.draw()
self.video.win.flip()
keys = event.getKeys(self.end_keys)
for key in self.end_keys:
if key in keys:
# Move to next video
self.video.stop()
eyelink.message('BUTTON_PRESS %s' % key)
eyelink.message('VIDEO_END')
return key
event.clearEvents()
def _play_timed(self, eyelink):
event.clearEvents()
eyelink.message('VIDEO_START')
self.video.play()
while not self.video.isFinished:
# Draw video frame
self.video.draw()
self.video.win.flip()
self.video.stop()
eyelink.message('VIDEO_END')
event.clearEvents()
return
def play(self, eyelink):
self.video = visual.MovieStim(
win=self.win,
filename=self.path,
size=(
config.DEFAULT_WIDTH * config.STIM_SCALE,
config.DEFAULT_HEIGHT * config.STIM_SCALE),
pos=(0, 0),
noAudio=True
)
event.clearEvents()
if self.end_keys:
self.response = self._play_button(eyelink)
event.clearEvents()
else:
self._play_timed(eyelink)
event.clearEvents()
event.clearEvents()
self.video = None
class FixationTrial(Trial):
"""A class wrapper for fixation stimuli (cross).
Wraps around visual.ShapeStim.
Attributes:
win: The window to render the shape to.
"""
def __init__(self, win):
self.win = win
self.shape = visual.ShapeStim(
win=win,
vertices=((0, -0.05), (0, 0.05), (0, 0), (-0.05, 0), (0.05, 0)),
lineWidth=6,
lineColor='black',
closeShape=False,
colorSpace='rgb'
)
def draw(self):
'''This method overrides psychopy's weird lazy importing issues.
If you don't do it this way, you'll get some weird type errors.
'''
self.shape.draw()
def run(self):
"""Method for running trial."""
duration = random.uniform(0.5, 2)
self.draw()
self.win.flip()
core.wait(duration)
event.clearEvents()
class ExperimentBlock(Block):
'''Class for an Experiment Block.
An ExperimentBlock is a tuple of VideoTrials, usually a pre-, intra-, and
post-video of a given scene.
Args:
eyelink: The pylink.EyeLink object (tracker).
videos: The sequence of VideoTrials that make the block.
win: Psychopy window object.
id: The id of the Block.
'''
def __init__(
self,
eyelink:eyetracking.EyeLink,
win,
videos:list[VideoTrial],
id:int):
self.eyelink = eyelink
self.videos = videos
self.response_recorded = visual.ImageStim(
win=win,
image='data/introduction/experiment_recorded.png',
pos=(0,0)
)
self.gaze_data = []
self.video_start_time = None
self.id = id
self.responses = {}
def stop_video_and_tracking(self, video) -> None:
"""Stop recording from eye tracker."""
self.eyelink.stop_recording()
# Get participant response time
response_time = time.time() - self.video_start_time
# Send trial variable info
self.eyelink.message('!V TRIAL_VAR scene_name %s' % (
video.name))
self.eyelink.message('!V TRIAL_VAR rt %f' % (response_time))
video.rt = response_time
event.clearEvents()
def run(self) -> None:
"""Run forward the video trial."""
# Send block onset message
self.eyelink.message('BLOCK_START')
self.eyelink.message('!V TRIAL_VAR block_index %d' % (self.id))
for trial_index, v in enumerate(self.videos):
self.eyelink.message('TRIAL_START')
self.eyelink.start_recording()
self.video_start_time = time.time()
v.play(eyelink=self.eyelink)
self.stop_video_and_tracking(v)
self.responses[v.name] = {'response': v.response, 'rt': v.rt}
self.eyelink.message('!V TRIAL_VAR trial_index %d' % (trial_index))
self.eyelink.message('TRIAL_END')
self.eyelink.message('BLOCK_END')
self.response_recorded.draw()
self.response_recorded.win.flip()
time.sleep(0.5)
# Send trial offset message
class ComprehensionBlock(Block):
'''Class for a Comprehension Block.
A ComprehensionBlock is a tuple of VideoTrials, usually a pre-, intra-, and
post-video of a given scene.
Args:
eyelink: The pylink.EyeLink object (tracker).
videos: The sequence of VideoTrials that make the block.
correct_response: The correct response to the Block.
id: The id of the Block.
'''
def __init__(
self,
eyelink:eyetracking.EyeLink | eyetracking.MouseLink,
videos:list[VideoTrial],
correct_response:str,
id:int):
self.eyelink = eyelink
self.videos = videos
self.gaze_data = []
self.video_start_time = None
self.id = id
self.correct_response = correct_response
self.passed = None
self.responses = {}
def reset(self):
self.passed = None
self.responses = {}
self.video_start_time = None
self.gaze_data = []
def stop_video_and_tracking(self, video) -> None:
"""Stop recording from eye tracker."""
event.clearEvents()
self.eyelink.stop_recording()
# Get participant response time
response_time = time.time() - self.video_start_time
# Send trial variable info
self.eyelink.message('!V TRIAL_VAR scene_name %s' % (
video.name))
self.eyelink.message('!V TRIAL_VAR rt %f' % (response_time))
video.rt = response_time
def run(self) -> None:
"""Run forward the video trial."""
# Send trial onset message
self.eyelink.message('BLOCK_START')
self.eyelink.message('!V TRIAL_VAR block_index %d' % (self.id))
for trial_index, v in enumerate(self.videos):
self.eyelink.message('TRIAL_START')
self.eyelink.start_recording()
self.video_start_time = time.time()
v.play(eyelink=self.eyelink)
self.stop_video_and_tracking(v)
self.responses[v.name] = {'response': v.response, 'rt': v.rt}
if '_post' in v.name:
self.passed = self.correct_response == v.response
self.eyelink.message('!V TRIAL_VAR trial_index %d' % (trial_index))
self.eyelink.message('TRIAL_END')
# Send trial offset message
self.eyelink.message('BLOCK_END')
if __name__ == '__main__':
pass