gwkokab.analysis.core.utils

Classes

IdentitySampleTransformer

Helper class that provides a standard way to create an ABC using

PRNGKeyMixin

Mixin class that provides a random number generator key and seed management for

SampleTransformer

Helper class that provides a standard way to create an ABC using

Functions

from_structured(→ tuple[numpy.ndarray, ...)

Converts a structured array to a 2D array.

read_attrs_from_hdf5(→ dict)

Reads attributes from a specified dataset in an HDF5 file.

read_from_hdf5(→ numpy.ndarray)

Reads data from an HDF5 file at the specified dataset path.

to_structured(→ numpy.ndarray)

Converts a 2D array to a structured array with given field names.

write_to_hdf5(→ None)

Writes data to an HDF5 file at the specified dataset path, with optional

Module Contents

class gwkokab.analysis.core.utils.IdentitySampleTransformer

Bases: SampleTransformer

Helper class that provides a standard way to create an ABC using inheritance.

log_abs_det_jacobian(samples: numpy.ndarray, transformed_samples: numpy.ndarray) numpy.ndarray

Compute the log absolute determinant of the Jacobian of the transformation.

Parameters:
  • samples (np.ndarray) – The original samples before transformation.

  • transformed_samples (np.ndarray) – The transformed samples after applying the transformation.

Returns:

The log absolute determinant of the Jacobian for each sample.

Return type:

np.ndarray

transform(samples: numpy.ndarray) numpy.ndarray

Transform the input samples to a new coordinate system.

Parameters:

samples (np.ndarray) – The input samples to be transformed.

Returns:

The transformed samples.

Return type:

np.ndarray

class gwkokab.analysis.core.utils.PRNGKeyMixin

Mixin class that provides a random number generator key and seed management for classes that require random number generation.

This mixin allows classes to initialize a random key based on a seed and provides a property to access the current random key, which is automatically split to ensure independent random streams across different parts of the code.

classmethod init_rng_seed(seed: int) None
property rng_key: jaxtyping.PRNGKeyArray

Accessor for the current random number generator key.

Each call to this property will split the key and return a new subkey, ensuring that different parts of the code can use independent random keys without interfering with each other.

Returns:

A new random key generated by splitting the current key.

Return type:

PRNGKeyArray

property seed: int

Accessor for the seed used to initialize the random number generator key.

Returns:

The seed used for RNG initialization.

Return type:

int

class gwkokab.analysis.core.utils.SampleTransformer

Bases: abc.ABC

Helper class that provides a standard way to create an ABC using inheritance.

check(samples: numpy.ndarray, transformed_samples: numpy.ndarray) numpy.ndarray

Additional checks for the transformation, such as ensuring that the transformed samples are within expected bounds.

abstractmethod log_abs_det_jacobian(samples: numpy.ndarray, transformed_samples: numpy.ndarray) numpy.ndarray

Compute the log absolute determinant of the Jacobian of the transformation.

Parameters:
  • samples (np.ndarray) – The original samples before transformation.

  • transformed_samples (np.ndarray) – The transformed samples after applying the transformation.

Returns:

The log absolute determinant of the Jacobian for each sample.

Return type:

np.ndarray

abstractmethod transform(samples: numpy.ndarray) numpy.ndarray

Transform the input samples to a new coordinate system.

Parameters:

samples (np.ndarray) – The input samples to be transformed.

Returns:

The transformed samples.

Return type:

np.ndarray

gwkokab.analysis.core.utils.from_structured(data: numpy.ndarray) tuple[numpy.ndarray, collections.abc.Sequence[str]]

Converts a structured array to a 2D array.

Parameters:

data (np.ndarray) – Structured array.

Returns:

A tuple containing: - 2D array of shape (n_samples, n_features). - List of field names from the structured array.

Return type:

tuple[np.ndarray, Sequence[str]]

gwkokab.analysis.core.utils.read_attrs_from_hdf5(target: str | h5py.File | h5py.Group, dataset_path: str) dict

Reads attributes from a specified dataset in an HDF5 file.

Parameters:
  • target (str | h5py.File | h5py.Group) – Path or file descriptor to the HDF5 file to read from.

  • dataset_path (str) – The path to the dataset within the HDF5 file.

Returns:

A dictionary containing the attributes of the specified dataset.

Return type:

dict

Raises:

ValueError – If the specified dataset_path does not exist in the HDF5 file.

gwkokab.analysis.core.utils.read_from_hdf5(target: str | h5py.File | h5py.Group, dataset_path: str) numpy.ndarray

Reads data from an HDF5 file at the specified dataset path.

Parameters:
  • target (str | h5py.File | h5py.Group) – Path or file descriptor to the HDF5 file to read from.

  • dataset_path (str) – The path to the dataset within the HDF5 file.

Returns:

The data read from the specified dataset in the HDF5 file.

Return type:

np.ndarray

Raises:

ValueError – If the specified dataset_path does not exist in the HDF5 file.

gwkokab.analysis.core.utils.to_structured(data: numpy.ndarray, names: collections.abc.Sequence[str]) numpy.ndarray

Converts a 2D array to a structured array with given field names.

Parameters:
  • data (np.ndarray) – 2D array of shape (n_samples, n_features).

  • names (Sequence[str]) – List of field names for the structured array.

Returns:

Structured array with fields named according to names.

Return type:

np.ndarray

gwkokab.analysis.core.utils.write_to_hdf5(target: str | h5py.File | h5py.Group, dataset_path: str, data: numpy.ndarray | None = None, attrs: dict | None = None, mode: Literal['r', 'r+', 'w', 'w-', 'a'] = 'a') None

Writes data to an HDF5 file at the specified dataset path, with optional attributes.

Parameters:
  • target (str | h5py.File | h5py.Group) – Path or file descriptor to the HDF5 file to write to.

  • dataset_path (str) – The path to the dataset within the HDF5 file, by default None

  • data (np.ndarray | None, optional) – The data to write to the dataset, by default None

  • attrs (dict | None, optional) – A dictionary of attributes to set on the dataset, by default None

  • mode (Literal["r", "r+", "w", "w-", "a"], optional) – The mode in which to open the HDF5 file, by default “a”

Raises:

ValueError – If either dataset_path or data is not provided when attempting to write to the HDF5 file.