docs: added code examples in potential method docstring#1820
docs: added code examples in potential method docstring#1820Jocho-Smith wants to merge 3 commits intosbi-dev:mainfrom
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1820 +/- ##
=======================================
Coverage 87.83% 87.83%
=======================================
Files 140 140
Lines 12958 12958
=======================================
Hits 11382 11382
Misses 1576 1576
Flags with carried forward coverage won't be shown. Click here to find out more. |
|
I could also do that for the methods listed here: https://sbi.readthedocs.io/en/latest/api_reference/potentials.html |
dgedon
left a comment
There was a problem hiding this comment.
I believe this is a very good start for the GSoC 2026 project 3 to get an overview. However, there are some issues:
- in the API reference we list the
likelihood_estimator_based_potentialfunction instead of theLikelihoodBasedPotentialwhere your current examples are located. So they are never shown in the API reference - more critical: the potentials are mostly used internally and have limited user-facing interaction. So examples have limited value
- With the re-design of the potential function, these examples would need a re-design as well.
Because of the second point, I think this PR is nice-to-have.
Changes that are necessary:
- add examples to the right location
- point out that the potentials can then be used for MCMC sampling, VI, ...
|
Hello @dgedon, thanks for correcting me on this one! Looking back, I could have noticed this myself. I understand your suggested changes and will implement them as soon as possible. |
|
Sorry for the long inactivity - I'll finally finish this, this week! |
|
However, there is a deprecation warning in the code from 2 years ago. When I try to use it in a minimal NLE example it fails (to me it looks like torch incompatibility) code: import torch
from sbi.inference import NLE, MCMCPosterior, mixed_likelihood_estimator_based_potential
from sbi.utils import BoxUniform
prior = BoxUniform(low=torch.zeros(3), high=torch.ones(3))
theta = prior.sample((100,))
x = theta + torch.randn_like(theta) * 0.1
x_o = torch.randn(1, 3)
trainer = NLE()
likelihood_estimator = trainer.append_simulations(theta, x).train()
potential_fn, parameter_transform = mixed_likelihood_estimator_based_potential(
likelihood_estimator, prior, x_o
)
posterior = MCMCPosterior(
potential_fn, proposal=prior, theta_transform=parameter_transform, warmup_steps=10
)output: The same example code worked without a problem for LikelihoodBasedPotential. Now the question is where to go from here. Should I just ignore it for the example doc string? Or is there some symbiosis to my main gsoc project if I remove it from the codebase or even try to fix it? |
This PR is inspired by #1763
In preparation of GSoC Project Idea 3 I investigated into the potential functions and wrote minimal examples to try them out.
Is it appreciated if we add these examples in the class docstrings? In the same way it appeared in #1534, this would enhance API references on the docs website.
Pre-commit hooks and fast tests passed
(update: I just realized I could have just run
pytest -m "not slow and not gpu", there was no need for--bm)