From e085a7f88ae0c549a0a3d4c2a14deff470a955aa Mon Sep 17 00:00:00 2001 From: Brady Planden Date: Tue, 24 Oct 2023 17:45:06 +0100 Subject: [PATCH 1/4] Add pre-commit config and run on repo --- .github/ISSUE_TEMPLATE/feature_request.yml | 2 +- .github/workflows/release-action.yaml | 4 +- .github/workflows/test_on_push.yaml | 2 +- .pre-commit-config.yaml | 43 ++++++++++++++++++++++ CONTRIBUTING.md | 2 +- README.md | 6 +-- examples/notebooks/rmse-estimisation.ipynb | 2 +- tests/unit/test_parameterisation.py | 1 - 8 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 .pre-commit-config.yaml diff --git a/.github/ISSUE_TEMPLATE/feature_request.yml b/.github/ISSUE_TEMPLATE/feature_request.yml index 84ffca5d8..5821d4f5c 100644 --- a/.github/ISSUE_TEMPLATE/feature_request.yml +++ b/.github/ISSUE_TEMPLATE/feature_request.yml @@ -27,4 +27,4 @@ body: id: context attributes: label: Additional context - description: Add any additional context about the feature request. \ No newline at end of file + description: Add any additional context about the feature request. diff --git a/.github/workflows/release-action.yaml b/.github/workflows/release-action.yaml index 8bfa74f15..c2cd834b2 100644 --- a/.github/workflows/release-action.yaml +++ b/.github/workflows/release-action.yaml @@ -36,7 +36,7 @@ jobs: runs-on: ubuntu-latest environment: name: pypi - url: https://pypi.org/p/pybop + url: https://pypi.org/p/pybop permissions: id-token: write # IMPORTANT: mandatory for trusted publishing @@ -107,4 +107,4 @@ jobs: - name: Publish distribution 📦 to TestPyPI uses: pypa/gh-action-pypi-publish@release/v1 with: - repository-url: https://test.pypi.org/legacy/ \ No newline at end of file + repository-url: https://test.pypi.org/legacy/ diff --git a/.github/workflows/test_on_push.yaml b/.github/workflows/test_on_push.yaml index 977fbba12..f5ee3b37d 100644 --- a/.github/workflows/test_on_push.yaml +++ b/.github/workflows/test_on_push.yaml @@ -66,4 +66,4 @@ jobs: - name: Upload coverage report uses: codecov/codecov-action@v3 env: - CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} \ No newline at end of file + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 000000000..d4a98ef4e --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,43 @@ +ci: + autoupdate_commit_msg: "chore: update pre-commit hooks" + autofix_commit_msg: "style: pre-commit fixes" + +repos: + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: "v0.0.292" + hooks: + - id: ruff + args: [--fix, --ignore=E741, --exclude=__init__.py] + + - repo: https://github.com/nbQA-dev/nbQA + rev: 1.7.0 + hooks: + - id: nbqa-ruff + additional_dependencies: [ruff==0.0.284] + args: ["--fix","--ignore=E501,E402"] + + # - repo: https://github.com/adamchainz/blacken-docs + # rev: "1.16.0" + # hooks: + # - id: blacken-docs + # additional_dependencies: [black==22.12.0] + + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.4.0 + hooks: + - id: check-added-large-files + - id: check-case-conflict + - id: check-merge-conflict + - id: check-yaml + - id: debug-statements + - id: end-of-file-fixer + - id: mixed-line-ending + - id: trailing-whitespace + + - repo: https://github.com/pre-commit/pygrep-hooks + rev: v1.10.0 + hooks: + - id: python-check-blanket-type-ignore + - id: rst-backticks + - id: rst-directive-colons + - id: rst-inline-touching-normal diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 30d2fe59f..fab971445 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -279,4 +279,4 @@ GitHub does some magic with particular filenames. In particular: ## Acknowledgements This CONTRIBUTING.md file, along with large sections of the code infrastructure, -was copied from the excellent [Pints repo](https://github.com/pints-team/pints), and [PyBaMM repo](https://github.com/pybamm-team/PyBaMM) \ No newline at end of file +was copied from the excellent [Pints repo](https://github.com/pints-team/pints), and [PyBaMM repo](https://github.com/pybamm-team/PyBaMM) diff --git a/README.md b/README.md index 128da7964..65bbe9c68 100644 --- a/README.md +++ b/README.md @@ -152,7 +152,7 @@ def getdata(x0): x0 = np.array([0.55, 0.63]) solution = getdata(x0) ``` -Next, the observed variables are defined, with the model construction and parameter definitions following. Finally, the parameterisation class is constructed and parameter fitting is completed. +Next, the observed variables are defined, with the model construction and parameter definitions following. Finally, the parameterisation class is constructed and parameter fitting is completed. ```python observations = [ pybop.Observed("Time [s]", solution["Time [s]"].data), @@ -198,7 +198,7 @@ learning from the success of the [PyBaMM](https://pybamm.org/) community. Our va - Inclusivity and fairness (those who want to contribute may do so, and their input is appropriately recognised) -- Inter-operability (aiming for modularity to enable maximum impact and inclusivity) +- Interoperability (aiming for modularity to enable maximum impact and inclusivity) - User-friendliness (putting user requirements first, thinking about user-assistance & workflows) @@ -227,4 +227,4 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d -This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specifications. Contributions of any kind are welcome! See `contributing.md` for ways to get started. \ No newline at end of file +This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specifications. Contributions of any kind are welcome! See `contributing.md` for ways to get started. diff --git a/examples/notebooks/rmse-estimisation.ipynb b/examples/notebooks/rmse-estimisation.ipynb index c4104d7be..ff7b47771 100644 --- a/examples/notebooks/rmse-estimisation.ipynb +++ b/examples/notebooks/rmse-estimisation.ipynb @@ -220,7 +220,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Next, we define the targetted forward model parameters for estimation. Furthermore, PyBOP provides functionality to define a prior for the parameters. The initial parameters values used in the estimiation will be randomly drawn from the prior distribution." + "Next, we define the targeted forward model parameters for estimation. Furthermore, PyBOP provides functionality to define a prior for the parameters. The initial parameters values used in the estimiation will be randomly drawn from the prior distribution." ] }, { diff --git a/tests/unit/test_parameterisation.py b/tests/unit/test_parameterisation.py index 3549e489e..aa5499076 100644 --- a/tests/unit/test_parameterisation.py +++ b/tests/unit/test_parameterisation.py @@ -143,4 +143,3 @@ def test_parameter_set(self): np.testing.assert_allclose( parameter_test["Negative electrode active material volume fraction"], 0.75 ) - From 0f11fb46ad02e4dd62c0e4e60c752c70a3c7d1a8 Mon Sep 17 00:00:00 2001 From: Brady Planden Date: Wed, 25 Oct 2023 10:44:12 +0100 Subject: [PATCH 2/4] Add pre-commit format check --- .github/workflows/test_on_push.yaml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/.github/workflows/test_on_push.yaml b/.github/workflows/test_on_push.yaml index f5ee3b37d..9fb2b2c37 100644 --- a/.github/workflows/test_on_push.yaml +++ b/.github/workflows/test_on_push.yaml @@ -14,6 +14,21 @@ concurrency: cancel-in-progress: true jobs: + style: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Setup Python + uses: actions/setup-python@v4 + with: + python-version: 3.11 + + - name: Check formatting with pre-commit + run: | + python -m pip install pre-commit + pre-commit run ruff + + build: runs-on: ubuntu-latest strategy: From 5fd104f37d0f92852bb09de6b6fdd983125ba007 Mon Sep 17 00:00:00 2001 From: Brady Planden Date: Wed, 25 Oct 2023 10:49:56 +0100 Subject: [PATCH 3/4] Add pre-commit to CONTRIBUTING --- CONTRIBUTING.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index fab971445..f65361cd2 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -8,6 +8,22 @@ Before you commit any code, please perform the following checks: - [All tests pass](#testing): `$ nox -s unit_test` +### Installing and using pre-commit + +`PyBOP` uses a set of `pre-commit` hooks and the `pre-commit` bot to format and prettify the codebase. The hooks can be installed locally using - + +```bash +pip install pre-commit +pre-commit install +``` + +This would run the checks every time a commit is created locally. The checks will only run on the files modified by that commit, but the checks can be triggered for all the files using - + +```bash +pre-commit run --all-files +``` + +If you would like to skip the failing checks and push the code for further discussion, use the `--no-verify` option with `git commit`. ## Workflow From 344e85fa7ad906b67794ac2a4e99b8d1a9551464 Mon Sep 17 00:00:00 2001 From: Brady Planden Date: Thu, 26 Oct 2023 17:26:27 +0100 Subject: [PATCH 4/4] Remove commented blacken repo, black format --- .pre-commit-config.yaml | 6 ------ pybop/identification/observations.py | 1 + pybop/identification/parameter_set.py | 3 ++- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d4a98ef4e..9791b659e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -16,12 +16,6 @@ repos: additional_dependencies: [ruff==0.0.284] args: ["--fix","--ignore=E501,E402"] - # - repo: https://github.com/adamchainz/blacken-docs - # rev: "1.16.0" - # hooks: - # - id: blacken-docs - # additional_dependencies: [black==22.12.0] - - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.4.0 hooks: diff --git a/pybop/identification/observations.py b/pybop/identification/observations.py index bd824af17..417a9b876 100644 --- a/pybop/identification/observations.py +++ b/pybop/identification/observations.py @@ -1,5 +1,6 @@ import pybamm + class Observed: """ Class for experimental Observations. diff --git a/pybop/identification/parameter_set.py b/pybop/identification/parameter_set.py index d5fc930e8..1e1dc23ec 100644 --- a/pybop/identification/parameter_set.py +++ b/pybop/identification/parameter_set.py @@ -1,5 +1,6 @@ import pybamm + class ParameterSet: """ Class for creating parameter sets in pybop. @@ -7,6 +8,6 @@ class ParameterSet: def __new__(cls, method, name): if method.casefold() == "pybamm": - return pybamm.ParameterValues(name).copy() + return pybamm.ParameterValues(name).copy() else: raise ValueError("Only PybaMM parameter sets are currently implemented")