-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpi_rig.py
More file actions
404 lines (324 loc) · 12 KB
/
pi_rig.py
File metadata and controls
404 lines (324 loc) · 12 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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
'''
pi_rig contrains basic functions for using the raspberry pi behavior and electrophysiology rig in the Katz Lab
These functions can be used directly via ipython in a terminal window or called by other codes
'''
# Import things for running pi codes
import time, random, easygui, os, csv
from math import floor
import RPi.GPIO as GPIO
# Import other things for video
from subprocess import Popen
import numpy as np
# Setup pi board
GPIO.setwarnings(False)
GPIO.cleanup()
GPIO.setmode(GPIO.BOARD)
# To empty taste lines
def clearout(outports=[7, 11, 12, 13, 16, 31, 32, 33, 35, 36, 37, 38, 40, 15, 5, 3, 29], dur=5):
#7 and 11 are for ortho odor vacuum, 32, 36, 38, 40 are rig 2 outports, 13 and 16 are ortho odor input
# Setup pi board GPIO ports
GPIO.setmode(GPIO.BOARD)
for i in outports:
GPIO.setup(i, GPIO.OUT)
for i in outports:
GPIO.output(i, 1)
time.sleep(dur)
for i in outports:
GPIO.output(i, 0)
print('Tastant line clearing complete.')
# To calibrate taste lines
def calibrate(outports=[7, 11, 13, 12, 16, 23, 29, 31, 32, 33, 35, 36, 37, 38, 40], opentime=0.05, repeats=5):
# Setup pi board GPIO ports
GPIO.setmode(GPIO.BOARD)
for i in outports:
GPIO.setup(i, GPIO.OUT)
# Open ports
for rep in range(repeats):
for i in outports:
GPIO.output(i, 1)
time.sleep(opentime)
for i in outports:
GPIO.output(i, 0)
time.sleep(1)
print('Calibration procedure complete.')
# Passive deliveries
def passive(outports=[37, 36, 38, 40, 32, 16, 18],
intaninputs=[15, 19, 21, 23, 11, 12, 13],
opentimes=[0.01, 0.01, 0.01, 0.01, 0.01, 0.01],
itimin=22, itimax=22, trials=30):
# Ask the user for the directory to save the video files in
directory = easygui.diropenbox(msg='Select the directory to save the delivery times from this experiment.', title='Select directory')
# Change to that directory
os.chdir(directory)
# Setup pi board GPIO ports
GPIO.setmode(GPIO.BOARD)
for i in outports:
GPIO.setup(i, GPIO.OUT)
for i in intaninputs:
GPIO.setup(i, GPIO.OUT)
# Set and radomize trial order
tot_trials = len(outports) * trials
count = 0
trial_array = trials * list(np.arange(len(outports)))
time_array = [] #Store delivery times
random.shuffle(trial_array)
time.sleep(15)
print(trial_array)
# Loop through trials
for i in trial_array:
time_array.append(time.ctime())
GPIO.output(outports[i], 1) #opens the solenoid
GPIO.output(intaninputs[i], 1) #changes dig_in signal to 1
time.sleep(opentimes[i]) #waits for opentime
GPIO.output(outports[i], 0) #closes the solenoid
GPIO.output(intaninputs[i], 0) #changes dig_in signal to 0
count += 1
iti = random.randint(itimin, itimax)
print('Trial '+str(count)+' of '+str(tot_trials) +
' completed. ITI = '+str(iti)+' sec.')
time.sleep(iti)
print('Passive deliveries completed')
#Save csv of trial delivery times
csv_name = 'output_times.csv'
with open(csv_name, 'w', newline='') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=' ',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
for r_i in range(len(time_array)):
spamwriter.writerow(time_array[r_i])
print('Delivery times .csv saved.')
def passive_cue(
outports=[7, 11, 13, 16, 31, 32, 33, 35, 36, 37, 38, 40],
intaninputs=[24, 26, 19, 21],
opentimes=[0.01], itimin=10, itimax=30, trials=150,
cue_input = 40):
# Setup pi board GPIO ports
GPIO.setmode(GPIO.BOARD)
for i in outports:
GPIO.setup(i, GPIO.OUT)
for i in intaninputs:
GPIO.setup(i, GPIO.OUT)
GPIO.setup(cue_input, GPIO.OUT)
# Set and radomize trial order
tot_trials = len(outports) * trials
count = 0
#trial_array = trials * range(len(outports))
trial_array = trials * list(np.arange(len(outports)))
time_array = [] #Store delivery times
random.shuffle(trial_array)
time.sleep(3)
# Loop through trials
for i in trial_array:
time_array.append(time.ctime())
GPIO.output(cue_input, 1)
#time.sleep(1) #if you want cue on before taste delivery, uncomment
#GPIO.output(cue_input, 0)
#time.sleep(1)
GPIO.output(outports[i], 1)
GPIO.output(intaninputs[i], 1)
time.sleep(opentimes[i])
GPIO.output(outports[i], 0)
GPIO.output(intaninputs[i], 0)
time.sleep(1)
GPIO.output(cue_input, 0)
count += 1
iti = random.randint(itimin, itimax)
print('Trial '+str(count)+' of '+str(tot_trials) +
' completed. ITI = '+str(iti)+' sec.')
time.sleep(iti)
print('Passive deliveries completed')
#Save csv of trial delivery times
csv_name = 'output_times.csv'
with open(csv_name, 'w', newline='') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=' ',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
for r_i in range(len(time_array)):
spamwriter.writerow(time_array[r_i])
print('Delivery times .csv saved.')
# Basic nose poking procedure to train poking for discrimination 2-AFC task
def basic_np(outport=40, opentime=0.012, iti=[.4, 1, 2], trials=200, outtime=0):
intaninput = 8
trial = 1
inport = 13
pokelight = 15 #edit
houselight = 22 #edit
lights = 0
maxtime = 60
# Setup pi board GPIO ports
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pokelight, GPIO.OUT)
GPIO.setup(houselight, GPIO.OUT)
GPIO.setup(inport, GPIO.IN)
GPIO.setup(outport, GPIO.OUT)
GPIO.setup(intaninput, GPIO.OUT)
time.sleep(15)
starttime = time.time()
while trial <= trials:
# Timer to stop experiment if over 60 mins
curtime = time.time()
elapsedtime = round((curtime - starttime)/60, 2)
if elapsedtime > maxtime:
GPIO.output(pokelight, 0)
GPIO.output(houselight, 0)
break
if lights == 0:
GPIO.output(pokelight, 1)
GPIO.output(houselight, 1)
lights = 1
# Check for pokes
if GPIO.input(inport) == 0:
poketime = time.time()
curtime = poketime
# Make rat remove nose from nose poke to receive reward
while (curtime - poketime) <= outtime:
if GPIO.input(inport) == 0:
poketime = time.time()
curtime = time.time()
# Taste delivery and switch off lights
GPIO.output(outport, 1)
GPIO.output(intaninput, 1)
time.sleep(opentime)
GPIO.output(outport, 0)
GPIO.output(intaninput, 1)
GPIO.output(pokelight, 0)
GPIO.output(houselight, 0)
print('Trial '+str(trial)+' of '+str(trials)+' completed.')
trial += 1
lights = 0
# Calculate and execute ITI delay. Pokes during ITI reset ITI timer.
if trial <= trials/2:
delay = floor((random.random()*(iti[1]-iti[0]))*100)/100+iti[0]
else:
delay = floor((random.random()*(iti[2]-iti[0]))*100)/100+iti[0]
poketime = time.time()
curtime = poketime
while (curtime - poketime) <= delay:
if GPIO.input(inport) == 0:
poketime = time.time()
curtime = time.time()
print('Basic nose poking has been completed.')
def odor_np(outport=40, odorport=36, vacport=37, t_opentime=0.012, o_opentime=0.5, v_opentime=1, iti=[.4, 1, 2], trials=200, outtime=0):
intaninput_t = 8
intaninput_o = 10
intaninput_v = 12
trial = 1
inport = 13
pokelight = 15 #edit
houselight = 22 #edit
# lights = 0
maxtime = 60
# Setup pi board GPIO ports
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pokelight, GPIO.OUT)
GPIO.setup(houselight, GPIO.OUT)
GPIO.setup(inport, GPIO.IN)
GPIO.setup(outport, GPIO.OUT)
GPIO.setup(intaninput_t, GPIO.OUT)
GPIO.setup(odorport, GPIO.OUT)
GPIO.setup(intaninput_o, GPIO.OUT)
GPIO.setup(vacport, GPIO.OUT)
GPIO.setup(intaninput_v, GPIO.OUT)
time.sleep(15)
starttime = time.time()
while trial <= trials:
# Timer to stop experiment if over 60 mins
curtime = time.time()
elapsedtime = round((curtime - starttime)/60, 2)
if elapsedtime > maxtime:
GPIO.output(pokelight, 0)
GPIO.output(houselight, 0)
break
# if lights == 0:
# GPIO.output(pokelight, 1)
GPIO.output(houselight, 1)
# lights = 1
# Check for pokes
if GPIO.input(inport) == 0:
poketime = time.time()
curtime = poketime
#vacuum
GPIO.output(vacport, 1)
GPIO.output(intaninput_v, 1)
time.sleep(0.2) # Overlap vacport with odorport
GPIO.output(odorport, 1)
GPIO.output(intaninput_o, 1)
time.sleep(0.5) # Remaining 0.2 seconds of odorport opentime
GPIO.output(vacport, 0)
GPIO.output(odorport, 0)
GPIO.output(intaninput_v, 0)
GPIO.output(intaninput_o, 0)
# Make rat remove nose from nose poke to receive reward
while (curtime - poketime) <= outtime:
if GPIO.input(inport) == 0:
poketime = time.time()
curtime = time.time()
# Taste delivery and switch off lights
GPIO.output(outport, 1)
GPIO.output(intaninput_t, 1)
time.sleep(t_opentime)
GPIO.output(outport, 0)
GPIO.output(intaninput_t, 1)
# GPIO.output(pokelight, 0)
GPIO.output(houselight, 0)
print('Trial '+str(trial)+' of '+str(trials)+' completed.')
trial += 1
# lights = 0
# Calculate and execute ITI delay. Pokes during ITI reset ITI timer.
delay = np.random.choice(np.arange(30, 40, 1), size=1)
time.sleep(delay)
# =============================================================================
# if trial <= trials/2:
# delay = floor((random.random()*(iti[1]-iti[0]))*100)/100+iti[0]
# else:
# delay = floor((random.random()*(iti[2]-iti[0]))*100)/100+iti[0]
#
# poketime = time.time()
# curtime = poketime
#
# while (curtime - poketime) <= delay:
# if GPIO.input(inport) == 0:
# poketime = time.time()
# curtime = time.time()
#
# =============================================================================
print('Basic nose poking has been completed.')
# Passive H2O deliveries
def affective(intaninputs=[24], tim_dur=1200):
# Setup pi board GPIO ports
GPIO.setmode(GPIO.BOARD)
for i in intaninputs:
GPIO.setup(i, GPIO.OUT)
# Loop through trials
GPIO.output(intaninputs[0], 1)
time.sleep(0.1)
GPIO.output(intaninputs[0], 0)
time.sleep(tim_dur)
GPIO.output(intaninputs[0], 1)
time.sleep(0.1)
GPIO.output(intaninputs[0], 0)
print('Test completed')
# Clear all pi board GPIO settings
def clearall():
# Pi ports to be cleared
outports = [7, 11, 13, 16, 31, 33, 35, 37, 32, 36, 16, 18, 38, 40]
inports = [11, 13, 15]
pokelights = [15]
houselight = 22
lasers = [12, 18, 23, 8]
intan = [12, 15, 19, 21, 23, 11, 13]
# Set all ports to default/low state
for i in intan:
GPIO.setup(i, GPIO.OUT)
GPIO.output(i, 0)
for i in outports:
GPIO.setup(i, GPIO.OUT)
GPIO.output(i, 0)
for i in inports:
GPIO.setup(i, GPIO.IN, GPIO.PUD_UP)
for i in pokelights:
GPIO.setup(i, GPIO.OUT)
GPIO.output(i, 0)
for i in lasers:
GPIO.setup(i, GPIO.OUT)
GPIO.output(i, 0)
GPIO.setup(houselight, GPIO.OUT)
GPIO.output(houselight, 0)