-
-
Notifications
You must be signed in to change notification settings - Fork 589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix a bug where the usage of the CasADi solver breaks reproducibility #4847
Fix a bug where the usage of the CasADi solver breaks reproducibility #4847
Conversation
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
This is a user-facing change as it fixes is/was a slight bug in the solver functionality, but I don't think the fix warrants adding a CHANGELOG entry. What do you think, @kratman? |
I have not looked at the code yet, but from your description I don't think it would need one |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## develop #4847 +/- ##
========================================
Coverage 98.70% 98.70%
========================================
Files 303 303
Lines 23335 23338 +3
========================================
+ Hits 23032 23035 +3
Misses 303 303 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I remember this. Thanks for the fix, @agriyakhetarpal!
I re-triggered the failing Windows test, as it looks like a flake that has been showing up a bit more often recently:
|
Yeah I have not fully debugged that issue yet. It started appearing after I enabled the IDAKLU tests on windows. The issue was probably there for a while, but not caught. I am hoping I end up tracking it down as I keep working on the PyBaMM solvers build/test issues |
Description
This is an issue that I highlighted during our presence at the PyBaMM conference around a week ago: in #3494 (which closed #3415), I introduced a bug wherein a NumPy random seed was set when using the
CasadiSolver
to solve a simulation. It is discouraged to add a random seed in library/module code – it breaks reproducibility by overriding randomness that may have been set by user-level code in researchers' scripts and simulations (it is acceptable for test code, though).As the PR was supposed to fix stochastic behaviour for the hall-cell model, I have moved the random seed to the notebook instead which used to fail at that time in the repository's development. Usually, this should be present for all notebooks, but one should be enough for now to keep the diff minimal.
Another potential fix is to look at the
globals()
andlocals()
to find if a seed has been set already and use that, but that seems to be a bit over-engineered.Type of change
Please add a line in the relevant section of CHANGELOG.md to document the change (include PR #)
Important checks
Please confirm the following before marking the PR as ready for review:
nox -s pre-commit
nox -s tests
nox -s doctests
Additional context
Stop Using NumPy's Global Random Seed – while the article mentions using
np.random.default_rng()
is a better way,np.random.seed()
should be sufficient for now. The rationale is that we don't want to be one of those packages that resets the global random seed set by a user at the top of their script (or by another library that sets it).