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

Discover Parameter Sets using Entry Points #2396

Merged
merged 12 commits into from
Oct 25, 2022
Merged

Discover Parameter Sets using Entry Points #2396

merged 12 commits into from
Oct 25, 2022

Conversation

awadell1
Copy link
Contributor

Description

Enables pybamm to discover parameter sets via the pybamm_parameter_set entry point:

  • Third parties can now distribute parameter sets (via python packages) that are automatically detected by PyBamm
  • Enables a decentralized approach to parameter sharing
  • External parameter sets can be version controlled

As a bonus, updated pybamm.Citations to support registering BibTex strings, as external parameter sets don't have access to pybamm/CITATIONS.txt

cfcb589: This is mostly superseded by c2be58d, which uses jinja + pybamm.parameter_sets.get_docstring to generate the docstrings for all parameter sets. On the plus side, this supports all the fun sphinx documentation features. The downside the current doc strings are not formatted as reStructuredText. I'm happy to fix that but wanted to get the ball rolling first.

Type of change

Please add a line in the relevant section of CHANGELOG.md to document the change (include PR #) - note reverse order of PR #s. If necessary, also add to the list of breaking changes.

  • New feature (non-breaking change which adds functionality)
  • Optimization (back-end change that speeds up the code)
  • Bug fix (non-breaking change which fixes an issue)

Key checklist:

  • No style issues: $ flake8
  • All tests pass: $ python run-tests.py --unit
  • The documentation builds: $ cd docs and then $ make clean; make html

You can run all three at once, using $ python run-tests.py --quick.

Further checks:

  • Code is commented, particularly in hard-to-understand areas
  • Tests added that prove fix is effective or that feature works

@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@codecov
Copy link

codecov bot commented Oct 21, 2022

Codecov Report

Base: 99.66% // Head: 99.72% // Increases project coverage by +0.05% 🎉

Coverage data is based on head (5cff60e) compared to base (c9e2aca).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #2396      +/-   ##
===========================================
+ Coverage    99.66%   99.72%   +0.05%     
===========================================
  Files          269      268       -1     
  Lines        20190    20149      -41     
===========================================
- Hits         20123    20093      -30     
+ Misses          67       56      -11     
Impacted Files Coverage Δ
pybamm/input/parameters/lead_acid/Sulzer2019.py 100.00% <ø> (ø)
pybamm/input/parameters/lithium_ion/Ai2020.py 100.00% <ø> (ø)
pybamm/input/parameters/lithium_ion/Chen2020.py 100.00% <ø> (ø)
...input/parameters/lithium_ion/Chen2020_composite.py 100.00% <ø> (ø)
pybamm/input/parameters/lithium_ion/Ecker2015.py 100.00% <ø> (ø)
pybamm/input/parameters/lithium_ion/Marquis2019.py 100.00% <ø> (ø)
pybamm/input/parameters/lithium_ion/Mohtat2020.py 100.00% <ø> (ø)
pybamm/input/parameters/lithium_ion/NCA_Kim2011.py 100.00% <ø> (ø)
pybamm/input/parameters/lithium_ion/OKane2022.py 100.00% <ø> (ø)
pybamm/input/parameters/lithium_ion/ORegan2022.py 100.00% <ø> (ø)
... and 6 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@awadell1
Copy link
Contributor Author

@tinosulzer Can we make all_parameter_sets private as pybamm.parameter_sets implements the dict interface?

if so, we can replace change this:

all_parameter_sets = pybamm.parameter_sets.all_parameter_sets["lithium_ion"]

To:

all_parameter_sets = [k for k,v in pybamm.parameter_sets.items() if v["chemistry"] == "lithium_ion"]

Probably also need to update the docs for Adding Parameter Values to:

  • Remove depreciated mixing of parameter sets Issue 2334 reformat parameters #2342
  • Remove depreciated dot access to parameter sets pybamm.parameter_sets.Marquis2019
  • Encourage new parameter sets to be added via entry points, and not contributions to PyBamm

awadell1 and others added 12 commits October 25, 2022 12:09
Building on #2342, this PR uses entry points to register and find new
ParameterSets. This enables third-parties to distribute new parameter
sets without opening a PR against PyBaMM.

- Enables a decentralized approach to parameter sharing
- Parameter sets provided by external packages can now be version controlled
- User can now (once available) just `pip install` new parameter sets

Both third-party and existing parameter sets are discovered via the
`pybamm_parameter_sets` entry point, putting both on equal footing.

Added the `get_docstring` method to ParameterSets to enable programmatic
access to a parameters set's documentation.
Instead of documenting parameter sets in `pybamm/parameters/parameter_sets.py`
document them at the point of declaration (i.e. at the entry point)
Now that parameters sets can come from outside of PyBamm, we can't rely
on pybamm/CITATIONS.txt as the sole source of citations.
This commit allows the `key` to either be a known citation key or a
bibtex formatted citation.

Supporting changes

- `_all_citations` now is a dict mapping citation keys to BibTex
  strings. This lets us add new citations entries as needed.
- `register()` can cite multiple entries if a BibTex string with
  multiple entries is added.
- Added `_add_citation` to handle adding new entries / overwriting old
  ones if a duplicate key is provided

Breaking: Registering a citation can now overwrite a previous citation.
For example, the following will replace the citation for `Sulzer2019physical`
with the new BibTex citation globally.

```python
import pybamm
pybamm.citation.register("Sulzer2019physical")
pybamm.citations.register("@book{Sulzer2019physical, title={New}}")
```

This should be tolerable as:

- Sane citation keys should generally not cause duplicates
- Duplicate citation keys are generally not supported, better to warn,
  then print out a BibTex file that could silently hide the duplication
- We warn the user when it happens
Doc Fixes:
- missing black lines after explicit ref tag
- Use labeled reference for link to pybamm issue template
- Doctest for parameter sets was missing `<BLANKLINE>` callout, added
- Doctest for citations failed to parse multi-line command, made single line

Testing:
- On windows, the NamedTemporaryFile get's deleted on first close. Fixed
  `test_citations.py` to manually delete the file instead.
- Using `list[str]` as a type hint is unsupported in python 3.8, removed

Coverage:
- Missing tests for `pybamm.Citations._add_citation`
- Missing tests for `__len__`, `__iter__` and `get_docstring` for
  `pybamm.parameters.parameter_sets.ParameterSets` -> added
- Added test to ensure all parameter sets are registered via entry points
Per conversation with @tinosulzer, removed:

- update_parameter_sets_docs.yml - No longer needed due to jinja
- add-parameter-values.rst - Out of date (This PR + #2342)

Fixed `examples/scripts/print_model_parameter_combinations.py` to use
the dict interface to `pybamm.parameter_sets`
Needed to ensure the doc strings are formatted correctly
Fix typo in test_citations
Copy link
Member

@valentinsulzer valentinsulzer left a comment

Choose a reason for hiding this comment

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

Looks good, merging

@valentinsulzer valentinsulzer merged commit 6d8c046 into pybamm-team:develop Oct 25, 2022
@valentinsulzer
Copy link
Member

@all-contributors add @awadell1 for code, tests, docs

@allcontributors
Copy link
Contributor

@tinosulzer

I've put up a pull request to add @awadell1! 🎉

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.

2 participants