torchsig.utils.random.Seedable

class torchsig.utils.random.Seedable(seed: int | None = None, parent: Seedable | None = None, **kwargs)[source]

Bases: object

A class/interface representing objects capable of accessing random numbers and being seeded.

Stores an internal random number generator object. Can be seeded with the Seedable.seed(seed_value: int) function. Two Seedable objects with the same seed will always generate/access the same random values in the same order. Containing or composing Seedable objects are generally responsible for seeding contained or composed Seedable objects.

Methods

add_parent

Add parent Seedable object and set up RNGs accordingly.

get_distribution

Create distribution function with proper seeding.

get_second_seed

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

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.

__init__(seed: int | None = None, parent: Seedable | None = None, **kwargs)[source]

Initializes seedable object with self.seed = seed.

If a parent Seedable object is passed in, they will share random number generators, and the seed argument will not be used.

Parameters:
  • seed – Seed for use for number generators. Defaults to None.

  • parent – Parent Seedable responsible for seeding this object. Defaults to None.

  • **kwargs – Additional keyword arguments.

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

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.

update_from_parent() None[source]

Update numpy and torch number generators with parent seed.

seed(seed: int) None[source]

Seed number generators with given seed.

Parameters:

seed – Seed to use.

get_second_seed(seed: int) int[source]

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

Parameters:

seed – Seed to use.

Returns:

New seed.

setup_rngs() None[source]

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

__repr__() str[source]

Printable representation with seed and parent.

Returns:

String representation of the object.

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

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