torchsig.signals.builder.ConcatSignalGenerator

class torchsig.signals.builder.ConcatSignalGenerator(signal_generators: list[BaseSignalGenerator], **kwargs: dict[str, Any])[source]

Bases: BaseSignalGenerator

A Signal Generator that wraps other signal generators and returns one of their outputs at random when called.

This generator randomly selects one of the provided signal generators and returns its output. Each wrapped signal generator must be a valid BaseSignalGenerator instance.

signal_generators

List of BaseSignalGenerator instances to choose from.

random_generator

Random number generator used to select a signal generator.

Methods

add_parent

Add parent Seedable object and set up RNGs accordingly.

copy

Creates a deep copy of the ConcatSignalGenerator with copied signal generators.

generate

Generates a signal by randomly selecting one of the wrapped generators.

get_distribution

Create distribution function with proper seeding.

get_full_metadata

Function for modifying and returning a new metadata with all the fields in parent or child, with child overriding parent in conflicts.

get_second_seed

Gets second seed, usually used to seed both torch and numpy generators with slightly different seeds.

key_lookup

Lookup a metadata key with enhanced error reporting.

keys

Get all metadata keys.

seed

Seed number generators with given seed.

set_default_class_name

Sets the class_name to name if there wasn't already a class name set.

setup_rngs

Initialize torch and numpy number generators, and update its children.

update_from_parent

Update numpy and torch number generators with parent seed.

validate_metadata_fields

Validates metadata fields for all wrapped signal generators.

__init__(signal_generators: list[BaseSignalGenerator], **kwargs: dict[str, Any]) None[source]

Initializes the ConcatSignalGenerator.

Parameters:
  • signal_generators – List of BaseSignalGenerator instances to wrap.

  • **kwargs – Additional keyword arguments to pass to the parent class.

Raises:

TypeError – If any of the signal_generators are not BaseSignalGenerator instances.

copy() ConcatSignalGenerator[source]

Creates a deep copy of the ConcatSignalGenerator with copied signal generators.

Returns:

A new instance of ConcatSignalGenerator with copied metadata and signal generators.

validate_metadata_fields() bool[source]

Validates metadata fields for all wrapped signal generators.

Calls validate_metadata_fields() on each wrapped signal generator.

Returns:

True if all validations pass.

Return type:

bool

Raises:

ValueError – If any of the wrapped signal generators are missing required metadata fields.

__repr__() str[source]

Returns a string representation of the ConcatSignalGenerator.

Returns:

A string representation showing the class name, metadata, and signal generators.

generate() Signal[source]

Generates a signal by randomly selecting one of the wrapped generators.

Returns:

The output of a randomly selected signal generator.

Return type:

Signal

__call__() Signal

Generates a new signal and applies all transforms.

Returns:

The generated signal after applying all transforms.

add_parent(parent: Seedable, register: bool = True) None

Add parent Seedable object and set up RNGs accordingly.

Parameters:
  • parent – Parent Seedable object to add.

  • register – If True (default), add self to parent.children so that future seed propagation reaches this object. Pass False for transient objects (e.g. per-sample Signal instances) that only need the parent link for metadata/RNG access during their lifetime but must not accumulate in the parent’s child list, which would otherwise cause unbounded memory growth.

get_distribution(params: list | tuple | float, scaling: str = 'linear') Distribution

Create distribution function with proper seeding.

Parameters:
  • params – Parameters for distribution.

  • scaling – Scaling param for distribution. Defaults to ‘linear’.

Returns:

Distribution function, seeded.

Return type:

Distribution

get_full_metadata() dict[str, Any]

Function for modifying and returning a new metadata with all the fields in parent or child, with child overriding parent in conflicts.

Returns:

Dictionary containing all metadata from parent and child, with child values overriding parent values in case of conflicts.

Example

>>> parent = HierarchicalMetadataObject(metadata={"field_1": 4, "field_2": 5})
>>> child = HierarchicalMetadataObject(parent=parent, metadata={"field_2": 6})
>>> child.get_full_metadata()
{'field_1': 4, 'field_2': 6}
get_second_seed(seed: int) int

Gets second seed, usually used to seed both torch and numpy generators with slightly different seeds.

Parameters:

seed – Seed to use.

Returns:

New seed.

key_lookup(key: str) Any

Lookup a metadata key with enhanced error reporting.

Parameters:

key – The metadata key to lookup.

Returns:

The value associated with the key.

Raises:

MetadataAttributeError – If the key is not found in the metadata or parent metadata.

Example

>>> obj = HierarchicalMetadataObject(metadata={"key": "value"})
>>> obj.key_lookup("key")
'value'
keys() list[str]

Get all metadata keys.

Returns:

List of all metadata keys.

Example

>>> obj = HierarchicalMetadataObject(metadata={"key1": 1, "key2": 2})
>>> list(obj.keys())
['key1', 'key2']
seed(seed: int) None

Seed number generators with given seed.

Parameters:

seed – Seed to use.

set_default_class_name(name: str) None

Sets the class_name to name if there wasn’t already a class name set.

Parameters:

name – The class name to set if no class name exists.

setup_rngs() None

Initialize torch and numpy number generators, and update its children.

update_from_parent() None

Update numpy and torch number generators with parent seed.