-
Notifications
You must be signed in to change notification settings - Fork 6
/
Logger.py
executable file
·371 lines (297 loc) · 14 KB
/
Logger.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
import numpy, socket
from Timer import *
from Database import *
from itertools import product
from queue import Queue
import time as systime
import datetime
#from threading import Thread
class Logger:
""" This class handles the database logging"""
def __init__(self):
self.session_key = dict()
self.setup = socket.gethostname()
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
self.ip = s.getsockname()[0]
print(self.ip)
self.init_params()
#self.thread = Thread(target=self.inserter)
#self.thread.daemon = True
#self.thread.start()
def init_params(self):
self.last_trial = 0
self.queue = Queue()
self.timer = Timer()
self.trial_start = 0
self.curr_cond = []
self.task_idx = []
self.reward_amount = []
def log_session(self):
"""Logs session"""
pass
def log_conditions(self, condition_table):
"""Logs conditions"""
pass
def start_trial(self, cond_idx):
self.trial_start = self.timer.elapsed_time()
def log_trial(self, last_flip_count=0):
"""Log experiment trial"""
pass
def log_setup(self):
"""Log setup information"""
pass
def update_setup_state(self, state):
pass
def get_setup_state(self):
pass
def get_setup_task(self):
pass
def get_session_key(self):
return self.session_key
def ping(self):
"""update timestamp"""
pass
def inserter(self): # insert worker, in case we need threading
while not self.queue.empty():
item = self.queue.get()
item['table'].insert1(item['tuple'], ignore_extra_fields=True)
class RPLogger(Logger):
""" This class handles the database logging for Raspberry pi"""
def init_params(self):
self.last_trial = 0
self.queue = Queue()
self.timer = Timer()
self.trial_start = 0
self.curr_cond = []
self.task_idx = []
self.reward_amount = []
def log_session(self):
animal_id, task_idx = (SetupInfo() & dict(setup=self.setup)).fetch1('animal_id', 'task_idx')
self.task_idx = task_idx
# create session key
self.session_key['animal_id'] = animal_id
last_sessions = (Session() & self.session_key).fetch('session_id')
if numpy.size(last_sessions) == 0:
last_session = 0
else:
last_session = numpy.max(last_sessions)
self.session_key['session_id'] = last_session + 1
# get task parameters for session table
task_fields = set(Session().heading.names).intersection(Task().heading.names)
task_params = (Task() & dict(task_idx=task_idx)).proj(*task_fields).fetch1()
del task_params['task_idx']
key = dict(self.session_key.items() | task_params.items())
key['setup'] = self.setup
self.queue.put(dict(table=Session(), tuple=key))
self.reward_amount = task_params['reward_amount']/1000 # convert to ml
# start session time
self.timer.start()
self.inserter()
(SetupInfo() & dict(setup=self.setup))._update('current_session', self.session_key['session_id'])
(SetupInfo() & dict(setup=self.setup))._update('last_trial', 0)
(SetupInfo() & dict(setup=self.setup))._update('total_liquid', 0)
def log_conditions(self, condition_table):
# generate factorial conditions
conditions = eval((Task() & dict(task_idx=self.task_idx)).fetch1('conditions'))
conditions = sum([list((dict(zip(conds, x)) for x in product(*conds.values()))) for conds in conditions], [])
# make sure condition_table is a list
if numpy.size(condition_table) < 2:
condition_table = [condition_table]
# iterate through all conditions and insert
cond_idx = 0
probes = numpy.empty(numpy.size(conditions))
for cond in conditions:
cond_idx += 1
self.queue.put(dict(table=Condition(), tuple=dict(self.session_key, cond_idx=cond_idx)))
if 'probe' in cond:
probes[cond_idx-1] = cond.pop('probe')
self.queue.put(dict(table=RewardCond(), tuple=dict(self.session_key,
cond_idx=cond_idx,
probe=probes[cond_idx-1])))
for condtable in condition_table:
self.queue.put(dict(table=condtable(), tuple=dict(cond.items() | self.session_key.items(),
cond_idx=cond_idx)))
self.inserter()
# outputs all the condition indexes of the session
cond_indexes = list(range(1, cond_idx + 1)) # assumes continuous & complete indexes for each session
return numpy.array(cond_indexes), numpy.array(probes)
def start_trial(self, cond_idx):
self.curr_cond = cond_idx
self.trial_start = self.timer.elapsed_time()
# return condition key
return dict(self.session_key, cond_idx=cond_idx)
def log_trial(self, last_flip_count=0):
timestamp = self.timer.elapsed_time()
trial_key = dict(self.session_key,
trial_idx=self.last_trial+1,
cond_idx=self.curr_cond,
start_time=self.trial_start,
end_time=timestamp,
last_flip_count=last_flip_count)
self.queue.put(dict(table=Trial(), tuple=trial_key))
self.last_trial += 1
self.inserter()
# insert ping
(SetupInfo() & dict(setup=self.setup))._update('last_trial', self.last_trial)
self.ping()
def log_liquid(self, probe):
timestamp = self.timer.elapsed_time()
self.queue.put(dict(table=LiquidDelivery(), tuple=dict(self.session_key, time=timestamp, probe=probe)))
self.inserter()
rew = (LiquidDelivery & self.session_key).__len__()*(Session() & self.session_key).fetch1('reward_amount')/1000
(SetupInfo() & dict(setup=self.setup))._update('total_liquid', rew)
def log_odor(self, odor_idx):
timestamp = self.timer.elapsed_time()
self.queue.put(dict(table=OdorDelivery(), tuple=dict(self.session_key, time=timestamp, odor_idx=odor_idx)))
self.inserter()
def log_lick(self, probe):
timestamp = self.timer.elapsed_time()
self.queue.put(dict(table=Lick(), tuple=dict(self.session_key,
time=timestamp,
probe=probe)))
#self.inserter()
def log_air(self, probe):
timestamp = self.timer.elapsed_time()
self.queue.put(dict(table=AirpuffDelivery(), tuple=dict(self.session_key, time=timestamp, probe=probe)))
self.inserter()
def log_pulse_weight(self, pulse_dur, probe, pulse_num, weight=0):
cal_key = dict(setup=self.setup, probe=probe, date=systime.strftime("%Y-%m-%d"))
LiquidCalibration().insert1(cal_key, skip_duplicates=True)
(LiquidCalibration.PulseWeight() & dict(cal_key, pulse_dur=pulse_dur)).delete_quick()
LiquidCalibration.PulseWeight().insert1(dict(cal_key,
pulse_dur=pulse_dur,
pulse_num=pulse_num,
weight=weight))
def log_setup(self):
key = dict(setup=self.setup)
# update values in case they exist
if numpy.size((SetupInfo() & dict(setup=self.setup)).fetch()):
key = (SetupInfo() & dict(setup=self.setup)).fetch1()
(SetupInfo() & dict(setup=self.setup)).delete_quick()
# insert new setup
key['ip'] = self.ip
key['state'] = 'ready'
SetupInfo().insert1(key)
def update_setup_state(self, state):
key = (SetupInfo() & dict(setup=self.setup)).fetch1()
in_state = key['state'] == state
if not in_state:
(SetupInfo() & dict(setup=self.setup))._update('state', state)
return in_state
def update_setup_notes(self, note):
(SetupInfo() & dict(setup=self.setup))._update('notes', note)
def get_setup_state(self):
state = (SetupInfo() & dict(setup=self.setup)).fetch1('state')
return state
def get_setup_task(self):
task = (SetupInfo() & dict(setup=self.setup)).fetch1('task')
return task
def get_session_key(self):
return self.session_key
def ping(self):
if numpy.size((SetupInfo() & dict(setup=self.setup)).fetch()):
lp = str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
(SetupInfo() & dict(setup=self.setup))._update('last_ping', lp)
class PCLogger(Logger):
""" This class handles the database logging for 2P systems"""
def __init__(self):
from DatabaseForControl import SetupControl
self.SetupControl = SetupControl
super(PCLogger, self).__init__()
def init_params(self):
self.queue = Queue()
self.timer = Timer()
self.task_idx = []
self.last_time = systime.time()
self.trial_idx = []
def log_session(self):
animal_id, task_idx = (self.SetupControl() & dict(setup=self.setup)).fetch1('animal_id', 'task_idx')
self.task_idx = task_idx
# create session key
self.session_key['animal_id'] = animal_id
last_sessions = (Session() & self.session_key).fetch('session_id')
if numpy.size(last_sessions) == 0:
last_session = 0
else:
last_session = numpy.max(last_sessions)
self.session_key['session_id'] = last_session + 1
# get task parameters for session table
if numpy.size((Session() & self.session_key).fetch()): # if session exists
self.reward_amount = (Session() & self.session_key).fetch1('reward_amount')/1000
else:
task_fields = set(Session().heading.names).intersection(Task().heading.names)
task_params = (Task() & dict(task_idx=task_idx)).proj(*task_fields).fetch1()
del task_params['task_idx']
key = dict(self.session_key.items() | task_params.items())
key['setup'] = self.setup
self.queue.put(dict(table=Session(), tuple=key))
self.reward_amount = task_params['reward_amount']/1000 # convert to ml
# start session time
self.timer.start()
self.inserter()
def log_liquid(self, probe):
timestamp = self.timer.elapsed_time()
self.queue.put(dict(table=LiquidDelivery(), tuple=dict(self.session_key, time=timestamp, probe=probe)))
self.inserter()
def log_lick(self, probe):
timestamp = self.timer.elapsed_time()
self.queue.put(dict(table=Lick(), tuple=dict(self.session_key,
time=timestamp,
probe=probe)))
def update_setup_state(self, state):
key = (self.SetupControl() & dict(setup=self.setup)).fetch1()
in_state = key['state'] == state
if not in_state:
(self.SetupControl() & dict(setup=self.setup))._update('state', state)
def get_setup_state(self):
state = (self.SetupControl() & dict(setup=self.setup)).fetch1('state')
return state
def get_setup_state_control(self):
state = (self.SetupControl() & dict(setup=self.setup)).fetch1('state_control')
return state
def get_setup_task(self):
task = (self.SetupControl() & dict(setup=self.setup)).fetch1('task')
return task
def get_stimulus(self):
task = (self.SetupControl() & dict(setup=self.setup)).fetch1('stimulus')
return task
def get_experimenter(self):
task = (self.SetupControl() & dict(setup=self.setup)).fetch1('experimenter')
return task
def setup_experiment_schema(self):
self.experiment = dj.create_virtual_module('experiment', 'pipeline_experiment')
def ping(self):
nw = systime.time()
if nw-self.last_time > 1:
lp = format(datetime.datetime.now(),"%Y-%m-%d %H:%M:%S")
(self.SetupControl() & dict(setup=self.setup))._update('last_ping', lp)
self.last_time = nw
def get_scan_key(self):
animal_id = (self.SetupControl() & dict(setup=self.setup)).fetch1('animal_id')
session = (self.SetupControl() & dict(setup=self.setup)).fetch1('session')
scan_idx = (self.SetupControl() & dict(setup=self.setup)).fetch1('scan_idx')
return dict(animal_id=animal_id, session=session, scan_idx=scan_idx)
def get_trial_key(self):
animal_id = (self.SetupControl() & dict(setup=self.setup)).fetch1('animal_id')
session = (self.SetupControl() & dict(setup=self.setup)).fetch1('session')
scan_idx = (self.SetupControl() & dict(setup=self.setup)).fetch1('scan_idx')
return dict(animal_id=animal_id, session=session, scan_idx=scan_idx, trial_idx=self.trial_idx)
def get_protocol_file(self):
protocol_table = self.experiment.VisProtocol()
tp = protocol_table & dict(vis_protocol=self.get_stimulus(),username=self.get_experimenter())
return tp.fetch1('vis_filename')
def update_next_trial(self, next_trial):
(self.SetupControl() & dict(setup=self.setup))._update('next_trial', next_trial)
self.trial_idx = next_trial
def get_trial_done(self):
trial_done = (self.SetupControl() & dict(setup=self.setup)).fetch1('trial_done')
return trial_done
def get_exp_done(self):
exp_done = (self.SetupControl() & dict(setup=self.setup)).fetch1('exp_done')
return exp_done
def get_sync_levels(self):
sync_levels = (self.SetupControl() & dict(setup=self.setup)).fetch1('level1','level2','level3')
return sync_levels
def update_trial_done(self, state):
(self.SetupControl() & dict(setup=self.setup))._update('trial_done', state)