etho-python 1.2.4__py3-none-any.whl → 1.2.6__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- etho/assets/etho_icon1_pipeline_dark.svg +23 -23
- etho/assets/etho_icon1_pipeline_light.svg +30 -30
- etho/res_app.py +225 -225
- etho/resumable.py +283 -283
- etho/services/DAQZeroService.py +2 -2
- etho/services/GOVZeroService.py +291 -291
- etho/services/__init__.py +52 -52
- etho/services/callbacks/_image.py +69 -16
- etho/services/callbacks/_trace.py +12 -10
- etho/services/camera/spinnaker_old.py +312 -312
- etho/services/daq/IOTask.py +16 -9
- etho/services/resumable.py +476 -476
- etho/utils/calibration.py +88 -88
- etho/utils/runner.py +121 -121
- {etho_python-1.2.4.dist-info → etho_python-1.2.6.dist-info}/METADATA +2 -1
- {etho_python-1.2.4.dist-info → etho_python-1.2.6.dist-info}/RECORD +19 -19
- {etho_python-1.2.4.dist-info → etho_python-1.2.6.dist-info}/licenses/LICENSE +21 -21
- {etho_python-1.2.4.dist-info → etho_python-1.2.6.dist-info}/WHEEL +0 -0
- {etho_python-1.2.4.dist-info → etho_python-1.2.6.dist-info}/entry_points.txt +0 -0
etho/resumable.py
CHANGED
|
@@ -1,283 +1,283 @@
|
|
|
1
|
-
import copy
|
|
2
|
-
import logging
|
|
3
|
-
import time
|
|
4
|
-
from pathlib import Path
|
|
5
|
-
|
|
6
|
-
import numpy as np
|
|
7
|
-
import rich
|
|
8
|
-
from rich.progress import Progress
|
|
9
|
-
|
|
10
|
-
from . import config
|
|
11
|
-
from .services import service_base_name
|
|
12
|
-
from .services.resumable import ResumableDAQ, ResumableGCM
|
|
13
|
-
from .utils.tui import rich_information
|
|
14
|
-
from .utils.config import defaultify, readconfig
|
|
15
|
-
from .utils.sound import build_playlist, load_sounds, parse_table
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
logger = logging.getLogger(__name__)
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
def _attenuation_for_host():
|
|
22
|
-
attenuation = config["ATTENUATION"]
|
|
23
|
-
host = config["host"] or "localhost"
|
|
24
|
-
if isinstance(attenuation, dict) and host in attenuation:
|
|
25
|
-
return attenuation[host]
|
|
26
|
-
return attenuation
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
def _playlist_cache_key(protocol, playlistfile):
|
|
30
|
-
daq_name = next((name for name in protocol["use_services"] if service_base_name(name) == "DAQ"), None)
|
|
31
|
-
if daq_name is None:
|
|
32
|
-
return None
|
|
33
|
-
daq_params = protocol[daq_name]
|
|
34
|
-
attenuation = _attenuation_for_host()
|
|
35
|
-
return (
|
|
36
|
-
daq_name,
|
|
37
|
-
str(Path(playlistfile).resolve()),
|
|
38
|
-
daq_params["samplingrate"],
|
|
39
|
-
str(config["stimfolder"]),
|
|
40
|
-
repr(attenuation),
|
|
41
|
-
)
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
def playlist_arrays(protocol, playlistfile, cache=None):
|
|
45
|
-
daq_name = next((name for name in protocol["use_services"] if service_base_name(name) == "DAQ"), None)
|
|
46
|
-
if daq_name is None:
|
|
47
|
-
return None, None, protocol["maxduration"]
|
|
48
|
-
|
|
49
|
-
daq_params = protocol[daq_name]
|
|
50
|
-
fs = daq_params["samplingrate"]
|
|
51
|
-
cache_key = _playlist_cache_key(protocol, playlistfile)
|
|
52
|
-
if cache is not None and cache_key in cache:
|
|
53
|
-
playlist, sounds = cache[cache_key]
|
|
54
|
-
logger.info(f"Reusing loaded stimuli for {playlistfile}.")
|
|
55
|
-
else:
|
|
56
|
-
playlist = parse_table(playlistfile)
|
|
57
|
-
attenuation = _attenuation_for_host()
|
|
58
|
-
sounds = load_sounds(playlist, fs, attenuation=attenuation, stimfolder=config["stimfolder"])
|
|
59
|
-
sounds = [sound.astype(np.float64) for sound in sounds]
|
|
60
|
-
if cache is not None:
|
|
61
|
-
cache[cache_key] = (playlist, sounds)
|
|
62
|
-
playlist_items, duration = build_playlist(sounds, protocol["maxduration"], fs, shuffle=daq_params["shuffle"])
|
|
63
|
-
data = np.concatenate([sounds[item] for item in playlist_items], axis=0)
|
|
64
|
-
|
|
65
|
-
nb_digital = len(daq_params["digital_chans_out"] or [])
|
|
66
|
-
if nb_digital:
|
|
67
|
-
return data[:, :-nb_digital], data[:, -nb_digital:].astype(np.uint8), duration
|
|
68
|
-
return data, None, duration
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
class ResumableExperimentRunner:
|
|
72
|
-
def __init__(self, protocolfile=None, *, protocol=None, save_prefix_root=None):
|
|
73
|
-
if protocolfile is None and protocol is None:
|
|
74
|
-
raise ValueError("Provide protocolfile or protocol.")
|
|
75
|
-
self.protocolfile = protocolfile
|
|
76
|
-
self.protocol = copy.deepcopy(protocol) if protocol is not None else None
|
|
77
|
-
self.save_prefix_root = save_prefix_root
|
|
78
|
-
self.run_count = 0
|
|
79
|
-
self.services = {}
|
|
80
|
-
self._playlist_cache = {}
|
|
81
|
-
|
|
82
|
-
def setup_hardware(self):
|
|
83
|
-
prot = defaultify(copy.deepcopy(self.protocol)) if self.protocol is not None else readconfig(self.protocolfile)
|
|
84
|
-
self.prot = prot
|
|
85
|
-
defaults = config
|
|
86
|
-
if defaults["host"] is None:
|
|
87
|
-
defaults["host"] = "localhost"
|
|
88
|
-
rich.print(defaults)
|
|
89
|
-
logger.info("Setting up resumable hardware.")
|
|
90
|
-
for service_name in prot["use_services"]:
|
|
91
|
-
base = service_base_name(service_name)
|
|
92
|
-
logger.info(f" {service_name}.")
|
|
93
|
-
if base == "GCM":
|
|
94
|
-
self.services[service_name] = ResumableGCM(prot[service_name]).setup_hardware()
|
|
95
|
-
elif base == "DAQ":
|
|
96
|
-
self.services[service_name] = ResumableDAQ(prot[service_name]).setup_hardware()
|
|
97
|
-
else:
|
|
98
|
-
raise ValueError(f"Resumable runner only supports DAQ and GCM, got {service_name}.")
|
|
99
|
-
services = ", ".join(prot["use_services"])
|
|
100
|
-
logger.info(f"Hardware {services} initialized. Ready to run an experiment")
|
|
101
|
-
self.print_information()
|
|
102
|
-
return self
|
|
103
|
-
|
|
104
|
-
def print_information(self):
|
|
105
|
-
for key, service in self.services.items():
|
|
106
|
-
if hasattr(service, "information"):
|
|
107
|
-
rich_information(service.information(), prefix=key)
|
|
108
|
-
|
|
109
|
-
def prepare_run(
|
|
110
|
-
self,
|
|
111
|
-
save_prefix=None,
|
|
112
|
-
analog_data_out=None,
|
|
113
|
-
digital_data_out=None,
|
|
114
|
-
duration=None,
|
|
115
|
-
metadata=None,
|
|
116
|
-
preview=False,
|
|
117
|
-
):
|
|
118
|
-
if not self.services:
|
|
119
|
-
self.setup_hardware()
|
|
120
|
-
self.run_count += 1
|
|
121
|
-
if save_prefix is None:
|
|
122
|
-
if self.save_prefix_root is not None:
|
|
123
|
-
save_prefix = self.save_prefix_root
|
|
124
|
-
else:
|
|
125
|
-
save_prefix = f"{config['host'] or 'localhost'}-{time.strftime('%Y%m%d_%H%M%S')}"
|
|
126
|
-
|
|
127
|
-
savefolder = Path(config["savefolder"] or ".")
|
|
128
|
-
if self.save_prefix_root is None:
|
|
129
|
-
while (savefolder / save_prefix).exists():
|
|
130
|
-
time.sleep(1)
|
|
131
|
-
save_prefix = f"{config['host'] or 'localhost'}-{time.strftime('%Y%m%d_%H%M%S')}"
|
|
132
|
-
run_duration = duration
|
|
133
|
-
if run_duration is None and self.prot["maxduration"] > 0:
|
|
134
|
-
run_duration = self.prot["maxduration"]
|
|
135
|
-
if run_duration is None:
|
|
136
|
-
n_samples = 0
|
|
137
|
-
if analog_data_out is not None:
|
|
138
|
-
n_samples = analog_data_out.shape[0]
|
|
139
|
-
if digital_data_out is not None:
|
|
140
|
-
n_samples = max(n_samples, digital_data_out.shape[0])
|
|
141
|
-
for service in self.services.values():
|
|
142
|
-
if isinstance(service, ResumableDAQ) and n_samples:
|
|
143
|
-
run_duration = n_samples / service.fs
|
|
144
|
-
if run_duration is None:
|
|
145
|
-
raise ValueError("Provide duration or DAQ output arrays so run duration can be inferred.")
|
|
146
|
-
if preview:
|
|
147
|
-
run_duration = 1_000_000
|
|
148
|
-
|
|
149
|
-
run_folder = savefolder / save_prefix
|
|
150
|
-
run_folder.mkdir(parents=True, exist_ok=True)
|
|
151
|
-
logger.info(f"Preparing resumable experiment {save_prefix}.")
|
|
152
|
-
service_counts = {}
|
|
153
|
-
for service_name, service in self.services.items():
|
|
154
|
-
service_type = service_base_name(service_name)
|
|
155
|
-
service_index = service_counts.get(service_type, 0)
|
|
156
|
-
service_counts[service_type] = service_index + 1
|
|
157
|
-
save_suffix = f"_{service_index + 1}" if service_index > 0 else ""
|
|
158
|
-
savefilename = run_folder / f"{save_prefix}{save_suffix}"
|
|
159
|
-
if isinstance(service, ResumableGCM):
|
|
160
|
-
logger.info(f" preparing {service_name}.")
|
|
161
|
-
service.prepare_run(str(savefilename), run_duration + 10, preview=preview)
|
|
162
|
-
elif isinstance(service, ResumableDAQ):
|
|
163
|
-
if preview:
|
|
164
|
-
logger.info(f" skipping {service_name} for preview.")
|
|
165
|
-
service.stop_run()
|
|
166
|
-
continue
|
|
167
|
-
logger.info(f" preparing {service_name}.")
|
|
168
|
-
service.prepare_run(
|
|
169
|
-
str(savefilename),
|
|
170
|
-
analog_data_out=analog_data_out,
|
|
171
|
-
digital_data_out=digital_data_out,
|
|
172
|
-
duration=run_duration,
|
|
173
|
-
metadata=metadata,
|
|
174
|
-
)
|
|
175
|
-
self.print_information()
|
|
176
|
-
return self
|
|
177
|
-
|
|
178
|
-
def prepare_playlist_run(self, playlistfile, save_prefix=None, metadata=None, preview=False):
|
|
179
|
-
analog_data_out, digital_data_out, duration = playlist_arrays(self.prot, playlistfile, cache=self._playlist_cache)
|
|
180
|
-
return self.prepare_run(
|
|
181
|
-
save_prefix=save_prefix,
|
|
182
|
-
analog_data_out=analog_data_out,
|
|
183
|
-
digital_data_out=digital_data_out,
|
|
184
|
-
duration=duration,
|
|
185
|
-
metadata=metadata,
|
|
186
|
-
preview=preview,
|
|
187
|
-
)
|
|
188
|
-
|
|
189
|
-
def start(self):
|
|
190
|
-
logger.info("Starting resumable experiment.")
|
|
191
|
-
for service in self.services.values():
|
|
192
|
-
if isinstance(service, ResumableGCM):
|
|
193
|
-
service.start()
|
|
194
|
-
time.sleep(0.5)
|
|
195
|
-
for service in self.services.values():
|
|
196
|
-
if isinstance(service, ResumableDAQ) and getattr(service, "state", "prepared") == "prepared":
|
|
197
|
-
service.start()
|
|
198
|
-
logger.info("Resumable experiment running.")
|
|
199
|
-
return self
|
|
200
|
-
|
|
201
|
-
def stop_run(self):
|
|
202
|
-
logger.info("Stopping resumable experiment.")
|
|
203
|
-
for service in self.services.values():
|
|
204
|
-
if isinstance(service, ResumableDAQ):
|
|
205
|
-
service.stop_run()
|
|
206
|
-
for service in self.services.values():
|
|
207
|
-
if isinstance(service, ResumableGCM):
|
|
208
|
-
service.stop_run()
|
|
209
|
-
logger.info("Resumable experiment stopped.")
|
|
210
|
-
return self
|
|
211
|
-
|
|
212
|
-
def close(self):
|
|
213
|
-
logger.info("Shutting down resumable hardware.")
|
|
214
|
-
for service in self.services.values():
|
|
215
|
-
service.close()
|
|
216
|
-
logger.info("Resumable hardware shut down.")
|
|
217
|
-
return self
|
|
218
|
-
|
|
219
|
-
def progress(self):
|
|
220
|
-
progress = {}
|
|
221
|
-
for service_name, service in self.services.items():
|
|
222
|
-
progress[service_name] = service.progress()
|
|
223
|
-
return progress
|
|
224
|
-
|
|
225
|
-
def monitor_progress(self, show_progress=True, stop_event=None):
|
|
226
|
-
with Progress(disable=not show_progress) as progress:
|
|
227
|
-
tasks = {}
|
|
228
|
-
for service_name, service in self.services.items():
|
|
229
|
-
service_progress = service.progress()
|
|
230
|
-
if service_progress["total"]:
|
|
231
|
-
tasks[service_name] = progress.add_task(f"[red]{service_name}", total=service_progress["total"])
|
|
232
|
-
while tasks and not progress.finished:
|
|
233
|
-
if stop_event is not None and stop_event.is_set():
|
|
234
|
-
break
|
|
235
|
-
for task_name, task_id in tasks.items():
|
|
236
|
-
if progress._tasks[task_id].finished:
|
|
237
|
-
continue
|
|
238
|
-
p = self.services[task_name].progress()
|
|
239
|
-
description = None
|
|
240
|
-
if "framenumber" in p and p["elapsed_delta"]:
|
|
241
|
-
description = f"{task_name} {p['framenumber_delta'] / p['elapsed_delta']: 7.2f} fps"
|
|
242
|
-
progress.update(task_id, completed=p["elapsed"], description=description)
|
|
243
|
-
time.sleep(1)
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
def _wait_for_run(runner, duration, show_progress=True):
|
|
247
|
-
runner.monitor_progress(show_progress=show_progress)
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
def run(
|
|
251
|
-
protocolfile,
|
|
252
|
-
playlistfile=None,
|
|
253
|
-
*,
|
|
254
|
-
save_prefix=None,
|
|
255
|
-
show_progress=True,
|
|
256
|
-
debug=False,
|
|
257
|
-
preview=False,
|
|
258
|
-
):
|
|
259
|
-
if debug:
|
|
260
|
-
logging.getLogger().setLevel(logging.DEBUG)
|
|
261
|
-
runner = ResumableExperimentRunner(protocolfile=protocolfile, save_prefix_root=save_prefix)
|
|
262
|
-
try:
|
|
263
|
-
runner.setup_hardware()
|
|
264
|
-
if preview:
|
|
265
|
-
runner.prepare_run(save_prefix=save_prefix, preview=True)
|
|
266
|
-
duration = 1_000_000
|
|
267
|
-
elif playlistfile is None:
|
|
268
|
-
runner.prepare_run(save_prefix=save_prefix)
|
|
269
|
-
duration = runner.prot["maxduration"]
|
|
270
|
-
else:
|
|
271
|
-
analog_data_out, digital_data_out, duration = playlist_arrays(runner.prot, playlistfile)
|
|
272
|
-
runner.prepare_run(
|
|
273
|
-
save_prefix=save_prefix,
|
|
274
|
-
analog_data_out=analog_data_out,
|
|
275
|
-
digital_data_out=digital_data_out,
|
|
276
|
-
duration=duration,
|
|
277
|
-
)
|
|
278
|
-
runner.start()
|
|
279
|
-
if duration and duration > 0:
|
|
280
|
-
_wait_for_run(runner, duration, show_progress=show_progress)
|
|
281
|
-
return runner.stop_run()
|
|
282
|
-
finally:
|
|
283
|
-
runner.close()
|
|
1
|
+
import copy
|
|
2
|
+
import logging
|
|
3
|
+
import time
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
import numpy as np
|
|
7
|
+
import rich
|
|
8
|
+
from rich.progress import Progress
|
|
9
|
+
|
|
10
|
+
from . import config
|
|
11
|
+
from .services import service_base_name
|
|
12
|
+
from .services.resumable import ResumableDAQ, ResumableGCM
|
|
13
|
+
from .utils.tui import rich_information
|
|
14
|
+
from .utils.config import defaultify, readconfig
|
|
15
|
+
from .utils.sound import build_playlist, load_sounds, parse_table
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
logger = logging.getLogger(__name__)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _attenuation_for_host():
|
|
22
|
+
attenuation = config["ATTENUATION"]
|
|
23
|
+
host = config["host"] or "localhost"
|
|
24
|
+
if isinstance(attenuation, dict) and host in attenuation:
|
|
25
|
+
return attenuation[host]
|
|
26
|
+
return attenuation
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _playlist_cache_key(protocol, playlistfile):
|
|
30
|
+
daq_name = next((name for name in protocol["use_services"] if service_base_name(name) == "DAQ"), None)
|
|
31
|
+
if daq_name is None:
|
|
32
|
+
return None
|
|
33
|
+
daq_params = protocol[daq_name]
|
|
34
|
+
attenuation = _attenuation_for_host()
|
|
35
|
+
return (
|
|
36
|
+
daq_name,
|
|
37
|
+
str(Path(playlistfile).resolve()),
|
|
38
|
+
daq_params["samplingrate"],
|
|
39
|
+
str(config["stimfolder"]),
|
|
40
|
+
repr(attenuation),
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def playlist_arrays(protocol, playlistfile, cache=None):
|
|
45
|
+
daq_name = next((name for name in protocol["use_services"] if service_base_name(name) == "DAQ"), None)
|
|
46
|
+
if daq_name is None:
|
|
47
|
+
return None, None, protocol["maxduration"]
|
|
48
|
+
|
|
49
|
+
daq_params = protocol[daq_name]
|
|
50
|
+
fs = daq_params["samplingrate"]
|
|
51
|
+
cache_key = _playlist_cache_key(protocol, playlistfile)
|
|
52
|
+
if cache is not None and cache_key in cache:
|
|
53
|
+
playlist, sounds = cache[cache_key]
|
|
54
|
+
logger.info(f"Reusing loaded stimuli for {playlistfile}.")
|
|
55
|
+
else:
|
|
56
|
+
playlist = parse_table(playlistfile)
|
|
57
|
+
attenuation = _attenuation_for_host()
|
|
58
|
+
sounds = load_sounds(playlist, fs, attenuation=attenuation, stimfolder=config["stimfolder"])
|
|
59
|
+
sounds = [sound.astype(np.float64) for sound in sounds]
|
|
60
|
+
if cache is not None:
|
|
61
|
+
cache[cache_key] = (playlist, sounds)
|
|
62
|
+
playlist_items, duration = build_playlist(sounds, protocol["maxduration"], fs, shuffle=daq_params["shuffle"])
|
|
63
|
+
data = np.concatenate([sounds[item] for item in playlist_items], axis=0)
|
|
64
|
+
|
|
65
|
+
nb_digital = len(daq_params["digital_chans_out"] or [])
|
|
66
|
+
if nb_digital:
|
|
67
|
+
return data[:, :-nb_digital], data[:, -nb_digital:].astype(np.uint8), duration
|
|
68
|
+
return data, None, duration
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class ResumableExperimentRunner:
|
|
72
|
+
def __init__(self, protocolfile=None, *, protocol=None, save_prefix_root=None):
|
|
73
|
+
if protocolfile is None and protocol is None:
|
|
74
|
+
raise ValueError("Provide protocolfile or protocol.")
|
|
75
|
+
self.protocolfile = protocolfile
|
|
76
|
+
self.protocol = copy.deepcopy(protocol) if protocol is not None else None
|
|
77
|
+
self.save_prefix_root = save_prefix_root
|
|
78
|
+
self.run_count = 0
|
|
79
|
+
self.services = {}
|
|
80
|
+
self._playlist_cache = {}
|
|
81
|
+
|
|
82
|
+
def setup_hardware(self):
|
|
83
|
+
prot = defaultify(copy.deepcopy(self.protocol)) if self.protocol is not None else readconfig(self.protocolfile)
|
|
84
|
+
self.prot = prot
|
|
85
|
+
defaults = config
|
|
86
|
+
if defaults["host"] is None:
|
|
87
|
+
defaults["host"] = "localhost"
|
|
88
|
+
rich.print(defaults)
|
|
89
|
+
logger.info("Setting up resumable hardware.")
|
|
90
|
+
for service_name in prot["use_services"]:
|
|
91
|
+
base = service_base_name(service_name)
|
|
92
|
+
logger.info(f" {service_name}.")
|
|
93
|
+
if base == "GCM":
|
|
94
|
+
self.services[service_name] = ResumableGCM(prot[service_name]).setup_hardware()
|
|
95
|
+
elif base == "DAQ":
|
|
96
|
+
self.services[service_name] = ResumableDAQ(prot[service_name]).setup_hardware()
|
|
97
|
+
else:
|
|
98
|
+
raise ValueError(f"Resumable runner only supports DAQ and GCM, got {service_name}.")
|
|
99
|
+
services = ", ".join(prot["use_services"])
|
|
100
|
+
logger.info(f"Hardware {services} initialized. Ready to run an experiment")
|
|
101
|
+
self.print_information()
|
|
102
|
+
return self
|
|
103
|
+
|
|
104
|
+
def print_information(self):
|
|
105
|
+
for key, service in self.services.items():
|
|
106
|
+
if hasattr(service, "information"):
|
|
107
|
+
rich_information(service.information(), prefix=key)
|
|
108
|
+
|
|
109
|
+
def prepare_run(
|
|
110
|
+
self,
|
|
111
|
+
save_prefix=None,
|
|
112
|
+
analog_data_out=None,
|
|
113
|
+
digital_data_out=None,
|
|
114
|
+
duration=None,
|
|
115
|
+
metadata=None,
|
|
116
|
+
preview=False,
|
|
117
|
+
):
|
|
118
|
+
if not self.services:
|
|
119
|
+
self.setup_hardware()
|
|
120
|
+
self.run_count += 1
|
|
121
|
+
if save_prefix is None:
|
|
122
|
+
if self.save_prefix_root is not None:
|
|
123
|
+
save_prefix = self.save_prefix_root
|
|
124
|
+
else:
|
|
125
|
+
save_prefix = f"{config['host'] or 'localhost'}-{time.strftime('%Y%m%d_%H%M%S')}"
|
|
126
|
+
|
|
127
|
+
savefolder = Path(config["savefolder"] or ".")
|
|
128
|
+
if self.save_prefix_root is None:
|
|
129
|
+
while (savefolder / save_prefix).exists():
|
|
130
|
+
time.sleep(1)
|
|
131
|
+
save_prefix = f"{config['host'] or 'localhost'}-{time.strftime('%Y%m%d_%H%M%S')}"
|
|
132
|
+
run_duration = duration
|
|
133
|
+
if run_duration is None and self.prot["maxduration"] > 0:
|
|
134
|
+
run_duration = self.prot["maxduration"]
|
|
135
|
+
if run_duration is None:
|
|
136
|
+
n_samples = 0
|
|
137
|
+
if analog_data_out is not None:
|
|
138
|
+
n_samples = analog_data_out.shape[0]
|
|
139
|
+
if digital_data_out is not None:
|
|
140
|
+
n_samples = max(n_samples, digital_data_out.shape[0])
|
|
141
|
+
for service in self.services.values():
|
|
142
|
+
if isinstance(service, ResumableDAQ) and n_samples:
|
|
143
|
+
run_duration = n_samples / service.fs
|
|
144
|
+
if run_duration is None:
|
|
145
|
+
raise ValueError("Provide duration or DAQ output arrays so run duration can be inferred.")
|
|
146
|
+
if preview:
|
|
147
|
+
run_duration = 1_000_000
|
|
148
|
+
|
|
149
|
+
run_folder = savefolder / save_prefix
|
|
150
|
+
run_folder.mkdir(parents=True, exist_ok=True)
|
|
151
|
+
logger.info(f"Preparing resumable experiment {save_prefix}.")
|
|
152
|
+
service_counts = {}
|
|
153
|
+
for service_name, service in self.services.items():
|
|
154
|
+
service_type = service_base_name(service_name)
|
|
155
|
+
service_index = service_counts.get(service_type, 0)
|
|
156
|
+
service_counts[service_type] = service_index + 1
|
|
157
|
+
save_suffix = f"_{service_index + 1}" if service_index > 0 else ""
|
|
158
|
+
savefilename = run_folder / f"{save_prefix}{save_suffix}"
|
|
159
|
+
if isinstance(service, ResumableGCM):
|
|
160
|
+
logger.info(f" preparing {service_name}.")
|
|
161
|
+
service.prepare_run(str(savefilename), run_duration + 10, preview=preview)
|
|
162
|
+
elif isinstance(service, ResumableDAQ):
|
|
163
|
+
if preview:
|
|
164
|
+
logger.info(f" skipping {service_name} for preview.")
|
|
165
|
+
service.stop_run()
|
|
166
|
+
continue
|
|
167
|
+
logger.info(f" preparing {service_name}.")
|
|
168
|
+
service.prepare_run(
|
|
169
|
+
str(savefilename),
|
|
170
|
+
analog_data_out=analog_data_out,
|
|
171
|
+
digital_data_out=digital_data_out,
|
|
172
|
+
duration=run_duration,
|
|
173
|
+
metadata=metadata,
|
|
174
|
+
)
|
|
175
|
+
self.print_information()
|
|
176
|
+
return self
|
|
177
|
+
|
|
178
|
+
def prepare_playlist_run(self, playlistfile, save_prefix=None, metadata=None, preview=False):
|
|
179
|
+
analog_data_out, digital_data_out, duration = playlist_arrays(self.prot, playlistfile, cache=self._playlist_cache)
|
|
180
|
+
return self.prepare_run(
|
|
181
|
+
save_prefix=save_prefix,
|
|
182
|
+
analog_data_out=analog_data_out,
|
|
183
|
+
digital_data_out=digital_data_out,
|
|
184
|
+
duration=duration,
|
|
185
|
+
metadata=metadata,
|
|
186
|
+
preview=preview,
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
def start(self):
|
|
190
|
+
logger.info("Starting resumable experiment.")
|
|
191
|
+
for service in self.services.values():
|
|
192
|
+
if isinstance(service, ResumableGCM):
|
|
193
|
+
service.start()
|
|
194
|
+
time.sleep(0.5)
|
|
195
|
+
for service in self.services.values():
|
|
196
|
+
if isinstance(service, ResumableDAQ) and getattr(service, "state", "prepared") == "prepared":
|
|
197
|
+
service.start()
|
|
198
|
+
logger.info("Resumable experiment running.")
|
|
199
|
+
return self
|
|
200
|
+
|
|
201
|
+
def stop_run(self):
|
|
202
|
+
logger.info("Stopping resumable experiment.")
|
|
203
|
+
for service in self.services.values():
|
|
204
|
+
if isinstance(service, ResumableDAQ):
|
|
205
|
+
service.stop_run()
|
|
206
|
+
for service in self.services.values():
|
|
207
|
+
if isinstance(service, ResumableGCM):
|
|
208
|
+
service.stop_run()
|
|
209
|
+
logger.info("Resumable experiment stopped.")
|
|
210
|
+
return self
|
|
211
|
+
|
|
212
|
+
def close(self):
|
|
213
|
+
logger.info("Shutting down resumable hardware.")
|
|
214
|
+
for service in self.services.values():
|
|
215
|
+
service.close()
|
|
216
|
+
logger.info("Resumable hardware shut down.")
|
|
217
|
+
return self
|
|
218
|
+
|
|
219
|
+
def progress(self):
|
|
220
|
+
progress = {}
|
|
221
|
+
for service_name, service in self.services.items():
|
|
222
|
+
progress[service_name] = service.progress()
|
|
223
|
+
return progress
|
|
224
|
+
|
|
225
|
+
def monitor_progress(self, show_progress=True, stop_event=None):
|
|
226
|
+
with Progress(disable=not show_progress) as progress:
|
|
227
|
+
tasks = {}
|
|
228
|
+
for service_name, service in self.services.items():
|
|
229
|
+
service_progress = service.progress()
|
|
230
|
+
if service_progress["total"]:
|
|
231
|
+
tasks[service_name] = progress.add_task(f"[red]{service_name}", total=service_progress["total"])
|
|
232
|
+
while tasks and not progress.finished:
|
|
233
|
+
if stop_event is not None and stop_event.is_set():
|
|
234
|
+
break
|
|
235
|
+
for task_name, task_id in tasks.items():
|
|
236
|
+
if progress._tasks[task_id].finished:
|
|
237
|
+
continue
|
|
238
|
+
p = self.services[task_name].progress()
|
|
239
|
+
description = None
|
|
240
|
+
if "framenumber" in p and p["elapsed_delta"]:
|
|
241
|
+
description = f"{task_name} {p['framenumber_delta'] / p['elapsed_delta']: 7.2f} fps"
|
|
242
|
+
progress.update(task_id, completed=p["elapsed"], description=description)
|
|
243
|
+
time.sleep(1)
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
def _wait_for_run(runner, duration, show_progress=True):
|
|
247
|
+
runner.monitor_progress(show_progress=show_progress)
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def run(
|
|
251
|
+
protocolfile,
|
|
252
|
+
playlistfile=None,
|
|
253
|
+
*,
|
|
254
|
+
save_prefix=None,
|
|
255
|
+
show_progress=True,
|
|
256
|
+
debug=False,
|
|
257
|
+
preview=False,
|
|
258
|
+
):
|
|
259
|
+
if debug:
|
|
260
|
+
logging.getLogger().setLevel(logging.DEBUG)
|
|
261
|
+
runner = ResumableExperimentRunner(protocolfile=protocolfile, save_prefix_root=save_prefix)
|
|
262
|
+
try:
|
|
263
|
+
runner.setup_hardware()
|
|
264
|
+
if preview:
|
|
265
|
+
runner.prepare_run(save_prefix=save_prefix, preview=True)
|
|
266
|
+
duration = 1_000_000
|
|
267
|
+
elif playlistfile is None:
|
|
268
|
+
runner.prepare_run(save_prefix=save_prefix)
|
|
269
|
+
duration = runner.prot["maxduration"]
|
|
270
|
+
else:
|
|
271
|
+
analog_data_out, digital_data_out, duration = playlist_arrays(runner.prot, playlistfile)
|
|
272
|
+
runner.prepare_run(
|
|
273
|
+
save_prefix=save_prefix,
|
|
274
|
+
analog_data_out=analog_data_out,
|
|
275
|
+
digital_data_out=digital_data_out,
|
|
276
|
+
duration=duration,
|
|
277
|
+
)
|
|
278
|
+
runner.start()
|
|
279
|
+
if duration and duration > 0:
|
|
280
|
+
_wait_for_run(runner, duration, show_progress=show_progress)
|
|
281
|
+
return runner.stop_run()
|
|
282
|
+
finally:
|
|
283
|
+
runner.close()
|
etho/services/DAQZeroService.py
CHANGED
|
@@ -181,7 +181,7 @@ class DAQ(BaseZeroService):
|
|
|
181
181
|
)
|
|
182
182
|
if analog_data_out[0].shape[-1] is not len(self.analog_chans_out):
|
|
183
183
|
raise ValueError(f"Number of analog output channels ({len(self.analog_chans_out)}) does not match the number of channels in the sound files ({analog_data_out[0].shape[-1]}).")
|
|
184
|
-
self.taskAO.
|
|
184
|
+
self.taskAO.set_data_generator(data_playlist(analog_data_out, play_order, playlist_info, self.log, name="AO"))
|
|
185
185
|
if clock_source is None:
|
|
186
186
|
self.taskAO.CfgDigEdgeStartTrig("ai/StartTrigger", DAQmx_Val_Rising)
|
|
187
187
|
else:
|
|
@@ -195,7 +195,7 @@ class DAQ(BaseZeroService):
|
|
|
195
195
|
clock_source=clock_source,
|
|
196
196
|
logger=self.log,
|
|
197
197
|
)
|
|
198
|
-
self.taskDO.
|
|
198
|
+
self.taskDO.set_data_generator(data_playlist(digital_data_out, play_order, name="DO"))
|
|
199
199
|
if clock_source is None:
|
|
200
200
|
self.taskDO.CfgDigEdgeStartTrig("ai/StartTrigger", DAQmx_Val_Rising)
|
|
201
201
|
else:
|