-
Notifications
You must be signed in to change notification settings - Fork 6
/
Experiment.py
executable file
·373 lines (307 loc) · 12.2 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
from Behavior import *
from Stimulus import *
import time, numpy
class Experiment:
""" this class handles the response to the licks
"""
def __init__(self, logger, timer, params):
self.logger = logger
self.air_dur = params['airpuff_duration']
self.timeout = params['timeout_duration']
self.silence = params['silence_thr']
self.ready_wait = params['init_duration']
self.trial_wait = params['delay_duration']
self.randomization = params['randomization']
self.timer = timer
self.reward_probe = []
self.conditions = []
self.probes = []
self.post_wait = 0
self.indexes = []
self.beh = self.get_behavior()(logger, params)
self.stim = eval(params['stim_type'])(logger, self.beh)
self.probe_bias = numpy.repeat(numpy.nan, 1) # History term for bias calculation
def prepare(self):
"""Prepare things before experiment starts"""
self.stim.setup()
def run(self):
return self.logger.get_setup_state() == 'running'
def pre_trial(self):
"""Prepare things before trial starts"""
self.stim.init_trial() # initialize stimulus
self.logger.ping()
return False
def trial(self):
"""Do stuff in trial, returns break condition"""
self.stim.present_trial() # Start Stimulus
return False
def post_trial(self):
"""Handle events after trial ends"""
self.stim.stop_trial() # stop stimulus
def inter_trial(self):
"""Handle intertrial period events"""
pass
def on_hold(self, status=False):
"""Handle events that happen in between experiments"""
pass
def cleanup(self):
self.beh.cleanup()
def get_behavior(self):
return DummyProbe # default is raspberry pi
def _get_new_cond(self):
"""Get curr condition & create random block of all conditions
Should be called within init_trial
"""
if self.randomization == 'block':
if numpy.size(self.indexes) == 0:
self.indexes = numpy.random.permutation(numpy.size(self.conditions))
cond = self.conditions[self.indexes[0]]
self.indexes = self.indexes[1:]
return cond
elif self.randomization == 'random':
return numpy.random.choice(self.conditions)
elif self.randomization == 'bias':
if len(self.probe_bias) == 0 or numpy.all(numpy.isnan(self.probe_bias)):
self.probe_bias = numpy.random.choice(self.probes, 5)
print('Initializing probe bias!')
return numpy.random.choice(self.conditions)
else:
mn = numpy.min(self.probes)
mx = numpy.max(self.probes)
bias_probe = numpy.random.binomial(1, 1 - numpy.nanmean((self.probe_bias - mn)/(mx-mn)))*(mx-mn) + mn
return numpy.random.choice(self.conditions[self.probes == bias_probe])
class MultiProbe(Experiment):
"""2AFC & GoNOGo tasks with lickspout"""
def __init__(self, logger, timer, params):
self.post_wait = 0
self.responded = False
super(MultiProbe, self).__init__(logger, timer, params)
def prepare(self):
self.conditions, self.probes = self.logger.log_conditions(self.stim.get_condition_table()) # log conditions
self.stim.setup()
self.stim.prepare(self.conditions) # prepare stimulus
def pre_trial(self):
cond = self._get_new_cond()
self.stim.init_trial(cond)
self.reward_probe = (RewardCond() & self.logger.session_key & dict(cond_idx=cond)).fetch1('probe')
self.beh.is_licking()
return False
def trial(self):
self.stim.present_trial() # Start Stimulus
probe = self.beh.is_licking()
if probe > 0 and not self.responded:
self.responded = True
self.probe_bias = np.concatenate((self.probe_bias[1:], [probe])) # bias correction
if self.reward_probe == probe:
print('Correct!')
self.reward(probe)
self.timer.start()
while self.timer.elapsed_time() < 1000: # give an extra second to associate the reward with stimulus
self.stim.present_trial()
return True
else:
print('Wrong!')
self.punish(probe)
return True # break trial
else:
return False
def post_trial(self):
self.stim.stop_trial() # stop stimulus when timeout
self.responded = False
self.timer.start()
if self.post_wait > 0:
self.stim.unshow([0, 0, 0])
while self.timer.elapsed_time()/1000 < self.post_wait and self.logger.get_setup_state() == 'running':
time.sleep(0.5)
self.post_wait = 0
self.stim.unshow()
def inter_trial(self):
if self.beh.is_licking():
self.timer.start()
elif self.beh.inactivity_time() > self.silence and self.logger.get_setup_state() == 'running':
self.logger.update_setup_state('sleeping')
self.stim.unshow([0, 0, 0])
self.probe_bias = numpy.repeat(numpy.nan, 1) # reset bias
while not self.beh.is_licking() and self.logger.get_setup_state() == 'sleeping':
self.logger.ping()
time.sleep(1)
self.stim.unshow()
if self.logger.get_setup_state() == 'sleeping':
self.logger.update_setup_state('running')
self.timer.start()
def punish(self, probe):
self.beh.punish_with_air(probe, self.air_dur)
self.post_wait = self.timeout
def reward(self, probe):
self.beh.water_reward(probe)
class FreeWater(Experiment):
"""Reward upon lick"""
def trial(self):
self.stim.present_trial() # Start Stimulus
probe = self.beh.is_licking()
if probe:
self.beh.water_reward(probe)
return True
else:
return False
def get_behavior(self):
return RPBehavior
class PassiveMatlab(Experiment):
""" Passive Matlab stimulation
"""
def __init__(self, logger, timer, params):
self.stim = eval(params['stim_type'])(logger, self.get_behavior())
super(PassiveMatlab, self).__init__(logger, timer, params)
def prepare(self):
self.stim.setup()
self.stim.prepare() # prepare stimulus
def pre_trial(self):
self.stim.init_trial() # initialize stimulus
return False
def trial(self):
return self.stim.trial_done()
def run(self):
return self.logger.get_setup_state() == 'stimRunning' and not self.stim.stimulus_done()
def cleanup(self):
self.beh.cleanup()
self.stim.cleanup()
self.stim.close()
class PassiveMatlabReward(PassiveMatlab):
""" Passive Matlab with reward in between scans"""
def on_hold(self, status=True):
if not status: # remove probe
self.beh.get_off_position()
else:
self.beh.get_in_position()
probe = self.beh.is_licking()
if probe == 1:
self.beh.water_reward(1)
def get_behavior(self):
return TPBehavior
class ActiveMatlab(Experiment):
""" Rewarded conditions with Matlab
"""
def __init__(self, logger, timer, params):
self.stim = eval(params['stim_type'])(logger, self.get_behavior())
super(ActiveMatlab, self).__init__(logger, timer, params)
def prepare(self):
self.stim.setup()
self.stim.prepare() # prepare stimulus
def pre_trial(self):
self.stim.init_trial() # initialize stimulus
self.reward_probe = self.stim.mat.stimulus.get_reward_probe(self, self.logger.get_trial_key())
self.beh.is_licking()
return False
def trial(self):
probe = self.beh.is_licking()
if probe > 0:
if self.reward_probe == probe:
print('Correct!')
self.reward(probe)
return self.stim.trial.done()
def get_behavior(self):
return SerialProbe
def run(self):
return self.logger.get_setup_state() == 'stimRunning' and not self.stim.stimulus_done()
def reward(self, probe):
self.beh.water_reward(probe)
def cleanup(self):
self.beh.cleanup()
self.stim.cleanup()
self.stim.close()
class CenterPort(Experiment):
"""2AFC with center init position"""
def __init__(self, logger, timer, params):
self.post_wait = 0
self.resp_ready = False
self.wait_time = Timer()
super(CenterPort, self).__init__(logger, timer, params)
def prepare(self):
self.conditions, self.probes = self.logger.log_conditions(self.stim.get_condition_table()) # log conditions
self.stim.setup()
self.stim.prepare(self.conditions) # prepare stimulus
def pre_trial(self):
cond = self._get_new_cond()
self.reward_probe = (RewardCond() & self.logger.session_key & dict(cond_idx=cond)).fetch1('probe')
is_ready, ready_time = self.beh.is_ready()
self.wait_time.start()
while self.logger.get_setup_state() == 'running' and (not is_ready or ready_time < self.ready_wait):
time.sleep(.02)
if self.wait_time.elapsed_time() > 5000: # ping every 5 seconds
self.logger.ping()
self.wait_time.start()
is_ready, ready_time = self.beh.is_ready() # update times
if self.logger.get_setup_state() == 'running':
print('Starting trial!')
self.stim.init_trial(cond)
self.beh.is_licking()
self.timer.start() # trial start counter
return False
else:
return True
def trial(self):
if self.logger.get_setup_state() != 'running':
return True
self.stim.present_trial() # Start Stimulus
probe = self.beh.is_licking()
# delayed response
is_ready, ready_time = self.beh.is_ready() # update times
if self.timer.elapsed_time() > self.trial_wait and not self.resp_ready:
self.resp_ready = True
elif not is_ready and not self.resp_ready:
print('Wrong!')
self.punish(probe)
return True # break trial
# response to probe lick
if probe > 0 and self.resp_ready:
if self.reward_probe != probe:
print('Wrong!')
self.punish(probe)
else:
print('Correct!')
self.reward(probe)
self.probe_bias = np.concatenate((self.probe_bias[1:], [probe]))
self.resp_ready = False
return True # break trial
else:
return False
def post_trial(self):
self.stim.stop_trial() # stop stimulus when timeout
self.timer.start()
if self.post_wait > 0:
self.stim.unshow([0, 0, 0])
while self.timer.elapsed_time()/1000 < self.post_wait and self.logger.get_setup_state() == 'running':
time.sleep(0.5)
self.post_wait = 0
self.stim.unshow()
def inter_trial(self):
if self.beh.is_licking():
self.timer.start()
def get_behavior(self):
return RPBehavior
def punish(self, probe):
self.post_wait = self.timeout
def reward(self, probe):
self.beh.water_reward(probe)
class CenterPortTrain(CenterPort):
"""Training on the 2AFC with center init position"""
def trial(self):
if self.logger.get_setup_state() != 'running':
return True
probe = self.beh.is_licking()
# delayed response
is_ready, ready_time = self.beh.is_ready() # update times
if self.timer.elapsed_time() > self.trial_wait and not self.resp_ready:
self.resp_ready = True
elif not is_ready and not self.resp_ready:
print('Wrong!')
self.punish(probe)
return True # break trial
# response to probe lick
if probe > 0 and self.resp_ready:
print('Correct!')
self.reward(probe)
self.resp_ready = False
return True # break trial
else:
return False