Helper functions

sorrel.utils.helpers.set_seed(seed: int) None

Sets a seed for replication. Sets the seed for random.seed(), numpy.random.seed(), and torch.manual_seed().

Parameters:

seed – An int setting the seed.

sorrel.utils.helpers.random_seed() int

Generates a random seed.

Returns:

The value of the seed generated

sorrel.utils.helpers.shift(array: ndarray, shift: Sequence | ndarray, cval: Any = nan) ndarray

Returns copy of array shifted by offset, with fill using constant.

Parameters:
  • array – The array to shift.

  • shift – A sequence of dimensions equivalent to the array passed into the function.

  • cval – The value to replace any new elements introduced into the offset array. By default, replaces them with nan’s.

Returns:

The shifted array.

Return type:

np.ndarray

sorrel.utils.helpers.nearest_2_power(n: int) int

Computes the next power of 2.

Useful for programmatically shifting batch and buffer sizes to computationally efficient values.

Parameters:

n – The number.

Returns:

The nearest power of two equal to or larger than n.

Return type:

int

sorrel.utils.helpers.clip(n: int, minimum: int, maximum: int) int

Clips an input to a number between the minimum and maximum values passed into the function.

Parameters:
  • n – The number.

  • minimum – The minimum number.

  • maximum – The maximum number.

Returns:

The clipped value of n.

Return type:

int

sorrel.utils.helpers.one_hot_encode(value: int, num_classes: int) ndarray

Create a numpy array of shape (num_classes, ) that encodes the position of value as a one-hot vector.

Parameters:
  • value – The position to one-hot encode.

  • num_classes – The length of the one-hot vector.

Note

value cannot be larger than num_classes - 1 without leading to an index error.