gwkokab.analysis.core.utils =========================== .. py:module:: gwkokab.analysis.core.utils Classes ------- .. autoapisummary:: gwkokab.analysis.core.utils.IdentitySampleTransformer gwkokab.analysis.core.utils.PRNGKeyMixin gwkokab.analysis.core.utils.SampleTransformer Functions --------- .. autoapisummary:: gwkokab.analysis.core.utils.from_structured gwkokab.analysis.core.utils.read_attrs_from_hdf5 gwkokab.analysis.core.utils.read_from_hdf5 gwkokab.analysis.core.utils.to_structured gwkokab.analysis.core.utils.write_to_hdf5 Module Contents --------------- .. py:class:: IdentitySampleTransformer Bases: :py:obj:`SampleTransformer` Helper class that provides a standard way to create an ABC using inheritance. .. py:method:: log_abs_det_jacobian(samples: numpy.ndarray, transformed_samples: numpy.ndarray) -> numpy.ndarray Compute the log absolute determinant of the Jacobian of the transformation. :param samples: The original samples before transformation. :type samples: np.ndarray :param transformed_samples: The transformed samples after applying the transformation. :type transformed_samples: np.ndarray :returns: The log absolute determinant of the Jacobian for each sample. :rtype: np.ndarray .. py:method:: transform(samples: numpy.ndarray) -> numpy.ndarray Transform the input samples to a new coordinate system. :param samples: The input samples to be transformed. :type samples: np.ndarray :returns: The transformed samples. :rtype: np.ndarray .. py:class:: 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. .. py:method:: init_rng_seed(seed: int) -> None :classmethod: .. py:property:: rng_key :type: 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. :rtype: PRNGKeyArray .. py:property:: seed :type: int Accessor for the seed used to initialize the random number generator key. :returns: The seed used for RNG initialization. :rtype: int .. py:class:: SampleTransformer Bases: :py:obj:`abc.ABC` Helper class that provides a standard way to create an ABC using inheritance. .. py:method:: 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. .. py:method:: log_abs_det_jacobian(samples: numpy.ndarray, transformed_samples: numpy.ndarray) -> numpy.ndarray :abstractmethod: Compute the log absolute determinant of the Jacobian of the transformation. :param samples: The original samples before transformation. :type samples: np.ndarray :param transformed_samples: The transformed samples after applying the transformation. :type transformed_samples: np.ndarray :returns: The log absolute determinant of the Jacobian for each sample. :rtype: np.ndarray .. py:method:: transform(samples: numpy.ndarray) -> numpy.ndarray :abstractmethod: Transform the input samples to a new coordinate system. :param samples: The input samples to be transformed. :type samples: np.ndarray :returns: The transformed samples. :rtype: np.ndarray .. py:function:: from_structured(data: numpy.ndarray) -> tuple[numpy.ndarray, collections.abc.Sequence[str]] Converts a structured array to a 2D array. :param data: Structured array. :type data: np.ndarray :returns: A tuple containing: - 2D array of shape (n_samples, n_features). - List of field names from the structured array. :rtype: tuple[np.ndarray, Sequence[str]] .. py:function:: read_attrs_from_hdf5(target: str | h5py.File | h5py.Group, dataset_path: str) -> dict Reads attributes from a specified dataset in an HDF5 file. :param target: Path or file descriptor to the HDF5 file to read from. :type target: str | h5py.File | h5py.Group :param dataset_path: The path to the dataset within the HDF5 file. :type dataset_path: str :returns: A dictionary containing the attributes of the specified dataset. :rtype: dict :raises ValueError: If the specified `dataset_path` does not exist in the HDF5 file. .. py:function:: 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. :param target: Path or file descriptor to the HDF5 file to read from. :type target: str | h5py.File | h5py.Group :param dataset_path: The path to the dataset within the HDF5 file. :type dataset_path: str :returns: The data read from the specified dataset in the HDF5 file. :rtype: np.ndarray :raises ValueError: If the specified `dataset_path` does not exist in the HDF5 file. .. py:function:: to_structured(data: numpy.ndarray, names: collections.abc.Sequence[str]) -> numpy.ndarray Converts a 2D array to a structured array with given field names. :param data: 2D array of shape (n_samples, n_features). :type data: np.ndarray :param names: List of field names for the structured array. :type names: Sequence[str] :returns: Structured array with fields named according to `names`. :rtype: np.ndarray .. py:function:: 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. :param target: Path or file descriptor to the HDF5 file to write to. :type target: str | h5py.File | h5py.Group :param dataset_path: The path to the dataset within the HDF5 file, by default None :type dataset_path: str :param data: The data to write to the dataset, by default None :type data: np.ndarray | None, optional :param attrs: A dictionary of attributes to set on the dataset, by default None :type attrs: dict | None, optional :param mode: The mode in which to open the HDF5 file, by default "a" :type mode: Literal["r", "r+", "w", "w-", "a"], optional :raises ValueError: If either `dataset_path` or `data` is not provided when attempting to write to the HDF5 file.