Skip to content

Utilities#

audioclass.utils #

Utility functions for audioclass.

This module provides various helper functions for working with audio data and models.

Functions:

Name Description
batched

Batch array data along an axis.

flat_sigmoid

Apply a flattened sigmoid function to an array.

load_artifact

Load an artifact from a local path or a URL.

Functions#

batched(array, n, axis=0, *, strict=False) #

Batch array data along an axis.

This function yields batches of data from an array along a specified axis. The final batch may be shorter than the specified batch size if the array length is not divisible by n.

Parameters:

Name Type Description Default
array ndarray

The input array.

required
n int

The batch size.

required
strict bool

Whether to raise an error if the final batch is shorter than n.

False

Yields:

Name Type Description
batch ndarray

The next batch of data from the array.

Raises:

Type Description
ValueError

If n is less than 1 or if strict is True and the final batch is shorter than n.

flat_sigmoid(x, sensitivity=1, vmin=-15, vmax=15) #

Apply a flattened sigmoid function to an array.

This function applies a sigmoid function to each element of the input array, but with a flattened shape to prevent extreme values. The output values are clipped between 0 and 1.

Parameters:

Name Type Description Default
x ndarray

The input array.

required
sensitivity float

The sensitivity of the sigmoid function. Defaults to 1.

1
vmin float

The minimum value to clip the input to. Defaults to -15.

-15
vmax float

The maximum value to clip the input to. Defaults to 15.

15

Returns:

Type Description
ndarray

The output array with the same shape as the input.

load_artifact(path, directory=None, download=True) #

Load an artifact from a local path or a URL.

If the path is a URL, the artifact is downloaded and cached in a local directory. If the artifact is already cached, it is loaded from the cache.

Parameters:

Name Type Description Default
path Union[Path, str]

The path to the artifact, either a local path or a URL.

required
directory Optional[Path]

The directory to cache the artifact in. If not provided, a default cache directory is used.

None
download bool

Whether to download the artifact if it is not found in the cache. Defaults to True.

True

Returns:

Type Description
Path

The path to the loaded artifact.

Raises:

Type Description
FileNotFoundError

If the artifact is not found and download is False.