Add explicit init() to BasePotential and NeuralPosterior#1829
Add explicit init() to BasePotential and NeuralPosterior#1829patelshivani2283-lab wants to merge 7 commits intosbi-dev:mainfrom
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1829 +/- ##
==========================================
- Coverage 88.54% 86.28% -2.27%
==========================================
Files 137 143 +6
Lines 11515 17808 +6293
==========================================
+ Hits 10196 15365 +5169
- Misses 1319 2443 +1124
Flags with carried forward coverage won't be shown. Click here to find out more.
|
|
Hi! This PR is ready for review. |
JiwaniZakir
left a comment
There was a problem hiding this comment.
The hasattr(self.potential_fn, "init") guard in base_posterior.py's init() is now vacuously true since BasePotential always defines init() — this check no longer provides meaningful protection and will silently do the wrong thing if a non-BasePotential potential function is ever passed that lacks init(). The lazy auto-initialization in both potential() (base_posterior.py line 104–105) and __call__ (base_potential.py line 144–145) also undermines the stated goal of explicit initialization: callers who skip init() will never notice, making the explicit method mostly ceremonial rather than enforcing a required lifecycle step. If the intent is truly to require initialization before use, consider raising an error instead of silently initializing, or dropping the explicit init() pattern entirely in favor of always-lazy init. Additionally, if init() is designed to be overridden in subclasses (e.g., to load model weights or move tensors to a device), the implicit call inside __call__ could silently swallow expensive repeated work if _initialized is somehow reset; a more defensive pattern would be to only expose the lazy path and remove the public init(), or vice versa — pick one and be consistent across both files.
|
Thanks for the feedback! I initially enforced explicit initialization by raising an error, but this caused failures in existing tests. I've now reverted to lazy initialization to preserve backward compatibility, while still supporting explicit init(). I also removed the hasattr(...) check as suggested. Happy to adjust further if a stricter approach is preferred. |
|
Thanks for the detailed feedback! I initially tried enforcing explicit initialization by raising an error, To keep things backward compatible, I restored lazy initialization while I also removed the hasattr(...) check as suggested. Please let me know if you'd prefer moving fully to explicit initialization |
|
Hi @patelshivani2283-lab, thanks for the iteration on this! I think @JiwaniZakir's point is right: with the lazy auto-init in But more generally, the underlying design question belongs to #1783 and our GSoC 2026 P3 project, which will redesign the potential function API end-to-end (replacing the current Thus, I'll close this PR, but would be happy to see your work on #1828! Thanks for your efforts 🙏 |
Summary
Added an
_initializedflag and aninit()method toBasePotentialandNeuralPosterior.What changed
You can now explicitly initialize the posterior or potential by calling
init().At the same time, things will still work as before if
init()is not called.Why
In some cases, using the posterior or potential without proper initialization could lead to subtle issues.
This change makes initialization clearer and more explicit, while keeping everything backward compatible.
Example