-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathOperators.py
More file actions
1374 lines (1178 loc) · 53.2 KB
/
Operators.py
File metadata and controls
1374 lines (1178 loc) · 53.2 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
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""This module has functions that convert operations on standard Python data structures
to operations on streams.
The module has three collections of functions:
(1) functions that convert operations on standard Python data structures
to operations on streams. These functions operate on a list of input
streams to generate a list of output streams. The functions deal with
the following data structures:
(a) lists,
(b) individual elements of lists,
(c) sliding windows, and
(d) timed windows.
(2) functions that map the general case of multiple input streams and
multiple output streams described above to the following special cases:
(a) merge: an arbitrary number of input streams and a single output stream.
(b) split: a single input stream and an arbitrary number of output streams.
(c) op: a single input stream and a single output stream.
(d) source: no input and an arbitrary number of output streams.
(e) sink: no ouput and an arbitrary number of input streams.
These special cases simplify functions that need to be written
for standard Python data structures. You can always use the multiple
inputs and outputs case even if there is only one or no input
or output; however, the functions for merge, split, op, source, and sink
are simpler than the multiple input and output case.
(3) a function that provides a single common signature for converting
operations on Python structures to operations on streams regardless of
whether the function has no inputs, a single input stream, a list of
input streams, or no outputs, a single output stream or a list of output
streams.
(12 October 2015. Mani. Changed initialization of output_lists.)
"""
from Agent import Agent
from Stream import Stream, StreamArray
from Stream import _no_value, _multivalue, _close, TimeAndValue
# ASSERTIONS USED IN FILE
def assert_is_list_of_streams_or_None(x):
assert isinstance(x, list) or isinstance(x, tuple) or x is None,\
'Expected {0} to be None or list or tuple.'.format(x)
if x is not None:
assert all(isinstance(l, Stream) for l in x),\
'Expected {0} to be a list (or tuple) of streams.'.format(x)
def assert_is_list_of_streams(x):
assert isinstance(x, list) or isinstance(x, tuple),\
'Expected {0} to be a list or tuple'.format(x)
assert all(isinstance(l, Stream) for l in x),\
'Expected {0} to be a list (or tuple) of streams'.format(x)
def assert_is_list_of_lists(x, list_size=None):
assert isinstance(x, list) or isinstance(x, tuple),\
'Expected {0} to be a list or tuple'.format(x)
assert all((isinstance(l, list) or isinstance(l, np.ndarray)) for l in x),\
'Expected {0} to be a list (or tuple) or np.ndarray of lists'.format(x)
assert list_size is None or list_size == len(x), \
'Expected len({0}) == {1}, or {1} to be None'.format(x, list_size)
def assert_is_list_or_None(x):
assert isinstance(x, list) or x is None, \
'Expected {0} to be a list or None'.format(x)
def assert_is_list(x):
assert isinstance(x, list), \
'Expected {0} to be a list'.format(x)
def remove_novalue_and_open_multivalue(l):
""" This function returns a list which is the
same as the input parameter l except that
(1) _no_value elements in l are deleted and
(2) each _multivalue element in l is opened
i.e., for an object _multivalue(list_x)
each element of list_x appears in the
returned list.
Parameter
---------
l : list
A list containing arbitrary elements
including, possibly _no_value and
_multi_value
Returns : list
-------
Same as l with every _no_value object
deleted and every _multivalue object
opened up.
Example
-------
l = [0, 1, _no_value, 10, _multivalue([20, 30])]
The function returns:
[0, 1, 10, 20, 30]
"""
if not isinstance(l, list):
return l
return_list = []
for v in l:
if v == _no_value:
continue
elif isinstance(v, _multivalue):
return_list.extend(v.lst)
else:
return_list.append(v)
return return_list
"""PART 1 OF MODULE
This part consists of functions that convert operations
on conventional data structures to operations on streams.
The functions are of two types:
(1) functions that return agents
e.g., list_agent, element_agent, window_agent,
dynamic_window_agent, timed_agent
(2) functions that return streams
e.g., list_func, element_func, window_func,
dynamic_window_func, timed_func
Functions that return agents have the following parameters:
f, inputs, num_outputs, state, call_streams, window_size,
step_size.
Parameters
----------
inputs : list of streams
The streams read by this agent.
inputs may be an empty list. (For example a data
source may not read any stream.)
inputs corresponds to parameter in_streams of agent.
outputs : list of streams.
The streams written by this agent.
outputs may be an empty list.
outputs corresponds to parameter out_streams of agent.
state : object
The state of the agent
call_streams : list of streams
See call_streams in agent
window_size : positive integer or None
For moving window operations, this is the size of
the window. The size is the number of elements in
the window.
window_size is None for operations that are not on
windows. For example, if the operation is on a single
element of a stream then window_size should be None
rather than 1 even though a window_size of 1 would work.
step_size : positive integer or None
step_size is the distance that a window is moved on
each step.
f : function
The function executed in a state transition.
Inputs to the function:
(1) A list of objects where the length of the list
is the number of input streams of the agent,
and where the object depends on the type of
wrapper used to convert f to a function on
streams. The objects are either elements of
the stream or windows into the stream.
(2) The state of the agent before a state transition.
Outputs of the function:
(1) A list of objects where the length of the list
is the number of output streams of the agent.
The j-th object in the list is appended to the
j-th output stream of the agent.
(2) The state of the agent after the transition.
Notes
-----
The structure of each of these functions is as follows:
The functions element_agent, window_agent, timed_agent
create agents. The functions element_func, window_func,
timed_func call element_agent, window_agent, timed_agent
(respectively) to create agents and also to create their
output streams. The functions element_func, window_func,
and timed_func are syntactic sugar; they are convenient
for functional composition.
"""
####################################################
# OPERATIONS ON LISTS
####################################################
def list_agent(f, inputs, outputs, state, call_streams,
window_size, step_size):
assert_is_list_of_streams_or_None(call_streams)
def transition(in_lists, state):
smallest_list_length = min(v.stop - v.start for v in in_lists)
input_lists = [v.list[v.start:v.start+smallest_list_length] for v in in_lists]
if not input_lists:
return ([[]]*num_outputs, state, [v.start for v in in_lists])
if state is None:
output_lists = f(input_lists)
else:
output_lists, state = f(input_lists, state)
## if num_outputs:
## assert_is_list_of_lists(output_lists, num_outputs)
in_lists_start_values = [v.start+smallest_list_length for v in in_lists]
return (output_lists, state, in_lists_start_values)
# Create agent
Agent(inputs, outputs, transition, state, call_streams)
def list_func(f, inputs, num_outputs, state, call_streams,
window_size, step_size):
outputs = [Stream() for i in range(num_outputs)]
list_agent(f, inputs, outputs, state, call_streams,
window_size, step_size)
return outputs
####################################################
# OPERATIONS ON SIMPLE ELEMENTS
####################################################
def element_agent(f, inputs, outputs, state, call_streams,
window_size, step_size):
assert_is_list_of_streams_or_None(call_streams)
num_outputs = len(outputs)
def transition(in_lists, state):
input_lists = zip(*[v.list[v.start:v.stop] for v in in_lists])
# If the new input data is empty then return empty lists for
# each output stream, and leave the state and the starting point
# for each input stream unchanged.
if not input_lists:
return ([[]]*num_outputs, state, [v.start for v in in_lists])
#list_of_output_list[i] will be set to the output value
# corresponding to the i-th value in each of the input
# streams
list_of_output_list = list()
for _ in range(len(input_lists)):
list_of_output_list.append(list())
for i,input_list in enumerate(input_lists):
if state is None:
output_list = f(input_list)
else:
output_list, state = f(input_list, state)
# The output_list returned by f must have
# one element for each output stream.
# The output list must be a list; so convert
# None values (for sinks) into empty lists.
if output_list is None: output_list = []
list_of_output_list[i] = output_list
# This function has at least one output because the sink case
# was considered in the last line.
# list_of_output_list[i] is a list with one element for each output stream.
# zip them up to get output_lists where output_lists[j] is the list that
# gets appended to output stream j.
output_lists = [list(v) for v in zip(*list_of_output_list)]
# Remove _no_value elements from the output list because they do not
# appear in streams.
# Open up _multivalue([a,b]) into separate a, b values.
output_lists = \
[remove_novalue_and_open_multivalue(l) for l in output_lists]
return (output_lists, state, [v.start+len(input_lists) for v in in_lists])
# Create agent
Agent(inputs, outputs, transition, state, call_streams)
def element_func(f, inputs, num_outputs, state, call_streams,
window_size, step_size):
outputs = [Stream() for i in range(num_outputs)]
element_agent(f, inputs, outputs, state, call_streams,
window_size, step_size)
return outputs
####################################################
# OPERATIONS ON WINDOWS
####################################################
def window_agent(f, inputs, outputs, state, call_streams,
window_size, step_size):
num_outputs = len(outputs)
#f: list, state -> element, state
def transition(in_lists, state=None):
range_out = range((num_outputs))
range_in = range(len(in_lists))
# This function will set the k-th element of output_lists
# to the value to be output on the k-th output stream.
output_lists = list()
for _ in range_out:
output_lists.append([])
# Avoids problems with output_list = [ [] for _ in range_out ]
# window_starts is the list of starting indices for the
# window in each input stream.
window_starts = [in_list.start for in_list in in_lists]
smallest_list_length = min(v.stop - v.start for v in in_lists)
if window_size > smallest_list_length:
# Do not have enough elements in an input stream
# for an operation on the window.
# So no changes are made.
return (output_lists, state, window_starts)
# Each input stream has enough elements for a window operation.
# num_steps is the number of window operations that can be
# carried out with the given numbers of unprocessed elements
# in the input streams.
num_steps = (smallest_list_length - window_size)/step_size
for i in range(num_steps):
# Calculate the output, 'increments', for this window operation.
# windows is a list with a window for each input stream.
# increments is a list with an element for each output stream.
# increments[k] will be appended to the k-th output stream
# by this function.
# The window for the j-th input stream starts at window_starts[j]
# and ends at window_starts[j]+window_size.
# in_lists[j].list is the list of messages on the j-th input stream.
windows = [in_lists[j].list[window_starts[j]:window_starts[j]+window_size] \
for j in range_in]
if state is None:
increments = f(windows)
else:
increments, state = f(windows, state)
# Remove _no_value and open up _multivalue elements in
# each [increments[k]].
# For example, _multivalue([11, 5, 9]) object will be
# added to the stream as three separate messages,
# 11, 5 and 9.
# Note that increments[k] is a value to be appended to
# the output stream. The function remove_novalue has
# a parameter which is a list. So we call the function
# with parameter [increments[k]] rather than increments[k]
# and we extend output_lists[k] rather than append to it.
for k in range_out:
output_lists[k].extend(
remove_novalue_and_open_multivalue([increments[k]]))
window_starts = [v+step_size for v in window_starts]
in_lists_start_values = [in_list.start + num_steps*step_size for in_list in in_lists]
return (output_lists, state, in_lists_start_values)
# Create agent
#output_streams = [Stream() for v in range(num_outputs)]
Agent(inputs, outputs, transition, state, call_streams)
#return output_streams
def window_func(f, inputs, num_outputs, state, call_streams,
window_size, step_size):
outputs = [Stream() for i in range(num_outputs)]
window_agent(f, inputs, outputs, state, call_streams,
window_size, step_size)
return outputs
####################################################
# OPERATIONS ON DYNAMIC WINDOWS
####################################################
def dynamic_window_agent(f, input_stream, output_stream, state,
min_window_size, max_window_size, step_size):
# state is a list where state[0] must be current_window_size
# state[1] is a boolean value, steady_state which
# indicates whether the max window size has been reached.
# reset is a boolean which is set to True when the window
# is to be reset to the min window size.
# state[3:] is defined by the user.
# current_window_size is state[0]
# steady_state is state[1]
# reset is state[2]
# This function produces a single output stream.
num_outputs = 1
def transition(in_lists, state):
current_window_size = state[0]
steady_state = state[1]
reset = state[2]
reset_increment = 0
# output_list is the list of messages that will be
# sent on the output stream in this transition.
output_list = list()
# input is the list of messages in the input
# stream that are the input for this transition.
# start, stop are pointers to the input stream
# where input begins at start and ends at stop.
input_in_list = in_lists[0]
start = input_in_list.start
stop = input_in_list.stop
input = input_in_list.list[start:stop]
input_length = stop - start
if input_length < step_size:
# Insufficient number of new messages in the input
# stream for a transition.
# No change
return ([output_list], state, [start])
# Increase the current window size in increments
# of step size, and ensure it does not exceed
# max window size.
# input[start_increment:start_increment+current_window_size]
# is the current window.
# start_increment is initially 0 and remains 0 until the
# current window size equals the max window size, and after
# that point the start_increment is increased by the step size.
start_increment = 0
for i in range(0, input_length, step_size):
## if not steady_state:
## current_window_size += step_size
if current_window_size < min_window_size:
# The current window size is too small
# for a window calculation.
current_window_size += step_size
continue
# Assert current_window_size >= min_window_size
# Ensure that current window size does not exceed max value.
current_window_size = min(current_window_size, max_window_size)
if start_increment+ reset_increment + current_window_size > input_length:
# Insufficient unprocessed messages in the input stream
# for the next window.
break
# input_window is the next window in the input stream.
input_window = input[
start_increment+reset_increment:start_increment+reset_increment+current_window_size]
# Compute increments to the output stream
# Note: function f MUST return state (where state[0]
# is the current_window_size and state[1] indicates whether
# the steady state, i.e., current window size equals max value,
# has been reached.
# Update the state to reflect the new value of current_window_size
state[0] = current_window_size
state[1] = steady_state
state[2] = reset
# Compute the new output
output_increment, state = f(input_window, state)
current_window_size = state[0]
steady_state = state[1]
reset = state[2]
if reset and steady_state:
steady_state = False
reset = False
start_increment += current_window_size - min_window_size
current_window_size = min_window_size - step_size
## reset_increment += start_increment
## reset_increment += max(0, current_window_size - min_window_size)
## start_increment = 0
if not steady_state:
# The start increment does not change because
# the starting point of the window remains
# unchanged until the current window size increases
# to the max window size. After that point,
# the starting point of the window moves forward
# by step size.
#start_increment = 0
if current_window_size >= max_window_size - step_size:
steady_state = True
start_increment += current_window_size - (max_window_size - step_size)
current_window_size = max_window_size
else:
current_window_size += step_size
#start_increment = 0
else:
start_increment += step_size
current_window_size = max_window_size
# Deal with special objects that should not be placed
# on the output stream.
output_increment = remove_novalue_and_open_multivalue(
[output_increment])
# Place the output increment on the output list.
# The messages in the output list will eventually
# be sent on the output stream
output_list.extend(output_increment)
# The start pointer for the input stream is moved forward
# to the starting point of the current window
start += start_increment
start_increment = 0
# Update state
state[0] = current_window_size
state[1] = steady_state
state[2] = reset
return ([output_list], state, [start])
# Create agent
Agent([input_stream], [output_stream], transition, state)
def dynamic_window_func(f, inputs, state,
min_window_size, max_window_size, step_size):
output_stream = Stream()
dynamic_window_agent(
f, inputs, output_stream, state,
min_window_size, max_window_size, step_size)
return output_stream
####################################################
# OPERATIONS ON TIMED WINDOWS
####################################################
def list_index_for_timestamp(in_list, start_index, timestamp):
""" A helper function for timed operators.
The basic idea is to return the earliest index in
in_list.list[start_index:in_list.stop] with a time field
that is greater than or equal to timestamp. If no such index
exists then return a negative number.
Parameters
----------
in_list: InList
InList = namedtuple('InList', ['list', 'start', 'stop'])
A slice into a stream.
start_index: nonnegative integer
A pointer into in_list.list
timestamp: number
Returns
-------
Returns positive integer i where:
either: 'FOUND TIME WINDOW IN IN_LIST'
i >= start_index and
i <= in_list.stop and
(in_list[start_index] >= timestamp
or
in_list.list[i-2].time < timestamp <= in_list.list[i-1].time
)
)
or: 'NO TIME WINDOW IN IN_LIST'
i < 0 (negative i indicates no time window) and
(in_list.list[in_list.stop-1] <= timestamp
or
the list is empty, i.e.
(in_list.start = in_list.stop)
Requires
--------
start_index >= in_list.start and
start_index < in_list.stop
"""
# If the list is empty then return a negative number to indicate
# absence of time window.
if in_list.start == in_list.stop:
return -1
if start_index < in_list.start or start_index >= in_list.stop:
raise Exception('start_index out of range: start_index =', start_index,
' in_list.start = ', in_list.start,
' in_list.stop = ', in_list.stop)
for i in range(start_index, in_list.stop):
# assert i <= in_list.stop-1
if in_list.list[i].time >= timestamp:
# Found an index i with a sufficiently large time.
return i
# All the times in in_list up to in_list.stop are less
# than timestamp.
# assert in_list.list[in_list.stop - 1] < timestamp
return -1 # Return a negative number to indicate absence of time window.
def timed_agent(f, inputs, outputs, state, call_streams,
window_size, step_size):
# inputs is a list of lists of TimeAndValue pairs with
# one list of TimeAndValue pairs for each input stream.
# num_outputs is the number of output streams
num_outputs = len(outputs)
range_out = range(num_outputs)
# num_inputs is the number of input streams.
num_inputs = len(inputs)
range_in = range(num_inputs)
window_start_time = 0
# state is the state of the underlying agent.
# Augment the state with the start time of the
# window; window times will be the times of
# TimeAndValue objects in the output streams.
combined_state = (window_start_time, state)
def transition(in_lists, combined_state):
window_start_time, state = combined_state
output_lists = list()
# output_lists is a list of lists.
# output_lists has one list for each output stream.
for _ in range_out:
output_lists.append([])
window_end_time = window_start_time + window_size
window_start_indexes = [ in_lists[j].start for j in range_in]
# Each iteration of the while loop carries out a
# calculation for one time window. At each successive
# iteration, the time window is moved forward by the
# step size. Both the window_size and step_size refer
# to time rather than the number of elements in the
# window.
# The while loop breaks when the next time window does
# not span all input streams, i.e. when the time stamps
# for some input stream aren't greater than or equal
# to the end-time of the time window.
while True:
# window_end_indexes is a list whose j-th
# element is either:
# (1) the earliest index in the j-th
# input list for which the stream element's time
# is window_end_time or greater, or
# (2) is a negative number if no such element
# exists in the list.
# In case (1) we have found a time window
# within this in_list, and in case (2)
# no time window exists within the in_list.
window_end_indexes = [list_index_for_timestamp(
in_lists[j],
window_start_indexes[j],
window_end_time) for j in range_in]
# If any time window is empty then do not
# carry out computations across the time windows
# of all the input streams. Return with no change
# to window_start_time or the state, and with
# the output_list for each stream set to the empty
# list.
if any(window_end_indexes[j] < 0 for j in range_in):
break
# Assert no time-window is empty.
# So, for each input stream j:
# window_end_indexes[j] > window_start_indexes[j]
windows = [in_lists[j].list[window_start_indexes[j]: \
window_end_indexes[j]] for j in range_in]
# windows is a list of num_inputs lists where:
# windows[j] is a list of TimeAndValue objects.
# Function f returns a list of num_outputs elements,
# one element for each output stream. These elements
# are usually objects other than TimeAndValue objects.
# increments[k] is the output element appended to
# the k-th output stream. increments[k] is a single object
# (and not necessarily a list).
if state is None:
increments = f(windows)
else:
increments, state = f(windows, state)
# The output list for each output stream contains TimeAndValue objects.
# The time field associated with increments[k] for all k is the
# window end time; so, all the messages on all the output streams
# associated with this input time-window have the same time-value.
for k in range_out:
output_lists[k].append(TimeAndValue(window_end_time, increments[k]))
# Increment the window start and end times by step size (which is also
# in units of time).
window_start_time += step_size
window_end_time += step_size
# Compute how far forward (measured in numbers of
# elements) the windows can move for each input
# stream.
# new_window_start_indexes[j] is the index
# of the start of the next window, IF all windows
# move forward.
new_window_start_indexes = [list_index_for_timestamp(
in_lists[j],
window_start_indexes[j],
window_start_time) for j in range_in]
# Exit the while-TRUE loop if a window on any stream
# cannot move forward because the stream doesn't have
# any more new data. This is indicated by a negative
# value for new_window_start_indexes[j] for stream j.
if any(new_window_start_indexes[j] < 0 for j in range_in):
break
## #CHECKING FOR PROGRESS TOWARDS TERMINATION
## if (any(new_window_start_indexes[j] < window_start_indexes[j]
## for j in range_in) or
## all(new_window_start_indexes[j] == window_start_indexes[j]
## for j in range_in)):
## raise Exception('TimedOperator: start_indexes')
window_start_indexes = new_window_start_indexes
combined_state = (window_start_time, state)
# return output messages, the new state, and the new start values of
# the input streams.
return (output_lists, combined_state, window_start_indexes)
# Create agent
combined_state = (window_start_time, state)
Agent(inputs, outputs, transition, combined_state)
def timed_func(f, inputs, num_outputs, state, call_streams,
window_size, step_size):
outputs = [Stream() for i in range(num_outputs)]
timed_agent(f, inputs, outputs, state, call_streams,
window_size, step_size)
return outputs
####################################################
# OPERATIONS ON ASYCHRONOUS INPUT STREAMS
####################################################
def asynch_element_agent(
f, inputs, outputs, state, call_streams,
window_size, step_size):
num_outputs = len(outputs)
assert_is_list_of_streams_or_None(call_streams)
def transition(in_lists, state):
# output_lists[j] will be sent on output stream j
output_lists = list()
for _ in range(num_outputs):
output_lists.append([])
# If the input data is empty, i.e., if v.stop == v.start for all
# v in in_lists, then return empty lists for each output stream,
# and leave the state and the starting point for each input
# stream unchanged.
if all(v.stop <= v.start for v in in_lists):
return (output_lists, state, [v.start for v in in_lists])
# Assert at least one input stream has unprocessed data.
for stream_number, v in enumerate(in_lists):
# if v.stop <= v.start then skip this input stream
# because no new messages have arrived on this stream.
if v.stop > v.start:
# Carry out a state transition for this input
# stream.
# In the following,input_list is the list of new values
# on this input stream. Compute the incremental list
# generated by each element in input list due to a
# transition, i.e., an execution of f.
input_list = v.list[v.start:v.stop]
# In the following, output_lists_increment is a list
# with length num_outputs. It is a list even when
# num_outputs is 0 or 1.
# Process each unprocessed message (element) in this
# input stream. output_lists_increment[k] is the message
# to be sent on outputs[k] due to the incoming message
# (element). Note that output_lists_increment[k] is an
# element and not a list of elements.
for element in input_list:
if state is None:
output_lists_increment = \
f((element, stream_number))
else:
# This function has state.
output_lists_increment, state = \
f((element, stream_number), state)
assert len(output_lists_increment) == num_outputs
for k in range(num_outputs):
# first remove _no_value and open up _multivalue
output_lists_increment[k] = \
remove_novalue_and_open_multivalue(
[output_lists_increment[k]])
output_lists[k].extend(output_lists_increment[k])
return (output_lists, state, [v.stop for v in in_lists])
# Create agent
Agent(inputs, outputs, transition, state, call_streams)
def asynch_element_func(
f, inputs, num_outputs, state, call_streams=None,
window_size=None, step_size=None):
assert_is_list_of_streams_or_None(call_streams)
def transition(in_lists, state):
# If the input data is empty then return empty lists for
# each output stream, and leave the state and the starting point
# for each input stream unchanged.
if all(v.stop <= v.start for v in in_lists):
return ([[]]*num_outputs, state, [v.start for v in in_lists])
# Assert at least one input stream has unprocessed data.
# output_lists[j] will be sent on output stream j
output_lists = []
for _ in range(num_outputs):
output_lists.append([])
#output_lists = [[]]*num_outputs
for stream_number, v in enumerate(in_lists):
# if v.stop <= v.start then skip this input stream
if v.stop > v.start:
# Carry out a state transition for this input
# stream.
# input_list is the list of new values on this
# stream. Compute the incremental list generated
# by each element in input list due to a transition,
# i.e., an execution of f.
input_list = v.list[v.start:v.stop]
for element in input_list:
if state is None:
output_lists_increment = \
f((element, stream_number))
else:
# This function has state.
output_lists_increment, state = \
f((element, stream_number), state)
for k in range(num_outputs):
output_lists_increment[k] = \
remove_novalue_and_open_multivalue(
[output_lists_increment[k]])
output_lists[k].extend(output_lists_increment[k])
return (output_lists, state, [v.stop for v in in_lists])
# Create agent
output_streams = [Stream() for i in range(num_outputs)]
Agent(inputs, output_streams, transition, state, call_streams)
return output_streams
"""
PART 2 OF MODULE.
Functions that map the general case of an arbitrary
number of input streams and an arbitrary number of output streams
to the following special cases:
(1) merge: multiple input streams, single output stream
(2) split: single input stream, multiple output streams
(3) op: single input stream, single output stream
(4) source: no input stream, single output stream
(5) sink: single input stream, no output streams.
Each of these functions has the following parameters:
f, h, in_streams, window_size, step_size, state, call_streams.
Parameters
----------
f_type: str
function on a standard Python data structure such as an
integer or a list.
f: A general case (muti-input, multi-output) function.
in_streams: A list of input streams
window_size: Either None or a positive integer
step_size: None if the window_size is None, otherwise a positive
integer.
state: The state of the computation.
call_streams: A list of streams. When a value is appended to any
stream in this list, the function is executed.
"""
def h(f_type, *args):
""" Calls the appropriate wrapper function given
the name of the wrapper. The wrapper functions are
list_func, element_func, window_func, ... for
wrapper names 'list', 'element', 'window',..
"""
if f_type is 'list':
return list_func(*args)
elif f_type is 'element':
return element_func(*args)
elif f_type is 'window':
return window_func(*args)
elif f_type is 'timed':
return timed_func(*args)
elif f_type is 'asynch_element':
return asynch_element_func(*args)
else:
return 'no match'
def h_agent(f_type, *args):
""" Calls the appropriate wrapper function given
the name of the wrapper. The wrapper functions are
list_agent, element_agent, window_agent, ... for
wrapper names 'list', 'element', 'window',..
"""
if f_type is 'list':
return list_agent(*args)
elif f_type is 'element':
return element_agent(*args)
elif f_type is 'window':
return window_agent(*args)
elif f_type is 'timed':
return timed_agent(*args)
elif f_type is 'asynch_element':
return asynch_element_agent(*args)
else:
return 'no match'
def many_to_many(f_type, f, in_streams, num_outputs, state,
call_streams, window_size, step_size):
def g(x, state=None):
if state is None: return f(x)
else:
output, new_state = f(x, state)
return (output, new_state)
out_streams = h(f_type, g, in_streams, num_outputs, state,
call_streams, window_size, step_size)
return out_streams
def many_to_many_agent(f_type, f, in_streams, out_streams, state,
call_streams, window_size, step_size):
def g(x, state=None):
if state is None: return f(x)
else:
output, new_state = f(x, state)
return (output, new_state)
h_agent(f_type, g, in_streams, out_streams,
state, call_streams, window_size, step_size)
def merge(f_type, f, in_streams, state, call_streams, window_size, step_size):
def g(x, state=None):
if state is None: return [f(x)]
else:
output, new_state = f(x, state)
return ([output], new_state)
out_streams = h(f_type, g, in_streams, 1, state, call_streams,
window_size, step_size)
return out_streams[0]
def merge_agent(f_type, f, in_streams, out_stream,
state, call_streams,
window_size, step_size):
def g(x, state=None):
if state is None: return [f(x)]
else:
output, new_state = f(x, state)
return ([output], new_state)
h_agent(f_type, g, in_streams, [out_stream],
state, call_streams, window_size, step_size)
def split(f_type, f, in_stream, num_outputs,
state, call_streams, window_size, step_size):
def g(x, state=None):
if state is None: return f(x[0])
else:
#output, new_state = f(x[0], state)
# return (output, new_state)
return f(x[0], state)
out_streams = h(f_type, g, [in_stream], num_outputs, state, call_streams,
window_size, step_size)
return out_streams
def split_agent(f_type, f, in_stream, out_streams,
state, call_streams, window_size, step_size):
def g(x, state=None):
if state is None: return f(x[0])
else:
# output, new_state = f(x[0], state)
# return (output, new_state)
return f(x[0], state)
h_agent(f_type, g, [in_stream], out_streams,
state, call_streams, window_size, step_size)
def op(f_type, f, in_stream, state, call_streams, window_size, step_size):
def g(x, state=None):
if state is None:
return [f(x[0])]
else:
output, new_state = f(x[0], state)
return ([output], new_state)
out_streams = h(f_type, g, [in_stream], 1, state, call_streams,
window_size, step_size)
return out_streams[0]
def op_agent(f_type, f, in_stream, out_stream,
state, call_streams, window_size, step_size):
def g(x, state=None):
if state is None:
return [f(x[0])]
else:
output, new_state = f(x[0], state)
return ([output], new_state)
h_agent(f_type, g, [in_stream], [out_stream],
state, call_streams, window_size, step_size)
def single_output_source(f_type, f, num_outputs, state, call_streams,