nessai.proposal.flowproposal.base
=================================

.. py:module:: nessai.proposal.flowproposal.base

.. autoapi-nested-parse::

   Base proposal class that contains common methods.

   ..
       !! processed by numpydoc !!


Classes
-------

.. autoapisummary::

   nessai.proposal.flowproposal.base.BaseFlowProposal


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

.. py:class:: BaseFlowProposal(model, rng: Optional[numpy.random.Generator] = None, flow_config=None, training_config=None, output=None, poolsize=None, plot='min', check_acceptance=False, max_poolsize_scale=10, update_poolsize=True, accumulate_weights=False, save_training_data=False, reparameterisations=None, fallback_reparameterisation='zscore', use_default_reparameterisations=None, reverse_reparameterisations=False, map_to_unit_hypercube=False)

   Bases: :py:obj:`nessai.proposal.rejection.RejectionProposal`


   
   Object that handles training and proposal points


   :Parameters:

       **model** : :obj:`nessai.model.Model`
           User defined model.

       **flow_config** : dict, optional
           Configuration for training the normalising flow. If None, uses default
           settings. Defaults to None.

       **output** : str, optional
           Path to output directory.

       **plot** : {True, False, 'all', 'min'}, optional
           Controls the plotting level: ``True`` - all plots; ``False`` - no
           plots; ``'all'`` -  all plots and ``'min'`` -  1d plots and loss.

       **poolsize** : int, optional
           Size of the proposal pool. Defaults to 10000.

       **update_poolsize** : bool, optional
           If True the poolsize is updated using the current acceptance of the
           nested sampler.

       **max_poolsize_scale** : int, optional
           Maximum scale for increasing the poolsize. E.g. if this value is 10
           and the poolsize is 1000 the maximum number of points in the pool
           is 10,000.

       **check_acceptance** : bool, optional
           If True the acceptance is computed after populating the pool. This
           includes computing the likelihood for every point. Default False.

       **reparameterisations** : Union[dict, str], optional
           Dictionary for configure more flexible reparameterisations. This
           ignores any of the other settings related to rescaling. For more
           details see the documentation.

       **fallback_reparameterisation** : None or str
           Name of the reparameterisation to be used for parameters that have not
           been specified in the reparameterisations dictionary. If None, the
           :py:class:`~nessai.reparameterisations.NullReparameterisation` is used.
           Reparameterisation should support multiple parameters.

       **use_default_reparameterisations** : bool, optional
           If True then reparameterisations will be used even if
           ``reparameterisations`` is None. The exact reparameterisations used
           will depend on
           :py:func:`~nessai.proposal.flowproposal.FlowProposal.add_default_reparameterisations`
           which may be overloaded by child classes. If not specified then the
           value of the attribute
           :py:attr:`~nessai.proposal.flowproposal.FlowProposal.use_default_reparameterisations`
           is used.

       **reverse_reparameterisations** : bool
           Passed to :code:`reverse_order` in
           :py:obj:`~nessai.reparameterisations.combined.CombinedReparameterisation`.
           Reverses the order of the reparameterisations.

       **map_to_unit_hypercube** : bool
           If true, samples will be mapped to the unit hypercube before any
           reparameterisations are applied.














   ..
       !! processed by numpydoc !!

   .. py:attribute:: use_default_reparameterisations
      :value: False


      
      Indicates whether reparameterisations will be used be default in this
      class. Child classes can change this value a force the default
      behaviour to change without changing the keyword arguments.
















      ..
          !! processed by numpydoc !!


   .. py:property:: flow_config

      
      Return the configuration for the flow
















      ..
          !! processed by numpydoc !!


   .. py:property:: training_config

      
      Return the configuration for the flow
















      ..
          !! processed by numpydoc !!


   .. py:property:: poolsize

      
      Return the poolsize based of the base value and the current
      value of the scaling
















      ..
          !! processed by numpydoc !!


   .. py:property:: dims

      
      Return the number of dimensions
















      ..
          !! processed by numpydoc !!


   .. py:property:: rescaled_dims

      
      Return the number of rescaled dimensions
















      ..
          !! processed by numpydoc !!


   .. py:property:: x_dtype

      
      Return the dtype for the x space
















      ..
          !! processed by numpydoc !!


   .. py:property:: x_prime_dtype

      
      Return the dtype for the x prime space
















      ..
          !! processed by numpydoc !!


   .. py:property:: population_dtype

      
      dtype used for populating the proposal, depends on if the prior
      is defined in the x space or x-prime space
















      ..
          !! processed by numpydoc !!


   .. py:property:: prior_bounds

      
      The priors bounds used when computing the priors.

      If :code:`map_to_unit_hypercube` is true, these will be [0, 1]















      ..
          !! processed by numpydoc !!


   .. py:method:: configure_poolsize(poolsize, update_poolsize, max_poolsize_scale)

      
      Configure settings related to the pool size
















      ..
          !! processed by numpydoc !!


   .. py:method:: configure_plotting(plot)

      
      Configure plotting.

      Plotting is split into training and pool. Training refers to plots
      produced during training and pool refers to plots produces during
      the population stage.

      :Parameters:

          **plot** : {True, False, 'all', 'train', 'pool', 'min', 'minimal'}
              Level of plotting. `all`, `train` and `pool` enable corner style
              plots. All other values that evaluate to True enable 1d histogram
              plots. False disables all plotting.














      ..
          !! processed by numpydoc !!


   .. py:method:: update_flow_config()

      
      Update the flow configuration dictionary.
















      ..
          !! processed by numpydoc !!


   .. py:method:: initialise(resumed: bool = False) -> None

      
      Initialise the proposal class.

      This includes:
          * Setting up the rescaling
          * Verifying the rescaling is invertible
          * Initialising the FlowModel

      :Parameters:

          **resumed** : bool
              Indicates if the proposal is being initialised after being resumed
              or not. When true, the reparameterisations will not be
              reinitialised.














      ..
          !! processed by numpydoc !!


   .. py:method:: update_poolsize_scale(acceptance)

      
      Update poolsize given the current acceptance.


      :Parameters:

          **acceptance** : float
              Current acceptance.














      ..
          !! processed by numpydoc !!


   .. py:method:: add_default_reparameterisations()

      
      Add any reparameterisations which are assumed by default
















      ..
          !! processed by numpydoc !!


   .. py:method:: get_reparameterisation(name)

      
      Get the reparameterisation from the name
















      ..
          !! processed by numpydoc !!


   .. py:method:: configure_reparameterisations(reparameterisations)

      
      Configure the reparameterisations.


      :Parameters:

          **reparameterisations** : Union[dict, str, None]
              Dictionary of reparameterisations. If None, then the defaults
              from :py:func`get_default_reparameterisations` are used.














      ..
          !! processed by numpydoc !!


   .. py:method:: set_rescaling()

      
      Set function and parameter names for rescaling
















      ..
          !! processed by numpydoc !!


   .. py:method:: verify_rescaling()

      
      Verify the rescaling functions are invertible
















      ..
          !! processed by numpydoc !!


   .. py:method:: rescale(x, **kwargs)

      
      Rescale from the physical space to the primed physical space


      :Parameters:

          **x: array_like**
              Array of live points to rescale

          **compute_radius: bool (False)**
              Used to indicate when rescaling is being used for computing the
              radius for population. This is important for rescaling that uses
              inversions.



      :Returns:

          array
              Array of rescaled values

          array
              Array of log det|J|











      ..
          !! processed by numpydoc !!


   .. py:method:: inverse_rescale(x_prime, return_unit_hypercube=False, **kwargs)

      
      Rescale from the primed physical space to the original physical
      space.


      :Parameters:

          **x_prime** : array_like
              Array of live points to rescale.



      :Returns:

          array
              Array of rescaled values in the data space.

          array
              Array of log-Jacobian determinants.











      ..
          !! processed by numpydoc !!


   .. py:method:: check_state(x)

      
      Update the state of the proposal given some training data.

      Includes updating the reparameterisations.

      :Parameters:

          **x: array_like**
              Array of training live points which can be used to set parameters














      ..
          !! processed by numpydoc !!


   .. py:method:: train(x, plot=True)

      
      Train the normalising flow given some of the live points.


      :Parameters:

          **x** : structured_array
              Array of live points

          **plot** : {True, False, 'all'}
              Enable or disable plots for during training. By default the plots
              are only one-dimensional histograms, `'all'` includes corner
              plots with samples, these are often a few MB in size so
              proceed with caution!














      ..
          !! processed by numpydoc !!


   .. py:method:: reset_model_weights(**kwargs)

      
      Reset the flow weights.


      :Parameters:

          **kwargs**
              Keyword arguments passed to
              :meth:`nessai.flowmodel.FlowModel.reset_model`.














      ..
          !! processed by numpydoc !!


   .. py:method:: check_prior_bounds(x, *args)

      
      Return only values that are within the prior bounds


      :Parameters:

          **x: array_like**
              Array of live points which will compared against prior bounds

          **\*args:**
              Additional arrays which correspond to the array of live points.
              Only those corresponding to points within the prior bounds
              are returned



      :Returns:

          out: tuple of arrays
              Array containing the subset of the original arrays which fall
              within the prior bounds











      ..
          !! processed by numpydoc !!


   .. py:method:: forward_pass(x, rescale=True, **kwargs)

      
      Pass a vector of points through the model


      :Parameters:

          **x** : array_like
              Live points to map to the latent space

          **rescale** : bool, optional (True)
              Apply rescaling function

          **compute_radius** : bool, optional (True)
              Flag parsed to rescaling for rescaling specific to radius
              computation



      :Returns:

          **x** : array_like
              Samples in the latent space

          **log_prob** : array_like
              Log probabilities corresponding to each sample (including the
              jacobian)











      ..
          !! processed by numpydoc !!


   .. py:method:: backward_pass(z, rescale=True, **kwargs)

      
      A backwards pass from the model (latent -> real)


      :Parameters:

          **z** : array_like
              Structured array of points in the latent space

          **rescale** : bool, optional (True)
              Apply inverse rescaling function



      :Returns:

          **x** : array_like
              Samples in the data space

          **log_j** : array_like
              Log Jacobian determinant











      ..
          !! processed by numpydoc !!


   .. py:method:: log_prior(x)

      
      Compute the prior probability using the user-defined model


      :Parameters:

          **x** : structured_array
              Array of samples



      :Returns:

          array_like
              Array of log prior probabilities











      ..
          !! processed by numpydoc !!


   .. py:method:: unit_hypercube_log_prior(x)

      
      Compute the prior in the unit hypercube space.


      :Parameters:

          **x** : array
              Samples in the unit hypercube.














      ..
          !! processed by numpydoc !!


   .. py:method:: compute_weights(x, log_q, return_log_prior=False)

      
      Compute weights for the samples.

      Does NOT normalise the weights

      :Parameters:

          **x** :  structured_arrays
              Array of points

          **log_q** : array_like
              Array of log proposal probabilities.

          **return_log_prior: bool**
              If true, the log-prior probability is also returned.



      :Returns:

          array_like
              Log-weights for rejection sampling.











      ..
          !! processed by numpydoc !!


   .. py:method:: convert_to_samples(x, plot=True)

      
      Convert the array to samples ready to be used.

      This removes are auxiliary parameters, (e.g. auxiliary radial
      parameters) and ensures the prior is computed. These samples can
      be directly used in the nested sampler.

      :Parameters:

          **x** : array_like
              Array of samples

          **plot** : bool, optional
              If true a 1d histogram for each parameter of the pool is plotted.
              This includes a comparison the live points used to train the
              current realisation of the flow.



      :Returns:

          array
              Structured array of samples.











      ..
          !! processed by numpydoc !!


   .. py:method:: populate(worst_point, n_samples=10000)
      :abstractmethod:


      
      Populate the pool by drawing from the proposal distribution and
      using rejection sampling.

      Will also evaluate the likelihoods if the proposal contains a
      multiprocessing pool.

      :Parameters:

          **N** : int, optional
              Number of samples to draw. Not all samples will be accepted to
              the number of samples saved will be less than N. If not specified
              ``poolsize`` will be used.














      ..
          !! processed by numpydoc !!


   .. py:method:: compute_acceptance(logL)

      
      Compute how many of the current pool have log-likelihoods greater
      than the specified log-likelihood using the current value in the
      `logL` field.


      :Parameters:

          **float** : logL
              Log-likelihood to use as the lower bound



      :Returns:

          float
              Acceptance defined on [0, 1]











      ..
          !! processed by numpydoc !!


   .. py:method:: draw(worst_point)

      
      Draw a replacement point. The new point is independent of the worst
      point. The worst point is only used during population.


      :Parameters:

          **worst_point** : structured_array
              The current worst point used to compute the radius of the contour
              in the latent space.



      :Returns:

          structured_array
              New live point











      ..
          !! processed by numpydoc !!


   .. py:method:: plot_pool(x)

      
      Plot the pool of points.


      :Parameters:

          **x** : array_like
              Corresponding samples to plot in the physical space.














      ..
          !! processed by numpydoc !!


   .. py:method:: resume(model, flow_config, weights_file=None)

      
      Resume the proposal.

      The model and config are not stored so these must be provided.

      :Parameters:

          **model** : :obj:`nessai.model.Model`
              User-defined model used.

          **flow_config** : dict
              Configuration dictionary for the flow.

          **weights_files** : str, optional
              Weights file to try and load. If not provided the proposal
              tries to load the last weights file.














      ..
          !! processed by numpydoc !!


   .. py:method:: reset()

      
      Reset the proposal
















      ..
          !! processed by numpydoc !!


