audtorch.utils

Utility functions.

flatten_list

audtorch.utils.flatten_list(nested_list)

Flatten an arbitrarily nested list.

Implemented without recursion to avoid stack overflows. Returns a new list, the original list is unchanged.

Parameters:nested_list (list) – nested list
Returns:flattened list
Return type:list

Example

>>> flatten_list([1, 2, 3, [4], [], [[[[[[[[[5]]]]]]]]]])
[1, 2, 3, 4, 5]
>>> flatten_list([[1, 2], 3])
[1, 2, 3]

to_tuple

audtorch.utils.to_tuple(input, *, tuple_len=2)

Convert to tuple of given length.

This utility function is used to convert single-value arguments to tuples of appropriate length, e.g. for multi-dimensional inputs where each dimension requires the same value. If the argument is already an iterable it is returned as a tuple if its length matches the desired tuple length. Otherwise a ValueError is raised.

Parameters:
  • input (non-iterable or iterable) – argument to be converted to tuple
  • tuple_len (int) – required length of argument tuple. Default: 2
Returns:

tuple of desired length

Return type:

tuple

Example

>>> to_tuple(2)
(2, 2)

energy

audtorch.utils.energy(signal)

Energy of input signal.

\[E = \sum_n |x_n|^2 \]
Parameters:signal (numpy.ndarray) – signal
Returns:energy of signal
Return type:float

Example

>>> a = np.array([[2, 2]])
>>> energy(a)
8

power

audtorch.utils.power(signal)

Power of input signal.

\[P = {1 \over N} \sum_n |x_n|^2 \]
Parameters:signal (numpy.ndarray) – signal
Returns:power of signal
Return type:float

Example

>>> a = np.array([[2, 2]])
>>> power(a)
4.0

run_worker_threads

audtorch.utils.run_worker_threads(num_workers, task_fun, params, *, progress_bar=False)

Run parallel tasks using worker threads.

Parameters:
  • num_workers (int) – number of worker threads
  • task_fun (Callable) – task function with one or more parameters, e.g. x, y, z, and optionally returning a value
  • params (list of tuples) – list of tuples holding parameters for each task, e.g. [(x1, y1, z1), (x2, y2, z2), …]
  • progress_bar (bool) – show a progress bar. Default: False
Returns:

result values in order of params

Return type:

list

Example

>>> power = lambda x, n: x ** n
>>> params = [(2, n) for n in range(10)]
>>> run_worker_threads(3, power, params)
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512]