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

Restructure optimisers to enable passing of kwargs #255

Merged
merged 95 commits into from
May 22, 2024

Conversation

NicolaCourtier
Copy link
Member

@NicolaCourtier NicolaCourtier commented Mar 23, 2024

Description

Enable valid keyword arguments to be passed through the set-up of an Optimisation class and restructure the optimiser classes so that all PINTS and SciPy optimisers are subclasses of the same BaseOptimiser.

Issue reference

Fixes #236.

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.
  • 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 docs

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.

Copy link

codecov bot commented Mar 23, 2024

Codecov Report

Attention: Patch coverage is 98.31461% with 6 lines in your changes missing coverage. Please review.

Project coverage is 96.93%. Comparing base (0d0e214) to head (6b37c3f).
Report is 444 commits behind head on develop.

Files Patch % Lines
pybop/optimisers/scipy_optimisers.py 95.45% 4 Missing ⚠️
pybop/optimisers/base_optimiser.py 97.14% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #255      +/-   ##
===========================================
+ Coverage    95.81%   96.93%   +1.12%     
===========================================
  Files           39       41       +2     
  Lines         2101     2222     +121     
===========================================
+ Hits          2013     2154     +141     
+ Misses          88       68      -20     

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

Copy link
Member

@BradyPlanden BradyPlanden left a comment

Choose a reason for hiding this comment

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

Well done @NicolaCourtier - thanks for the addition!

I've added a few comments for you to look at. One addition is to update optimiser.needs_sensitivities depending on self.options["method"]. If not added in this PR, we should open an issue to add gradient support for the scipy optimisers.

Also, we should probably align the name of the bounds variable between the pints and the scipy optimisers. This can also be done in a separate PR.

pybop/_optimisation.py Outdated Show resolved Hide resolved
pybop/optimisers/scipy_optimisers.py Outdated Show resolved Hide resolved
pybop/_optimisation.py Outdated Show resolved Hide resolved
pybop/optimisers/scipy_optimisers.py Outdated Show resolved Hide resolved
@NicolaCourtier
Copy link
Member Author

This PR now includes changes to the PINTS interface as well as SciPy and introduces a true BaseOptimiser. Addressing @BradyPlanden's points above:

  1. Added gradient support for the SciPy optimisers, via the jac option. Note that SciPy optimisers do not need the needs_sensitivities property.

  2. The same name bounds is used for all optimisers, although more could be done to align the bounds type.

  3. Added update_options to the BasePintsOptimiser to unpack the optimiser_kwargs and try to apply them to the corresponding optimiser variables, with a final catch for unexpected keywords.

Note that this update nests the PINTS optimiser within the optimiser property of an Optimisation object... so I'm open to discussion around these changes.

Copy link
Member

@BradyPlanden BradyPlanden left a comment

Choose a reason for hiding this comment

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

I thought I would add a review on this even though it's not marked ready. It's a great step for the optimisers. There are a few areas I think we can improve, but thanks for the addition @NicolaCourtier!

examples/scripts/spm_NelderMead.py Outdated Show resolved Hide resolved
examples/scripts/spm_adam.py Outdated Show resolved Hide resolved
examples/scripts/spm_descent.py Outdated Show resolved Hide resolved
pybop/_optimisation.py Outdated Show resolved Hide resolved
pybop/optimisers/base_optimiser.py Outdated Show resolved Hide resolved
pybop/optimisers/base_optimiser.py Outdated Show resolved Hide resolved
pybop/optimisers/pints_optimisers.py Outdated Show resolved Hide resolved
pybop/optimisers/pints_optimisers.py Show resolved Hide resolved
pybop/optimisers/pints_optimisers.py Outdated Show resolved Hide resolved
pybop/optimisers/pints_optimisers.py Outdated Show resolved Hide resolved
NicolaCourtier and others added 9 commits April 22, 2024 18:52
Co-authored-by: Brady Planden <55357039+BradyPlanden@users.noreply.github.com>
Co-authored-by: Brady Planden <55357039+BradyPlanden@users.noreply.github.com>
Co-authored-by: Brady Planden <55357039+BradyPlanden@users.noreply.github.com>
Copy link
Member

@BradyPlanden BradyPlanden left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the additions @NicolaCourtier!

@NicolaCourtier NicolaCourtier changed the title Enable passing of kwargs to optimisers Restructure optimisers to enable passing of kwargs May 22, 2024
BradyPlanden and others added 6 commits May 22, 2024 16:45
* Splits Optimisation -> BaseOptimiser/Optimisation, enables two optimisation APIs, updts where required, moves _optimisation to optimisers/

* increase coverage

* Pass optimiser_kwargs though run()

* updt examples

* Converts DefaultOptimiser -> Optimisation

* split Optimisation and BaseOptimsier classes, loosen standalone cost unit test

* add incorrect attr test

* fix: updt changelog entry, optimsation_interface notebook, review suggestions

* fix: updt notebook state

* Updt assertions, optimisation object name

---------

Co-authored-by: NicolaCourtier <45851982+NicolaCourtier@users.noreply.github.com>
@NicolaCourtier NicolaCourtier merged commit 9e0058f into develop May 22, 2024
29 checks passed
@NicolaCourtier NicolaCourtier deleted the 236-scipy-kwargs branch May 22, 2024 20:32
BradyPlanden added a commit that referenced this pull request Jun 3, 2024
* Enable passing of kwargs to SciPy

* Update checks on bounds

* Add update_options and error for Pints

* Rename opt to optim

* Remove stray comments

* Add BaseSciPyOptimiser and BasePintsOptimiser

* Align optimiser option setting

* Update notebooks with kwargs

* Update scripts with kwargs

* Update notebooks

* Align optimisers with Optimisation as base class

* Update stopping criteria in spm_NelderMead.py

Co-authored-by: Brady Planden <55357039+BradyPlanden@users.noreply.github.com>

* Update stopping criteria in spm_adam.py

Co-authored-by: Brady Planden <55357039+BradyPlanden@users.noreply.github.com>

* Update sigma0 in spm_descent.py

Co-authored-by: Brady Planden <55357039+BradyPlanden@users.noreply.github.com>

* Update GradientDescent

* Change update to set and check pints_method

* Update test_optimisation_options

* Update notebooks

* Update set learning rate

* Pop threshold

* Fix bug in model.simulate

* Update notebooks

* Update test_models.py

* Store SciPy result

* Update x0 input and add tests

* Update bounds to avoid x0 outside

* Re-initialise pints_method on certain options

* Update x0_new test

* Update test_optimisation.py

* Create initialise_method for PINTS optimisers

* Align optimisation result

* Update checks on bounds

* Apply suggestions

Co-authored-by: Brady Planden <55357039+BradyPlanden@users.noreply.github.com>

* Add standalone optimiser

* Simplify optimiser set-up and align _minimising

* Update option setting in notebooks

* Take abs of cost0

* Implement suggestions from Brady

* Update tests and base option setting

* Update test_invalid_cost

* Increase coverage

* Sort out notebook changes

* Reset scale parameter

* Move settings into arguments

* Update comments

* Update optimiser call

* Move check on jac

* Add assertions

* Add maxiter to test

* Add assertion

* Update to lambda functions

Co-authored-by: Brady Planden <55357039+BradyPlanden@users.noreply.github.com>

* Update comment

Co-authored-by: Brady Planden <55357039+BradyPlanden@users.noreply.github.com>

* Update to list comprehension

Co-authored-by: Brady Planden <55357039+BradyPlanden@users.noreply.github.com>

* Formatting

* Revert "Update to lambda functions"

This reverts commit aa73bff.

* Move minimising out of costs

* Update description

* Updates to #236 to avoid breaking change to `pybop.Optimisation` (#309)

* Splits Optimisation -> BaseOptimiser/Optimisation, enables two optimisation APIs, updts where required, moves _optimisation to optimisers/

* increase coverage

* Pass optimiser_kwargs though run()

* updt examples

* Converts DefaultOptimiser -> Optimisation

* split Optimisation and BaseOptimsier classes, loosen standalone cost unit test

* add incorrect attr test

* fix: updt changelog entry, optimsation_interface notebook, review suggestions

* fix: updt notebook state

* Updt assertions, optimisation object name

---------

Co-authored-by: NicolaCourtier <45851982+NicolaCourtier@users.noreply.github.com>

* Rename method to pints_optimiser

* Rename base_optimiser to pints_base_optimiser

* Rename _optimisation to base_optimiser

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Brady Planden <55357039+BradyPlanden@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocker An issue which must be resolved before work can resume on other issues.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pass kwargs to SciPy optimisers
2 participants