-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_experiment_jan.py
167 lines (143 loc) · 5.84 KB
/
main_experiment_jan.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
import datetime
import csv
import random
from parameter_list_jan import *
clock = pygame.time.Clock()
def draw_stimulus(trialType):
"""
Function to draw the stimulus
green circle for trialType == 1
red circle for trialType == 0
parameters: trialType
"""
if trialType:
SCREEN.fill(BG_COLOR)
pygame.draw.circle(SCREEN,GO_COLOR, [Cx, Cy], RADIUS, 0)
else:
SCREEN.fill(BG_COLOR)
pygame.draw.circle(SCREEN,NOGO_COLOR, [Cx, Cy], RADIUS, 0)
def message_display(text):
"""
Function to display a given message in the middle of the SCREEN
handles the button press of the user to go to the main loop
parameters: text to be shown
Returns: 1 when button is pressed
"""
f = pygame.font.SysFont('',FONTSIZE,False, False)
SCREEN.fill(BG_COLOR)
line = f.render(text,True, WHITE,BG_COLOR)
textrect = line.get_rect()
textrect.centerx = SCREEN.get_rect().centerx
textrect.centery = SCREEN.get_rect().centery
SCREEN.blit(line, textrect)
pygame.display.flip()
#wait for button press from the user
buttonpress=0
while buttonpress == 0:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_c:
buttonpress = 1
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
pygame.display.set_mode(SCREENSIZE)
if buttonpress == 1:
return 1
# draw fixation cross
def draw_fixation():
"""
Function to draw fixation cross based on the parameters listed in
parameter_list
"""
SCREEN.fill(BG_COLOR)
pygame.draw.line(SCREEN,WHITE, VLINE[0], VLINE[1],VLINE[2])
pygame.draw.line(SCREEN,WHITE, HLINE[0], HLINE[1],HLINE[2])
def fill_background():
SCREEN.fill(BG_COLOR)
# We use this function to generate the filename depending on the subID
# e.g., to check before starting the experiment if there has already been a subject with this ID
def csv_filename(subID):
return "Sub{}.csv".format(subID)
def csv_filepath(subID):
return os.path.join(DATADIR, csv_filename(subID))
# Renamed writeData to write_data. Please stick to your own naming conventions
def write_data(datalist, subID):
"""
Function to write the list of responses to a csv dataFile
"""
data_filepath = csv_filepath(subID)
file_existed = os.path.isfile(data_filepath)
with open(data_filepath, mode='a+') as file:
writer = csv.writer(file)
if not file_existed:
writer.writerow(['SubjectID', 'StimulusType', 'response', 'RT'])
writer.writerows(datalist)
###### main experiment loop ##########
def experiment(subID):
# List where all the repsonses are stored
dataFile = []
pygame.mouse.set_visible(False)
stimuli_list = [1]*int(NUMTRIAL- NUMTRIAL*PCT_NOGO)
nogo_trials = [0]*int(NUMTRIAL*PCT_NOGO)
stimuli_list.extend(nogo_trials)
random.shuffle(stimuli_list)
# Flag to check when the experiment loop ends
done = False
while not done:
text = 'Only press SPACE when GREEN circle is shown. Press c to continue'
code = message_display(text)
if code == 1:
for stim in stimuli_list:
response = 0 # should be assigned 1 if K_SPACE is pressed
RT = 0 # should be assigned value based on elapsed time from when stimulus is shown
countdown = 2
draw_fixation()
pygame.display.flip()
pygame.time.wait(500) # Display fixation cross for 500 milliseconds
# clear event buffer so they are not misunderstood as responses
pygame.event.clear(pygame.KEYDOWN)
# show stimulus and get RT and response
draw_stimulus(stim)
pygame.display.flip()
# get time at which stimulus is shown
start = pygame.time.get_ticks()
# check for events
countdown_check = pygame.USEREVENT+1 # custom event to track counter
pygame.time.set_timer(countdown_check, 1000) # timer that tracks counter every 1000ms
while countdown > 0 and response == 0:
clock.tick(FPS)
for event in pygame.event.get():
# if the pygame exit button is pressed
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# if 1000ms have passed do a countdown check
if event.type == countdown_check:
countdown -= 1
# if subject has pressed a button
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
# Time elapsed from stimulus to button press
end = pygame.time.get_ticks()
RT = end-start
# We assign 1 here because K_SPACE was pressed
response = 1
fill_background() # clear the screen
pygame.display.flip()
pygame.time.wait(TRIALINTERVAL)
dataFile.append([subID, stim, response, RT]) # append the data to the datafile
done = True
return dataFile
if __name__ == "__main__":
# Fill this before start of the experiment
subID = None # TODO ID of the subject
if subID is None:
subID = '{:%Y%m%d%H%M}'.format(datetime.datetime.now())
dataFile = experiment(subID)
print('*'*30)
print('Writing in data file: Sub{}.csv'.format(subID))
print('*'*30)
write_data(dataFile, subID)
pygame.quit()
quit()