Skip to content
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

Fixes #320 #321

Merged
merged 15 commits into from
May 17, 2024
Merged

Fixes #320 #321

merged 15 commits into from
May 17, 2024

Conversation

BradyPlanden
Copy link
Member

@BradyPlanden BradyPlanden commented May 10, 2024

Description

This PR updates the test_parameterisation.py integration tests. This is completed by hyper-parameter updates for the extended synthetic data and a change to the parameter priors to remove incorrect biasing to the MAP cost function.

In addition, it adds a prior resampling method to improve pybop.SciPyMinimize stability. This avoids failing test when allow_infeasible_solutions == False as x0 will be resampling when possible. To achieve this, the pybop.Prior classes have been refactored through the addition of a base class pybop.BasePrior.

Issue reference

Fixes #320

Review

Before you mark your PR as ready for review, please ensure that you've considered the following:

  • Updated the CHANGELOG.md in reverse chronological order (newest at the top) with a concise description of the changes, including the PR number.
  • Noted any breaking changes, including details on how it might impact existing functionality.

Type of change

  • New Feature: A non-breaking change that adds new functionality.
  • Optimization: A code change that improves performance.
  • Examples: A change to existing or additional examples.
  • Bug Fix: A non-breaking change that addresses an issue.
  • Documentation: Updates to documentation or new documentation for new features.
  • Refactoring: Non-functional changes that improve the codebase.
  • Style: Non-functional changes related to code style (formatting, naming, etc).
  • Testing: Additional tests to improve coverage or confirm functionality.
  • Other: (Insert description of change)

Key checklist:

  • No style issues: $ pre-commit run (or $ nox -s pre-commit) (see CONTRIBUTING.md for how to set this up to run automatically when committing locally, in just two lines of code)
  • All unit tests pass: $ nox -s tests
  • The documentation builds: $ nox -s doctest

You can run integration tests, unit tests, and doctests together at once, using $ nox -s quick.

Further checks:

  • Code is well-commented, especially in complex or unclear areas.
  • Added tests that prove my fix is effective or that my feature works.
  • Checked that coverage remains or improves, and added tests if necessary to maintain or increase coverage.

Thank you for contributing to our project! Your efforts help us to deliver great software.

@BradyPlanden BradyPlanden linked an issue May 10, 2024 that may be closed by this pull request
Copy link

codecov bot commented May 10, 2024

Codecov Report

Attention: Patch coverage is 98.30508% with 1 line in your changes missing coverage. Please review.

Project coverage is 95.81%. Comparing base (7a9bcc4) to head (0d0e214).
Report is 423 commits behind head on develop.

Files Patch % Lines
pybop/optimisers/scipy_optimisers.py 88.88% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #321      +/-   ##
===========================================
- Coverage    95.85%   95.81%   -0.04%     
===========================================
  Files           39       39              
  Lines         2145     2101      -44     
===========================================
- Hits          2056     2013      -43     
+ Misses          89       88       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Adds a method to sample initial points within the
problem class, enabling a resample method within
SciPy.Minimize for an infinite initial cost, and
updating prior classes to have a shared base.
Add tests for the prior cls update and SciPyMinimize resampling.
Also updates sigma0 for GradientDescent and loosens assertion
@@ -25,6 +25,7 @@ def __init__(self, method=None, bounds=None, maxiter=None, tol=1e-5):
super().__init__()
self.method = method
self.bounds = bounds
self.num_resamples = 40
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is intentionally left without an set/get as #255 enables kwargs to this class, which will be added once that is merged.

@BradyPlanden
Copy link
Member Author

This is now ready for review, it appears to solve the test_on_push workflow, but scheduled is still breaking for pybop.GradientDescent and pybop.SciPyMinimize. These methods are randomly unstable, perhaps #255 will help us calibrate pybop.SciPyMinimize with the updated interface.

This PR splits the test_spm_optimisersassertions dependent on the the pybamm version and the optimiser. This gives us a better signal for performance regressions

Copy link
Member

@NicolaCourtier NicolaCourtier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @BradyPlanden - I've added a few small comments to be addressed, as well as some notes for myself regarding future updates.

if np.isinf(self.cost0):
raise Exception("The initial parameter values return an infinite cost.")
for i in range(1, self.num_resamples):
x0 = cost_function.problem.sample_initial_conditions(seed=i)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be updated in #322 to work with costs without a problem.

pybop/parameters/priors.py Outdated Show resolved Hide resolved
pybop/parameters/priors.py Show resolved Hide resolved
elif len(x0) != self.n_parameters:
# Set initial conditions
if self.x0 is None:
self.x0 = self.sample_initial_conditions()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be updated in #322

raise ValueError("x0 dimensions do not match number of parameters")

# Add the initial values to the parameter definitions
for i, param in enumerate(self.parameters):
param.update(initial_value=self.x0[i])

def sample_initial_conditions(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be removed as part of #322 when sampling is implemented on the new Parameters class

tests/unit/test_optimisation.py Outdated Show resolved Hide resolved
@NicolaCourtier
Copy link
Member

One more: please add a Changelog entry - thanks!

BradyPlanden and others added 5 commits May 15, 2024 16:10
This commit reduces the possible variance of solutions for the
parameterisation test, it also updates hyper-parameters, defends
against IC samples that equal the ground truth solution. The
priors are also reduced to improve robustness
@BradyPlanden
Copy link
Member Author

BradyPlanden commented May 17, 2024

Ok, this is a step in the right direction. The scheduled tests are much closer to passing with the addition of the flaky plugin and moving the flakiest tests to a Thevenin parameterisation test. I've triggered the CI three times on this PR and it's passed so far. I suspect we will get some sporadic failures after this merge, as shown on the scheduled CI above, but this will be a big improvement over the current state.

@NicolaCourtier if you want to take another look that would be great, otherwise I'll merge as is with a name change to test_parameterisation.py for consistency.

@NicolaCourtier
Copy link
Member

Thanks @BradyPlanden ! Let's proceed with the name change and merge as it looks like a significant improvement.

@BradyPlanden BradyPlanden merged commit e149cfb into develop May 17, 2024
29 checks passed
@BradyPlanden BradyPlanden deleted the 320-bug-fix-integration-tests-post-304 branch May 17, 2024 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Fix Integration tests post #304
2 participants