/
experiment.py
executable file
·384 lines (315 loc) · 12.5 KB
/
experiment.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
# -*- coding: utf-8 -*-
import os
import copy
import random
import numpy
from constants import *
from pygaze.display import Display
from pygaze.screen import Screen
from pygaze.keyboard import Keyboard
from pygaze.mouse import Mouse
from pygaze.logfile import Logfile
from pygaze.eyetracker import EyeTracker
import pygaze.libtime as timer
from scansync.mri import MRITriggerBox
##############
# INITIALISE #
##############
# Initialise a new Display instance.
disp = Display()
# Present a start-up screen.
scr = Screen()
scr.draw_text("Loading, please wait...", fontsize=24)
disp.fill(scr)
disp.show()
# Open a new log file.
log = Logfile()
# TODO: Write header.
log.write(["trialnr", "block", "run","stim", "keypress", "go_nogo", "face_onset", "signal_onset","resp_onset", "RT", "accuracy", "respmap", "block_type"])
# Open a new log file to log events.
event_log = Logfile(filename=EVENT_LOG)
event_log.write(["time", "event"])
# Initialise the eye tracker.
tracker = EyeTracker(disp)
# Create a new Keyboard instance to process key presses.
kb = Keyboard(keylist=None, timeout=5000)
mouse = Mouse()
# intitliase the MEG interface NI box
if MRI:
trigbox = MRITriggerBox()
###################
# GENERATE TRIALS #
###################
"""
A run is a split of experiment, so each run should be matched by randomised
We have 8 blocks: 4 distractors types x 2 go or no-go conditions
The trial information is head in the trial object. The structure is as follows:
trial[runNo][blockNo][trialNo][infoNo]
infoNo is:
0 = distractor filename
1 = trial type
2 = ITI
3 = Stimonset
4 = screens
0 = face only screen
1 = face + signal screen
"""
trials = [[]] * len(RUNS) # for populating
# calulate range of ITIs and Stim onsets
stepsize = (ITI_RANGE[1] - ITI_RANGE[0]) /TRIALS_PER_BLOCK
itis = numpy.arange(ITI_RANGE[0], ITI_RANGE[1], stepsize).astype(int)
stepsize = (SIGNAL_ONSET_RANGE[1] - SIGNAL_ONSET_RANGE[0]) /TRIALS_PER_BLOCK
onsets = numpy.arange(SIGNAL_ONSET_RANGE[0], SIGNAL_ONSET_RANGE[1], stepsize).astype(int)
for run in RUNS: # for each run
trials[run] = [[]] * len(BLOCKS)
for i, block in enumerate(BLOCKS):# for each block
trials[run][i] = [[]] * TRIALS_PER_BLOCK
prefix = block[0] # get filename the prefix for the image we need
if block[-1] == 'n': # generate condition vector for this block based on condition
conds = ['go'] * (TRIALS_PER_BLOCK - NO_GO_TRIALS) + ['no-go']*NO_GO_TRIALS # go/no-go block
random.shuffle(conds)
else:
conds = ['go'] * TRIALS_PER_BLOCK # go only block
# shuffle the jittering
random.shuffle(itis)
random.shuffle(onsets)
print(itis[0])
for ii in range(TRIALS_PER_BLOCK): # loop through all the trials
trials[run][i][ii] = [[]] * 6
trials[run][i][ii][0] = prefix + '_' + str(ii) + '.jpg' #image name
trials[run][i][ii][1] = conds[ii] # trial type
trials[run][i][ii][2] = itis[ii] # iti time
trials[run][i][ii][3] = onsets[ii] # signal onset time
trials[run][i][ii][4] = [[]] * 3
trials[run][i][ii][5] = block
# now pre-generate the screens!
# Note: this is code efficient but not memory efficient (i.e. I am lazy and would rather pre-load almost twice the number of screens, because this is why we have RAM)
tempstim = Screen()
#tempstim.draw_image(RESDIR + trials[run][i][ii][0], pos=DISPCENTRE)# draw face on center of screen
tempstim.draw_image(os.path.join(RESDIR, trials[run][i][ii][0]), pos=DISPCENTRE, scale=0.5)# draw face on center of screen
trials[run][i][ii][4][0] = tempstim
tempsig = Screen()
#tempsig.draw_image(RESDIR + trials[run][i][ii][0], pos=DISPCENTRE)# draw face on center of screen
tempsig.draw_image(os.path.join(RESDIR, trials[run][i][ii][0]), pos=DISPCENTRE, scale=0.5)# draw face on center of screen
tempfeed = Screen()
tempfeed.draw_image(os.path.join(RESDIR, trials[run][i][ii][0]), pos=DISPCENTRE, scale=0.5)#
#RESPMAP 0 = go squares, no-go circles
square_sz = 100
square_off = square_sz/2
pen_width = 6
circle_sz = 50
# 1 = go circles, no-go squares
if conds[ii] == 'go':
if RESPMAP == 0:
tempsig.draw_rect(colour=None, x=DISPCENTRE[0]-square_off, y=DISPCENTRE[1]-square_off, w=square_sz, h=square_sz, pw=pen_width, fill=False)
tempfeed.draw_rect(colour=(128,128,128), x=DISPCENTRE[0]-square_off, y=DISPCENTRE[1]-square_off, w=square_sz, h=square_sz, pw=pen_width, fill=False)
else:
tempsig.draw_circle(colour=None, pos=DISPCENTRE, r=circle_sz, pw=pen_width, fill=False)
tempfeed.draw_circle(colour=(128,128,128), pos=DISPCENTRE, r=circle_sz, pw=pen_width, fill=False)
if conds[ii] == 'no-go':
if RESPMAP == 1:
tempsig.draw_rect(colour=None, x=DISPCENTRE[0]-square_off, y=DISPCENTRE[1]-square_off, w=square_sz, h=square_sz, pw=pen_width, fill=False)
tempfeed.draw_rect(colour=(128,128,128), x=DISPCENTRE[0]-square_off, y=DISPCENTRE[1]-square_off, w=square_sz, h=square_sz, pw=pen_width, fill=False)
else:
tempsig.draw_circle(colour=None, pos=DISPCENTRE, r=circle_sz, pw=pen_width, fill=False)
tempfeed.draw_circle(colour=(128,128,128), pos=DISPCENTRE, r=circle_sz, pw=pen_width, fill=False)
trials[run][i][ii][4][1] = tempsig
trials[run][i][ii][4][2] = tempfeed
# now shuffe the order of blocks and shuffle the trials in each block
for i in range(len(trials)):
random.shuffle(trials[i])
for ii in range(len(trials[i])):
random.shuffle(trials[i][ii])
#############################
# GENERATE STANDARD SCREENS #
#############################
"""
These are screens that we re-use multiple times with no changes, so pre-generation is optimal
"""
# Fixation cross - we use this for ITI and the response window
fix_screen = Screen()
fix_screen.draw_fixation(fixtype='cross', pw=2, diameter=15)
#Response Mapping (0 = GO squares; 1 = GO circles):
if RESPMAP == 0:
mapimg = '0_inst.png'
else:
mapimg = '1_inst.png'
# Inter run screen
inter_run = Screen()
txt = \
"""
Welcome to the go or no-go game.
In this game you will see a number of pictures - please ignore these.
The aim is to look at the shape in the middle of the picture
PRESS ANY BUTTON TO START
"""
inter_run.draw_text(txt, fontsize=36,pos=(DISPCENTRE[0], DISPCENTRE[1]-200))
inter_run.draw_image(os.path.join(RESDIR, mapimg), pos=(DISPCENTRE[0],DISPCENTRE[1]+250), scale=0.5)
# loop through runs
for i, currRun in enumerate(trials):
disp.fill(inter_run) # fill display
t = disp.show()# show display
event_log.write([t, "run %d onset" % (i)])
### CONTINUE WHEN BUTTON PRESSED ###
if MRI: # if MEG repeatedly loop until button state changes
button, t1 = trigbox.wait_for_button_press(allowed=[MAIN_BUT], timeout=None)
t1 = timer.get_time()
# btn_pressed = False # set flag to false
# while btn_pressed != True:
# btn_list, state = trigbox.get_button_state(button_list = [MAIN_BUT])
# # State turns to False, the button was pressed.
# if state[0] == False:
# btn_pressed = True
else:
mousebutton, clickpos, t1 = mouse.get_clicked()
event_log.write([t1, "buttonpress"])
# loop through blocks
for ii, currBlock in enumerate(currRun):
# Inter-block break screen.
#inter block screen
inter_block = Screen()
txt = \
"""
This is a break, take as long as you need
PRESS ANY BUTTON TO START THE BLOCK
"""
inter_block.draw_image(os.path.join(RESDIR, mapimg), pos=(DISPCENTRE[0],DISPCENTRE[1]+50), scale=0.5)
inter_block.draw_text(txt, fontsize=36, pos=(DISPCENTRE[0],DISPCENTRE[1]-200))
left = int(((1-i) * len(currRun)) + int(len(currRun) - ii))
inter_block.draw_text(str(left) + " blocks left", fontsize=36, pos=(DISPCENTRE[0],DISPCENTRE[1]+350), colour='green')
prg = 1.0 - (float(left) / float((len(trials)*len(currRun))))
#draw fill with proportion
inter_block.draw_rect(colour = 'green', x=DISPSIZE[0]*0.1, y=DISPCENTRE[1]+220, w=(DISPSIZE[0]*0.8)*prg, h=99, pw=1, fill=True)
#draw empty square
inter_block.draw_rect(x=DISPSIZE[0]*0.1, y=DISPCENTRE[1]+220, w=DISPSIZE[0]*0.8, h=100, pw=1)
# Countdown before a the pause screen to make sure that BOLD is down to
# baseline.
scr.clear()
scr.draw_text("Please wait for %d seconds..." % \
(numpy.ceil(INTERBLOCK_MIN_PAUSE/1000.0)), fontsize=24)
disp.fill(scr)
t0 = disp.show()
event_log.write([t, "block %d onset " % (ii) + currBlock[0][5]])
t1 = copy.deepcopy(t0)
while t1 - t0 < INTERBLOCK_MIN_PAUSE:
scr.clear()
scr.draw_text("Please wait for %d seconds..." % \
(numpy.ceil((INTERBLOCK_MIN_PAUSE-(t1-t0))/1000.0)), fontsize=24)
disp.fill(scr)
t1 = disp.show()
timer.pause(100)
disp.fill(scr); # fill display
disp.show() # show display
disp.fill(inter_block)
disp.show()
### CONTINUE WHEN BUTTON PRESSED ###
if MRI: # if MEG repeatedly loop until button state changes
button, t1 = trigbox.wait_for_button_press(allowed=[MAIN_BUT], timeout=None)
t1 = timer.get_time()
# btn_pressed = False # set flag to false
# while not btn_pressed:
# btn_list, state = trigbox.get_button_state(button_list=[MAIN_BUT])
# if not state[0]:
# btn_pressed = True
else:
mousebutton, clickpos, t1 = mouse.get_clicked()
event_log.write([t1, "buttonpress"])
# Wait for Sync
# adjusted_ITI = currTrial[2] - synctime
if MRI:
t, triggered, timed_out = trigbox.wait_for_sync(timeout=None)
event_log.write([timer.get_time(), "MRI pulse"])
# i = 0
# triggered = False
# while not triggered:
# t, triggered, timed_out = trigbox.wait_for_sync(timeout=60.0)
# i += 1
# if i > 3:
# print("DEBUG: TOO MANY TIMEOUTS! BROKEN SYNC!")
# break
#loop through trials
for iii, currTrial in enumerate(currBlock):
key, presstime = kb.get_key(keylist=['q', 'f', 'j'], timeout=1, flush=False)
if key == 'q':
event_log.write([presstime, "DEBUG KILL"])
log.close()
tracker.close()
disp.close()
raise Exception('DEBUG KILL')
"""trial[runNo][blockNo][trialNo][infoNo]
infoNo is:
0 = distractor filename
1 = trial type
2 = ITI
3 = Stimonset
4 = screens
0 = face only screen
1 = face + signal screen
"""
#display ITI and pause for length of ITI
disp.fill(fix_screen)
iti_onset = disp.show()
event_log.write([iti_onset, "trial %d onset" % iii])
timer.pause(currTrial[2]) #ITI delay
#display face only and pause for the stimulus onset
disp.fill(currTrial[4][0])
stim_onset = disp.show()
event_log.write([stim_onset, "stim onset"])
timer.pause(currTrial[3])
#display face and signal and pause for the remainder of face duration
disp.fill(currTrial[4][1])
signal_onset = disp.show()
event_log.write([signal_onset, "signal onset"])
#timer.pause(FACE_DURATION - currTrial[3])
# response window and wait for response or timeout
#disp.fill(fix_screen)
#response_onset = disp.show()
t1 = copy.copy(signal_onset)
if MRI: # if MRI repeatedly loop until button state changes or response timeout is met
button, t1 = trigbox.wait_for_button_press(allowed=[MAIN_BUT], timeout=RESPONSE_TIMEOUT/1000.0)
t1 = timer.get_time()
if button is not None:
event_log.write([t1, "button press"])
# btn_pressed = False # set flag to false
# while btn_pressed != True and t1 - signal_onset < RESPONSE_TIMEOUT:
# btn_list, state = trigbox.get_button_state(button_list = [MAIN_BUT])
# t1 = timer.get_time()
# if state[0] != 0:
# btn_pressed = True
else:
button, pos, t1, = mouse.get_clicked(timeout = RESPONSE_TIMEOUT)
if button is not None:
event_log.write([t1, "button press"])
# Turn the signal grey if a press occurred.
if button is not None:
disp.fill(currTrial[4][2])
time_resp = disp.show()
else:
time_resp = "NaN"
rt = t1 - signal_onset
if rt >= RESPONSE_TIMEOUT:
keypress = 0
else:
keypress = 1
time_left = RESPONSE_TIMEOUT - rt
timer.pause(time_left)
# log this trial
if currTrial[1] == 'go':
if button is not None:
accu = 1
else:
accu = 0
else:
if button is not None:
accu = 0
else:
accu = 1
#log.write(["trialnr", "block", "run","stim", "keypress", "go_nogo", "face_onset", "signal_onset","resp_onset", "RT", "accuracy", "respmap"])
#log.write(["trialnr", "block","run", "stim", "keypress", "go_nogo", "face_onset", "signal_onset","resp_onset", "RT", "accuracy", "respmap"])
#log.write([str(iii), str(ii), str(i), currTrial[0], str(keypress), currTrial[1], str(stim_onset), str(signal_onset), str(time_resp), str(rt), str(accu), str(RESPMAP), currTrial[5]])
log.write([iii, ii, i, currTrial[0], keypress, currTrial[1], stim_onset, signal_onset, time_resp, rt, accu, RESPMAP, currTrial[5]])
#Ending stuff
log.close()
event_log.close()
tracker.close()
disp.close()