🟡 unaiverse.streams.streamlib
What this module does 🟡
Library of synthetic signal stream generators (random, sinusoidal, square, smooth/square frequency-amplitude variants) plus a one-hot label stream for testing and demos.
streamlib
¶
█████ █████ ██████ █████ █████ █████ █████ ██████████ ███████████ █████████ ██████████
░░███ ░░███ ░░██████ ░░███ ░░███ ░░███ ░░███ ░░███░░░░░█░░███░░░░░███ ███░░░░░███░░███░░░░░█
░███ ░███ ░███░███ ░███ ██████ ░███ ░███ ░███ ░███ █ ░ ░███ ░███ ░███ ░░░ ░███ █ ░
░███ ░███ ░███░░███░███ ░░░░░███ ░███ ░███ ░███ ░██████ ░██████████ ░░█████████ ░██████
░███ ░███ ░███ ░░██████ ███████ ░███ ░░███ ███ ░███░░█ ░███░░░░░███ ░░░░░░░░███ ░███░░█
░███ ░███ ░███ ░░█████ ███░░███ ░███ ░░░█████░ ░███ ░ █ ░███ ░███ ███ ░███ ░███ ░ █
░░████████ █████ ░░█████░░████████ █████ ░░███ ██████████ █████ █████░░█████████ ██████████
░░░░░░░░ ░░░░░ ░░░░░ ░░░░░░░░ ░░░░░ ░░░ ░░░░░░░░░░ ░░░░░ ░░░░░ ░░░░░░░░░ ░░░░░░░░░░
A Collectionless AI Project (https://collectionless.ai)
Registration/Login: https://unaiverse.io
Code Repositories: https://github.com/collectionlessai/
Main Developers: Stefano Melacci (Project Leader), Christian Di Maio, Tommaso Guidi
AllHotLabelStream
¶
Bases: BufferedStream
A static buffered stream that repeatedly yields an all-ones tensor with named labels.
The stream holds a single torch.float32 tensor of shape (1, len(labels)) filled with
ones. Because it is constructed with is_static=True, the same tensor is returned on every
read without any index-based generation. This is useful as a dummy or constant input stream
whose labelling rule ("geq0.5") marks all labels as active.
The tensor is placed on the requested device at construction time and does not move
afterwards. The DataProps name is set to AllHotLabelStream and the data description
to "dummy stream".
Examples:
>>> import torch
>>> stream = AllHotLabelStream(labels=["cat", "dog", "bird"])
>>> data, cycle_offset = stream[0, None]
>>> data.shape
torch.Size([1, 3])
>>> data
tensor([[1., 1., 1.]])
Initialize an AllHotLabelStream with the given label names.
Constructs the underlying BufferedStream with a DataProps that describes a
single-row float tensor whose columns correspond to labels. The tensor is immediately
set to all-ones and the stream is restarted so it is ready for reading.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
list[str]
|
List of label strings to associate with the all-ones tensor. The length determines the number of columns in the output tensor. |
required |
device
|
device
|
PyTorch device on which the tensor is allocated. Defaults to
|
device('cpu')
|
Source code in unaiverse/streams/streamlib.py
Random
¶
Bases: BufferedStream
A buffered stream that yields random tensors drawn from a scaled uniform distribution.
Each call to __getitem__ generates a fresh tensor by sampling from torch.rand
(values in [0, 1)) and multiplying by std. The resulting values are therefore
in [0, std), not zero-centred. The stream name in DataProps is "rand" and
the data description is "stream of random numbers".
The tensor shape is fixed at construction time and every sample has the same shape.
Generation happens on the fly: no buffer is pre-filled. The stream must be restarted
(via restart) before the first read, which the constructor does automatically.
Examples:
>>> import torch
>>> stream = Random(std=1.0, shape=(1, 4))
>>> data, cycle_offset = stream[0, None]
>>> data.shape
torch.Size([1, 4])
Initialize a Random stream.
Sets up DataProps for a float tensor of the given shape, stores std and
device, and calls restart so the stream is ready immediately.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
std
|
float
|
Scale factor applied to the uniformly-sampled values. Each generated
tensor element is in the range |
required |
shape
|
tuple[int, ...] | None
|
Shape of each output tensor. Defaults to |
(1,)
|
device
|
device
|
PyTorch device on which tensors are generated. Defaults to
|
device('cpu')
|
Source code in unaiverse/streams/streamlib.py
Sin
¶
Bases: BufferedStream
A buffered stream that generates samples from a sine function.
Each sample at integer index idx is computed as
sin(2 * pi * freq * (idx * delta + phase * period)), where period = 1 / freq.
The output tensor has shape (1, 1) and dtype torch.float.
The stream name in DataProps is "sin" and the data description is
"stream of samples from the sin function". The stream calls restart
during construction so it is ready for reading immediately.
Examples:
>>> import torch
>>> stream = Sin(freq=1.0, phase=0.0, delta=0.25)
>>> data, _ = stream[0, None] # sin(0) = 0
>>> data.shape
torch.Size([1, 1])
Initialize a Sin stream.
Stores the frequency, phase, derived period, and time step, then calls
restart so the stream is ready for reading immediately.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
freq
|
float
|
Frequency of the sine wave in Hz. |
required |
phase
|
float
|
Phase offset in units of full periods (0.0 = no offset, 1.0 = one
full period offset). The effective time shift is |
required |
delta
|
float
|
Time step between consecutive sample indices in seconds. The sample at
index |
required |
device
|
device
|
PyTorch device on which the output tensor is allocated. Defaults to
|
device('cpu')
|
Source code in unaiverse/streams/streamlib.py
Square
¶
Bases: BufferedStream
A buffered stream that generates samples from a square wave function.
Each sample at integer index idx is computed as
ampl * (-1) ** floor(2 * freq * t) where t = idx * delta + phase * period
and period = 1 / freq. The result alternates between +ampl and -ampl
at the given frequency. The output tensor has shape (1, 1) and dtype torch.float.
The stream name in DataProps is "square" and the data description is
"stream of samples from the square function". The stream calls restart
during construction so it is ready for reading immediately.
Examples:
>>> import torch
>>> stream = Square(freq=1.0, ampl=1.0, phase=0.0, delta=0.1)
>>> data, _ = stream[0, None] # t=0, value = ampl * (-1)^0 = 1.0
>>> float(data)
1.0
Initialize a Square wave stream.
Stores the frequency, amplitude, phase, derived period, and time step, then calls
restart so the stream is ready for reading immediately.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
freq
|
float
|
Frequency of the square wave in Hz. |
required |
ampl
|
float
|
Peak amplitude of the square wave. The output alternates between
|
required |
phase
|
float
|
Phase offset in units of full periods. The effective time shift is
|
required |
delta
|
float
|
Time step between consecutive sample indices in seconds. |
required |
device
|
device
|
PyTorch device on which the output tensor is allocated. Defaults to
|
device('cpu')
|
Source code in unaiverse/streams/streamlib.py
CombSin
¶
CombSin(f_cap: float | list, c_cap: float | list, order: int, delta: float, device: device = device('cpu'))
Bases: BufferedStream
A buffered stream that generates samples from a weighted sum of sine functions.
The output at index idx is computed as:
``sum_k( coeffs[k] * sin(2 * pi * freqs[k] * idx * delta + phases[k]) )``
where all phases are initialised to zero. Frequencies and coefficients can be either
randomly sampled (when a scalar cap is given) or prescribed explicitly (when a list is
provided). The output tensor has shape (1, 1) and dtype torch.float.
The stream name in DataProps is "combsin" and the data description is
"stream of samples from combined sin functions". The stream calls restart
during construction so it is ready for reading immediately.
This class is the base for the preset streams SmoothHFHA, SmoothHFLA,
SmoothLFLA, and SmoothLFHA.
Examples:
>>> import torch
>>> # Prescribed frequencies and coefficients:
>>> stream = CombSin(f_cap=[0.1, 0.2], c_cap=[0.5, 0.5], order=2, delta=0.1)
>>> data, _ = stream[0, None]
>>> data.shape
torch.Size([1, 1])
>>>
>>> # Random frequencies and coefficients:
>>> stream_rand = CombSin(f_cap=1.0, c_cap=1.0, order=4, delta=0.05)
Initialize a CombSin stream.
When f_cap is a float, order frequencies are sampled uniformly from
[0, f_cap) using torch.rand. When it is a list, those values are used
directly (the order argument is ignored for sizing in this case, but the
assertion that len(coeffs) == len(freqs) still applies). The same logic
applies to c_cap for coefficients, except that random coefficients are drawn
from a symmetric distribution: c_cap * (2 * torch.rand(order) - 1), so they
lie in (-c_cap, c_cap).
All phases are initialised to zero. The stream calls restart at the end of
construction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f_cap
|
float | list
|
If a |
required |
c_cap
|
float | list
|
If a |
required |
order
|
int
|
Number of sine components. Used as the sample size when |
required |
delta
|
float
|
Time step between consecutive sample indices in seconds. |
required |
device
|
device
|
PyTorch device on which the output tensor is allocated. Defaults to
|
device('cpu')
|
Raises:
| Type | Description |
|---|---|
Exception
|
If |
Exception
|
If |
AssertionError
|
If the number of coefficients does not equal the number of frequencies after construction. |
Source code in unaiverse/streams/streamlib.py
SmoothHFHA
¶
Bases: CombSin
Preset CombSin stream: smooth signal with high frequency and high amplitude.
Fixed frequencies [0.11, 0.07, 0.05] Hz and coefficients [0.8, 0.16, 0.16]
are used with delta=0.1 s. The highest-frequency component carries the largest weight,
producing a high-amplitude, high-frequency smooth waveform. The stream name is set to
"smoHfHa" after construction.
The FEATURES class attribute ['3sin', 'hf', 'ha'] can be used by processor
code to discover this stream's signal characteristics.
Initialize a SmoothHFHA stream with fixed high-frequency, high-amplitude parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
device
|
PyTorch device on which the output tensor is allocated. Defaults to
|
device('cpu')
|
Source code in unaiverse/streams/streamlib.py
SmoothHFLA
¶
Bases: CombSin
Preset CombSin stream: smooth signal with high frequency and low amplitude.
Fixed frequencies [0.11, 0.07, 0.05] Hz and coefficients [0.4, 0.08, 0.08]
are used with delta=0.1 s. The highest-frequency component carries the largest weight,
producing a high-frequency but low-amplitude smooth waveform. The stream name is set to
"smoHfLa" after construction.
The FEATURES class attribute ['3sin', 'hf', 'la'] can be used by processor
code to discover this stream's signal characteristics.
Initialize a SmoothHFLA stream with fixed high-frequency, low-amplitude parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
device
|
PyTorch device on which the output tensor is allocated. Defaults to
|
device('cpu')
|
Source code in unaiverse/streams/streamlib.py
SmoothLFLA
¶
Bases: CombSin
Preset CombSin stream: smooth signal with low frequency and low amplitude.
Fixed frequencies [0.11, 0.07, 0.05] Hz and coefficients [0.08, 0.08, 0.4]
are used with delta=0.1 s. The lowest-frequency component carries the largest weight,
producing a low-frequency, low-amplitude smooth waveform. The stream name is set to
"smoLfLa" after construction.
The FEATURES class attribute ['3sin', 'lf', 'la'] can be used by processor
code to discover this stream's signal characteristics.
Initialize a SmoothLFLA stream with fixed low-frequency, low-amplitude parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
device
|
PyTorch device on which the output tensor is allocated. Defaults to
|
device('cpu')
|
Source code in unaiverse/streams/streamlib.py
SmoothLFHA
¶
Bases: CombSin
Preset CombSin stream: smooth signal with low frequency and high amplitude.
Fixed frequencies [0.11, 0.07, 0.05] Hz and coefficients [0.16, 0.16, 0.8]
are used with delta=0.1 s. The lowest-frequency component carries the largest weight,
producing a low-frequency, high-amplitude smooth waveform. The stream name is set to
"smoLfHa" after construction.
The FEATURES class attribute ['3sin', 'lf', 'ha'] can be used by processor
code to discover this stream's signal characteristics.
Initialize a SmoothLFHA stream with fixed low-frequency, high-amplitude parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
device
|
PyTorch device on which the output tensor is allocated. Defaults to
|
device('cpu')
|
Source code in unaiverse/streams/streamlib.py
SquareHFHA
¶
Bases: Square
Preset Square wave stream: high frequency and high amplitude.
Uses freq=0.06 Hz, ampl=1.0, phase=0.5, and delta=0.1 s.
The stream name is set to "squHfHa" after construction.
The FEATURES class attribute ['square', 'hf', 'ha'] can be used by processor
code to discover this stream's signal characteristics.
Initialize a SquareHFHA stream with fixed high-frequency, high-amplitude parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
device
|
PyTorch device on which the output tensor is allocated. Defaults to
|
device('cpu')
|
Source code in unaiverse/streams/streamlib.py
SquareHFLA
¶
Bases: Square
Preset Square wave stream: high frequency and low amplitude.
Uses freq=0.06 Hz, ampl=0.5, phase=0.5, and delta=0.1 s.
The stream name is set to "squHfLa" after construction.
The FEATURES class attribute ['square', 'hf', 'la'] can be used by processor
code to discover this stream's signal characteristics.
Initialize a SquareHFLA stream with fixed high-frequency, low-amplitude parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
device
|
PyTorch device on which the output tensor is allocated. Defaults to
|
device('cpu')
|
Source code in unaiverse/streams/streamlib.py
SquareLFHA
¶
Bases: Square
Preset Square wave stream: low frequency and high amplitude.
Uses freq=0.03 Hz, ampl=1.0, phase=0.5, and delta=0.1 s.
The stream name is set to "squLfHa" after construction.
The FEATURES class attribute ['square', 'lf', 'ha'] can be used by processor
code to discover this stream's signal characteristics.
Initialize a SquareLFHA stream with fixed low-frequency, high-amplitude parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
device
|
PyTorch device on which the output tensor is allocated. Defaults to
|
device('cpu')
|
Source code in unaiverse/streams/streamlib.py
SquareLFLA
¶
Bases: Square
Preset Square wave stream: low frequency and low amplitude.
Uses freq=0.03 Hz, ampl=0.5, phase=0.5, and delta=0.1 s.
The stream name is set to "squLfLa" after construction.
The FEATURES class attribute ['square', 'lf', 'la'] can be used by processor
code to discover this stream's signal characteristics.
Initialize a SquareLFLA stream with fixed low-frequency, low-amplitude parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
device
|
PyTorch device on which the output tensor is allocated. Defaults to
|
device('cpu')
|