Skip to content

Add parameters class#322

Merged
NicolaCourtier merged 52 commits into
developfrom
258-parameters-class
Jun 5, 2024
Merged

Add parameters class#322
NicolaCourtier merged 52 commits into
developfrom
258-parameters-class

Conversation

@NicolaCourtier

Copy link
Copy Markdown
Member

Description

Add the Parameters class to store and access multiple parameters in one object.

Issue reference

Fixes #258

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.

@codecov

codecov Bot commented May 10, 2024

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 97.57282% with 5 lines in your changes missing coverage. Please review.

Project coverage is 97.13%. Comparing base (877abbc) to head (fbb4b23).
Report is 619 commits behind head on develop.

Files with missing lines Patch % Lines
pybop/parameters/parameter.py 97.24% 3 Missing ⚠️
pybop/optimisers/base_pints_optimiser.py 96.15% 1 Missing ⚠️
pybop/problems/base_problem.py 91.66% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #322      +/-   ##
===========================================
+ Coverage    95.55%   97.13%   +1.58%     
===========================================
  Files           40       41       +1     
  Lines         2203     2305     +102     
===========================================
+ Hits          2105     2239     +134     
+ Misses          98       66      -32     

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

@NicolaCourtier NicolaCourtier marked this pull request as ready for review May 13, 2024 13:57
@NicolaCourtier

Copy link
Copy Markdown
Member Author

In this implementation, I have kept the pybop.Parameter class and added the pybop.Parameters class.

I think it would be good to move towards a solution where the user only needs to interact with pybop.Parameters, since having the two similarly named classes is confusing. One suggestion would be to replace pybop.Parameter with pybop.InternalParameter - a class that provides the same functionality but we would not expect the user to interact with it.

@NicolaCourtier

Copy link
Copy Markdown
Member Author

All tests should pass after #321 is merged.

@NicolaCourtier NicolaCourtier changed the title 258 parameters class Add parameters class May 13, 2024
@NicolaCourtier NicolaCourtier mentioned this pull request May 15, 2024
15 tasks

@BradyPlanden BradyPlanden left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is looking very good @NicolaCourtier. I've left some suggestions and comments for you to consider.

Comment thread pybop/costs/_likelihoods.py Outdated
raise ValueError("Sigma must be positive")
else:
self.sigma0 = sigma
self.sigma = sigma

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

For consistency, we should move these to all be self.sigma0 instead of self.sigma. I was loose with this initial creation, but they should be consistent with the rest of the codebase. The same goes for the sigma variables.

Comment thread pybop/parameters/parameter.py Outdated
Comment thread pybop/parameters/parameter.py Outdated

self.update_bounds()

def update_bounds(self):

@BradyPlanden BradyPlanden May 30, 2024

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If we add an optional arg here and inspect it on entry, we can add functionality to either iterate over the entire dict and update bounds, or just for the arg (parameter) passed. This would be helpful for the above update_bounds and remove_bounds, as they currently iterate over the parameters on order O(N!) O(n(n+1)/2).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I've switched update_bounds to the more accurate get_bounds and removed the property bounds on the Parameters class. We could implement an iterative set_bounds in future but for now the functionality of set_bounds relates to an individual Parameter.

Comment thread pybop/parameters/parameter.py
Comment thread tests/unit/test_cost.py
Comment thread tests/integration/test_spm_parameterisations.py
Comment thread pybop/costs/base_cost.py
@property
def n_parameters(self):
return self._n_parameters
return len(self.parameters)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The update to n_parameters across the codebase has us recalculating len(self.parameters) quite a bit. I think this is fine since len(self.parameters) is inheritantly very small, but it's worth noting for the future that this could be an issue with larger objects (or if self.parameters) becomes very large.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We should consider moving the property onto the Parameters class

Comment thread pybop/optimisers/base_pints_optimiser.py Outdated
sigma0.append(param.prior.sigma)
else:
all_have_sigma = False
if not all_have_sigma:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I wonder if a better implementation would be to insert either Nan or np.inf and return the list. This would ensure that we get a value ofsigma0 for all defined parameters.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good idea, let's implement and test this in another PR

@BradyPlanden BradyPlanden left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just one comment, otherwise LGTM! Thanks @NicolaCourtier

Comment thread pybop/costs/_likelihoods.py Outdated
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.

Update Parameter class to store and access multiple parameters

2 participants