-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathlick_led.py
85 lines (61 loc) · 1.86 KB
/
lick_led.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
"""
DeepLabCut Toolbox (deeplabcut.org)
© A. & M. Mathis Labs
Licensed under GNU Lesser General Public License v3.0
"""
import serial
import struct
import time
import numpy as np
from dlclive import Processor
class MouseLickLED(Processor):
def __init__(self, com, lik_thresh=0.5, baudrate=int(9600)):
super().__init__()
self.ser = serial.Serial(com, baudrate, timeout=0)
self.lik_thresh = lik_thresh
self.lick_frame_time = []
self.out_time = []
self.in_time = []
def close_serial(self):
self.ser.close()
def switch_led(self):
### flush input buffer ###
self.ser.reset_input_buffer()
### turn on IR LED ###
self.out_time.append(time.time())
self.ser.write(b"I")
### wait for receiver ###
while True:
led_byte = self.ser.read()
if len(led_byte) > 0:
break
self.in_time.append(time.time())
def process(self, pose, **kwargs):
### bodyparts
# 0. pupil-top
# 1. pupil-left
# 2. pupil-bottom
# 3. pupil-right
# 4. lip-upper
# 5. lip-lower
# 6. tongue
# 7. tube
if kwargs["record"]:
if pose[6, 2] > self.lik_thresh:
self.lick_frame_time.append(kwargs["frame_time"])
self.switch_led()
return pose
def save(self, filename):
### save stim on and stim off times
filename += ".npy"
out_time = np.array(self.out_time)
in_time = np.array(self.in_time)
frame_time = np.array(self.lick_frame_time)
try:
np.savez(
filename, out_time=out_time, in_time=in_time, frame_time=frame_time
)
save_code = True
except Exception:
save_code = False
return save_code