/
My_Tapping_and_vis.py
236 lines (190 loc) · 10.1 KB
/
My_Tapping_and_vis.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
#!/usr/bin/env python
from psychopy import core, visual, event, gui, logging
#Renzo setup for first ecperiment
import time
import datetime
import os
import numpy
import string
import AppKit
def screenRes(myScreen):
screens = AppKit.NSScreen.screens() # get list of screen objects
screenRes = (int(screens[myScreen].frame().size.width), int(screens[myScreen].frame().size.height))
print "screenRes = [%d,%d]"%screenRes
return screenRes
# Default Definitions
screen_to_show = 1 # Choose monitor [0 = Laptop | 1 = Second Monitor]
triggerKey = 't'
exitKey = 'q'
useEyeTracker = True
doCalibration = True
autoScreenSize = True
outputPrefix = 'output'
numTrials=12 #Number of trials of stimulation+rest blocks
bufferDur=0 #Duration of additional fixation period immediately after scan starts and following last rest block [TRs]
visStimDur=20 #Duration of flickering checkerboard blocks
restDur=20
defaultScreenRes = [800,600]
# Ask user
myFields = {'Monitor':['External','Laptop'],'Auto Screen Size Detection':['Yes','No'],'Log Prefix':outputPrefix,'Trigger':triggerKey,'Numer of trials':numTrials,'initial rest [TR]':bufferDur,'rest dur [TR]':restDur,'activity dur [TR]':visStimDur}
configDlg = gui.DlgFromDict(dictionary=myFields, title='Run Configuration',order=['Monitor','Auto Screen Size Detection','Log Prefix','Trigger'])
if (configDlg.OK==False):
print 'User Cancelled'
core.quit()
# Import visual here to avoid silly issue with GUI not selecting drop list options properly
from psychopy import visual
from psychopy.visual import DotStim
# Read values from the user GUI
if myFields.get('Monitor')=='Laptop':
screen_to_show=0
else:
screen_to_show=0
screenSize=[0,0]
if myFields.get('Auto Screen Size Detection')=='No':
autoScreenSize = False
screenSize = defaultScreenRes
else:
autoScreenSize = True
screenSize = screenRes(screen_to_show)
outputPrefix = myFields.get('Log Prefix')
triggerKey = myFields.get('Trigger')
numTrials= myFields.get('Numer of trials')
bufferDur= myFields.get('initial rest [TR]')
restDur=myFields.get('rest dur [TR]')
visStimDur= myFields.get('activity dur [TR]')
# Print Selection
print "Selected Monitor: %i" % (screen_to_show)
print "Auto Screen Size Detection (True/False): %s" % (str(autoScreenSize))
print "Screen Size: (%i,%i)" % (screenSize[0],screenSize[1])
# Create Log File
LogFileName='../ResponseLogs/'+outputPrefix+'_ResponseLog.txt';
Logger=logging.LogFile(LogFileName,34,'w');
params = {'portName': '/dev/tty.usbserial', 'portBaud': 115200, 'screenColor': (0,0,0), 'textColor': (255,255,255), 'screenSize':screenRes, 'screen_to_show':screen_to_show}
print params
# CREATE SCREEN
# ================
win=visual.Window([screenSize[0],screenSize[0]],fullscr=False,allowGUI=False,monitor='testMonitor',screen=screen_to_show,units='pix');
# STIMULI PARAMETERS
#=========================
stimA = visual.RadialStim(win,tex="sqrXsqr", ori=0, color=1, mask="none",units='deg',pos=(0,0),texRes=256,angularRes=200,
size=[100,100], visibleWedge=[0, 360], radialCycles=35, angularCycles=12,name='stimA')
stimAInt=visual.RadialStim(win,tex="sqrXsqr", ori=0, color=1, mask="none",units='deg',pos=(0,0),texRes=256,angularRes=200,
size=[20,20], visibleWedge=[0, 360], radialCycles=10, angularCycles=12,name='stimAInt')
stimACent=visual.RadialStim(win,tex="sqrXsqr", ori=0, color=1, mask="none",units='deg',pos=(0,0),texRes=256,angularRes=200,
size=[8,8], visibleWedge=[0, 360], radialCycles=6, angularCycles=12,name='stimAInt')
stimB = visual.RadialStim(win,tex="sqrXsqr", ori=0, color=-1, mask="none",units='deg',pos=(0,0),texRes=256,angularRes=200,
size=[100,100], visibleWedge=[0, 360], radialCycles=35, angularCycles=12,name='stimB')
stimBInt=visual.RadialStim(win,tex="sqrXsqr", ori=0, color=-1, mask="none",units='deg',pos=(0,0),texRes=256,angularRes=200,
size=[20,20], visibleWedge=[0, 360], radialCycles=10, angularCycles=12,name='stimAInt')
stimBCent=visual.RadialStim(win,tex="sqrXsqr", ori=0, color=-1, mask="none",units='deg',pos=(0,0),texRes=256,angularRes=200,
size=[8,8], visibleWedge=[0, 360], radialCycles=6, angularCycles=12,name='stimAInt')
flashRate = 0.125 #Frequency of flicker in seconds (i.e. 8 Hz = 0.125 s)
xhr = visual.ShapeStim(win,lineColor='#000000',lineWidth=3.0,vertices=((-30,0),(30,0),(0,0),(0,30),(0,-30)),units='pix',closeShape=False,name='rest');
# =========== INSTRUCTIONS ==============
# ==============================================================
Instr_Rest_T='experiment will start shortly';
Instr_Rest_GRAPPA= 'do\'t move for a moment';
Instr_Rest_GRAPPA1= 'GRAPPA ref line acq';
Instr_Rest_WAIT= 'initial buffer'; # for initial rest
Instr_TAP='TAPPING';
Instr_RELAX='RELAXING';
Instr_DONE='Finished \nThank you';
Instr_Rest_S=visual.TextStim(win,text=Instr_Rest_T,height=50,units='pix',name='intro', color='black',wrapWidth=600,pos=(0,0));
Instr_Rest_GRAPPA=visual.TextStim(win,text=Instr_Rest_GRAPPA,height=50,units='pix',name='intro', color='black',wrapWidth=600,pos=(0,0));
Instr_Rest_GRAPPA1=visual.TextStim(win,text=Instr_Rest_GRAPPA1,height=10,units='pix',name='intro', color='black',wrapWidth=600,pos=(0,-100));
Instr_Rest_WAIT=visual.TextStim(win,text=Instr_Rest_WAIT,height=10,units='pix',name='intro', color='black',wrapWidth=600,pos=(0,-100));
Instr_TAP=visual.TextStim(win,text=Instr_TAP,height=100,units='pix',name='intro', color='black',wrapWidth=600,pos=(0,0));
Instr_RELAX=visual.TextStim(win,text=Instr_RELAX,height=100,units='pix',name='intro', color='black',wrapWidth=600,pos=(0,0));
Instr_DONE=visual.TextStim(win,text=Instr_DONE,height=100,units='pix',name='intro', color='black',wrapWidth=600,pos=(0,0));
Instr_Rest_Window=visual.Rect(win,width=300,height=200,pos=(0,-90),lineColor='black',lineWidth=3);
Instr_Rest_Crosshair=visual.ShapeStim(win,lineColor='#000000',lineWidth=3.0,vertices=((-30,-90),(30,-90),(0,-90),(0,-60),(0,-120)),units='pix',closeShape=False);
# SETTING GENERAL CLOCK AND LOGGING
# =========================================
clock=core.Clock();
logging.setDefaultClock(clock) #use this for timing of log messages, although these shouldn't be used for experimental events
logging.console.setLevel(logging.DATA) #set the console to receive nearly all messges
Logger=logging.LogFile(LogFileName,logging.DATA,'w');
win.setRecordFrameIntervals(True); #capture frame intervals
win.saveFrameIntervals(fileName=LogFileName, clear=False); #write frame intervals to LogFileName
# (0) PRINT FIRST SET OF INSTRUCTIONS AND WAIT FOR TRIGGER
# ==================================================================
win.logOnFlip('INITIAL INSTRUCTIONS',logging.DATA);
Instr_Rest_S.draw();win.flip(); # Plot Instructions.
event.waitKeys(keyList=['return','t']); # Wait for Scanner Trigger.
Instr_Rest_GRAPPA.draw();Instr_Rest_GRAPPA1.draw();win.flip();
event.waitKeys(keyList=['t']);
clock.reset();
Exp_Start_Time=clock.getTime(); # Record Scanning State Time
# BEGIN EXPERIMENT
#=====================
numbtrigger=0
while numbtrigger<bufferDur:
for keys in event.getKeys(timeStamped=True): #handle key presses each frame
if keys[0]in ['escape','q']:
win.close()
core.quit()
Instr_RELAX.draw();Instr_Rest_WAIT.draw();win.flip();
event.waitKeys(keyList=['t'])
numbtrigger=numbtrigger+1
#xhr.setAutoDraw(True); #Draw the crosshair every time the screen flips
#xhr.draw();
#win.flip();
#win.logOnFlip('[Starting Buffer] starts FRAME TIME = {0}'.format(win.lastFrameT),logging.DATA);
#count=0
numbtrigger=0
numbtrials=0
while numbtrials<numTrials:
numbtrials=numbtrials+1
numbtrigger=0
while numbtrigger<restDur:
for keys in event.getKeys(timeStamped=True): #handle key presses each frame
if keys[0]in ['escape','q']:
win.close()
core.quit()
Instr_RELAX.draw();win.flip();
event.waitKeys(keyList=['t'])
numbtrigger=numbtrigger+1
numbtrigger=0
while numbtrigger<visStimDur:
for keys in event.getKeys(timeStamped=True): #handle key presses each frame
if keys[0]in ['escape','q']:
win.close()
core.quit()
#stimBCent.draw();win.flip();
Instr_TAP.draw();win.flip();
event.waitKeys(keyList=['t'])
numbtrigger=numbtrigger+1
numbtrigger=0
while numbtrigger<100:
for keys in event.getKeys(timeStamped=True): #handle key presses each frame
if keys[0]in ['escape','q']:
win.close()
core.quit()
Instr_DONE.draw();win.flip();
event.waitKeys(keyList=['t'])
numbtrigger=numbtrigger+1
#for i in range(numTrials):
# elapsedTime=elapsedTime+visStimDur;
# win.flip();
# win.logOnFlip('[Stim Block {0}] starts FRAME TIME = {1}'.format(i,(win.lastFrameT)),logging.DATA);
# while clock.getTime()<elapsedTime: #Block of flickering checkerboard
# t=clock.getTime()
# if (t%flashRate) < (flashRate/2.0):
# displayStim1 = stimA
# displayStim2 = stimAInt
# displayStim3 = stimACent
# else:
# displayStim1 = stimB
# displayStim2 = stimBInt
# displayStim3 = stimBCent
# displayStim1.draw()
# displayStim2.draw()
# displayStim3.draw()
# win.flip()
# for keys in event.getKeys(timeStamped=True): #handle key presses each frame
# if keys[0]in ['escape','q']:
# win.close()
# core.quit()
win.flip()
win.close()
core.quit()