unaiverse.streams.streamlib¶
Tests, demos and tutorials need a stream that produces data without a sensor or a dataset behind it. The classes of this module are BufferedStream subclasses that compute each sample when the replay asks for it. An agent offers one of them to other peers like any other stream (see Data streams):
from unaiverse.streams.streams import Stream
from unaiverse.streams.streamlib import Sin
agent.add_streams([Stream.create(Sin(freq=0.1, phase=0., delta=1.), name="temperature", delta=1.)])
| Class | Produces |
|---|---|
AllHotLabelStream |
A constant (1, n) vector of ones whose components are named by labels. |
Random |
Uniform random tensors in [0, std). |
Sin |
Samples of a sine wave. |
Square |
Samples of a square wave. |
CombSin |
Samples of a sum of sines with random or given frequencies and amplitudes. |
SmoothHFHA, SmoothHFLA, SmoothLFLA, SmoothLFHA |
CombSin presets with three sines. |
SquareHFHA, SquareHFLA, SquareLFHA, SquareLFLA |
Square presets. |
The generators share the replay of BufferedStream. At each clock cycle the first call to get advances an index (0, 1, 2, ...) and the class computes the sample of that index. Because the constructor calls restart(), the index starts from 0 at the first read. The tag of a sample is the current clock cycle minus the cycle at which the replay of the uuid None started. The buffers stay empty. len(stream) is 0 and the generators never read the samples stored with set.
In Sin, Square and CombSin the argument delta is the time step of the signal and sample i is the value of the function at time i * delta. The description has a separate delta that paces the stream on the network and it stays -1. Use Stream.create(..., delta=...) to pace the stream on the network.
AllHotLabelStream
¶
Bases: BufferedStream
Source code in unaiverse/streams/streamlib.py
A static stream that always returns the vector torch.ones((1, len(labels))) in which every label is active. It serves as a constant target or as a placeholder label stream.
| Argument | Type | Default | Meaning |
|---|---|---|---|
labels |
list[str] |
The names of the components. | |
device |
torch.device |
torch.device("cpu") |
The device of the stored tensor. |
The description is a float tensor stream named "AllHotLabelStream" (data_desc="dummy stream") of shape (1, len(labels)) with the labels and the rule "geq0.5". The stream is static (is_static=True).
>>> s = AllHotLabelStream(["cat", "dog"])
>>> s.get(), s.props.to_text(s.get()) # after the clock has run a cycle
(tensor([[1., 1.]]), 'cat, dog')
Random
¶
Bases: BufferedStream
Source code in unaiverse/streams/streamlib.py
A stream of tensors drawn from a uniform distribution. Each sample is std * torch.rand(shape). The values lie in [0, std). The docstring of the class calls them zero-centred but the code never subtracts the mean.
| Argument | Type | Default | Meaning |
|---|---|---|---|
std |
float |
The width of the interval of the values. | |
shape |
tuple[int, ...] |
(1,) |
The shape of each sample. Every entry must be an int. The annotation allows None but Random(std, shape=None) fails with AssertionError because a tensor stream needs a shape. |
device |
torch.device |
torch.device("cpu") |
The device of the samples. |
The description is a torch.float32 tensor stream named "rand" with tensor_shape=shape. A new tensor is drawn at each step of the replay.
Sin
¶
Bases: BufferedStream
Source code in unaiverse/streams/streamlib.py
A stream of samples of a sine wave. Sample i is the (1, 1) float tensor sin(2 * pi * freq * t) with t = i * delta + phase / freq.
| Argument | Type | Default | Meaning |
|---|---|---|---|
freq |
float |
The frequency in Hz. 0 raises ZeroDivisionError. |
|
phase |
float |
The phase as a fraction of a period. With 0 the wave starts at zero and with 0.25 at its peak. |
|
delta |
float |
The time in seconds between two samples of the signal. | |
device |
torch.device |
torch.device("cpu") |
The device of the samples. |
The description is named "sin" with shape (1, 1) and dtype torch.float32. The attributes freq, phase, period (1 / freq), delta and device hold the arguments.
>>> s = Sin(freq=0.25, phase=0., delta=1.)
>>> [round(s.get().item(), 3) for _ in range(4)] # one get per clock cycle
[0.0, 1.0, -0.0, -1.0]
Square
¶
Bases: BufferedStream
Source code in unaiverse/streams/streamlib.py
A stream of samples of a square wave. Sample i is the (1, 1) float tensor ampl * (-1) ** floor(2 * freq * t) with t = i * delta + phase / freq. The value is ampl in the first half of each period and -ampl in the second.
| Argument | Type | Default | Meaning |
|---|---|---|---|
freq |
float |
The frequency in Hz. 0 raises ZeroDivisionError. |
|
ampl |
float |
The amplitude. | |
phase |
float |
The phase in periods. | |
delta |
float |
The time in seconds between two samples of the signal. | |
device |
torch.device |
torch.device("cpu") |
The device of the samples. |
The description is named "square" with shape (1, 1) and dtype torch.float32.
>>> s = Square(freq=0.5, ampl=2., phase=0., delta=0.5)
>>> [s.get().item() for _ in range(4)] # one get per clock cycle
[2.0, 2.0, -2.0, -2.0]
CombSin
¶
CombSin(f_cap: float | list, c_cap: float | list, order: int, delta: float, device: device = device('cpu'))
Bases: BufferedStream
Source code in unaiverse/streams/streamlib.py
A stream of samples of a sum of sines. Sample i is the (1, 1) float tensor sum_k coeffs[k] * sin(2 * pi * freqs[k] * t) with t = i * delta.
| Argument | Type | Default | Meaning |
|---|---|---|---|
f_cap |
float or list |
A float draws order frequencies uniformly in [0, f_cap). A list gives the frequencies directly. |
|
c_cap |
float or list |
A float draws order coefficients uniformly in [-c_cap, c_cap). A list gives the coefficients directly. |
|
order |
int |
The number of sines drawn for a float cap. It is ignored for a list. |
|
delta |
float |
The time in seconds between two samples of the signal. | |
device |
torch.device |
torch.device("cpu") |
Stored in device. |
A cap that is neither a float nor a list raises a plain Exception. An int counts as neither and CombSin(5, 1.0, 3, 0.1) fails with "expected float or list for f_cap, not <class 'int'>". Write 5.0. Frequencies and coefficients of different lengths fail with AssertionError. The attributes freqs, coeffs and phases (zeros) are 1-dimensional tensors. The description is named "combsin".
SmoothHFHA
¶
Bases: CombSin
Source code in unaiverse/streams/streamlib.py
A CombSin preset with frequencies [0.11, 0.07, 0.05], coefficients [0.8, 0.16, 0.16] and delta=0.1. The highest frequency has the largest coefficient and the amplitude is large. The description is renamed "smoHfHa". The only argument is device.
['3sin', 'hf', 'ha']: three sines, high frequency, high amplitude. Each preset tags its signal with a FEATURES list so that experiments can compare the presets.
SmoothHFLA
¶
Bases: CombSin
Source code in unaiverse/streams/streamlib.py
A CombSin preset with frequencies [0.11, 0.07, 0.05], coefficients [0.4, 0.08, 0.08] and delta=0.1. The highest frequency has the largest coefficient and the amplitude is small. The description is renamed "smoHfLa".
['3sin', 'hf', 'la'].
SmoothLFLA
¶
Bases: CombSin
Source code in unaiverse/streams/streamlib.py
A CombSin preset with frequencies [0.11, 0.07, 0.05], coefficients [0.08, 0.08, 0.4] and delta=0.1. The lowest frequency has the largest coefficient and the amplitude is small. The description is renamed "smoLfLa".
['3sin', 'lf', 'la'].
SmoothLFHA
¶
Bases: CombSin
Source code in unaiverse/streams/streamlib.py
A CombSin preset with frequencies [0.11, 0.07, 0.05], coefficients [0.16, 0.16, 0.8] and delta=0.1. The lowest frequency has the largest coefficient and the amplitude is large. The description is renamed "smoLfHa".
['3sin', 'lf', 'ha'].
SquareHFHA
¶
Bases: Square
Source code in unaiverse/streams/streamlib.py
A Square preset with freq=0.06, ampl=1.0, phase=0.5 and delta=0.1. The description is renamed "squHfHa". The only argument is device.
['square', 'hf', 'ha'].
SquareHFLA
¶
Bases: Square
Source code in unaiverse/streams/streamlib.py
A Square preset with freq=0.06, ampl=0.5, phase=0.5 and delta=0.1. The description is renamed "squHfLa".
['square', 'hf', 'la'].
SquareLFHA
¶
Bases: Square
Source code in unaiverse/streams/streamlib.py
A Square preset with freq=0.03, ampl=1.0, phase=0.5 and delta=0.1. The description is renamed "squLfHa".
['square', 'lf', 'ha'].
SquareLFLA
¶
Bases: Square
Source code in unaiverse/streams/streamlib.py
A Square preset with freq=0.03, ampl=0.5, phase=0.5 and delta=0.1. The description is renamed "squLfLa".
['square', 'lf', 'la'].