/
grating.py
261 lines (203 loc) · 9.44 KB
/
grating.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
from psychopy import visual, core, event, logging
import numpy as np
import time
import subprocess, sys
# change the logger path as required
logging.console.setLevel(logging.CRITICAL)
log = logging.LogFile("/media/zfishlab/Data/FlyCapture/stimuli_timing.log", level=logging.CRITICAL,filemode='w')
globalClock = core.MonotonicClock(start_time=0)
logging.setDefaultClock(globalClock)
def write_file(output_name,list_name):
csvfile = str(output_name)
with open(csvfile,"w") as output:
writer = csv.writer(output,lineterminator = '\n')
for n in list_name:
if isinstance(n,list):
writer.writerow(n)
elif isinstance(n,dict):
writer.writerow([n["x"],n["y"],n["t"]])
else:
writer.writerow([n])
# These are the dimensions of the plate.
# DO NOT CHANGE!
width_plate = 620*1.56
height_plate = 400*1.61
grating_width = 380*0.85
grating_height = 568*0.85
white_duration=-1
duration_max=3600
switched=False
'''
Request for the duration of the capture from the user
'''
capture_duration=15
if (len(sys.argv) == 2):
capture_duration=(int)(sys.argv[1])
#print('Capture requested for %d seconds'%(capture_duration))
###
speed=0.01
pix_per_cycle=160
duration_one=60
duration_two=60
# "+","-",None
direction="-"
###
scalingFactor = 70.0
wheel = np.asarray([[-1,0],[0,0],[-1/np.sqrt(2),1/np.sqrt(2)],[0,0],[0,1],[0,0],[1/np.sqrt(2),1/np.sqrt(2)],[0,0],[1,0],[0,0],[1/np.sqrt(2),-1/np.sqrt(2)],[0,0],[0,-1],[0,0],[-1/np.sqrt(2),-1/np.sqrt(2)],[0,0],[-1,0]])
#mywin=visual.Window([1280,1024],monitor="Dell Inc. 17",units="pix",fullscr=True,screen=1)
#plate=visual.Rect(win=mywin,size=(width_plate,height_plate),lineColor=[0,0,0],lineColorSpace="rgb255",lineWidth=4)
#white=visual.ImageStim(win=mywin,image="Solid_white.png",size=(1280,1024),pos=[0,0])
mywin=None
plate=None
white=None
#background=visual.Circle(win=mywin,radius=1, fillColor='red')
#rad = visual.RadialStim(win=mywin,mask='circle',size=(20,20),pos=[20,20])
log_file=[]
timer=core.Clock()
def startCamera(nframes):
'''
Link to (and initiate) capture from the camera and pause for a few (3) seconds before
displaying the stimuli on the screen
'''
# the duration is in frames -- need to convert it into seconds based on the screen's
# refresh rate
nSec = nframes/60.0 # assuming 60 Hz emprically validated
nSec = nSec + 10
args = ("/usr/src/flycapture/bin/SaveImageToAviEx",str(nSec))
p = subprocess.Popen(args)
time.sleep(3)
def displayCircularStimuli(directions,colors,colors_wheel,thicknesses,speeds,duration):
global mywin, plate, white
mywin=visual.Window([1280,1024],monitor="Dell Inc. 17",units="pix",fullscr=False,screen=1,color='white')
plate=visual.Rect(win=mywin,size=(width_plate,height_plate),lineColor=[0,0,0],lineColorSpace="rgb255",lineWidth=4)
white=visual.ImageStim(win=mywin,image="Solid_white.png",size=(1280,1024),pos=[0,0])
ops = []
for d in directions:
if d == 'Clockwise':
ops.append('+')
else:
ops.append('-')
shape1 = visual.ShapeStim(mywin,units='', lineWidth=thicknesses[0], lineColor=colors_wheel[0], lineColorSpace='rgb', fillColor='red', fillColorSpace='rgb', vertices=np.multiply(wheel,scalingFactor), windingRule=None, closeShape=True, pos=(-width_plate/6.4,height_plate/8.2), size=1, ori=0.0, opacity=1.0, contrast=1.0, depth=0, interpolate=True, name=None, autoLog=None, autoDraw=False)
shape1b = visual.Circle(mywin,radius=scalingFactor,fillColor=colors[0],pos=(-width_plate/6.4,height_plate/8.2))
shape2 = visual.ShapeStim(mywin,units='', lineWidth=thicknesses[1], lineColor=colors_wheel[1], lineColorSpace='rgb', fillColor='red', fillColorSpace='rgb', vertices=np.multiply(wheel,scalingFactor), windingRule=None, closeShape=True, pos=(0,height_plate/8.2), size=1, ori=0.0, opacity=1.0, contrast=1.0, depth=0, interpolate=True, name=None, autoLog=None, autoDraw=False)
shape2b = visual.Circle(mywin,radius=scalingFactor,fillColor=colors[1],pos=(0,height_plate/8.2))
shape3 = visual.ShapeStim(mywin,units='', lineWidth=thicknesses[2], lineColor=colors_wheel[2], lineColorSpace='rgb', fillColor='red', fillColorSpace='rgb', vertices=np.multiply(wheel,scalingFactor), windingRule=None, closeShape=True, pos=(width_plate/6.4,height_plate/8.2), size=1, ori=0.0, opacity=1.0, contrast=1.0, depth=0, interpolate=True, name=None, autoLog=None, autoDraw=False)
shape3b = visual.Circle(mywin,radius=scalingFactor,fillColor=colors[2],pos=(width_plate/6.4,height_plate/8.2))
shape4 = visual.ShapeStim(mywin,units='', lineWidth=thicknesses[3], lineColor=colors_wheel[3], lineColorSpace='rgb', fillColor='red', fillColorSpace='rgb', vertices=np.multiply(wheel,scalingFactor), windingRule=None, closeShape=True, pos=(-width_plate/6.4,-height_plate/8.2), size=1, ori=0.0, opacity=1.0, contrast=1.0, depth=0, interpolate=True, name=None, autoLog=None, autoDraw=False)
shape4b = visual.Circle(mywin,radius=scalingFactor,fillColor=colors[3],pos=(-width_plate/6.4,-height_plate/8.2))
shape5 = visual.ShapeStim(mywin,units='', lineWidth=thicknesses[4], lineColor=colors_wheel[4], lineColorSpace='rgb', fillColor='red', fillColorSpace='rgb', vertices=np.multiply(wheel,scalingFactor), windingRule=None, closeShape=True, pos=(0,-height_plate/8.2), size=1, ori=0.0, opacity=1.0, contrast=1.0, depth=0, interpolate=True, name=None, autoLog=None, autoDraw=False)
shape5b = visual.Circle(mywin,radius=scalingFactor,fillColor=colors[4],pos=(0,-height_plate/8.2))
shape6 = visual.ShapeStim(mywin,units='', lineWidth=thicknesses[5], lineColor=colors_wheel[5], lineColorSpace='rgb', fillColor='red', fillColorSpace='rgb', vertices=np.multiply(wheel,scalingFactor), windingRule=None, closeShape=True, pos=(width_plate/6.4,-height_plate/8.2), size=1, ori=0.0, opacity=1.0, contrast=1.0, depth=0, interpolate=True, name=None, autoLog=None, autoDraw=False)
shape6b = visual.Circle(mywin,radius=scalingFactor,fillColor=colors[5],pos=(width_plate/6.4,-height_plate/8.2))
mywin.winHandle.maximize()
mywin.winHandle.set_fullscreen(True)
mywin.winHandle.activate()
# display white
while True:
white.draw()
plate.draw()
mywin.flip()
if event.waitKeys(0.5)==["escape"]:
break
event.clearEvents()
startCamera(duration)
clock = core.Clock()
for frameN in range(duration):
white.draw()
plate.draw()
shape1.setOri(speeds[0],operation=ops[0])
shape1b.draw()
shape1.draw()
shape2.setOri(speeds[1],operation=ops[1])
shape2b.draw()
shape2.draw()
shape3.setOri(speeds[2],operation=ops[2])
shape3b.draw()
shape3.draw()
shape4.setOri(speeds[3],operation=ops[3])
shape4b.draw()
shape4.draw()
shape5.setOri(speeds[4],operation=ops[4])
shape5b.draw()
shape5.draw()
shape6.setOri(speeds[5],operation=ops[5])
shape6b.draw()
shape6.draw()
mywin.logOnFlip(level=logging.CRITICAL, msg='sent on actual flip')
mywin.flip()
for frameN in range(300):
white.draw()
plate.draw()
mywin.flip()
mywin.close()
core.quit()
def displayGrating(grating,t1,speed1,dir1,t2,speed2,dir2):
mywin.winHandle.maximize()
mywin.winHandle.set_fullscreen(True)
mywin.winHandle.activate()
# display white
while True:
white.draw()
plate.draw()
mywin.flip()
if event.waitKeys(0.5)==["escape"]:
break
event.clearEvents()
startCamera(t1+t2)
t=0
while (t < (t1+t2)):
white.draw()
if (t < t1):
grating.setPhase(speed1,dir1)
else:
grating.setPhase(speed2,dir2)
grating.draw()
if (t==0) or (t==t1) or (t==(t1+t2-1)):
mywin.logOnFlip(level=logging.CRITICAL, msg='sent on actual flip')
mywin.flip()
t=t+1
for frameN in range(300):
white.draw()
plate.draw()
mywin.flip()
mywin.close()
def displayHorizontalGrating(t1,speed1,dir1,t2,speed2,dir2,thickness):
global mywin,plate,white
mywin=visual.Window([1280,1024],monitor="Dell Inc. 17",units="pix",fullscr=False,screen=1,color='white')
plate=visual.Rect(win=mywin,size=(width_plate,height_plate),lineColor=[0,0,0],lineColorSpace="rgb255",lineWidth=4)
white=visual.ImageStim(win=mywin,image="Solid_white.png",size=(1280,1024),pos=[0,0])
#startCamera(t1+t2)
grating=visual.GratingStim(win=mywin,mask=None,size=(grating_width,grating_height),ori=270,color=[80,80,80],colorSpace="rgb255",pos=[0,0],sf=(1.0/pix_per_cycle)*thickness)
displayGrating(grating,t1,speed1,dir1,t2,speed2,dir2)
def displayVerticalGrating(t1,speed1,dir1,t2,speed2,dir2,thickness):
global mywin, plate,white
mywin=visual.Window([1280,1024],monitor="Dell Inc. 17",units="pix",fullscr=False,screen=1,color='white')
plate=visual.Rect(win=mywin,size=(width_plate,height_plate),lineColor=[0,0,0],lineColorSpace="rgb255",lineWidth=4)
white=visual.ImageStim(win=mywin,image="Solid_white.png",size=(1280,1024),pos=[0,0])
#startCamera(t1+t2)
grating=visual.GratingStim(win=mywin,mask=None,size=(grating_height,grating_width),ori=0,color=[80,80,80],colorSpace="rgb255",pos=[0,0],sf=(1.0/pix_per_cycle)*thickness)
displayGrating(grating,t1,speed1,dir1,t2,speed2,dir2)
def displayNoStimuli(duration):
global mywin, plate, white
mywin=visual.Window([1280,1024],monitor="Dell Inc. 17",units="pix",fullscr=False,screen=1,color='white')
plate=visual.Rect(win=mywin,size=(width_plate,height_plate),lineColor=[0,0,0],lineColorSpace="rgb255",lineWidth=4)
white=visual.ImageStim(win=mywin,image="Solid_white.png",size=(1280,1024),pos=[0,0])
mywin.winHandle.maximize()
mywin.winHandle.set_fullscreen(True)
mywin.winHandle.activate()
# display white
while True:
white.draw()
plate.draw()
mywin.flip()
if event.waitKeys(0.5)==["escape"]:
break
event.clearEvents()
startCamera(duration)
t=0
while (t < duration):
white.draw()
plate.draw()
mywin.flip()
t=t+1
mywin.close()