-
Notifications
You must be signed in to change notification settings - Fork 0
/
Experimental_sequence.py
453 lines (335 loc) · 15.7 KB
/
Experimental_sequence.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
from psychopy import core, visual, event, gui, data
import random
import pandas as pd
import os
import numpy as np
from scipy.stats import norm
from matplotlib import pyplot as plt
quest = data.QuestHandler(startVal=0.3, startValSd=0.2, pThreshold=0.75, gamma=0.5,
nTrials=50, minVal=0.0, maxVal=1.0, beta=3.5, delta=0.1)
def occluder_jump(win, duration=0.3, video_jump=1):
start_time = core.getTime()
occluder.autoDraw = True
video.autoDraw = False
win.flip()
video.pause()
video.seek((video.getCurrentFrameTime() + video_jump) % video.duration)
video.play()
occluder.autoDraw = False
core.wait(core.getTime() + duration - start_time)
end_time = core.getTime()
video.autoDraw = True
win.flip()
return start_time, end_time
def blink_jump(win, duration=0.3, video_jump=1):
start_time = core.getTime()
video.pause()
video.seek((video.getCurrentFrameTime() + video_jump) % video.duration)
video.play()
core.wait(core.getTime() + duration - start_time)
end_time = core.getTime()
win.flip()
return start_time, end_time
def check_response(cycle_number, space_click_time, occ_end_time, IsJump, IsDetected, response_data):
if cycle_number > 0:
global video_jump
video_jump_old = video_jump
for value in quest:
video_jump = value
break
print(f"Cycle Number: {cycle_number}, Video jump: {video_jump}")
response_speed = space_click_time - occ_end_time
if IsJump and IsDetected:
response_type = 'correct'
quest.addResponse(1)
elif IsJump and not IsDetected:
response_type = 'missed'
quest.addResponse(0)
elif not IsJump and IsDetected:
response_type = 'false positive'
quest.addResponse(0)
else: # not IsJump and not IsDetected
response_type = 'correct rejection'
quest.addResponse(1)
response_data = response_data._append({'time': core.getTime() - main_start_time, 'video_jump': video_jump_old, 'response_speed': response_speed, 'response_type': response_type, 'quest_threshold': quest.mean(), 'quest_sd': quest.sd()}, ignore_index=True)
cycle_number += 1
return cycle_number, response_data
def blink_condition(win):
event_data = pd.DataFrame(columns=['event_type', 'time'])
response_data = pd.DataFrame(columns=['time', 'video_jump', 'response_speed', 'response_type', 'quest_threshold', 'quest_sd'])
occ_end_time = 0
blink_durations = []
cycle_number = 0
space_click_time = 0
IsJump = False
IsDetected = False
global video
video.autoDraw = True
video.play()
Blinking = False
global video_jump
video_jump = VIDEO_JUMP_START
condition_start_time = core.getTime()
event_data = event_data._append({'event_type': 'condition_start', 'time': condition_start_time-main_start_time}, ignore_index=True)
while True:
# use holds of
keys= event.getKeys()
if 'b' in keys:
if Blinking == False:
blink_start = core.getTime()
Blinking = True
# draw a "blink" text in the top left corner
blink_text = visual.TextStim(win, text='BLINK', pos=(-0.9, 0.9))
blink_text.draw()
blink_text.autoDraw = True
# randomly choose if there is a blink or not
if random.random() < 0.5:
IsDetected=False
IsJump=True
occ_start_time, occ_end_time= blink_jump(win, video_jump=video_jump)
else:
IsDetected=False
IsJump=False
occ_start_time, occ_end_time= blink_jump(win, video_jump=0)
#append occlusion on and offset to the event data
event_data = event_data._append({'event_type': 'occlusion_start', 'time': occ_start_time - main_start_time}, ignore_index=True)
event_data = event_data._append({'event_type': 'occlusion_end', 'time': occ_end_time - main_start_time }, ignore_index=True)
event_data = event_data._append({'event_type': 'blink_start', 'time': blink_start - main_start_time}, ignore_index=True)
elif Blinking == True:
blink_end = core.getTime()
Blinking = False
event_data = event_data._append({'event_type': 'blink_end', 'time': blink_end - main_start_time}, ignore_index=True)
blink_durations.append(blink_end - blink_start)
blink_text.autoDraw = False
cycle_number, response_data = check_response(cycle_number, space_click_time, occ_end_time, IsJump, IsDetected, response_data)
else:
#do nothing
pass
win.flip()
if 'space' in keys and cycle_number > 0:
space_click_time = core.getTime() - main_start_time
if space_click_time - occ_end_time < 2:
IsDetected = True
if 'escape' in keys or 'q' in keys or cycle_number >= MAX_CYCLES:
break
return event_data, response_data, blink_durations
def random_replay_condition(win, blink_data):
event_data = pd.DataFrame(columns=['event_type', 'time'])
response_data = pd.DataFrame(columns=['time', 'video_jump', 'response_speed', 'response_type', 'quest_threshold', 'quest_sd'])
#pick blink start and end from the data
blink_start = blink_data[blink_data['event_type'] == 'blink_start']['time']
blink_end = blink_data[blink_data['event_type'] == 'blink_end']['time']
#transorm the data to a numpy array
blink_start = np.array(blink_start)
blink_end = np.array(blink_end)
#calculate the duration of each blink
blink_durations = blink_end - blink_start
#calculate the distance between each blink
blink_distance = blink_start[1:] - blink_end[:-1]
space_click_time = 0
occ_end_time = 0
# Fit a Gaussian distribution to previous blink durations
dur_mu, dur_std = norm.fit(blink_durations)
dis_mu, dis_std = norm.fit(blink_distance)
# Optionally, you can use mu and std for further analysis or generation of durations
# For example, generating a new set of durations from the fitted Gaussian:
# new_durations = np.random.normal(mu, std, size=len(blink_durations))
IsJump = False
IsDetected = False
cycle_number = 0
global video
video.autoDraw = True
video.play()
# sample an initial duration and distance from the Gaussian distribution
duration = np.random.normal(dur_mu, dur_std)
distance = np.random.normal(dis_mu, dis_std)
condition_start_time = core.getTime()
event_data = event_data._append({'event_type': 'condition_start', 'time': condition_start_time-main_start_time}, ignore_index=True)
while True:
video.draw()
time_since_last_blink = core.getTime() - occ_end_time
if time_since_last_blink > distance:
cycle_number, response_data = check_response(cycle_number, space_click_time, occ_end_time, IsJump, IsDetected, response_data)
#randomly choose whether there is a jump or not
if random.random() < 0.5:
IsDetected=False
IsJump=True
occ_start_time, occ_end_time= occluder_jump(win, video_jump=video_jump, duration=duration)
else:
IsDetected=False
IsJump=False
occ_start_time, occ_end_time= occluder_jump(win, video_jump=0, duration=duration)
event_data = event_data._append({'event_type': 'occluder_start', 'time': occ_start_time - main_start_time}, ignore_index=True)
event_data = event_data._append({'event_type': 'occluder_end', 'time': occ_end_time - main_start_time}, ignore_index=True)
duration = np.random.normal(dur_mu, dur_std)
distance = np.random.normal(dis_mu, dis_std)
win.flip()
keys=event.getKeys()
if 'space' in keys:
space_click_time = core.getTime() - main_start_time
if space_click_time - occ_end_time < 2:
IsDetected = True
if 'escape' in keys or 'q' in keys:
break
if cycle_number >= np.size(blink_durations):
break
return event_data, response_data
def true_replay_condition(win, blink_data):
event_data = pd.DataFrame(columns=['event_type', 'time'])
response_data = pd.DataFrame(columns=['time', 'video_jump', 'response_speed', 'response_type', 'quest_threshold', 'quest_sd'])
#pick blink start and end from the data
blink_start = blink_data[blink_data['event_type'] == 'blink_start']['time']
blink_end = blink_data[blink_data['event_type'] == 'blink_end']['time']
#get condition start time
condition_start_time = blink_data[blink_data['event_type'] == 'condition_start']['time'].values[0]
#transorm the data to a numpy array
blink_start = np.array(blink_start)-condition_start_time
blink_end = np.array(blink_end)-condition_start_time
#calculate the duration of each blink
blink_durations = blink_end - blink_start
space_click_time = 0
occ_end_time = 0
IsJump = False
IsDetected = False
cycle_number = 0
video.autoDraw = True
video.play()
condition_start_time = core.getTime()
event_data = event_data._append({'event_type': 'condition_start', 'time': condition_start_time-main_start_time}, ignore_index=True)
Blinked = False
#add a refractory period of 1 second between blink events
while True:
video.draw()
if core.getTime() - condition_start_time > blink_start[cycle_number] and not Blinked:
Blinked = True
cycle_number, response_data = check_response(cycle_number, space_click_time, occ_end_time, IsJump, IsDetected, response_data)
#randomly choose whether there is a jump or not
if random.random() < 0.5:
IsDetected=False
IsJump=True
occ_start_time, occ_end_time= occluder_jump(win, video_jump=video_jump, duration=blink_durations[cycle_number-1])
else:
IsDetected=False
IsJump=False
occ_start_time, occ_end_time= occluder_jump(win, video_jump=0)
event_data = event_data._append({'event_type': 'occluder_start', 'time': occ_start_time - main_start_time}, ignore_index=True)
event_data = event_data._append({'event_type': 'occluder_end', 'time': occ_end_time - main_start_time}, ignore_index=True)
elif core.getTime()- condition_start_time > blink_end[cycle_number] and Blinked:
Blinked = False
win.flip()
keys=event.getKeys()
if 'space' in keys and cycle_number > 1:
space_click_time = core.getTime() - main_start_time
if space_click_time - occ_end_time < 2:
IsDetected = True
if 'escape' in keys or 'q' in keys:
break
if cycle_number > np.size(blink_durations-1) or cycle_number > MAX_CYCLES-1:
break
return event_data, response_data
VIDEO_JUMP_START = 0.5
main_start_time = core.getTime()
MAX_CYCLES = 10
# Initialize window
global win
win = visual.Window(size=[1920, 1200], fullscr=True, units='pix', screen=1)
#get size of winow
# Initialize mouse
mouse = event.Mouse(win=win)
# Load the .avi video
video_path = './materials/David.avi'
video = visual.MovieStim3(win, video_path, size=(1920, 1200), flipVert=False, flipHoriz=False, loop=True)
#get video frame rate
frame_rate = video.getFPS()
# Create an occluder stimulus
occluder = visual.Rect(win, width=1920, height=1200, fillColor='black')
# QUEST parameters
quest_forward = data.QuestHandler(startVal=0.3, startValSd=0.2, pThreshold=0.75, gamma=0.5,
nTrials=50, minVal=0.0, maxVal=1.0, beta=3.5, delta=0.1)
quest_forward = data.QuestHandler(startVal=0.3, startValSd=0.2, pThreshold=0.75, gamma=0.5,
nTrials=50, minVal=0.0, maxVal=1.0, beta=3.5, delta=0.1)
def main_experiment(win):
try:
global main_start_time
main_start_time = core.getTime()
global folder_path
participant_data = {'participant_id': ''}
dlg = gui.DlgFromDict(dictionary=participant_data, title="Participant Data")
if dlg.OK:
print("Participant Data:", participant_data)
else:
print("User cancelled")
return
folder_path = os.path.join('data', participant_data['participant_id'])
if not os.path.exists(folder_path):
os.makedirs(folder_path)
#give information about the experiment
background = visual.Rect(win, width=1920, height=1200, fillColor='gray')
background.draw()
info_text = visual.TextStim(win, text='In this experiment, you will see a rotating object. Press the spacebar whenever you feel like they are discontinuities in its movement', pos=(0, 0))
info_text.draw()
win.flip()
#press space to continue
event.waitKeys(keyList=['space'])
win.flip()
# Natural blink condition
bc_event_data, bc_response_data, blink_durations = blink_condition(win)
# Save natural blink durations
bc_event_data.to_csv(os.path.join(folder_path, 'bc_event_data.csv'))
bc_response_data.to_csv(os.path.join(folder_path, 'bc_response_data.csv'))
video.pause()
video.autoDraw = False
background.draw()
# check if at least 10 blinks were detected
'''
if len(blink_durations) < 10:
error_text = visual.TextStim(win, text='Not enough blinks detected. Experiment ending', pos=(0, 0))
error_text.draw()
win.flip()
win.close()
core.quit()
return
'''
# Give information about the break,
break_text = visual.TextStim(win, text='Please take a short break. Press space to continue.', pos=(0, 0))
break_text.draw()
win.flip()
#plot histogram of blink durations
plt.hist(blink_durations, bins=5)
plt.show()
# Wait for spacebar press to continue
event.waitKeys(keyList=['space'])
win.flip()
# Replay condition
tr_event_data, tr_response_data = true_replay_condition(win, bc_event_data)
# Save natural blink durations
tr_event_data.to_csv(os.path.join(folder_path, 'tr_event_data.csv'))
tr_response_data.to_csv(os.path.join(folder_path, 'tr_response_data.csv'))
video.pause()
video.autoDraw = False
background.draw()
# Give information about the break,
break_text = visual.TextStim(win, text='Please take a short break. Press space to continue.', pos=(0, 0))
break_text.draw()
win.flip()
# Wait for spacebar press to continue
event.waitKeys(keyList=['space'])
win.flip()
# Random replay condition
rr_event_data, rr_response_data = random_replay_condition(win, bc_event_data)
rr_event_data.to_csv(os.path.join(folder_path, 'rr_event_data.csv'))
rr_response_data.to_csv(os.path.join(folder_path, 'rr_response_data.csv'))
# thank participant for participating
end_text = visual.TextStim(win, text='Thank you for participating in this experiment. Press space to exit.', pos=(0, 0))
end_text.draw()
win.flip()
except KeyboardInterrupt:
bc_event_data.to_csv(os.path.join(folder_path, 'bc_event_data.csv'))
bc_response_data.to_csv(os.path.join(folder_path, 'bc_response_data.csv'))
win.close()
core.quit()
# Run the main experiment
main_experiment(win)
# Clean up
win.close()
core.quit()