-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_channels.py
More file actions
69 lines (49 loc) · 2.63 KB
/
array_channels.py
File metadata and controls
69 lines (49 loc) · 2.63 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
"""Channels that process array input.
All channels accept either a single array or a sequence of arrays as input.
All input arrays must have the same dimensions.
sum_channel -- outputs the elementwise sum of the input
moving_average_channel - outputs the moving average over a given number of inputs
inverse_channel -- outputs the negative of the summed input
process_sequence -- helper function to process a sequence of inputs on a channel
See also modular.channels.channels
"""
from .channels import memoryless_channel, multi_input_channel
from .channels import process_sequence as process
from .numeric_channels import _single_input_moving_average_channel as _numeric_moving_average
from ._util import identity
import numpy as np
def _sum(value):
if value is None:
raise TypeError("NoneType")
value = np.asarray([v for v in value])
if len(value.shape) < 2 or value.shape[1] == 0:
return value
return np.sum(value, axis=0)
def sum_channel():
"""Returns an initialized generator that outputs the sum over the input."""
return memoryless_channel(_sum)
@multi_input_channel(sum_channel)
def moving_average_channel(n, initial_values = (), operation = identity, zero_val=0):
"""Returns a generator that returns the moving average over n elements preceeding its input.
The average is the arithmetic vector valued mean of the elements.
Keyword arguments:
initial_values -- At most n default values for the first iterations (default empty)
operation -- a function that operates on the inputs to the generator (default identity)
zero_value -- zero like value used to initialize the channel (default = 0)
"""
def array_operation(array):
np_array = np.asarray(array)
return operation(np_array)
return _numeric_moving_average(n, initial_values, array_operation, zero_val)
def process_sequence(channel, input_sequence, zero_val=tuple):
"""Returns an infinite generator of outputs for the given sequence of inputs.
The input_sequence must be a single sequence, a sequence of sequences or a
sequence of sequences of numbers.
The generator contains the output values of the module that is consecutively feed
with the elements from the input sequence followed by an infinte sequence of zeros.
None values in the output are converted to zeros.
Keyword arguments:
zero_val -- A function creating zero inputs for the channel (default = tuple)
The created values must not be None.
"""
return process(channel, input_sequence, iter(zero_val, None), zero_val())#"infinite generator of zeros")))