Skip to content

docs: improve logging during posterior sampling #1403

Closed

Description

@janfb

At the moment our command line logging during posterior sampling is not consistent:

for NPE / direct posteriors which we usually sample using accept and reject sampling within prior bounds, we just show the number of posterior samples:

pbar = tqdm(
disable=not show_progress_bars,
total=num_samples,
desc=f"Drawing {num_samples} posterior samples",
)

for MCMC based posteriors, we do

if self.verbose:
pbar = tqdm(
range(self.num_chains * (num_samples + self.tuning)),
desc=f"Running vectorized MCMC with {self.num_chains} chains",
)

Problems:

  1. this is inconsistent and should have more information for the user.
  2. the logging can be confusing when using batched sampling because it just shows the number of samples (NPE, no info about the number of x in batch) or the number of chains (MCMC, no info about how many xs, how many samples per x).

see also #1388

One idea would be to have a function that builds an logging message given num samples, num x, num chains etc, and to re-use it across sampling algorithms.

Activity

  1. added
    hackathon
    on Feb 21, 2025
  2. added
    docs
    on Feb 24, 2025
  3. Pratyusha-DS13 commented on Mar 22, 2026

    @Pratyusha-DS13

    Hi! I’d like to work on this issue.

    From my understanding, the goal is to make logging during posterior sampling more consistent and informative across different methods.

    To start, I was thinking of:

    • Identifying inconsistencies in current logging messages
    • Proposing clearer and more uniform logging outputs
    • Potentially refactoring smaller parts before attempting larger changes

    Does this approach sound good, or would you recommend focusing on a specific part first?

    Thanks!

  4. janfb commented on Mar 23, 2026

    @janfb
    ContributorAuthor

    Hi @Pratyusha-DS13 , yes, this sounds like a good general approach to these kind of issues.

    You are welcome to look into this, let's continue discussing the problem and a potential fix here under the issue.

  5. Pratyusha-DS13 commented on Mar 23, 2026

    @Pratyusha-DS13

    After reading through the code, here's a fuller picture of the current logging across all slice samplers:

    • SliceSampler.gen() — trange with "Generating samples" (no count, no context)
    • SliceSampler._tune_bracket_width() — trange with "Tuning bracket width..." (fine as-is)
    • SliceSamplerSerial.run() — shows num_chains and num_workers, but not num_samples
    • SliceSamplerVectorized.run() — shows num_chains only, missing num_samples and num_samples_per_chain

    My proposed fix is a small shared helper, e.g.:

    def get_sampling_description(
        num_samples: int,
        num_chains: Optional[int] = None,
        num_workers: Optional[int] = None,
    ) -> str:
        desc = f"Drawing {num_samples} posterior samples"
        if num_chains is not None:
            desc += f" | {num_chains} chain(s)"
        if num_workers is not None:
            desc += f", {num_workers} worker(s)"
        return desc

    Then each sampler calls this instead of its own string. This keeps things consistent without a big refactor.

    A couple of questions:

    1. Should SliceSampler.gen() also show n_samples in its description, or is the trange total sufficient there?
    2. Is there a preferred place for this helper — sbi/utils/logging.py, or somewhere closer to the samplers?

    Happy to open a draft PR with these changes if the direction looks good.

  6. added
    samplers
    on Aug 11, 2026
  7. janfb commented on Sep 30, 2026

    @janfb
    ContributorAuthor

    Hi @Pratyusha-DS13, thanks for the careful look at the slice sampler bars, and sorry that your proposal stayed without a reply for so long.

    In the meantime, PR #1922 reworked the progress bars of most samplers, so the code changed quite a bit. Your survey was useful: it showed that the MCMC case needs information that only the MCMC posterior has, namely the samples and chains per observation for batched sampling. The fix touched these posterior internals, so I ended up implementing it myself together with the other samplers.

    The PR is #2023. It uses one shared helper for all samplers, very close to what you suggested. Thanks again for your input here.

    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

    docshackathonsamplers

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions