aspire.samplers.base
====================

.. py:module:: aspire.samplers.base


Attributes
----------

.. autoapisummary::

   aspire.samplers.base.logger


Classes
-------

.. autoapisummary::

   aspire.samplers.base.Sampler


Module Contents
---------------

.. py:data:: logger

.. py:class:: Sampler(log_likelihood, log_prior, dims, prior_flow, xp, dtype = None, parameters = None, preconditioning_transform = None)

   Base class for all samplers.

   :param log_likelihood: The log likelihood function.
   :type log_likelihood: :py:class:`Callable`
   :param log_prior: The log prior function.
   :type log_prior: :py:class:`Callable`
   :param dims: The number of dimensions.
   :type dims: :py:class:`int`
   :param flow: The flow object.
   :type flow: :py:class:`Flow`
   :param xp: The array backend to use.
   :type xp: :py:class:`Callable`
   :param parameters: The list of parameter names. If None, any samples objects will not
                      have the parameters names specified.
   :type parameters: :py:class:`list[str] | None`


   .. py:attribute:: prior_flow


   .. py:attribute:: dims


   .. py:attribute:: xp


   .. py:attribute:: backend_str
      :value: 'numpy'



   .. py:attribute:: dtype
      :value: None



   .. py:attribute:: parameters
      :value: None



   .. py:attribute:: history
      :value: None



   .. py:attribute:: n_likelihood_evaluations
      :value: 0



   .. py:method:: fit_preconditioning_transform(x)

      Fit the data transform to the data.



   .. py:method:: sample(n_samples)
      :abstractmethod:



   .. py:method:: log_likelihood(samples)

      Computes the log likelihood of the samples.

      Also tracks the number of likelihood evaluations.



   .. py:method:: log_prior(samples)

      Computes the log prior of the samples.



   .. py:method:: config_dict(include_sample_calls = 'last')

      Returns a dictionary with the configuration of the sampler.

      :param include_sample_calls: Whether to include the sample calls in the configuration.
                                   Default is True. If True, and if the sampler has a sample method
                                   with a calls attribute, the calls will be included in the config
                                   under the key "sample_calls". If this fails for any reason, a
                                   warning will be logged and the sample calls will be omitted.
      :type include_sample_calls: :py:class:`bool | str`, *optional*



   .. py:method:: build_checkpoint_state(samples, iteration = None, meta = None, include_sample_calls = 'last')

      Prepare a serializable checkpoint payload for the sampler state.



   .. py:method:: serialize_checkpoint(state, protocol = None)

      Serialize a checkpoint state to bytes with pickle.



   .. py:method:: default_checkpoint_callback(state)

      Store the latest checkpoint (state + pickled bytes) on the sampler.



   .. py:method:: default_file_checkpoint_callback(file_path)

      Return a simple default callback that overwrites an HDF5 file.



   .. py:method:: save_checkpoint_to_hdf(state, h5_file, path = 'sampler_checkpoints', dsetname = None, protocol = None)

      Save a checkpoint state into an HDF5 file as a pickled blob.



   .. py:method:: load_checkpoint_from_file(file_path, h5_path = 'checkpoint', dsetname = 'state')

      Load a checkpoint dictionary from .pkl or .hdf5 file.



   .. py:method:: restore_from_checkpoint(source)

      Restore sampler state from a checkpoint source.



   .. py:property:: last_checkpoint_state
      :type: dict | None


      Return the most recent checkpoint state stored by the default callback.


   .. py:property:: last_checkpoint_bytes
      :type: bytes | None


      Return the most recent pickled checkpoint produced by the default callback.


