aspire.utils#
Attributes#
Classes#
Context manager to temporarily replace the log_likelihood method of a |
|
A subclass of h5py.File that adds metadata to the file. |
|
Class to store the history of calls to a function. |
Functions#
|
Configure the logger. |
|
Logit function that also returns log Jacobian determinant. |
|
Sigmoid function that also returns log Jacobian determinant. |
|
Implementation of logsumexp that works with array api. |
|
Convert an array to a numpy array. |
|
Convert an array to the specified array API. |
|
Determine the backend name from an array or array API module. |
|
Resolve a dtype specification into an XP-specific dtype. |
|
Convert a dtype between array API namespaces. |
|
Copy an array based on the array API being used. |
|
|
|
Disable gradients for a specific array API. |
|
Encode a dtype for storage in an HDF5 file. |
|
Decode a dtype from an HDF5 file. |
|
Encode a BaseSamples object for storage in an HDF5 file. |
|
Decode a BaseSamples object from a dictionary loaded from an HDF5 file. |
|
Encode a value for storage in an HDF5 file. |
|
Decode a value loaded from an HDF5 file, reversing encode_for_hdf5. |
|
Dump pickled data to an HDF5 file object. |
|
Pickle a state object and store it in an HDF5 dataset. |
|
Resolve a backend name to the corresponding array_api_compat module. |
|
Best-effort device inference that avoids non-portable identifiers. |
|
Move to device if specified; otherwise return input. |
|
Save a dictionary to an HDF5 file with flattened keys under a given group path. |
|
Load a flattened dictionary from an HDF5 group and rebuild nesting. |
|
Get the version of a package. |
|
Update an array at specific indices." |
|
Decorator to track calls to a function. |
|
Get a unique identifier for a function. |
Set the environment variable to enable array API compatibility in SciPy. |
Module Contents#
- aspire.utils.configure_logger(log_level='INFO', log_file=None, additional_loggers=None, include_aspire_loggers=True, stream=None)[source]#
Configure the logger.
Adds a stream handler to the logger and optionally a file handler.
- Parameters:
log_level (
strorint, optional) – The log level to use. Defaults to “INFO”.log_file (
strorPath, optional) – The file to write logs to. If None, logs are not written to a file.additional_loggers (
listofstr, optional) – Additional loggers to configure. Defaults to None.include_aspire_loggers (
bool, optional) – Whether to include all loggers that start with “aspire_” or “aspire-“. Defaults to True.stream (
str, optional) – The stream to use for the stream handler. Defaults to None, which uses sys.stderr.
- Returns:
The configured logger.
- Return type:
logging.Logger
- class aspire.utils.PoolHandler(aspire_instance, pool, close_pool=True, parallelize_prior=False)[source]#
Context manager to temporarily replace the log_likelihood method of a aspire instance with a version that uses a multiprocessing pool to parallelize computation.
- Parameters:
aspire_instance (
aspire) – The aspire instance to modify. The log_likelihood method of this instance must accept amap_fnkeyword argument.pool (
multiprocessing.Pool) – The pool to use for parallel computation.close_pool (
bool, optional) – Whether to close the pool when exiting the context manager. Defaults to True.parallelize_prior (
bool, optional) – Whether to parallelize the log_prior method as well. Defaults to False. If True, the log_prior method of the aspire instance must also accept amap_fnkeyword argument.
- aspire.utils.logit(x, eps=None)[source]#
Logit function that also returns log Jacobian determinant.
- Parameters:
x (
floatorndarray) – Array of valueseps (
float, optional) – Epsilon value used to clamp inputs to [eps, 1 - eps]. If None, then inputs are not clamped.
- Returns:
floatorndarray– Rescaled values.floatorndarray– Log Jacobian determinant.
- Return type:
tuple[array_api_compat.common._typing.Array, array_api_compat.common._typing.Array]
- aspire.utils.sigmoid(x, eps=None)[source]#
Sigmoid function that also returns log Jacobian determinant.
- Parameters:
x (
floatorndarray) – Array of valueseps (
float, optional) – Epsilon value used to clamp inputs to [eps, 1 - eps]. If None, then inputs are not clamped.
- Returns:
floatorndarray– Rescaled values.floatorndarray– Log Jacobian determinant.
- Return type:
tuple[array_api_compat.common._typing.Array, array_api_compat.common._typing.Array]
- aspire.utils.logsumexp(x, axis=None)[source]#
Implementation of logsumexp that works with array api.
This will be removed once the implementation in scipy is compatible.
- Parameters:
x (array_api_compat.common._typing.Array)
axis (int | None)
- Return type:
array_api_compat.common._typing.Array
- aspire.utils.to_numpy(x, **kwargs)[source]#
Convert an array to a numpy array.
This automatically moves the array to the CPU.
- Parameters:
x (
Array) – The array to convert.kwargs (
dict) – Additional keyword arguments to pass to numpy.asarray.
- Return type:
array_api_compat.numpy.ndarray
- aspire.utils.asarray(x, xp=None, dtype=None, **kwargs)[source]#
Convert an array to the specified array API.
- Parameters:
x (
Array) – The array to convert.xp (
Any) – The array API to use for the conversion. If None, the array API is inferred from the input array.dtype (
Any | str | None) – The dtype to use for the conversion. If None, the dtype is not changed.kwargs (
dict) – Additional keyword arguments to pass to xp.asarray.
- Return type:
array_api_compat.common._typing.Array
- aspire.utils.determine_backend_name(x=None, xp=None)[source]#
Determine the backend name from an array or array API module.
- Parameters:
x (
ArrayorNone) – The array to infer the backend from. If None, xp must be provided.xp (
AnyorNone) – The array API module to infer the backend from. If None, x must be provided.
- Returns:
The name of the backend. If the backend cannot be determined, returns “unknown”.
- Return type:
str
- aspire.utils.resolve_dtype(dtype, xp)[source]#
Resolve a dtype specification into an XP-specific dtype.
- Parameters:
dtype (
Any | str | None) – The dtype specification. Can be None, a string, or a dtype-like object.xp (
module) – The array API module that should interpret the dtype.
- Returns:
The resolved dtype object compatible with
xp(or None if unspecified).- Return type:
Any | None
- aspire.utils.convert_dtype(dtype, target_xp, *, source_xp=None)[source]#
Convert a dtype between array API namespaces.
- Parameters:
dtype (
Any | str | None) – The dtype to convert. Can be a dtype object, string, or None.target_xp (
module) – The target array API namespace to convert the dtype into.source_xp (
module, optional) – The source namespace of the dtype. Provided for API symmetry and future use; currently unused but accepted.
- Returns:
The dtype object compatible with
target_xp(or None ifdtypeis None).- Return type:
Any | None
- aspire.utils.copy_array(x, xp=None)[source]#
Copy an array based on the array API being used.
This uses the most appropriate method to copy the array depending on the array API.
- Parameters:
x (
Array) – The array to copy.xp (
Any) – The array API to use for the copy.
- Returns:
The copied array.
- Return type:
Array
- aspire.utils.effective_sample_size(log_w)[source]#
- Parameters:
log_w (array_api_compat.common._typing.Array)
- Return type:
float
- aspire.utils.disable_gradients(xp, inference=True)[source]#
Disable gradients for a specific array API.
Usage:
```python with disable_gradients(xp):
# Do something
- Parameters:
xp (
module) – The array API module to use.inference (
bool, optional) – When using PyTorch, set to True to enable inference mode.
- aspire.utils.encode_dtype(xp, dtype)[source]#
Encode a dtype for storage in an HDF5 file.
- Parameters:
xp (
module) – The array API module to use.dtype (
dtype) – The dtype to encode.
- Returns:
The encoded dtype.
- Return type:
str
- aspire.utils.decode_dtype(xp, encoded_dtype)[source]#
Decode a dtype from an HDF5 file.
- Parameters:
xp (
module) – The array API module to use.encoded_dtype (
dict) – The encoded dtype.
- Returns:
The decoded dtype.
- Return type:
dtype
- aspire.utils.encode_samples(samples)[source]#
Encode a BaseSamples object for storage in an HDF5 file.
- Parameters:
samples (
BaseSamples) – The samples to encode.- Returns:
A dictionary containing the encoded samples. This includes a marker to indicate that it is an encoded samples object, the type of the samples, and the data needed to reconstruct the samples.
- Return type:
dict
- aspire.utils.decode_samples(encoded_samples)[source]#
Decode a BaseSamples object from a dictionary loaded from an HDF5 file.
- Parameters:
encoded_samples (
dict) – The dictionary containing the encoded samples. This should have been produced by theencode_samplesfunction.- Raises:
ValueError – If the encoded_samples dictionary does not have the expected format or if the samples type is unknown.
- Return type:
- aspire.utils.encode_for_hdf5(value)[source]#
Encode a value for storage in an HDF5 file.
Special cases: - None is replaced with “__none__” - Empty dictionaries are replaced with “__empty_dict__” - BaseSamples objects are encoded using
encode_samples- Parameters:
value (Any)
- Return type:
Any
- aspire.utils.decode_from_hdf5(value)[source]#
Decode a value loaded from an HDF5 file, reversing encode_for_hdf5.
- Parameters:
value (Any)
- Return type:
Any
- aspire.utils.dump_pickle_to_hdf(memfp, fp, path=None, dsetname='state')[source]#
Dump pickled data to an HDF5 file object.
- Parameters:
memfp (
BytesIO) – A BytesIO object containing the pickled data.fp (
h5py.File) – An open h5py.File object to write to.path (
str, optional) – The group path within the HDF5 file to store the dataset. If None, the dataset is stored at the root level. Defaults to None.dsetname (
str, optional) – The name of the dataset to create or overwrite. Defaults to “state”.
- aspire.utils.dump_state(state, fp, path=None, dsetname='state', protocol=pickle.HIGHEST_PROTOCOL)[source]#
Pickle a state object and store it in an HDF5 dataset.
- aspire.utils.resolve_xp(xp_name)[source]#
Resolve a backend name to the corresponding array_api_compat module.
Returns None if the name is None or cannot be resolved.
- Parameters:
xp_name (str | None)
- aspire.utils.infer_device(x, xp)[source]#
Best-effort device inference that avoids non-portable identifiers.
Returns None for numpy/jax backends; returns the backend device object for torch/cupy if available.
- aspire.utils.safe_to_device(x, device, xp)[source]#
Move to device if specified; otherwise return input.
Skips moves for numpy/jax/None devices; logs and returns input on failure.
- aspire.utils.recursively_save_to_h5_file(h5_file, path, dictionary)[source]#
Save a dictionary to an HDF5 file with flattened keys under a given group path.
- aspire.utils.load_from_h5_file(h5_file, path)[source]#
Load a flattened dictionary from an HDF5 group and rebuild nesting.
- aspire.utils.get_package_version(package_name)[source]#
Get the version of a package.
- Parameters:
package_name (
str) – The name of the package.- Returns:
The version of the package.
- Return type:
str
- class aspire.utils.AspireFile(*args, **kwargs)[source]#
Bases:
h5py.FileA subclass of h5py.File that adds metadata to the file.
- aspire.utils.update_at_indices(x, slc, y)[source]#
Update an array at specific indices.”
This is a workaround for the fact that array API does not support advanced indexing with all backends.
Examples
>>> x = xp.array([[1, 2], [3, 4], [5, 6]]) >>> update_at_indices(x, (slice(None), 0), xp.array([10, 20, 30])) [[10 2] [20 4] [30 6]]
- Parameters:
x (
Array) – The array to update.slc (
Array) – The indices to update.y (
Array) – The values to set at the indices.
- Returns:
The updated array.
- Return type:
Array
- class aspire.utils.CallHistory[source]#
Class to store the history of calls to a function.
- to_dict(list_to_dict=False)[source]#
Convert the call history to a dictionary.
- Parameters:
list_to_dict (
bool) – If True, convert the lists of args and kwargs to dictionaries with string keys. If False, keep them as lists. This is useful when encoding the history for HDF5.- Return type:
dict[str, Any]
- aspire.utils.track_calls(wrapped=None)[source]#
Decorator to track calls to a function.
The decorator adds a
callsattribute to the wrapped function, which is aCallHistoryobject that stores the arguments and keyword arguments of each call.