-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathendo.py
704 lines (632 loc) · 22.7 KB
/
endo.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
#############################
# Import required libraries #
#############################
import pyglet, math, sys, os, random, time
from pyglet import window, image, font #get the window, image & font/text manipulation commands
from pyglet.window import key
from pyglet.gl import * #get the commands to manipulate graphics
from pyglet.media import Player #get sound player
random.seed()
#######################
# Important constants #
#######################
font_size = 20 #vary this until it looks right on your monitor
computer = 'tracker'
if computer == 'tracker':
screen_width = 40 #tracker mac mini
elif computer == 'mike':
screen_width = 37.0 #mike's laptop
elif computer == 'emac':
screen_width = 33.5 #emac
elif computer == 'eyelink':
screen_width = 37.5 #eyelink mac mini
viewing_distance = 60.0 #units can be anything so long as they match those used in screen_width
ITI = 1.000 #specify the inter-trial-interval (sec)
fixation_interval = 1.000
target_duration = 0.200 #specify the cue duration (sec)
response_timeout = 1.500
SOA_list = [.8 ] #list the cue-target stimulus onset asynchrony intervals (sec)
cue_validity_list = ['valid','invalid','neutral']
target_location_list = [-1,1]
target_trials_per_cell = 4
catch_trials_per_cell = 1
valid_trials_per_cell = 4
invalid_trials_per_cell = 1
neutral_trials_per_cell = 1
# trials_per_block = (target_trials_per_cell + catch_trials_per_cell) * len(cue_validity_list) * len(SOA_list) * len(['right','left'])
number_of_blocks = 7 #specify the number of blocks
number_of_sub_blocks = 6 #when the full combination of IVs yields large numbers of trials per block, this lets you put breaks inside the "block". Best to use a factor of the number of trials per block.
num_trials_in_practice_block = 30
box_offset_in_degrees = 7
box_size_in_degrees = 1.5
box_thickness_proportion = .2
target_size_in_degrees = .5
target_thickness_proportion = .1
text_proportion = .9 #the proportion of the screen to use for text messages
dpi = 72 #use 72 for mac, 96 for PC
text_font = 'Helvetica' #use 'Helvetica' for mac, 'Arial' for PC
data_path = os.path.join('_Data', 'endo')
#########################
# Initialize the window #
#########################
#create a pyglet window class that facilitates keypress detection
class my_win(window.Window):
def __init__(self, *args, **kwargs):
self.response=False
self.text=""
self.text_done=False
self.mouse_pressed=False
self.mouse_x = 0
self.mouse_y = 0
window.Window.__init__(self, *args, **kwargs)
def on_key_press(self, symbol, modifiers):
self.response=symbol
self.abs_rt = time.time()
if symbol==key.ESCAPE:
sys.exit()
elif symbol==key.BACKSPACE:
if len(self.text) < 2:
self.text=""
else:
self.text=self.text[0:-1]
elif symbol==key.RETURN:
self.text_done=True
def on_text(self,text):
self.text = self.text+text
def on_mouse_press(self, x, y, dx, dy):
self.mouse_x = x
self.mouse_y = y
self.mouse_pressed=True
win = my_win(fullscreen=True) #make a fullscreen window (happily, highest native resolution is captured by default and double buffering is a default behavior)
win.x_center = win.width/2 #store the location of the horizontal center for later reference
win.y_center = win.height/2 #store the location of the vertical center for later reference
win.set_exclusive_mouse() #ensures the mouse is invisible
glClearColor(0, 0, 0, 0) #set the 'cleared' background to white
cursor = win.get_system_mouse_cursor(win.CURSOR_CROSSHAIR)
win.set_exclusive_mouse(True)
win.set_mouse_cursor(cursor)
win.dispatch_events() #always do a dispatch soon after creating a new window
image.get_buffer_manager().get_color_buffer().get_image_data #necessary (for some reason) to ensure openGL drawing coordinates are in pixels with (0,0) in lower left corner
# Perform some calculations to convert stimulus measurements in degrees to pixels
screen_width_in_degrees = math.degrees(math.atan((screen_width/2.0)/viewing_distance)*2)
PPD = win.width/screen_width_in_degrees
target_size = target_size_in_degrees*PPD
box_offset = box_offset_in_degrees*PPD
box_size = box_size_in_degrees*PPD
wheel_size = box_size*5
target_size = target_size_in_degrees*PPD
text_width = text_proportion*win.width
glLineWidth(target_size*target_thickness_proportion)
color_wheel_inner_rim = math.sqrt(((wheel_size/2.0)**2)+((wheel_size/2.0)**2))
color_wheel_outer_rim = wheel_size
#############################
# Define graphics functions #
#############################
def draw_eight():
glColor3f(.5,.5,.5)
glBegin(GL_LINE_STRIP)
glVertex2f(
win.x_center+box_size/2*.25
,win.y_center+box_size/2*.5
)
glVertex2f(
win.x_center-box_size/2*.25
,win.y_center+box_size/2*.5
)
glVertex2f(
win.x_center-box_size/2*.25
,win.y_center
)
glVertex2f(
win.x_center+box_size/2*.25
,win.y_center
)
glVertex2f(
win.x_center+box_size/2*.25
,win.y_center-box_size/2*.5
)
glVertex2f(
win.x_center-box_size/2*.25
,win.y_center-box_size/2*.5
)
glVertex2f(
win.x_center-box_size/2*.25
,win.y_center
)
glVertex2f(
win.x_center+box_size/2*.25
,win.y_center
)
glVertex2f(
win.x_center+box_size/2*.25
,win.y_center+box_size/2*.5
)
glEnd()
def draw_x(target_color,location):
glLineWidth(target_size*target_thickness_proportion)
glColor3f(target_color[0],target_color[1],target_color[2])
glBegin(GL_LINE_STRIP)
glVertex2f(win.x_center+location*box_offset-target_size/2.0,win.y_center-target_size/2.0)
glVertex2f(win.x_center+location*box_offset+target_size/2.0,win.y_center+target_size/2.0)
glEnd()
glBegin(GL_LINE_STRIP)
glVertex2f(win.x_center+location*box_offset-target_size/2.0,win.y_center+target_size/2.0)
glVertex2f(win.x_center+location*box_offset+target_size/2.0,win.y_center-target_size/2.0)
glEnd()
def draw_box(location,cue=False):
glColor3f(.2,.2,.2)
glRectf(
win.x_center + box_offset*location - box_size/2
,win.y_center - box_size/2
,win.x_center + box_offset*location + box_size/2
,win.y_center + box_size/2
)
glColor3f(0,0,0)
glRectf(
win.x_center + box_offset*location - box_size/2 + box_size*box_thickness_proportion/2
,win.y_center - box_size/2 + box_size*box_thickness_proportion/2
,win.x_center + box_offset*location + box_size/2 - box_size*box_thickness_proportion/2
,win.y_center + box_size/2 - box_size*box_thickness_proportion/2
)
def draw_fixation():
win.clear()
draw_box(0)
draw_eight()
draw_box(-1)
draw_box(1)
def draw_target(target,target_color,location):
if target:
draw_x(target_color,location)
def draw_cue(cue_validity,target_location):
draw_eight()
draw_box(0)
draw_box(-1)
draw_box(1)
glColor3f(.5,.5,.5)
if cue_validity=='neutral':
glBegin(GL_LINE_STRIP)
glVertex2f(
win.x_center+box_size/2*.25
,win.y_center+box_size/2*.5
)
glVertex2f(
win.x_center+box_size/2*.25
,win.y_center-box_size/2*.5
)
glEnd()
glBegin(GL_LINE_STRIP)
glVertex2f(
win.x_center-box_size/2*.25
,win.y_center+box_size/2*.5
)
glVertex2f(
win.x_center-box_size/2*.25
,win.y_center-box_size/2*.5
)
glEnd()
glBegin(GL_LINE_STRIP)
glVertex2f(
win.x_center-box_size/2*.25
,win.y_center
)
glVertex2f(
win.x_center+box_size/2*.25
,win.y_center
)
glEnd()
else:
if (cue_validity=='invalid' and mapping=='2') or (cue_validity=='valid' and mapping=='5'):
target_location=target_location*-1
glBegin(GL_LINE_STRIP)
glVertex2f(
win.x_center+box_size/2*.25*target_location
,win.y_center+box_size/2*.5
)
glVertex2f(
win.x_center-box_size/2*.25*target_location
,win.y_center+box_size/2*.5
)
glVertex2f(
win.x_center-box_size/2*.25*target_location
,win.y_center
)
glVertex2f(
win.x_center+box_size/2*.25*target_location
,win.y_center
)
glVertex2f(
win.x_center+box_size/2*.25*target_location
,win.y_center-box_size/2*.5
)
glVertex2f(
win.x_center-box_size/2*.25*target_location
,win.y_center-box_size/2*.5
)
glEnd()
####################
# Helper functions #
####################
def angle_to_color(angle,wheel_rotation):
angle=float(angle)
if angle < wheel_rotation:
angle = angle-wheel_rotation+360
else:
angle = angle-wheel_rotation
if angle<60:
red=1
green=angle/60
blue=0
elif angle < 120:
red=1-(angle-60)/60
green=1
blue=0
elif angle < 180:
red=0
green=1
blue=(angle-120)/60
elif angle < 240:
red=0
green=1-(angle-180)/60
blue=1
elif angle < 300:
red=(angle-240)/60
green=0
blue=1
else:
red=1
green=0
blue=1-(angle-300)/60
return [red,green,blue]
def color_to_angle(color,wheel_rotation):
if color[0]==1:
if color[2]==0:
angle = color[1]*60
else:
angle = 300 + (1-color[2])*60
elif color[1]==1:
if color[2]==0:
angle = 60 + (1-color[0])*60
else:
angle = 120 + color[2]*60
else:
if color[0]==0:
angle = 180 + (1-color[1])*60
else:
angle = 240 + color[0]*60
if angle<wheel_rotation:
true_angle = angle-wheel_rotation+360
else:
true_angle = angle-wheel_rotation
return true_angle
def mouse_to_angle():
this_angle = math.atan2(float(win.mouse_x-win.x_center),float(win.mouse_y-win.y_center))*180/math.pi
if this_angle<0:
this_angle=360+this_angle
return this_angle
def draw_color_wheel(wheel_rotation):
win.clear()
for angle in range(360):
this_color = angle_to_color(angle,wheel_rotation)
glColor3f(this_color[0],this_color[1],this_color[2])
glBegin(GL_TRIANGLES)
glVertex2f(win.x_center , win.y_center)
glVertex2f(
win.x_center+ math.sin(angle*math.pi/180) * wheel_size
,win.y_center + math.cos(angle*math.pi/180) * wheel_size
)
glVertex2f(
win.x_center + math.sin((angle+1)*math.pi/180) * wheel_size
,win.y_center + math.cos((angle+1)*math.pi/180) * wheel_size
)
glEnd()
glColor3f(0,0,0)
for angle in range(360):
glBegin(GL_TRIANGLES)
glVertex2f(win.x_center , win.y_center)
glVertex2f(
win.x_center + math.sin(angle*math.pi/180) * math.sqrt(((wheel_size/2.0)**2)+((wheel_size/2.0)**2))
,win.y_center + math.cos(angle*math.pi/180) * math.sqrt(((wheel_size/2.0)**2)+((wheel_size/2.0)**2))
)
glVertex2f(
win.x_center + math.sin((angle+1)*math.pi/180) * math.sqrt(((wheel_size/2.0)**2)+((wheel_size/2.0)**2))
,win.y_center + math.cos((angle+1)*math.pi/180) * math.sqrt(((wheel_size/2.0)**2)+((wheel_size/2.0)**2))
)
glEnd()
def clicked_within_wheel():
x=win.mouse_x-win.x_center
y=win.mouse_y-win.y_center
h = math.sqrt((x**2)+(y**2))
return (h<color_wheel_outer_rim) & (h>color_wheel_inner_rim)
#define a function that checks if a file exists
def file_exists(f):
try:
file = open(f)
except IOError:
exists = 0
else:
exists = 1
return exists
#define a function that waits for a given duration to pass
def wait(duration):
start = time.time()
while time.time() <= (start + duration):
win.dispatch_events()
def wait_for_response():
win.response = False
while not win.response:
win.dispatch_events()
win.response = False
#define a function that prints a message on the screen while looking for user input to continue. The function returns the total time it waited
def show_message(text):
wait_start = time.time()
win.clear()
win.flip()
win.clear()
rendered_text = pyglet.text.Label(
text, text_font, font_size, multiline=True, width=win.width*text_proportion,
x=win.width//2, y=win.height//2,anchor_x='center', anchor_y='center',
color=[200,200,200,255]
)
rendered_text.draw()
wait(.5)
win.flip()
win.response=False
done=False
while not done:
win.dispatch_events()
if win.response:
done=True
win.response=False
win.flip()
win.clear()
wait(.5)
return time.time()-wait_start
#define a function that prints a feedback message on the screen for a specified amount of time.
def show_feedback(text):
win.clear()
rendered_text = pyglet.text.Label(
text, text_font, font_size, x=win.width//2, y=win.height//2,
anchor_x='center', anchor_y='center', color=[200,200,200,255]
)
rendered_text.draw()
win.flip()
draw_fixation()
wait_start = time.time()
wait_end = wait_start + feedback_duration
while time.time() < wait_end:
win.dispatch_events()
win.flip()
win.response=False
#define a function that helps get_input (below) render an input request and user input to screen.
def input_message(text,location=0):
rendered_text = pyglet.text.Label(
text, text_font, font_size, x=win.width//2, y=win.height//2,
anchor_x='center', anchor_y='center',color=[200,200,200,255]
)
if location != 0:
rendered_text = pyglet.text.Label(
text, text_font, font_size, x=win.width//2, y=(win.height//2)-50,
anchor_x='center', anchor_y='center', color=[200,200,200,255]
)
rendered_text.draw()
#define a function that requests user input
def get_input(get_what):
win.text=''
win.clear()
win.flip()
wait(.5)
win.text_done=False
while not win.text_done:
win.dispatch_events()
win.clear()
input_message(get_what)
input_message(win.text,1)
win.flip()
text_to_return = win.text[0:-1]
win.text = ""
return text_to_return
#define a function that obtains subject info via user input
def get_sub_info():
if not os.path.exists(data_path):
os.makedirs(data_path)
done = False #set done as false in order to enter the loop
while not done:
done = True #set done as true then change if we find an existing file
id = get_input('ID (\'test\' to demo):')
if id != 'test':
existing_files=os.listdir(data_path)
for this_file in existing_files:
if id==this_file.rsplit('_')[0]:
win.clear()
overwrite_id = get_input('Oops! ID already exists! Do you really want to overwrite the existing data? (y or n)')
if overwrite_id == 'n':
done=False
mapping = get_input('Left mapped to (2 or 5):')
if id != 'test':
gender = get_input('Gender (m or f):')
age = get_input('Age (2-digit number):')
handedness = get_input('Handedness (r or l):')
else:
gender='test'
age='test'
handedness='test'
year = time.strftime('%Y')
month = time.strftime('%m')
day = time.strftime('%d')
hour = time.strftime('%H')
sub_info = [id, mapping, gender, age, handedness, year, month, day, hour, computer]
return sub_info
#define a function that initializes the data file
def initialize_data_file():
if not os.path.exists(data_path):
os.makedirs(data_path)
data_file_name = os.path.join(data_path, sub_info[0]+'.txt')
data_file = open(data_file_name, 'w')
header ='\t'.join([
'id', 'mapping', 'gender', 'age', 'handedness', 'year', 'month', 'day', 'hour',
'computer', 'block', 'trial', 'cue_validity', 'SOA', 'target', 'target_location',
'rt', 'pre_target_response', 'wheel_rotation', 'target_angle', 'response_angle'
])
data_file.write(header+'\n')
return data_file
#define a function that generates a randomized list of trial-by-trial stimulus information representing a factorial combination of the independent variables.
def get_trials():
target_list = []
for i in range(target_trials_per_cell):
target_list.append(True)
for i in range(catch_trials_per_cell):
target_list.append(False)
cue_list = []
for i in range(valid_trials_per_cell):
cue_list.append('valid')
for i in range(invalid_trials_per_cell):
cue_list.append('invalid')
for i in range(neutral_trials_per_cell):
cue_list.append('neutral')
trials=[]
for cue_validity in cue_list:
for SOA in SOA_list:
for target_location in target_location_list:
for target in target_list:
wheel_rotation = random.uniform(0,360)
target_angle = random.uniform(0,360)
trials.append([
cue_validity, SOA, target_location,
target, wheel_rotation, target_angle
])
random.shuffle(trials)
return trials
#define a function that runs a block of trials
def run_block(block):
if block=='practice':
trials = get_trials()[0:num_trials_in_practice_block]
sub_block_split = len(trials)
else:
trials = get_trials()
sub_block_split = len(trials)/number_of_sub_blocks
trial_num = 1
win.clear()
draw_eight()
win.flip()
win.clear()
draw_fixation()
wait(ITI)
for trial in trials:
win.flip()
trial_start_time = time.time()
cue_validity = trial[0]
SOA = trial[1]
target_location = trial[2]
target = trial[3]
wheel_rotation = trial[4]
target_angle = trial[5]
target_color = angle_to_color(target_angle,wheel_rotation)
cue_shown = False
target_shown = False
target_done = False
pre_target_response = 'FALSE' #R format logical
ITI_response = 'FALSE' #R format logical
trial_done=False
cue_on_time = trial_start_time + fixation_interval
target_on_time = cue_on_time + SOA
target_off_time = target_on_time + target_duration
response_timeout_time = target_on_time + response_timeout
draw_cue(cue_validity,target_location) #buffer cue screen
while not trial_done:
now = time.time()
if not cue_shown:
if now >= cue_on_time:
win.flip()
cue_shown = True
win.clear()
draw_cue(cue_validity,target_location)
draw_target(target,target_color,target_location) #buffer target
elif not target_shown:
if now >= target_on_time:
win.flip() #show target
target_shown = time.time() #record the time that the target is being shown
win.clear()
draw_cue(cue_validity,target_location) #buffer cue screen
elif not target_done:
if now >= target_off_time:
win.flip() #show cue screen
target_done = True
elif now >= response_timeout_time:
trial_done = True
rt = 'NA'
#look for key responses
win.dispatch_events()
if win.response:
if target_shown==False:
pre_target_response = 'TRUE'
else:
trial_done=True
rt = win.abs_rt-target_shown #get the rt relative to the time at which the target was shown
win.response = False
if target:
draw_color_wheel(wheel_rotation)
win.flip()
win.set_exclusive_mouse(False)
response_done = False
while not response_done:
win.dispatch_events()
if win.mouse_pressed:
if clicked_within_wheel():
response_done = True
response_angle = mouse_to_angle()
win.mouse_pressed = False
win.response = False
win.set_exclusive_mouse(True)
win.clear()
win.flip()
wait(.5)
else:
response_angle = 'NA'
win.clear()
draw_eight()
win.flip()
win.clear()
draw_fixation()
wait(ITI)
#define trial info to write to file
trial_info = '\t'.join(map(str,[
sub_info_for_file, block, trial_num, cue_validity, SOA, target, target_location,
rt, pre_target_response, wheel_rotation, target_angle, response_angle
]))
data_file.write(trial_info+'\n')
if trial_num%sub_block_split ==0:
if block =='practice':
show_message('You\'re all done the practice phase of this experiment.\n\nWhen you are ready, press any key to continue to the experiment.')
else:
show_message('Take a break!\n\nWhen you are ready, press any key to continue the experiment.')
if trial_num != len(trials):
win.clear()
draw_eight()
win.flip()
win.clear()
draw_fixation()
wait(ITI)
#reset the response and iterate the trial counter
win.response=False
trial_num = trial_num + 1
######################
# Run the experiment #
######################
#get subject info
sub_info = get_sub_info()
sub_info_for_file = '\t'.join(map(str,sub_info))
mapping = sub_info[1]
#initialize the data file
data_file = initialize_data_file()
win.clear()
draw_fixation()
win.flip()
wait_for_response()
# show_message('Press any key to begin practice.')
run_block('practice')
for i in range(number_of_blocks):
block = i+1
run_block(block)
show_message('You\'re all done!\n\nPlease alert the person conducting this experiment that you have finished.')
win.close()
sys.exit()