aspire_bilby.utils
==================

.. py:module:: aspire_bilby.utils


Classes
-------

.. autoapisummary::

   aspire_bilby.utils.Inputs
   aspire_bilby.utils.Functions
   aspire_bilby.utils.GlobalFunctions


Functions
---------

.. autoapisummary::

   aspire_bilby.utils.update_global_functions
   aspire_bilby.utils.get_aspire_functions
   aspire_bilby.utils.get_prior_bounds
   aspire_bilby.utils.get_periodic_parameters
   aspire_bilby.utils.samples_from_bilby_result
   aspire_bilby.utils.samples_from_bilby_priors
   aspire_bilby.utils.sample_missing_parameters
   aspire_bilby.utils.temporary_logger_level
   aspire_bilby.utils.load_bilby_pipe_ini
   aspire_bilby.utils.get_inputs_from_bilby_pipe_ini
   aspire_bilby.utils.get_function_from_path


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

.. py:class:: Inputs

   Bases: :py:obj:`tuple`


   .. py:attribute:: log_likelihood


   .. py:attribute:: log_prior


   .. py:attribute:: dims


   .. py:attribute:: parameters


   .. py:attribute:: prior_bounds


   .. py:attribute:: periodic_parameters


.. py:class:: Functions

   Bases: :py:obj:`tuple`


   .. py:attribute:: log_likelihood


   .. py:attribute:: log_prior


.. py:class:: GlobalFunctions

   Dataclass to store global functions.


   .. py:attribute:: bilby_likelihood
      :type:  bilby.core.likelihood.Likelihood


   .. py:attribute:: bilby_priors
      :type:  bilby.core.prior.PriorDict


   .. py:attribute:: parameters
      :type:  list


   .. py:attribute:: use_ratio
      :type:  bool


.. py:function:: update_global_functions(bilby_likelihood, bilby_priors, fixed_parameters, parameters, use_ratio)

   Update the global functions for log likelihood and log prior.


.. py:function:: get_aspire_functions(bilby_likelihood, bilby_priors, parameters, use_ratio = False, likelihood_dtype = 'float64')

   Get the log likelihood function for a bilby likelihood object.

   :param bilby_likelihood: The bilby likelihood object.
   :type bilby_likelihood: :py:class:`Likelihood`
   :param bilby_priors: The bilby prior object.
   :type bilby_priors: :py:class:`PriorDict`
   :param parameters: The parameters to evaluate the log likelihood and log prior for.
                      The order should match the order of the parameters in the aspire sampler.
   :type parameters: :py:class:`list[str]`
   :param use_ratio: Whether to use the log likelihood ratio function if available. If False,
                     the log likelihood function is used instead.
   :type use_ratio: :py:class:`bool`
   :param likelihood_dtype: The dtype to use for the log likelihood values. Default is "float64".
   :type likelihood_dtype: :py:class:`str`


.. py:function:: get_prior_bounds(bilby_priors, parameters)

   Get a dictionary of prior bounds.


.. py:function:: get_periodic_parameters(bilby_priors)

   Determine which parameters are periodic.


.. py:function:: samples_from_bilby_result(result, parameters = None, bilby_priors = None, sample_from_prior = None, conversion_function = None)

   Get samples from a bilby result object.

   :param result: The bilby result object.
   :type result: :py:class:`Result`
   :param parameters: The parameters to read from the result. If None, all parameters will be read.
   :type parameters: :py:class:`str`
   :param bilby_priors: The bilby prior object. If not specified, the initial result must contain
                        all parameters.
   :type bilby_priors: :py:class:`PriorDict`
   :param sample_from_prior: A list of parameters to explicitly sample from the prior rather reading
                             from the result.
   :type sample_from_prior: :py:class:`list[str]`


.. py:function:: samples_from_bilby_priors(bilby_priors, n_samples, parameters = None)

   Get samples from a bilby prior object.

   :param bilby_priors: The bilby prior object.
   :type bilby_priors: :py:class:`PriorDict`
   :param n_samples: The number of samples to draw.
   :type n_samples: :py:class:`int`
   :param parameters: The parameters to sample. If None, all parameters will be sampled.
   :type parameters: :py:class:`str`


.. py:function:: sample_missing_parameters(bilby_result, bilby_priors, parameters = None, parameters_to_sample = None)

   Sample the missing parameters from the bilby result.

   :param bilby_result: The initial bilby result object.
   :type bilby_result: :py:class:`Result`
   :param bilby_priors: The bilby prior object.
   :type bilby_priors: :py:class:`PriorDict`
   :param parameters: The parameters that should be present in the final set of samples.
                      If not specified, the parameters will be inferred from the priors.
   :type parameters: :py:class:`list[str]`
   :param parameters_to_sample: A list of parameters to explicitly sample from the prior rather reading
                                from the result. Each entry can be a regex pattern to match multiple
                                parameters.
   :type parameters_to_sample: :py:class:`list[str]`

   :returns: The samples from the bilby result and the missing parameters from the bilby priors.
             The order will be the same as :code:`parameters`.
   :rtype: :py:class:`pd.DataFrame`


.. py:function:: temporary_logger_level(logger, level)

   Temporarily set the logger level.

   Example usage

   ```python
   with temporary_logger_level(logger, "DEBUG"):
       # Do something
   ```


.. py:function:: load_bilby_pipe_ini(config_file, data_dump_file, suppress_bilby_logger = True)

   Load a bilby_pipe ini file and return the likelihood and priors.


.. py:function:: get_inputs_from_bilby_pipe_ini(config_file, data_dump_file, use_ratio = False, suppress_bilby_logger = True)

   Get the aspire inputs from a bilby_pipe ini file.

   :returns: A namedtuple with the log_likelihood and log_prior functions.
   :rtype: :py:class:`namedtuple`


.. py:function:: get_function_from_path(path)

   Get a function from a module path.

   :param path: The path to the function, e.g. "module.submodule.function".
   :type path: :py:class:`str`

   :returns: The function object.
   :rtype: :py:class:`Callable`


