-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions_nidaq.py
More file actions
373 lines (316 loc) · 14.4 KB
/
functions_nidaq.py
File metadata and controls
373 lines (316 loc) · 14.4 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
import time
import os
import csv
import keyboard
import nidaqmx as ni
import matplotlib.pyplot as plt
import numpy as np
from functions_osc4py3 import OSCManager
import paths
from datetime import timedelta
from pypixxlib.propixx import PROPixxCTRL, PROPixx
# import pypixxlib._libdpx as libd
from pypixxlib import digitalOut
def normalize_row(row_in):
"""Simple function to normalize the triggers before plotting"""
row_out = (row_in - row_in.min())/(row_in.max() - row_in.min())
return row_out
def initialize_projector():
"""Initialize the projector controller"""
# get the device object for the controller
my_device = PROPixxCTRL()
# enable pixel mode
# libd.DPxEnableDoutPixelMode()
dout = digitalOut.DigitalOut()
dout.enablePixelMode()
return my_device
def restore_projector():
"""Disable pixel mode"""
dout = digitalOut.DigitalOut()
dout.disablePixelMode()
return
def plot_inputs_miniscope(frame_list):
"""Plot the sync data"""
fig = plt.figure()
ax = fig.add_subplot(111)
ax1, = ax.plot(frame_list[:, 0], normalize_row(frame_list[:, 1]), marker='o')
ax2, = ax.plot(frame_list[:, 0], normalize_row(frame_list[:, 2]) + 2, marker='o')
ax3, = ax.plot(frame_list[:, 0], normalize_row(frame_list[:, 3]) + 4, marker='o')
ax.legend((ax1, ax2, ax3), ('Miniscope', 'Camera', 'Sync_trigger'))
plt.show()
def plot_inputs_vr(frame_list):
"""Plot the sync data"""
fig = plt.figure()
ax = fig.add_subplot(111)
ax1, = ax.plot(frame_list[:, 0], normalize_row(frame_list[:, 1]), marker='o')
ax2, = ax.plot(frame_list[:, 0], normalize_row(frame_list[:, 2]) + 2, marker='o')
ax3, = ax.plot(frame_list[:, 0], normalize_row(frame_list[:, 3]) + 4, marker='o')
ax4, = ax.plot(frame_list[:, 0], normalize_row(frame_list[:, 4]) + 6, marker='o')
ax5, = ax.plot(frame_list[:, 0], normalize_row(frame_list[:, 5]) + 8, marker='o')
ax6, = ax.plot(frame_list[:, 0], normalize_row(frame_list[:, 6]), marker='o')
ax.legend((ax1, ax2, ax3, ax4, ax5, ax6), ('Projector', 'Camera', 'Sync', 'Miniscope',
'Wheel', 'Projector 2'))
plt.show()
def calculate_frames(frame_list, target_column, time_column=0, sync_column=3, column_type=None):
"""Determine the number of recorded frames and effective frame rates"""
# trim the list at the start frame
start_frame = np.argwhere(frame_list[:, sync_column] == 1)[0][0]
stop_frame = np.argwhere(frame_list[:, sync_column] == 2)[0][0]
# get the frame times of the target column
if column_type == 'projector':
#TODO: fix for two frame columns
frame_times = \
frame_list[np.argwhere(np.abs(np.diff(np.round(frame_list[:, target_column]/2))) > 0).flatten()+1, 0]
else:
frame_times = frame_list[np.argwhere(np.diff(np.round(frame_list[:, target_column])) > 0).flatten()+1, 0]
# if it's empty, return nan
if len(frame_times) == 0:
return np.nan, np.nan, np.nan, np.nan
# get the deltas between the start frame and the camera frames
delta_start = frame_list[start_frame, time_column] - frame_times
# if the shutter was active during the start, still take it
if np.round(frame_list[start_frame, target_column]) > 0:
# find the first trigger before or at the start frame
start_time = np.argwhere(delta_start >= 0)[-1][0]
else:
# otherwise, find the first trigger after the start frame
start_time = np.argwhere(delta_start < 0)[0][0]
# get the time of the last frame
delta_stop = frame_list[stop_frame, time_column] - frame_times
# it'll be the first trigger after the stop signal
stop_time = np.argwhere(delta_stop >= 0)[-1][0]
# trim the frame times and list accordingly
frame_times = frame_times[start_time:stop_time+1]
# calculate the framerate
framerate = 1/np.mean(np.diff(frame_times))
frame_number = frame_times.shape[0]
return framerate, frame_number, start_frame, stop_frame
def load_csv(path):
"""Load csv data from a file path"""
with open(path) as f:
csv_reader = csv.reader(f, delimiter=',', quoting=csv.QUOTE_NONNUMERIC)
frame_list = [row for row in csv_reader]
return np.array(frame_list)
def detect_trigger(task_in, channel, threshold=3):
"""wait on an infinite loop for a trigger to be detected in the selected input channel according to the specified
threshold"""
# initiate a counter to drive the baseline
baseline_counter = 0
# allocate the baseline
baseline = []
while True:
# only in the first iteration
if baseline_counter == 0:
print('Acquiring photodiode baseline')
# read the values from the analog in
values = task_in.read()
# select the target value
target_value = values[channel]
# integrate for a defined number of frames to acquire a background subtraction
for i in np.arange(100):
baseline.append(target_value)
time.sleep(0.01)
# calculate the baseline
baseline = np.mean(baseline)
# update the counter
baseline_counter = 1
else:
# display a message only the first time
if baseline_counter == 1:
print('Waiting for photodiode trigger')
baseline_counter = 2
# read the values from the analog in
values = task_in.read()
# select the target value
target_value = values[channel]
# normalize using the baseline
target_value = np.abs((target_value - baseline)/baseline)
# evaluate if it passes threshold
if target_value > threshold:
print('Photodiode trigger detected')
break
if keyboard.is_pressed('Shift'):
print('Manually triggered')
break
# pause for next iteration
time.sleep(0.01)
return
def record_miniscope_ni(path_in, name_in, device='Dev1'):
"""Write the sync data to a text file"""
# get the startup time
t_start = time.perf_counter()
# initialize the osc class
osc = OSCManager()
# define the servers to use
osc.create_server(paths.recorder_ip, paths.recorder_port, 'server_recorder')
osc.create_client(paths.cam_ip, paths.cam_port, 'client_cam')
# define the file to save the path to
file_name = os.path.join(path_in, name_in + '_syncMini_suffix.csv')
# open the file
with open(file_name, mode='w', newline='') as f:
# initialize the writer
f_writer = csv.writer(f, delimiter=',')
with ni.Task() as task:
# create the tasks
task.ai_channels.add_ai_voltage_chan(device+'/ai0:1')
# wait for the camera
osc.wait_for_message('cam')
# initialize a line counter
line_counter = 0
# initialize a terminator counter
end_counter = 100
# for several frames
while True:
# read from the DAQ
miniscope_trigger, cam_trigger = task.read()
# set the trigger
sync_trigger = 0
# after 100 reads, start the camera acquisition
if line_counter == 100:
sync_trigger = 1
# osc.create_send_close(paths.cam_ip, paths.cam_port, '/cam_loop', [1])
osc.simple_send('client_cam', '/SimpleRead', 1)
if keyboard.is_pressed('Escape'):
# kill the camera process
osc.simple_send('client_cam', '/SimpleRead', 2)
# signal the off trigger
sync_trigger = 2
# start the end counter
end_counter -= 1
if (end_counter < 100) & (end_counter > 0):
end_counter -= 1
elif end_counter <= 0:
break
# get the timestamp
t = time.perf_counter() - t_start
# write to the file
f_writer.writerow([t, miniscope_trigger, cam_trigger, sync_trigger])
# update the counter
line_counter += 1
# terminate the osc
osc.stop()
return 'Total duration: ' + str(time.perf_counter() - t_start), file_name
def record_vr_trial_experiment(session, path_in, name_in, exp_type, unity_osc, device='Dev1', line_wait=100):
"""Handle the trial communication structure and write the sync data to a text file"""
# define the file to save the path to
file_name = os.path.join(path_in, name_in + "_sync" + exp_type + '_suffix.csv')
# Triggers a callback to the 'received_trial' function
unity_osc.bind('/TrialReceived', session.received_trial)
# The EndTrial message triggers a callback to the 'end_trial' function
unity_osc.bind('/EndTrial', session.end_trial)
# open the file
with open(file_name, mode='w', newline='') as f:
# initialize the writer
f_writer = csv.writer(f, delimiter=',')
t_start = time.time()
with ni.Task() as task, ni.Task() as task2:
# create the tasks
if exp_type in ['VWheel', 'VWheelWF']:
task.ai_channels.add_ai_voltage_chan(device + '/ai2')
task.ai_channels.add_ai_voltage_chan(device + '/ai7')
task.ai_channels.add_ai_voltage_chan(device + '/ai4:6') # NEEDS TO COME LAST OR THESE CHANNELS BREAK
else:
task.ai_channels.add_ai_voltage_chan(device + '/ai2:6')
# create the write task to control the miniscope
task2.do_channels.add_do_chan('Dev1/port1/line0')
# wait for the camera
unity_osc.wait_for_message('device')
unity_osc.wait_for_message('device')
# check for the wirefree flag
if 'WF' in exp_type:
# hold execution until the PD is detected
detect_trigger(task, 2, threshold=3)
# record the trigger
t = time.time() - t_start
f_writer.writerow([t, 0, 0, 0, 5, 0, 0])
# release unity
unity_osc.send_message('client_unity', '/ReleaseWait', [0])
# initialize a frame counter
line_counter = 0
# initialize the end counter
end_counter = 1000
# for several frames
while True:
# set the trigger
sync_trigger = 0
# trigger the start of the whole experiment after 100 frames
if line_counter == line_wait:
# start the camera
unity_osc.simple_send('client_cam', '/SimpleRead', 1)
# signal unity to start the actual trial
unity_osc.send_message('client_unity', '/SessionStart', [0])
# start the miniscope
task2.write(True)
# signal the trigger in the sync file
sync_trigger = 1
print('Start sent')
session.setup_trial = True
# Process the OSC communication - trial structure is handled by Unity
if session.setup_trial:
# Reset setup trial bool
session.setup_trial = False
# Generate a message to be sent via OSC client
message = session.assemble_trial_message()
# Send trial string to Unity
print('Trial {} sent'.format(message[0]))
print(message)
unity_osc.send_message('client_unity', '/SetupTrial', message)
# Received OSC messages are automatically picked up by a separate thread
# and resets the session.start_trial boolean
if (keyboard.is_pressed('Escape')) | (session.in_experiment is False):
# signal off trigger
sync_trigger = 2
# signal the camera to stop
unity_osc.simple_send('client_cam', '/SimpleRead', 2)
# signal unity to stop
unity_osc.simple_send('client_unity', '/Close', 0)
# stop the miniscope
task2.write(False)
# start the end counter
end_counter -= 1
if (end_counter < 1000) & (end_counter > 0):
end_counter -= 1
elif end_counter <= 0:
break
# read the DAQ
proj_trigger, cam_trigger, miniscope_trigger, running_wheel, proj_trigger2 = task.read()
t = time.time() - t_start
# write to the file
f_writer.writerow([t, proj_trigger, cam_trigger, sync_trigger, miniscope_trigger,
running_wheel, proj_trigger2])
# update the counter
line_counter += 1
return 'Total duration: ' + str(timedelta(seconds=(time.time() - t_start))), file_name
def record_daq_direct(target_path, target_channels='/ai2', length=5, device='Dev1', **kwargs):
"""Record the inputs from the DAQ to a text file"""
# open the file
with open(target_path, mode='w', newline='') as f:
# initialize the writer
f_writer = csv.writer(f, delimiter=',')
# get the starting time
t_start = time.time()
with ni.Task() as task:
# create the tasks
task.ai_channels.add_ai_voltage_chan(device+target_channels, **kwargs)
# initialize the time counter
t = 0
# loop until the defined length is over
while t < length:
# read the DAQ
daq_output = task.read()
# update the time
t = time.time() - t_start
# write to the file
if type(daq_output) == list:
f_writer.writerow([t, *daq_output])
else:
f_writer.writerow([t, daq_output])
return
if __name__ == '__main__':
# get the parameters for reading
daq_path = paths.daq_path
daq_channels = paths.daq_channels
daq_length = paths.daq_length
extra_params = paths.extra_params
# run the recording
record_daq_direct(daq_path, target_channels=daq_channels, length=daq_length, **extra_params)