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¶
The transformer that changes nothing. |
|
Mixin class that provides a random number generator key and seed management for |
|
Change of coordinates applied to per-event samples as they are read. |
Functions¶
|
Converts a structured array to a 2D array. |
|
Reads attributes from a specified dataset in an HDF5 file. |
|
Reads data from an HDF5 file at the specified dataset path. |
|
Converts a 2D array to a structured array with given field names. |
|
Writes data to an HDF5 file at the specified dataset path, with optional |
Module Contents¶
- class gwkokab.analysis.core.utils.IdentitySampleTransformer¶
Bases:
SampleTransformerThe 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
seedis 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
- class gwkokab.analysis.core.utils.SampleTransformer¶
Bases:
abc.ABCChange 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
IdentitySampleTransformerThe 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.
- 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:
- Returns:
A dictionary containing the attributes of the specified dataset.
- Return type:
- 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:
- 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.