-
Notifications
You must be signed in to change notification settings - Fork 2
/
block.py
474 lines (338 loc) · 12.6 KB
/
block.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
import threading
import traceback
import zmq
import logging
import sys
import time
from circusort.base.endpoint import Endpoint, EOCError, LOCError
from circusort.base.utils import get_log
from circusort.io.time_measurements import save_time_measurements
if sys.version_info.major == 3:
unicode = str # Python 3 compatibility.
class Block(threading.Thread):
"""Block base class."""
name = "Block"
params = {}
inputs = {}
outputs = {}
def __init__(self, name=None, log_address=None, log_level=logging.INFO, **kwargs):
"""Initialize block.
Arguments:
name: none | string (optional)
The default value is None.
log_address: none | string (optional)
The default value is None.
log_level: integer (optional)
The default value is logging.INFO.
"""
threading.Thread.__init__(self)
self.daemon = True
self.log_address = log_address
self.log_level = log_level
if name is not None:
self.name = name
if self.log_address is None:
raise NotImplementedError("no logger address")
self.log = get_log(self.log_address, name=__name__, log_level=self.log_level)
self.parent = None
self.host = None
self.running = False
self.ready = False
self.stop_pending = False
self.t_start = None
self.nb_steps = None
self.is_active = False
self.start_steps = None
self.check_interval = 100
self._measured_times = {}
self.counter = 0
self.mpl_display = False
self.introspection_path = None
self._timeout = 60000 # ms
self.context = zmq.Context()
self.params.update(kwargs)
self.configure(**self.params)
self.log.debug("{n} has been created".format(n=self.name))
self.log.debug(str(self))
def __del__(self):
# Log debug message.
string = "{} is destroyed"
message = string.format(self.name)
self.log.debug(message)
return
def set_manager(self, manager_name):
self.parent = manager_name
def set_host(self, host):
self.host = host
def add_output(self, name, structure='array'):
self.outputs[name] = Endpoint(self, name, structure)
def add_input(self, name, structure='array'):
self.inputs[name] = Endpoint(self, name, structure)
def _initialize(self):
raise NotImplementedError()
def initialize(self):
# Log debug message.
string = "{} is initialized."
message = string.format(self.name)
self.log.debug(message)
self.ready = True
self.counter = 0
return self._initialize()
@property
def input(self):
if len(self.inputs) == 1:
input_ = self.inputs[list(self.inputs.keys())[0]]
elif len(self.inputs) == 0:
# Log error message.
string = "{} has no inputs"
message = string.format(self.name)
self.log.error(message)
# Raise error.
raise AttributeError()
else:
# Log error message.
string = "{} has multiple inputs:{}, you must be more explicit"
message = string.format(self.name, self.inputs.keys())
self.log.error(message)
# Raise error.
raise AttributeError()
return input_
@property
def output(self):
if len(self.outputs) == 1:
output = self.outputs[list(self.outputs.keys())[0]]
elif len(self.outputs) == 0:
# Log error message.
string = "{} has no outputs"
message = string.format(self.name)
self.log.error(message)
# Raise error.
raise AttributeError()
else:
# Log error message.
string = "{} has multiple outputs:{}, you must be more explicit"
message = string.format(self.name, self.outputs.keys())
self.log.error(message)
# Raise error.
raise AttributeError()
return output
@property
def nb_inputs(self):
return len(self.inputs)
@property
def nb_outputs(self):
return len(self.outputs)
def get_input(self, key):
return self.inputs[key]
def get_output(self, key):
return self.outputs[key]
def connect(self, key):
# Log debug message.
string = "{} establishes connections"
message = string.format(self.name)
self.log.debug(message)
self.get_input(key).socket = self.context.socket(zmq.SUB)
self.get_input(key).socket.setsockopt(zmq.RCVTIMEO, self._timeout)
self.get_input(key).socket.connect(self.get_input(key).addr)
self.get_input(key).socket.setsockopt_string(zmq.SUBSCRIBE, unicode(""))
return
def _get_output_parameters(self):
return {}
def get_output_parameters(self):
params = self._get_output_parameters()
return params
def configure(self, **kwargs):
for key, value in kwargs.items():
self.params[key] = kwargs[key]
try:
self.__setattr__(key, value)
except AttributeError:
string = "can't set attribute (key: {}, value: {})"
message = string.format(key, value)
raise AttributeError(message)
self.ready = False
# Log debug message.
string = "{} is configured"
message = string.format(self.name)
self.log.debug(message)
return
def _configure_input_parameters(self, **kwargs):
return
def configure_input_parameters(self, **kwargs):
self._configure_input_parameters(**kwargs)
return
def _update_initialization(self):
return
def update_initialization(self):
self._update_initialization()
return
def _guess_output_endpoints(self, **kwargs):
return
def guess_output_endpoints(self, **kwargs):
if self.nb_inputs > 0 and self.nb_outputs > 0:
# Log debug message.
string = "{} guesses output connections"
message = string.format(self.name)
self.log.debug(message)
self._guess_output_endpoints(**kwargs)
return
def _sync_buffer(self, dictionary, nb_samples,
nb_parallel_blocks=1, parallel_block_id=0, shift=0):
offset = dictionary['offset']
buffer_id = self.counter * nb_parallel_blocks + parallel_block_id + shift
is_synced = buffer_id * nb_samples <= offset
return is_synced
def run(self):
if not self.ready:
self.initialize()
# Log debug message.
string = "{} is running"
message = string.format(self.name)
self.log.debug(message)
self.running = True
self._set_start_step()
try:
if self.nb_steps is not None:
while self.counter < self.nb_steps:
self._process()
self.counter += 1
# if numpy.mod(self.counter, self.check_interval) == 0:
# self._check_real_time_ratio()
else:
try:
while self.running and not self.stop_pending:
self._process()
self.counter += 1
except (LOCError, EOCError):
for output in self.outputs.values():
output.send_end_connection()
self.stop_pending = True
self.running = False
if self.running and self.stop_pending and self.nb_inputs == 0:
# In this condition, the block is a source block.
for output in self.outputs.values():
output.send_end_connection()
self.running = False
try:
while self.running and self.stop_pending:
self._process()
self.counter += 1
# if numpy.mod(self.counter, self.check_interval) == 0:
# self._check_real_time_ratio()
except (LOCError, EOCError):
for output in self.outputs.values():
output.send_end_connection()
self.running = False
except Exception as e: # i.e. unexpected exception
# Send EOC signal through each output.
for output in self.outputs.values():
output.send_end_connection()
# Switch running flag.
self.running = False
# Log exception name and trace.
exception_name = e.__class__.__name__
exception_trace = traceback.format_exc()
# Log debug message.
string = "{} in block {}: {}"
message = string.format(exception_name, self.name, exception_trace)
self.log.error(message)
# Log debug message.
string = "{} is stopped"
message = string.format(self.name)
self.log.debug(message)
self._introspect()
if self.introspection_path is not None:
self._save_introspection()
return
def _process(self):
"""Abstract method, processing task of this block."""
raise NotImplementedError()
def _introspect(self):
"""Introspection of this block."""
# Log info message.
nb_buffers = self.counter - self.start_step
if self.real_time_ratio is not None:
string = "{} processed {} buffers [{} x real time]"
message = string.format(self.name, nb_buffers, self.real_time_ratio)
else:
string = "{} processed {} buffers"
message = string.format(self.name, nb_buffers)
self.log.info(message)
return
def _save_introspection(self):
"""Save introspection of this block."""
assert self.introspection_path is not None
name = self.name.lower().replace(' ', '_')
save_time_measurements(self.introspection_path, self._measured_times, name=name)
return
def stop(self):
"""Send a stop signal to the block.
The block will wait until the termination of the underlying process.
"""
self.stop_pending = True
return
def kill(self):
"""Kill the block.
The block won't wait until the termination of the underlying process.
"""
self.running = False
return
def _check_real_time_ratio(self):
"""Check real time ratio for this block.
This method is deprecated.
"""
data = self.real_time_ratio
if data is not None and data <= 1 and self.is_active:
# Log warning message.
string = "{} is lagging, running at {} x real time"
message = string.format(self.name_and_counter, data)
self.log.warning(message)
return
def list_parameters(self):
return self.params.keys()
def _measure_time(self, label='default', frequency=1):
if self.counter % frequency == 0:
time_ = time.time()
try:
self._measured_times[label].append(time_)
except KeyError:
self._measured_times[label] = [time_]
return
@property
def real_time_ratio(self):
if hasattr(self, 'nb_samples') and hasattr(self, 'sampling_rate'):
data_time = float(self.counter) * (float(self.nb_samples) / self.sampling_rate)
ratio = data_time / self.run_time
else:
ratio = None
return ratio
@property
def run_time(self):
"""The time elapsed since the start time [s]."""
time_ = time.time() - self.t_start
return time_
@property
def name_and_counter(self):
string = "{}[{} steps]".format(self.name, self.counter)
return string
def _set_start_step(self):
self.t_start = time.time()
self.start_step = self.counter
return
def _set_active_mode(self):
# Log debug message.
string = "{} is now active"
message = string.format(self.name_and_counter)
self.log.debug(message)
self.is_active = True
self._set_start_step()
return
def __str__(self):
lines = [
"Block object {} with params:".format(self.name),
] + [
" {}: {}".format(key, value)
for key, value in self.params.items()
]
string = "\n".join(lines)
return string