-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRPScript_full_bhv_newtimer.py
More file actions
346 lines (314 loc) · 14.6 KB
/
RPScript_full_bhv_newtimer.py
File metadata and controls
346 lines (314 loc) · 14.6 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
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
# Reward and Punishment Psychopy version
# adapted from Zhihao & Jennifer's e-prime script
from psychopy import visual, core, event, data, monitors, tools
from randomizer import randomizeOrder
import random, datetime, time,itertools,math
from copy import deepcopy
import sys, datetime
import os
import numpy as np
from psychopy.iohub import launchHubServer
import scipy.io as sio
def RPbhv(subjectID):
currentTime=str('%.0f' % time.time())
currentDirectory=os.path.dirname(os.path.realpath(__file__))
fileName = currentDirectory + '/data/ETRP_' + str(subjectID) +'/ETRP_' + currentTime + '_' + str(subjectID)
if not os.path.isdir('data/ETRP_' + str(subjectID)):
os.makedirs('data/ETRP_'+ str(subjectID))
dataFile = open(fileName+'.csv', 'w')
dataFile.write('subjID,Block,TrialNum,StimuliRefNum,Type,Level,Actualization,CueRating,OutcomeRating,CueMarkerStartPos,OutcomeMarkerStartPos,CueRT,RewardRT,TrialDur,Delay,ITIjitter\n')
win = visual.Window(color=[0,0,0], screen = 1, fullscr=True, mouseVisible = False)
startKey = '5'
# Set up rating scale stimuli
buttons=[]
x=-0.8
for b in range(9):
buttons.append(visual.TextStim(win, alignHoriz='center',units = 'norm',text=str(b+1),pos=(x,-0.7), height=0.1,font='Courier New',color = 'white'))
x+=0.2
labels=[]
labels.append(visual.TextStim(win, alignHoriz='center',text='Unpleasant',units = 'norm',pos=(-0.8,-0.8),font='Courier New', height=0.08,color='white'))
labels.append(visual.TextStim(win, alignHoriz='center',text='Pleasant',units = 'norm',pos=(0.8,-0.8), font='Courier New', height=0.08,color='white'))
# Set up rating marker stimuli
xpos=0
xposList = [-0.8,-0.6,-0.4,-0.2,0,0.2,0.4,0.6,0.8]
rect = visual.Rect(
win=win,units="norm",width=0.2,height=0.15,lineWidth = 4,fillColor=None,lineColor='Grey',pos=(xpos,-0.7))
# Text stimuli setup, default as a + mark
Text = visual.TextStim(win,text=u"+", height = 0.08, wrapWidth = 1,font='Courier New',
alignHoriz = 'center', alignVert = 'center', color = 'white',units = 'norm')
# Fixation dot
fixationCircle=visual.Circle(win, radius=10, fillColor='black',lineColor='black',units = 'pix')
# Ready screen
Ready = visual.ImageStim(win,image = "images/Ready.png",size = (1.25,1), units = 'norm')
# Trial randomizer for the whole 96 trials; trials will be further randomized to follow designated rules in each block
trialList = list(range(1,97))
oList1 = list(range(1,len(trialList)-4,6))
oList2 = list(range(2,len(trialList)-3,6))
outcomeList = oList1+oList2
outcomeList.sort()
NonOutcome = list(set(outcomeList) ^ set(trialList))
x = 0
NOList = []
OList = []
while x < 1:
NOList.append(sorted(set(NonOutcome[int(x*len(NonOutcome)):int((x+0.25)*len(NonOutcome))])))
OList.append(sorted(set(outcomeList[int(x*len(outcomeList)):int((x+0.25)*len(outcomeList))])))
x += 0.25
refNOList = deepcopy(NOList)
refOList = deepcopy(OList)
#shuffle within each type
for s in range(len(NOList)):
random.shuffle(NOList[s])
random.shuffle(OList[s])
# Block counter:
for block in range(1,5):
# Order randomization using "randomizeOrder" function:
Index = randomizeOrder(24)
# a list of index number (1-24), shuffled so that there is no more than two trials from the same category
idxList = Index['typeA']+Index['typeB']+Index['typeC']+Index['typeD']
# a list of index number [1,2,7,8,13,14,19,20] for outcome trials, shuffled;
idxListO = Index['outcomeA']+Index['outcomeB']+Index['outcomeC']+Index['outcomeD']
# create a trial list of randomized reference number (24 out of 1~96) based on randomization
trialList = []
for t in range(4):
olist=[]
tlist=[]
olist= OList[t][0:2]
for p in range(2):
OList[t].pop(0)
tlist=NOList[t][0:4]
for q in range(4):
NOList[t].pop(0)
trialList += olist+tlist
orderList = []
for i in idxList:
orderList.append(trialList[i-1])
# Set up time components, shuffle delay and ITI jitter within block
Delay = list(itertools.repeat(1,8))+list(itertools.repeat(2,8))+list(itertools.repeat(3,8)) # 1,2,3
random.shuffle(Delay)
ITI = list(itertools.repeat(4,8))+list(itertools.repeat(6,8))+list(itertools.repeat(8,8)) # change back to int if getting rid of 100 wait at the end of each trial; 4,6,8
random.shuffle(ITI)
CueDur = 6 # for the task, 6
OutcomeDur = 4 # for the task, 4
if block is 4:
MonetaryOutcome = refOList[2]+refOList[3]
random.shuffle(MonetaryOutcome)
orderList.append(MonetaryOutcome[0])
MonOutcomeTime = [2,6]
Delay.append(MonOutcomeTime[0])
ITI.append(MonOutcomeTime[1])
# Block start
event.clearEvents()
Rsp = False
while Rsp == False:
Text.height= 0.08
Text.text = 'Start of block.'
Text.draw()
win.flip()
buttonPress=event.getKeys(keyList = ['7'])
if len(buttonPress)>0:
Rsp = True
event.clearEvents()
Rsp = False
while Rsp == False:
Text.height= 0.08
Text.text = 'Waiting for the scanner to start...'
Text.draw()
win.flip()
buttonPress=event.getKeys(keyList = ['5'])
if len(buttonPress)>0:
Rsp = True
# Ready screen
Clock = core.Clock()
Clock.add(5)
while Clock.getTime()<0:
Ready.draw()
win.flip()
# ITI
#Clock.reset()
Clock.add(2)
while Clock.getTime() < 0:
fixationCircle.draw()
win.flip()
n = 0 # n = 0 for indexing purposes
while n < len(orderList): #len(orderList)
trialClock = core.MonotonicClock()
#Clock.reset()
trialNum = n+1 # for the purpose of indcating the correct trial
# the Grey box will start at a random number for each trial
markStart = random.choice(xposList)
rect.lineColor = 'White'
xpos = markStart
rect.pos=(xpos,-0.7)
typ=[]
lvl=[]
#print('trial',n,'orderList[n]',orderList[n])
if orderList[n] not in outcomeList:
Act = 0
# Determine type and level
for c in refNOList:
if orderList[n] in c:
typ = refNOList.index(c)+1
# from a list of 16 numbers, divided by 4 and ceil, to find level
lvl = math.ceil((c.index(orderList[n])+1)/4)
# Cue setup
Cue = visual.ImageStim(win,image='images/typ'+str(typ)+'lv'+str(lvl)+'.png', size = (1.25,1), units = 'norm')
OutcomeStim = visual.TextStim(win,text=u"No Outcome", height = 0.1, wrapWidth = 1,font='Courier New', alignHoriz = 'center',
alignVert = 'center', color = 'yellow',units = 'norm',pos=(0,-0.6))
else:
Act = 1
for c in refOList:
if orderList[n] in c:
typ = refOList.index(c)+1
# from a list of 8 numbers, divided by 2 and ceil, to find level
lvl = math.ceil((c.index(orderList[n])+1)/2)
Cue = visual.ImageStim(win,image='images/typ'+str(typ)+'lv'+str(lvl)+'.png', size = (1.3,1), units = 'norm')
if typ < 3:
OutcomeStim = visual.ImageStim(win,image = os.path.join('data//ETRP_PicRating_'+str(subjectID)+'//typ'+str(typ)+'lv'+str(lvl)+'o.jpg'),size = (1,0.85),units = 'norm') # size = (0.4,0.5)
else:
OutcomeStim = visual.ImageStim(win,image = 'images/typ'+str(typ)+'lv'+str(lvl)+'o.png',size = (0.65,0.78), units = 'norm')
Delayjit = Delay[n]
ITIjit = ITI[n]
# set stimuli autoDraw to True so they would show up on every frame, avoid screen display twinkle:
for b in buttons:
b.autoDraw = True
for l in labels:
l.autoDraw = True
Cue.autoDraw = True
rect.autoDraw = True
# Cue rating
event.clearEvents()
#print('beforeAdd', Clock.getTime())
#Clock.reset() # clock for rating scale
Clock.add(CueDur)
#print('afterAdd', Clock.getTime())
Rsp = False
while Rsp is False:
win.flip()
buttonPress=event.getKeys(keyList = ['1','2','3'])
if len(buttonPress) > 0 and Clock.getTime() < 0:
# Moving the marker to the left with key 1
if buttonPress == ['1']:
xpos-=0.2
if xpos < -0.8:
xpos = -0.8
rect.pos=(xpos,-0.7)
rect.lineColor = 'Yellow'
win.flip()
# Moving the marker to the right with key 3
if buttonPress == ['3']:
xpos+=0.2
if xpos > 0.8:
xpos = 0.8
rect.pos=(xpos,-0.7)
rect.lineColor = 'Yellow'
win.flip()
# Confirmation key = 2
if buttonPress == ['2']:
Rating = xposList.index(round(xpos,2))+1
RT= CueDur + Clock.getTime() #Clock.getTime()
rect.lineColor = 'Red'
CueRemainDur = CueDur - RT # 0 - RT
win.flip()
core.wait(CueRemainDur)
Rsp = True
elif buttonPress != ['2'] and Clock.getTime() > 0:
#buttonPress != ['2'] and Clock.getTime() > 0:
Rating = 0
RT = Clock.getTime() #-1
Rsp = True
# Set all autoDraw back to False, so that you can present new stimuli on the screen
for b in buttons:
b.autoDraw = False
for l in labels:
l.autoDraw = False
Cue.autoDraw = False
rect.autoDraw = False
# Delay period - fixation point
#Clock.reset()
Clock.add(Delayjit)
while Clock.getTime() < 0:
fixationCircle.draw()
win.flip()
# Outcome display - camera image and outcome image
#Clock.reset()
Clock.add(2)
while Clock.getTime() < 0:
Cue.draw()
OutcomeStim.draw()
win.flip()
# Reward rating
for b in buttons:
b.autoDraw = True
for l in labels:
l.autoDraw = True
rect.autoDraw = True
Rsp = False
RewardMarkStart = random.choice(xposList)
rect.lineColor = 'White'
xpos = RewardMarkStart
rect.pos=(xpos,-0.7)
event.clearEvents()
#Clock.reset() # clock reset adds system time lag
Clock.add(OutcomeDur)
while Rsp is False:
win.flip()
buttonPress=event.getKeys(keyList = ['1','2','3'])
if len(buttonPress) > 0 and Clock.getTime() < 0:
# Moving the marker to the left with key 1
if buttonPress == ['1']:
xpos-=0.2
if xpos < -0.8:
xpos = -0.8
rect.pos=(xpos,-0.7)
rect.lineColor = 'Yellow'
rect.autoDraw = True
win.flip()
# Moving the marker to the right with key 3
if buttonPress == ['3']:
xpos+=0.2
if xpos > 0.8:
xpos = 0.8
rect.pos=(xpos,-0.7)
rect.lineColor = 'Yellow'
rect.autoDraw=True
win.flip()
# Confirmation key = 2, the subject has to move the marker in order to confirm
# (in other words, the marker does not have to be Yellow in order to accept the choice)
if buttonPress == ['2'] and Clock.getTime() < 0: #rect.lineColor is 'Yellow' and
RewardRating = xposList.index(round(xpos,2))+1
RewardRT= OutcomeDur + Clock.getTime() # Clock.getTime()
rect.lineColor = 'Red'
CueRemainDur = OutcomeDur - RewardRT # 0 - RewardRT
win.flip()
core.wait(CueRemainDur)
Rsp=True
elif buttonPress != ['2'] and Clock.getTime() > 0: # (rect.lineColor is 'Grey' and Clock.getTime() > OutcomeDur) or ()
RewardRating = 0
RewardRT = Clock.getTime()#-1
Rsp = True
for b in buttons:
b.autoDraw = False
for l in labels:
l.autoDraw = False
rect.autoDraw = False
# ITI - fixation point
Clock.add(ITIjit)
while Clock.getTime() < 0:
fixationCircle.draw()
win.flip()
TrialDur = trialClock.getTime()
dataFile.write('%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%f,%f,%f,%f,%f\n' %(subjectID,block,trialNum,orderList[n],typ,lvl,Act,Rating,RewardRating,xposList.index(markStart)+1,xposList.index(RewardMarkStart)+1,RT,RewardRT,TrialDur,Delayjit,ITIjit))
n+=1
# Log data
block += 1
event.clearEvents()
Rsp = False
while Rsp == False:
Text.height= 0.08
Text.text = 'End of block.'
Text.draw()
win.flip()
buttonPress=event.getKeys(keyList = ['9'])
if len(buttonPress)>0:
Rsp = True