torchsig.datasets.datasets.TorchSigIterableDataset

class torchsig.datasets.datasets.TorchSigIterableDataset(signal_generators: str | ConcatSignalGenerator | list = 'all', transforms: list[Transform | callable] = [], component_transforms: list[Transform | callable] = [], target_labels: list | None = None, validate_init: bool = True, **kwargs)[source]

Bases: HierarchicalMetadataObject, IterableDataset

Base class for generating signals.

The dataset will continue to generate samples infinitely.

signal_generators

The signal generators to use. Can be a string, ConcatSignalGenerator, or list.

transforms

List of transforms to apply to the entire signal.

component_transforms

List of transforms to apply to individual signal components.

target_labels

Labels to extract from the signal.

validate_init

Whether to validate metadata during initialization.

Methods

add_parent

Add parent Seedable object and set up RNGs accordingly.

add_signal_generator

Adds a signal generator to this dataset.

copy

Create a copy of the object.

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.

init_signal_generator

Initializes the signal generator.

key_lookup

Lookup a metadata key with enhanced error reporting.

keys

Get all metadata keys.

seed

Seed number generators with given seed.

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 signal metadata for each signal generators.

__init__(signal_generators: str | ConcatSignalGenerator | list = 'all', transforms: list[Transform | callable] = [], component_transforms: list[Transform | callable] = [], target_labels: list | None = None, validate_init: bool = True, **kwargs)[source]

Initializes the dataset.

Parameters:
  • signal_generators – The signal generators to use. Can be a string, ConcatSignalGenerator, or list.

  • transforms – List of transforms to apply to the entire signal.

  • component_transforms – List of transforms to apply to individual signal components.

  • target_labels – Labels to extract from the signal.

  • validate_init – Whether to validate metadata during initialization.

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

init_signal_generator(signal_generator: str | callable) None[source]

Initializes the signal generator.

Parameters:

signal_generator – The signal generator to be initialized. If a string, it is first looked up to retrieve the corresponding signal generator function.

Raises:

TypeError – If the signal_generator is neither a string nor a callable.

add_signal_generator(signal_generator: callable, class_name: str | None = None, class_index: int | None = None, likelihood: int = 1) None[source]

Adds a signal generator to this dataset.

Parameters:
  • signal_generator – A callable object which takes no arguments and returns a Signal.

  • class_name – (optional) A name for this signal class in the dataset. If None, the signal will be generated and added to the data, but no labels will be made for the signal.

  • likelihood – (optional) The relative likelihood of this signal type in the dataset. Doubling the likelihood will make this signal twice as likely to be placed in the data.

validate_metadata_fields() bool[source]

Validates signal metadata for each signal generators.

Returns:

Whether Signal metadata is valid.

__call__() Signal | ndarray | tuple[source]

Same as next(); returns the next item in the dataset.

Allows datasets to be treated as signal generators for other datasets.

__repr__() str[source]

Returns a string representation of the dataset.

Returns:

String representation of the dataset.

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.

copy() HierarchicalMetadataObject

Create a copy of the object.

Returns:

A new instance of the same class with the same metadata and parent.

Example

>>> obj = HierarchicalMetadataObject(metadata={"key": "value"})
>>> copy_obj = obj.copy()
>>> copy_obj["key"]
'value'
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.

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.