Skip to content

Refactor NeuralPosterior interface #1407

Open

Description

@janfb

There seem to be a couple of problems with the NeuralPosterior class as the main interface for all the posterior classes:

  • it is treated as an abstract class, but it is not declared as such, e.g., what happens if one instantiates it and then calls sample?
  • the doc string says it has a log_prob but it does not
  • for a abstract base class, it has a lot of logic happening in this class, e.g., all the x-handling
  • sample_batched and sample are quite related but have different signatures, e.g., the order differs.

How to fix this (?):

  • get an overview how this class is used in the overall class structure
  • define as a pure abstract class and move logic about x somewhere else, e.g., have a separate abstraction of x (Observation)?

@schroedk will probably be very helpful for working on this.

Activity

  1. added
    API changes
    architecture
    bug
    hackathon
    on Feb 24, 2025
  2. added
    posteriors
    on Aug 11, 2026
  3. BHARATH0153 commented on Oct 18, 2026

    @BHARATH0153
    Contributor

    @janfb went through the current code and all four points still hold, though some have moved:
    NeuralPosterior (base_posterior.py:39) is not an ABC. It uses abstractmethod on sample (:133) and sample_batched (:157) but does not inherit from abc.ABC, so the decorators are inert. I checked at runtime: instantiating it succeeds, and you only get a confusing error later when the method is actually called.
    The class docstring (:40) advertises log_prob(), but the base class has no such method. Every concrete posterior implements it (Direct, MCMC, VI, Rejection, Importance, Ensemble, VectorField, FilteredDirect; NPE_A inherits Direct), so the base is simply lying about its interface.
    sample and sample_batched still disagree on parameter order. Base declares sample(sample_shape, x=None, show_progress_bars=True, **kwargs), while DirectPosterior.sample (:146) inserts max_sampling_batch_size, reject_outside_prior, max_sampling_time, return_partial_on_timeout before show_progress_bars, and MCMCPosterior.sample (:237) inserts method, thin, warmup_steps, num_chains, ... So positional calls are not portable across posterior types.
    x-handling still sits in the base (_x, set_default_x, get_default_x, _x_else_default_x, _assert_finite_x, potential()), mixing the abstract interface with concrete logic.

    How I would fix it (incrementally, so nothing breaks):
    Make it a real ABC: class NeuralPosterior(ABC) and add abstractmethod def log_prob(self, theta, x=None, track_gradients=False). Leave log_prob_batched non-abstract for now, since only Direct and FilteredDirect implement it; a default that raises NotImplementedError would keep the other subclasses working.
    Keep the two abstract sampling methods, but document that subclasses must accept their extra arguments as keywords, not rely on a fixed positional order.
    Move the x-handling into a small mixin or helper (e.g. a PosteriorObservationHandler) so the ABC stays interface-only and the logic is reusable. This keeps _x_else_default_x and friends available to subclasses without bloating the base.
    Add tests: instantiating the base raises TypeError, and every concrete posterior exposes the abstract methods.

  4. janfb commented on Oct 29, 2026

    @janfb
    ContributorAuthor

    @BHARATH0153 thanks for checking these points against the current code. The clean-up of the posterior interface belongs to the posterior refactoring I am working on (PR #2104 for device handling, issue #1994 for binding observations), so this issue is not open for a PR.

    cheers, Jan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    API changesarchitecturebughackathonposteriors

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions