-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathQueueControl.py
More file actions
176 lines (145 loc) · 8.1 KB
/
QueueControl.py
File metadata and controls
176 lines (145 loc) · 8.1 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
from PyQt5 import QtCore
from time import sleep
import daqface.DAQ as daq
from PyPulse import PulseInterface
import scipy.io as sio
import numpy as np
from datetime import datetime
class QueueWorker(QtCore.QObject):
finished = QtCore.pyqtSignal()
trial_start = QtCore.pyqtSignal()
def __init__(self, parent, experiment, get_global_params, get_hardware_params, get_export_params):
super(self.__class__, self).__init__(None)
self.parent = parent
self.experiment = experiment
self.get_global_params = get_global_params
self.get_hardware_params = get_hardware_params
self.get_export_params = get_export_params
@QtCore.pyqtSlot()
def trial(self):
while True:
sleep(0.05)
if self.parent.should_run:
self.trial_start.emit()
trial_params = self.experiment.arraydata[self.experiment.current_trial][1]
hardware_params = self.get_hardware_params()
global_params = self.get_global_params()
export_params = self.get_export_params()
invert_valves = []
if global_params['inverted_blank_off_state']:
invert_valves = global_params['inverted_blank_valves']
pulses, t = PulseInterface.make_pulse(hardware_params['samp_rate'],
global_params['global_onset'],
global_params['global_offset'],
trial_params, invert_chan_list=invert_valves)
if hardware_params['control_carrier']:
while len(pulses) < hardware_params['digital_channels']:
pulses = np.append(pulses, np.zeros((1, pulses.shape[1])), axis=0)
carrier_control = np.append(np.ones(pulses.shape[1]-0), np.zeros(0))
pulses = np.append(pulses, carrier_control[np.newaxis], axis=0)
# in standard configuration we want to run each trial sequentially
if not self.parent.trigger_state():
if hardware_params['analog_channels'] > 0:
self.trial_daq = daq.DoAiMultiTask(hardware_params['analog_dev'], hardware_params['analog_channels'],
hardware_params['digital_dev'], hardware_params['samp_rate'],
len(t) / hardware_params['samp_rate'], pulses,
hardware_params['sync_clock'])
self.analog_data = self.trial_daq.DoTask()
else:
self.trial_daq = daq.DoCoTask(hardware_params['digital_dev'], '', hardware_params['samp_rate'],
len(t) / hardware_params['samp_rate'], pulses)
self.trial_daq.DoTask()
# close_valves= daq.DoCoTask(hardware_params['digital_dev'], '', hardware_params['samp_rate'],
# len(t) / hardware_params['samp_rate'], np.zeros((len(pulses), 10)))
# close_valves.DoTask()
self.analog_data = []
# unless the 'wait for trigger' box is checked, in which case we want to wait for our trigger in
else:
if hardware_params['analog_channels'] > 0 :
self.trial_daq = daq.DoAiTriggeredMultiTask(hardware_params['analog_dev'],
hardware_params['analog_channels'],
hardware_params['digital_dev'],
hardware_params['samp_rate'],
len(t) / hardware_params['samp_rate'], pulses,
hardware_params['sync_clock'],
hardware_params['trigger_source'])
self.analog_data = self.trial_daq.DoTask()
else:
self.trial_daq= daq.DoTriggeredCoTask(hardware_params['digital_dev'], '', hardware_params['samp_rate'], len(t) / hardware_params['samp_rate'],
pulses, hardware_params['trigger_source'])
self.trial_daq.DoTask()
self.analog_data = []
# Save data
if export_params['save_pulses']:
save_string = export_params['export_path'] + str(self.experiment.current_trial) + \
export_params['pulse_suffix'] + '.mat'
sio.savemat(save_string, {'analog_data': self.analog_data, 'pulses': pulses, 't': t})
if self.experiment.total_trials() - self.experiment.current_trial == 1:
self.experiment.reset_trials()
if export_params['save_names']:
names = [i[-1] for i in self.experiment.arraydata]
date = datetime.today().strftime('%Y-%m-%d')
time = datetime.today().strftime('%H:%M:%S')
f = open(export_params['export_path']+date+export_params['trial_suffix']+'.txt', 'a')
f.write(time)
f.write('\n')
f.write('\n'.join(names))
f.write('\n')
f.close()
self.parent.repeats_done += 1
print('repeats done ', self.parent.repeats_done)
if self.parent.repeats_done == global_params['repeats']:
self.parent.should_run = False
self.parent.repeats_done = 0
else:
if global_params['shuffle_repeats']:
self.experiment.randomise_trials(global_params)
elif self.parent.should_run:
self.experiment.advance_trial()
self.finished.emit()
class QueueController(QtCore.QObject):
trial_start = QtCore.pyqtSignal()
def __init__(self, experiment, get_global_params, get_hardware_params, get_export_params, trigger_control):
super(self.__class__, self).__init__(None)
self.experiment = experiment
self.get_global_params = get_global_params
self.get_hardware_params = get_hardware_params
self.get_export_params = get_export_params
self.prepare_thread()
self.repeats_done = 0
self.should_run = False
self.trigger_control = trigger_control
def prepare_thread(self):
self.thread = QtCore.QThread()
self.worker = QueueWorker(self, self.experiment, self.get_global_params, self.get_hardware_params,
self.get_export_params)
self.worker.moveToThread(self.thread)
self.worker.trial_start.connect(self.trial_start.emit)
self.thread.started.connect(self.worker.trial)
self.thread.start()
def start(self):
if not self.should_run:
self.should_run = True
def pause(self):
if self.should_run:
self.should_run = False
self.experiment.advance_trial()
def stop(self):
if self.should_run:
self.should_run = False
self.experiment.reset_trials()
self.repeats_done = 0
def run_selected(self, trial):
if not self.should_run:
self.should_run = True
self.experiment.current_trial = trial
sleep(0.05)
self.should_run = False
def run_from_selected(self, trial):
if not self.should_run:
self.should_run = True
self.experiment.current_trial = trial
def finished(self):
print("not implemented")
def trigger_state(self):
return self.trigger_control.isChecked()