gwkokab.analysis.core.utils

Shared helpers for the analysis drivers.

PRNGKeyMixin gives an analysis a class-level PRNG key that splits on every access, so independent parts of a run get independent randomness from a single recorded seed. write_to_hdf5() and read_from_hdf5() are the two ends of the output format, and handle the encoding of non-array attributes – None, lists and dicts – into what HDF5 accepts. SampleTransformer is the hook by which a data loader changes the coordinates its events are expressed in, carrying the Jacobian along so densities stay normalised.

Classes

IdentitySampleTransformer

The transformer that changes nothing.

PRNGKeyMixin

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

SampleTransformer

Change of coordinates applied to per-event samples as they are read.

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

The transformer that changes nothing.

Used when the data loader’s coordinates already match the model’s, so the samples pass through untouched and the log Jacobian determinant is zero.

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

The log Jacobian determinant of the identity, which is zero.

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

  • transformed_samples (np.ndarray) – The transformed samples; identical to samples.

Returns:

Zeros of shape samples.shape[:-1].

Return type:

np.ndarray

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

Return the samples unchanged.

Parameters:

samples (np.ndarray) – The samples to transform.

Returns:

samples, unchanged.

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

Initialise the class-level random key from a seed.

The key lives on the class rather than the instance, so every analysis object in a run draws from the same reproducible stream.

Parameters:

seed (int) – A non-negative integer seed.

Raises:

LoggedValueError – If seed is not an integer, or is negative.

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

Change of coordinates applied to per-event samples as they are read.

A data loader may express its events in coordinates the model does not use. Subclasses implement the forward map and its log Jacobian determinant, which is applied to the PE prior so that the reweighted density stays normalised across the change.

See also

IdentitySampleTransformer

The no-op transformer, used when no change is needed.

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

Additional validity checks on a transformation.

Subclasses may override this to reject samples the transform sends somewhere unusable – outside the model’s support, say. The base implementation accepts everything.

Parameters:
  • samples (np.ndarray) – The original samples, of shape (n_samples, n_parameters).

  • transformed_samples (np.ndarray) – The transformed samples.

Returns:

Boolean mask of length n_samples, true for samples to keep.

Return type:

np.ndarray

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.