petitRADTRANS.sbi.posterior
===========================

.. py:module:: petitRADTRANS.sbi.posterior

.. autoapi-nested-parse::

   Posterior estimator interfaces and concrete flow backends.

   This module re-exports all public symbols from the split posterior
   sub-modules for backward compatibility.  New code should import from
   :mod:`posterior_base`, :mod:`flow_posterior`, or
   :mod:`flow_matching_posterior` directly.



Attributes
----------

.. autoapisummary::

   petitRADTRANS.sbi.posterior.POSTERIOR_METADATA_SCHEMA_VERSION


Classes
-------

.. autoapisummary::

   petitRADTRANS.sbi.posterior.FlowMatchingPosterior
   petitRADTRANS.sbi.posterior.ConditionalAutoregressiveFlowPosterior
   petitRADTRANS.sbi.posterior.ConditionalFlowPosterior
   petitRADTRANS.sbi.posterior.ConditionalNeuralAutoregressiveFlowPosterior
   petitRADTRANS.sbi.posterior.ConditionalSplineFlowPosterior
   petitRADTRANS.sbi.posterior.PersistentPosteriorEstimator
   petitRADTRANS.sbi.posterior.PosteriorBatch
   petitRADTRANS.sbi.posterior.PosteriorEstimator
   petitRADTRANS.sbi.posterior.PosteriorSamples
   petitRADTRANS.sbi.posterior.TrainingArtifacts


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

.. py:class:: FlowMatchingPosterior(parameter_dim: int, embedding_dim: int = 128, hidden_dim: int = 128, num_velocity_layers: int = 3, learning_rate: float = 0.001, batch_size: int = 32, num_epochs: int = 5, parameter_space: str = 'unconstrained', integration_steps: int = 32, early_stopping_patience: int | None = None, early_stopping_min_delta: float = 0.0, checkpoint_directory: str | None = None, checkpoint_backend: str = 'auto', resume_from_checkpoint: bool = False, seed: int = 0, task_metadata: Mapping[str, Any] | None = None)

   Bases: :py:obj:`petitRADTRANS.sbi.posterior_base.PersistentPosteriorEstimator`


   Conditional flow-matching posterior skeleton.

   .. warning::

       This estimator is **experimental**. It does not expose ``log_prob``
       and the ODE integration scheme is a simple midpoint rule.  Expect
       the API and numerical behaviour to change in future releases.

   This estimator family trains a conditional vector field on straight-line
   interpolation paths between Gaussian noise and target parameters, then
   generates posterior samples by integrating the learned field from noise to
   the terminal parameter state.


   .. py:attribute:: estimator_family
      :value: 'flow_matching'



   .. py:attribute:: embedding_dim
      :value: 128



   .. py:attribute:: hidden_dim
      :value: 128



   .. py:attribute:: num_velocity_layers
      :value: 3



   .. py:attribute:: learning_rate


   .. py:attribute:: batch_size
      :value: 32



   .. py:attribute:: num_epochs
      :value: 5



   .. py:attribute:: integration_steps
      :value: 32



   .. py:attribute:: early_stopping_patience
      :value: None



   .. py:attribute:: early_stopping_min_delta


   .. py:attribute:: checkpoint_directory
      :value: None



   .. py:attribute:: checkpoint_backend
      :value: 'auto'



   .. py:attribute:: resume_from_checkpoint
      :value: False



   .. py:attribute:: model


   .. py:method:: _batch_embeddings(model: _FlowMatchingModel, observations: Any) -> jax.numpy.ndarray
      :staticmethod:



   .. py:method:: _loss(model: _FlowMatchingModel, batch: petitRADTRANS.sbi.posterior_base.PosteriorBatch) -> jax.numpy.ndarray
      :staticmethod:



   .. py:method:: fit(dataset: Any) -> petitRADTRANS.sbi.posterior_base.TrainingArtifacts

      Train the posterior estimator on a simulation dataset.



   .. py:method:: _build_estimator_config() -> dict[str, Any]

      Return backend-specific configuration for metadata persistence.



   .. py:method:: _resolve_estimator_config(metadata: Mapping[str, Any]) -> dict[str, Any]
      :staticmethod:



   .. py:method:: _build_serialized_metadata(artifact_metadata) -> dict[str, Any]


   .. py:method:: from_serialized_metadata(metadata: Mapping[str, Any]) -> FlowMatchingPosterior
      :classmethod:


      Rebuild an estimator instance from persisted metadata only.



   .. py:method:: save_backend_state(output_path: pathlib.Path) -> None

      Persist backend-specific model state into the output directory.



   .. py:method:: load_backend_state(input_path: pathlib.Path) -> None

      Restore backend-specific model state from the input directory.



   .. py:method:: encode_observation(blocks: list[petitRADTRANS.sbi.observation.ObservationBlock]) -> petitRADTRANS.sbi.observation.EncodedObservation

      Encode a structured observation into the estimator input space.



   .. py:method:: batch_encode_observation(blocks_list: list[list[petitRADTRANS.sbi.observation.ObservationBlock]]) -> list[petitRADTRANS.sbi.observation.EncodedObservation]

      Encode a batch of observations using a single vmapped forward pass.



   .. py:method:: sample_posterior(observation: petitRADTRANS.sbi.observation.EncodedObservation, n_samples: int, seed: int | None = None) -> petitRADTRANS.sbi.posterior_base.PosteriorSamples

      Sample the amortized posterior for one encoded observation.



   .. py:method:: log_prob(observation: petitRADTRANS.sbi.observation.EncodedObservation, parameters: Any) -> Any

      Evaluate posterior log-density when supported by the backend.



.. py:class:: ConditionalAutoregressiveFlowPosterior(*args: Any, **kwargs: Any)

   Bases: :py:obj:`ConditionalFlowPosterior`


   Posterior estimator specialized to the autoregressive flow backend.


.. py:class:: ConditionalFlowPosterior(parameter_dim: int, embedding_dim: int = 128, num_coupling_layers: int = 4, hidden_dim: int = 128, conditioner_depth: int = 2, autoregressive_transform_units: int = 16, neural_autoregressive_min_slope: float = 0.001, neural_autoregressive_min_residual: float = 0.05, neural_autoregressive_inverse_bisection_steps: int = 48, learning_rate: float = 0.001, batch_size: int = 32, num_epochs: int = 5, parameter_space: str = 'unconstrained', flow_family: str = 'spline', num_spline_bins: int = 8, spline_bound: float = 10.0, base_distribution: str = 'gaussian', use_base_affine: bool = False, training_objective: str = 'npe', early_stopping_patience: int | None = None, early_stopping_min_delta: float = 0.0, checkpoint_directory: str | None = None, checkpoint_backend: str = 'auto', resume_from_checkpoint: bool = False, gradient_clip_norm: float | None = 1.0, embedding_noise_std: float = 0.0, embedding_noise_min_scale: float = 0.0, aux_scale_loss_weight: float = 0.0, aux_parameter_loss_weight: float = 0.0, parameter_noise_floor: float = 0.0, spline_small_bin_regularization_weight: float | None = None, spline_min_bin_ratio_target: float = 0.2, spline_entropy_regularization_weight: float = 0.0, spline_derivative_regularization_weight: float = 0.0, spline_entropy_floor: float = 0.6, spline_min_derivative_target: float = 0.25, spline_max_derivative_target: float = 5.0, weight_decay: float = 0.0, use_cosine_schedule: bool = False, warmup_fraction: float = 0.02, warmup_epochs: float | None = None, min_learning_rate: float = 1e-06, lr_schedule_total_epochs: float | None = None, stable_inverse_forward_max_abs_error_threshold: float | None = 0.0001, stable_inverse_forward_logdet_closure_max_abs_error_threshold: float | None = 0.0001, stable_cube_edge_hit_rate_threshold: float | None = 0.0, spectrum_encoder_type: str = 'conv1d', n_wavelengths: int = 233, spectrum_embedding_dim: int = 64, photometry_embedding_dim: int = 64, encoder_hidden_dim: int | None = None, encoder_patch_size: int = 32, seed: int = 0, task_metadata: Mapping[str, Any] | None = None, verbose_diagnostics: bool = False, diagnostics_output_directory: str | None = None, diagnostics_plot_interval: int = 1)

   Bases: :py:obj:`petitRADTRANS.sbi.posterior_base.PersistentPosteriorEstimator`


   Concrete amortized posterior using a conditional flow backend.

   Parameters
   ----------
   parameter_dim:
       Number of inferred free parameters represented by the posterior.
   embedding_dim:
       Size of the learned observation embedding consumed by the flow.
   num_coupling_layers:
       Number of conditional coupling or spline-transform layers in the flow.
   hidden_dim:
       Hidden width used by encoder-side and flow-side MLP conditioners.
   learning_rate:
       Optimizer learning rate used by :class:`SBITrainer`.
   batch_size:
       Number of simulations processed per optimization step.
   num_epochs:
       Maximum number of passes through the training split.
   parameter_space:
       Parameter coordinates learned by the posterior. Supported values are
       ``'physical'``, ``'cube'``, and ``'unconstrained'``.
   flow_family:
       Conditional density-transform family. Supported values are
       ``'spline'``, ``'affine'``, ``'autoregressive'``, and
       ``'neural_autoregressive'``.
   num_spline_bins:
       Number of rational-quadratic spline bins when ``flow_family='spline'``.
   spline_bound:
       Finite support bound of each spline transform in latent space.
   early_stopping_patience:
       Optional number of non-improving epochs tolerated before stopping.
   early_stopping_min_delta:
       Minimum improvement required for early-stopping comparisons.
   checkpoint_directory:
       Optional directory used to persist resumable trainer checkpoints.
   checkpoint_backend:
       Checkpoint persistence backend name. ``'auto'`` selects Orbax when
       available and otherwise falls back to Equinox serialization.
   resume_from_checkpoint:
       Whether ``fit`` should attempt to resume from the latest checkpoint.
   seed:
       Base random seed used for flow initialization and posterior sampling.
   task_metadata:
       Optional user-supplied metadata persisted alongside the trained model.

   Notes
   -----
   The posterior stores task fingerprinting, observation schema, and
   preprocessing payload information when those are available from the training
   dataset reader. That metadata is later reused by inference and artifact
   registration paths.


   .. py:attribute:: estimator_family
      :value: 'conditional_flow'



   .. py:attribute:: embedding_dim
      :value: 128



   .. py:attribute:: num_coupling_layers
      :value: 4



   .. py:attribute:: hidden_dim
      :value: 128



   .. py:attribute:: conditioner_depth
      :value: 2



   .. py:attribute:: autoregressive_transform_units
      :value: 16



   .. py:attribute:: neural_autoregressive_min_slope


   .. py:attribute:: neural_autoregressive_min_residual


   .. py:attribute:: neural_autoregressive_inverse_bisection_steps
      :value: 48



   .. py:attribute:: learning_rate


   .. py:attribute:: batch_size
      :value: 32



   .. py:attribute:: num_epochs
      :value: 5



   .. py:attribute:: flow_family
      :value: ''



   .. py:attribute:: effective_flow_family
      :value: ''



   .. py:attribute:: base_distribution
      :value: ''



   .. py:attribute:: use_base_affine
      :value: False



   .. py:attribute:: training_objective
      :value: ''



   .. py:attribute:: num_spline_bins
      :value: 8



   .. py:attribute:: early_stopping_patience
      :value: None



   .. py:attribute:: early_stopping_min_delta


   .. py:attribute:: checkpoint_directory
      :value: None



   .. py:attribute:: checkpoint_backend
      :value: 'auto'



   .. py:attribute:: resume_from_checkpoint
      :value: False



   .. py:attribute:: gradient_clip_norm
      :value: 1.0



   .. py:attribute:: embedding_noise_std


   .. py:attribute:: embedding_noise_min_scale


   .. py:attribute:: aux_scale_loss_weight


   .. py:attribute:: aux_parameter_loss_weight


   .. py:attribute:: parameter_noise_floor


   .. py:attribute:: spline_entropy_regularization_weight


   .. py:attribute:: spline_small_bin_regularization_weight


   .. py:attribute:: spline_derivative_regularization_weight


   .. py:attribute:: spline_min_bin_ratio_target


   .. py:attribute:: spline_entropy_floor


   .. py:attribute:: spline_min_derivative_target


   .. py:attribute:: spline_max_derivative_target


   .. py:attribute:: weight_decay


   .. py:attribute:: use_cosine_schedule
      :value: False



   .. py:attribute:: warmup_fraction


   .. py:attribute:: warmup_epochs
      :value: None



   .. py:attribute:: min_learning_rate


   .. py:attribute:: lr_schedule_total_epochs
      :value: None



   .. py:attribute:: stable_inverse_forward_max_abs_error_threshold
      :value: None



   .. py:attribute:: stable_inverse_forward_logdet_closure_max_abs_error_threshold
      :value: None



   .. py:attribute:: stable_cube_edge_hit_rate_threshold
      :value: None



   .. py:attribute:: spectrum_encoder_type
      :value: ''



   .. py:attribute:: n_wavelengths
      :value: 233



   .. py:attribute:: spectrum_embedding_dim
      :value: 64



   .. py:attribute:: photometry_embedding_dim
      :value: 64



   .. py:attribute:: encoder_hidden_dim


   .. py:attribute:: encoder_patch_size
      :value: 32



   .. py:attribute:: verbose_diagnostics
      :value: False



   .. py:attribute:: diagnostics_output_directory
      :value: None



   .. py:attribute:: diagnostics_plot_interval
      :value: 1



   .. py:attribute:: model


   .. py:method:: _build_flow(key: jax.Array) -> Any


   .. py:method:: _batch_embeddings(model: _PosteriorModel, observations: Any) -> jax.numpy.ndarray
      :staticmethod:



   .. py:method:: _loss(model: _PosteriorModel, batch: petitRADTRANS.sbi.posterior_base.PosteriorBatch) -> jax.numpy.ndarray
      :staticmethod:



   .. py:method:: _training_loss(model: _PosteriorModel, batch: petitRADTRANS.sbi.posterior_base.PosteriorBatch) -> jax.numpy.ndarray
      :staticmethod:



   .. py:method:: _loss_from_observations(model: _PosteriorModel, parameters: jax.numpy.ndarray, observations: Any, parameter_space: str, *, include_spline_regularization: bool) -> jax.numpy.ndarray
      :staticmethod:



   .. py:method:: _validation_diagnostics(model: _PosteriorModel, batch: petitRADTRANS.sbi.posterior_base.PosteriorBatch) -> dict[str, float]


   .. py:method:: _make_parameter_noise_loss(base_loss_fn: Callable[[_PosteriorModel, petitRADTRANS.sbi.posterior_base.PosteriorBatch], jax.numpy.ndarray], noise_floor: float, parameter_space: str) -> Callable[[_PosteriorModel, petitRADTRANS.sbi.posterior_base.PosteriorBatch], jax.numpy.ndarray]
      :staticmethod:


      Wrap a training loss so the parameter *targets* are jittered.

      Minimizing the conditional NLL in a very-low-noise regime with a highly
      parameter-predictive embedding drives the learned conditional density
      toward a delta (NLL -> -inf), which an over-sharp flow realizes as
      exploded transforms (and, for spline flows, cube-edge collapse). A small
      Gaussian jitter on the parameter targets gives the conditional a hard
      minimum width, capping sharpness so the NLL has a finite minimum and the
      flow's inverse stays well-conditioned.

      The jitter is applied in *unconstrained* (logit) space -- the space the
      flow actually models -- not in cube space. Additive cube-space noise is
      asymmetric near the [0, 1] bounds: clipping piles jittered targets onto
      the edges, and the cube->logit Jacobian rewards edge mass, so it
      worsens the very edge-collapse it was meant to prevent. Jittering in
      logit space is symmetric and boundary-free. Applied only during
      training; evaluation and checkpoint selection use the clean targets.



   .. py:method:: _make_elbo_loss(log_likelihood_fn: Callable[[jax.numpy.ndarray, Any], jax.numpy.ndarray], parameter_space: str, *, num_samples: int = 1) -> Callable[[_PosteriorModel, petitRADTRANS.sbi.posterior_base.PosteriorBatch], jax.numpy.ndarray]
      :staticmethod:


      Return an amortized-variational (ELBO) training loss.

      Maximizes, for observations ``x`` drawn from the (prior-predictive)
      dataset,

          ELBO(x) = E_{q(theta|x)}[ log p(x|theta) + log p(theta) - log q(theta|x) ]

      with a single- (or few-) sample reparameterized estimator. ``q(theta|x)``
      is the conditional flow: latents ``z`` are drawn from the flow base and
      pushed through ``flow.inverse`` to reparameterized samples, so the
      gradient flows through both the flow and the encoder.

      The ``-log q`` (entropy) term diverges to ``-inf`` as ``q`` collapses to
      a point mass, so a delta posterior is *structurally* impossible -- the
      width is set by the likelihood/entropy balance rather than by the
      (near-deterministic) embedding. ``log p(x|theta)`` is supplied by the
      injected differentiable forward-model likelihood evaluated at the
      physical parameters reconstructed from the sampled cube coordinates.
      With ``parameter_space='cube'`` the prior is uniform on the unit
      hypercube, so ``log p(theta_cube) = 0`` and is dropped.

      Parameters
      ----------
      log_likelihood_fn:
          ``(theta_cube, observations) -> (batch,)`` returning the Gaussian
          observational log-likelihood ``log p(x | theta)`` for each batch
          element. Receives unit-cube coordinates (shape ``(batch, dim)``)
          and the batch's prestacked observations; it owns the cube->physical
          transform, the differentiable forward model, and the noise model.
          Must be JAX-differentiable w.r.t. ``theta_cube``.
      parameter_space:
          Must be ``"cube"``.
      num_samples:
          Number of reparameterized posterior draws per observation used to
          estimate the ELBO expectation. ``1`` is standard for amortized VI;
          larger values reduce gradient variance at a proportional
          forward-model cost.



   .. py:method:: _make_noisy_loss(noise_std: float, preprocessing_metadata: Any = None, *, noise_min_scale: float = 0.0, include_spline_regularization: bool = True) -> Callable[[_PosteriorModel, petitRADTRANS.sbi.posterior_base.PosteriorBatch], jax.numpy.ndarray]
      :staticmethod:


      Return a loss function that injects on-the-fly Gaussian noise into
      the observation spectra before encoding.

      For spectra preprocessed into a per-sample transformed value space,
      the uncertainty channel is assumed to already be expressed in the same
      transformed units as the value channel, so noise can be injected
      directly from that channel. When preprocessing metadata is provided,
      non-spectral blocks still reconstruct raw measurement uncertainties so
      noise is injected at the correct physical scale in normalized-value
      space. ``noise_std`` acts as a multiplier on the observational noise
      level (1.0 = exact observational noise, >1 = regularising over-noise),
      and ``noise_min_scale`` is treated as a minimum fraction of the
      typical transformed uncertainty for the block rather than as an
      absolute floor in normalized-value units.

      When preprocessing metadata is unavailable, a flat Gaussian noise with
      scale ``noise_std`` is applied as a fallback.



   .. py:method:: fit(dataset: Any, *, elbo_log_likelihood_fn: Callable[[jax.numpy.ndarray, Any], jax.numpy.ndarray] | None = None, elbo_num_samples: int = 1) -> petitRADTRANS.sbi.posterior_base.TrainingArtifacts

      Train the posterior on one normalized simulation dataset reader.

      Parameters
      ----------
      dataset:
          Reader-like object that yields :class:`PosteriorBatch` instances via
          ``iter_batches`` and exposes dataset manifest metadata when
          available.
      elbo_log_likelihood_fn:
          Required when ``training_objective='elbo'``. A differentiable
          ``(theta_cube, observations) -> (batch,)`` callable returning the
          Gaussian observational log-likelihood through the forward model. See
          :meth:`_make_elbo_loss`. Ignored for the default NPE objective.
      elbo_num_samples:
          Number of reparameterized posterior draws per observation for the
          ELBO estimator (default 1).

      Returns
      -------
      TrainingArtifacts
          Training history, validation metrics, and trainer metadata for the
          completed optimization run.

      Notes
      -----
      If the reader exposes preprocessing metadata or a manifest fingerprint,
      those are cached on the posterior so they can be saved and reused by the
      inference and artifact layers.



   .. py:method:: _build_estimator_config() -> dict[str, Any]

      Return backend-specific configuration for metadata persistence.



   .. py:method:: _build_serialized_metadata(artifact_metadata) -> dict[str, Any]


   .. py:method:: _resolve_estimator_config(metadata: Mapping[str, Any]) -> dict[str, Any]
      :staticmethod:



   .. py:method:: hydrate_loaded_metadata(metadata: Mapping[str, Any]) -> None


   .. py:method:: from_serialized_metadata(metadata: Mapping[str, Any]) -> ConditionalFlowPosterior
      :classmethod:


      Rebuild an estimator instance from persisted metadata only.



   .. py:method:: save_backend_state(output_path: pathlib.Path) -> None

      Persist backend-specific model state into the output directory.



   .. py:method:: load_backend_state(input_path: pathlib.Path) -> None

      Restore backend-specific model state from the input directory.



   .. py:method:: encode_observation(blocks: list[petitRADTRANS.sbi.observation.ObservationBlock]) -> petitRADTRANS.sbi.observation.EncodedObservation

      Encode one structured observation into the posterior context space.

      Parameters
      ----------
      blocks:
          Observation blocks describing one spectral/photometric observation.

      Returns
      -------
      EncodedObservation
          Aggregated embedding and lightweight metadata describing the input
          observation family.



   .. py:method:: batch_encode_observation(blocks_list: list[list[petitRADTRANS.sbi.observation.ObservationBlock]]) -> list[petitRADTRANS.sbi.observation.EncodedObservation]

      Encode a batch of observations using a single vmapped forward pass.

      Parameters
      ----------
      blocks_list:
          List of per-sample observation block lists.

      Returns
      -------
      list[EncodedObservation]
          One encoded observation per input sample.



   .. py:method:: sample_posterior(observation: petitRADTRANS.sbi.observation.EncodedObservation, n_samples: int, seed: int | None = None) -> petitRADTRANS.sbi.posterior_base.PosteriorSamples

      Draw posterior samples conditioned on one encoded observation.

      Parameters
      ----------
      observation:
          Encoded observation produced by :meth:`encode_observation`.
      n_samples:
          Number of posterior draws to generate.
      seed:
          Optional random seed overriding the model-level default seed.

      Returns
      -------
      PosteriorSamples
          Samples in the posterior's configured parameter space. Non-finite
          outputs are clamped to large finite values for downstream stability.



   .. py:method:: batch_sample_posterior(embeddings: Any, n_samples: int, base_seed: int = 0) -> numpy.ndarray

      Draw posterior samples for a batch of encoded observations.

      Runs a single JIT-compiled vmapped call over all contexts rather than
      looping ``sample_posterior`` once per observation, eliminating the
      Python overhead of per-observation dispatch.

      Parameters
      ----------
      embeddings:
          Float32 array of shape ``(batch_size, embedding_dim)`` produced by
          stacking :attr:`EncodedObservation.embedding` vectors.
      n_samples:
          Number of posterior draws per observation.
      base_seed:
          Base random seed. Each observation in the batch receives a unique
          sub-key derived from this seed.

      Returns
      -------
      np.ndarray
          Array of shape ``(batch_size, n_samples, parameter_dim)`` with
          non-finite values clamped to large finite values.



   .. py:method:: log_prob(observation: petitRADTRANS.sbi.observation.EncodedObservation, parameters: Any) -> Any

      Evaluate posterior log-density for one or many parameter vectors.

      Parameters
      ----------
      observation:
          Encoded observation that defines the posterior context.
      parameters:
          One parameter vector or a batch of parameter vectors in the
          posterior's configured parameter space.

      Returns
      -------
      Any
          Scalar log-density or a vector of log-densities matching the input
          batch structure.



.. py:class:: ConditionalNeuralAutoregressiveFlowPosterior(*args: Any, **kwargs: Any)

   Bases: :py:obj:`ConditionalFlowPosterior`


   Posterior estimator specialized to the neural autoregressive backend.


.. py:class:: ConditionalSplineFlowPosterior(*args: Any, **kwargs: Any)

   Bases: :py:obj:`ConditionalFlowPosterior`


   Posterior estimator specialized to the spline flow backend.

   Notes
   -----
   This convenience subclass forces ``flow_family='spline'`` while keeping the
   rest of the :class:`ConditionalFlowPosterior` API unchanged.


.. py:data:: POSTERIOR_METADATA_SCHEMA_VERSION
   :value: '0.2.0'


.. py:class:: PersistentPosteriorEstimator(parameter_dim: int, parameter_space: str = 'unconstrained', seed: int = 0, task_metadata: Mapping[str, Any] | None = None)

   Bases: :py:obj:`PosteriorEstimator`


   Shared persistence helper for estimator backends with on-disk artifacts.


   .. py:attribute:: estimator_family
      :value: 'persistent_estimator'



   .. py:attribute:: metadata_schema_version
      :value: '0.2.0'



   .. py:attribute:: parameter_dim


   .. py:attribute:: parameter_space
      :value: 'unconstrained'



   .. py:attribute:: seed
      :value: 0



   .. py:attribute:: task_metadata


   .. py:attribute:: training_artifacts
      :type:  TrainingArtifacts | None
      :value: None



   .. py:attribute:: task_name
      :type:  str | None


   .. py:attribute:: task_version
      :type:  str | None
      :value: None



   .. py:attribute:: task_fingerprint
      :type:  str | None
      :value: None



   .. py:attribute:: observation_schema
      :type:  Mapping[str, Any]


   .. py:attribute:: preprocessing_metadata_payload
      :type:  Mapping[str, Any]


   .. py:attribute:: artifact_metadata
      :type:  petitRADTRANS.sbi.artifacts.ArtifactMetadata | None
      :value: None



   .. py:method:: _build_estimator_config() -> dict[str, Any]
      :abstractmethod:


      Return backend-specific configuration for metadata persistence.



   .. py:method:: from_serialized_metadata(metadata: Mapping[str, Any]) -> PersistentPosteriorEstimator
      :classmethod:

      :abstractmethod:


      Rebuild an estimator instance from persisted metadata only.



   .. py:method:: save_backend_state(output_path: pathlib.Path) -> None
      :abstractmethod:


      Persist backend-specific model state into the output directory.



   .. py:method:: load_backend_state(input_path: pathlib.Path) -> None
      :abstractmethod:


      Restore backend-specific model state from the input directory.



   .. py:method:: _load_training_artifacts(metadata: Mapping[str, Any]) -> TrainingArtifacts | None
      :staticmethod:



   .. py:method:: hydrate_loaded_metadata(metadata: Mapping[str, Any]) -> None


   .. py:method:: _build_artifact_metadata_payload() -> dict[str, Any]


   .. py:method:: build_artifact_metadata(version: str) -> petitRADTRANS.sbi.artifacts.ArtifactMetadata

      Assemble registry metadata for the currently trained estimator.



   .. py:method:: _build_serialized_metadata(artifact_metadata: petitRADTRANS.sbi.artifacts.ArtifactMetadata) -> dict[str, Any]


   .. py:method:: save(output_directory: str, artifact_registry: petitRADTRANS.sbi.artifacts.ArtifactRegistry | None = None, artifact_version: str = '0.1.0') -> None

      Persist model weights, metadata, and optional artifact registration.



   .. py:method:: load(input_directory: str) -> PersistentPosteriorEstimator
      :classmethod:


      Restore a saved persistent estimator from disk.



.. py:class:: PosteriorBatch

   Training batch passed to amortized posterior estimators.


   .. py:attribute:: parameters
      :type:  Any


   .. py:attribute:: observations
      :type:  Any


   .. py:attribute:: metadata
      :type:  Mapping[str, Any]


.. py:class:: PosteriorEstimator

   Bases: :py:obj:`abc.ABC`


   Backend-agnostic interface for amortized posterior models.


   .. py:method:: fit(dataset: Any) -> TrainingArtifacts
      :abstractmethod:


      Train the posterior estimator on a simulation dataset.



   .. py:method:: encode_observation(blocks: list[petitRADTRANS.sbi.observation.ObservationBlock]) -> petitRADTRANS.sbi.observation.EncodedObservation
      :abstractmethod:


      Encode a structured observation into the estimator input space.



   .. py:method:: sample_posterior(observation: petitRADTRANS.sbi.observation.EncodedObservation, n_samples: int, seed: int | None = None) -> PosteriorSamples
      :abstractmethod:


      Sample the amortized posterior for one encoded observation.



   .. py:method:: log_prob(observation: petitRADTRANS.sbi.observation.EncodedObservation, parameters: Any) -> Any
      :abstractmethod:


      Evaluate posterior log-density when supported by the backend.



   .. py:method:: save(output_directory: str) -> None
      :abstractmethod:


      Persist trained model weights and metadata.



   .. py:method:: load(input_directory: str) -> PosteriorEstimator
      :classmethod:

      :abstractmethod:


      Restore a saved estimator from disk.



.. py:class:: PosteriorSamples

   Posterior samples and optional per-sample diagnostics.


   .. py:attribute:: samples
      :type:  Any


   .. py:attribute:: log_probabilities
      :type:  Any
      :value: None



   .. py:attribute:: weights
      :type:  Any
      :value: None



   .. py:attribute:: metadata
      :type:  Mapping[str, Any]


.. py:class:: TrainingArtifacts

   Structured outputs of a posterior training run.


   .. py:attribute:: history
      :type:  Mapping[str, Any]


   .. py:attribute:: validation_metrics
      :type:  Mapping[str, Any]


   .. py:attribute:: metadata
      :type:  Mapping[str, Any]


