Skip to content

Commit

Permalink
Merge pull request #193 from agriyakhetarpal/test-against-pybamm
Browse files Browse the repository at this point in the history
Create dynamic GitHub Actions matrix to test against PyBaMM
  • Loading branch information
BradyPlanden authored Feb 16, 2024
2 parents a8b54d1 + 9572d8f commit c2423a8
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 22 deletions.
File renamed without changes.
57 changes: 43 additions & 14 deletions .github/workflows/scheduled_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,82 @@ on:
schedule:
- cron: '0 9 * * *'

# Check noxfile.py for associated environment variables
env:
PYBOP_SCHEDULED: 1

jobs:
# Dynamically create a matrix of OS, Python, and PyBaMM versions
create_pybamm_matrix:
name: Dynamically create GitHub Actions matrix
runs-on: ubuntu-latest
steps:
- name: Check out PyBOP repository
uses: actions/checkout@v4
with:
sparse-checkout-cone-mode: false
sparse-checkout: |
scripts/ci/build_matrix.sh
- name: Run script to create matrix
id: set-matrix
run: |
echo "matrix=$(bash scripts/ci/build_matrix.sh)" >> "$GITHUB_OUTPUT"
outputs:
pybop_matrix: ${{ steps.set-matrix.outputs.matrix }}

build:
needs: [create_pybamm_matrix]
name: Build (${{ matrix.os }}, Python ${{ matrix.python_version }}, PyBaMM ${{ matrix.pybamm_version }})
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
python-version: ["3.8", "3.9", "3.10", "3.11"]
matrix: ${{fromJson(needs.create_pybamm_matrix.outputs.pybop_matrix)}}
env:
PYBAMM_VERSION: ${{ matrix.pybamm_version }}

steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
- name: Set up Python ${{ matrix.python_version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
python-version: ${{ matrix.python_version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip nox
- name: Unit tests with nox
run: |
python -m nox -s unit
python -m nox -s notebooks
run: python -m nox -s unit

- name: Run notebooks with nox
run: python -m nox -s notebooks

#M-series Mac Mini
# M-series Mac Mini
build-apple-mseries:
runs-on: [self-hosted, macOS, ARM64]
if: github.repository == 'pybop-team/PyBOP'
env:
GITHUB_PATH: ${PYENV_ROOT/bin:$PATH}
strategy:
fail-fast: false
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
python_version: ["3.8", "3.9", "3.10", "3.11"]

steps:
- uses: actions/checkout@v4
- name: Install python & create virtualenv
shell: bash
run: |
eval "$(pyenv init -)"
pyenv install ${{ matrix.python-version }} -s
pyenv virtualenv ${{ matrix.python-version }} pybop-${{ matrix.python-version }}
pyenv install ${{ matrix.python_version }} -s
pyenv virtualenv ${{ matrix.python_version }} pybop-${{ matrix.python_version }}
- name: Install dependencies & run unit tests
shell: bash
run: |
eval "$(pyenv init -)"
pyenv activate pybop-${{ matrix.python-version }}
pyenv activate pybop-${{ matrix.python_version }}
python -m pip install --upgrade pip wheel setuptools nox
python -m nox -s unit
python -m nox -s notebooks
Expand All @@ -66,5 +95,5 @@ jobs:
shell: bash
run: |
eval "$(pyenv init -)"
pyenv activate pybop-${{ matrix.python-version }}
pyenv activate pybop-${{ matrix.python_version }}
pyenv uninstall -f $( python --version )
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Features

- [#123](https://github.com/pybop-team/PyBOP/issues/123) - Configures scheduled tests to run against the last three PyPI releases of PyBaMM via dynamic GitHub Actions matrix generation.
- [#187](https://github.com/pybop-team/PyBOP/issues/187) - Adds M1 Github runner to `test_on_push` workflow, updt. self-hosted supported python versions in scheduled tests.
- [#118](https://github.com/pybop-team/PyBOP/issues/118) - Adds example jupyter notebooks.
- [#151](https://github.com/pybop-team/PyBOP/issues/151) - Adds a standalone version of the Problem class.
Expand All @@ -10,11 +11,13 @@

## Bug Fixes

- [#123](https://github.com/pybop-team/PyBOP/issues/123) - Reinstates check for availability of parameter sets via PyBaMM upon retrieval by `pybop.ParameterSet.pybamm()`.
- [#196](https://github.com/pybop-team/PyBOP/issues/196) - Fixes failing observer cost tests.
- [#63](https://github.com/pybop-team/PyBOP/issues/63) - Removes NLOpt Optimiser from future releases. This is to support deployment to the Apple M-Series platform.
- [#164](https://github.com/pybop-team/PyBOP/issues/164) - Fixes convergence issues with gradient-based optimisers, changes default `model.check_params()` to allow infeasible solutions during optimisation iterations. Adds a feasibility check on the optimal parameters.

# [v23.12](https://github.com/pybop-team/PyBOP/tree/v23.12) - 2023-12-19

## Features

- [#141](https://github.com/pybop-team/PyBOP/pull/141) - Adds documentation with Sphinx and PyData Sphinx Theme. Updates docstrings across package, relocates `costs` and `dataset` to top-level of package. Adds noxfile session and deployment workflow for docs.
Expand Down
27 changes: 20 additions & 7 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
import os
import nox


# nox options
nox.options.reuse_existing_virtualenvs = True
nox.options.venv_backend = "virtualenv"

# Environment variables to control CI behaviour for nox sessions
PYBOP_SCHEDULED = int(os.environ.get("PYBOP_SCHEDULED", 0))
PYBAMM_VERSION = os.environ.get("PYBAMM_VERSION", None)


@nox.session
def unit(session):
session.run_always("pip", "install", "-e", ".[all]")
session.install("pytest", "pytest-mock")
session.install("-e", ".[all]", silent=False)
if PYBOP_SCHEDULED:
session.run("pip", "install", f"pybamm=={PYBAMM_VERSION}", silent=False)
session.install("pytest", "pytest-mock", silent=False)
session.run("pytest", "--unit")


@nox.session
def coverage(session):
session.run_always("pip", "install", "-e", ".[all]")
session.install("pytest", "pytest-cov", "pytest-mock")
session.install("-e", ".[all]", silent=False)
if PYBOP_SCHEDULED:
session.run("pip", "install", f"pybamm=={PYBAMM_VERSION}", silent=False)
session.install("pytest", "pytest-cov", "pytest-mock", silent=False)
session.run(
"pytest",
"--unit",
Expand All @@ -27,8 +38,10 @@ def coverage(session):
@nox.session
def notebooks(session):
"""Run the examples tests for Jupyter notebooks."""
session.run_always("pip", "install", "-e", ".[all]")
session.install("pytest", "nbmake")
session.install("-e", ".[all]", silent=False)
if PYBOP_SCHEDULED:
session.run("pip", "install", f"pybamm=={PYBAMM_VERSION}", silent=False)
session.install("pytest", "nbmake", silent=False)
session.run("pytest", "--nbmake", "--examples", "examples/", external=True)


Expand All @@ -39,7 +52,7 @@ def docs(session):
Credit: PyBaMM Team
"""
envbindir = session.bin
session.install("-e", ".[all,docs]")
session.install("-e", ".[all,docs]", silent=False)
session.chdir("docs")
# Local development
if session.interactive:
Expand Down
6 changes: 6 additions & 0 deletions pybop/parameters/parameter_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,10 @@ def pybamm(cls, name):
pybamm.ParameterValues
A PyBaMM parameter set corresponding to the provided name.
"""

msg = f"Parameter set '{name}' is not a valid PyBaMM parameter set. Available parameter sets are: {list(pybamm.parameter_sets)}"

if name not in list(pybamm.parameter_sets):
raise ValueError(msg)

return pybamm.ParameterValues(name).copy()
43 changes: 43 additions & 0 deletions scripts/ci/build_matrix.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash

# This helper script generates a matrix for further use in the
# scheduled/nightly builds for PyBOP, i.e., in scheduled_tests.yaml
# It generates a matrix of all combinations of the following variables:
# - python_version: 3.X
# - os: ubuntu-latest, windows-latest, macos-latest
# - pybamm_version: the last X versions of PyBaMM from PyPI, excluding release candidates

# To update the matrix, the variables below can be modified as needed.

python_version=("3.8" "3.9" "3.10" "3.11")
os=("ubuntu-latest" "windows-latest" "macos-latest")
# This command fetches the last three PyBaMM versions excluding release candidates from PyPI
pybamm_version=($(curl -s https://pypi.org/pypi/pybamm/json | jq -r '.releases | keys[]' | grep -v rc | tail -n 3 | awk '{print "\"" $1 "\"" }' | paste -sd " " -))

# open dict
json='{
"include": [
'

# loop through each combination of variables to generate matrix components
for py_ver in "${python_version[@]}"; do
for os_type in "${os[@]}"; do
for pybamm_ver in "${pybamm_version[@]}"; do
json+='{
"os": "'$os_type'",
"python_version": "'$py_ver'",
"pybamm_version": '$pybamm_ver'
},'
done
done
done

# fix structure, removing trailing comma
json=${json%,}

# close dict
json+='
]
}'

echo "$json" | jq -c .
2 changes: 1 addition & 1 deletion tests/unit/test_parameter_sets.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TestParameterSets:

@pytest.mark.unit
def test_parameter_set(self):
# Tests parameter set creation
# Tests parameter set creation and validation
with pytest.raises(ValueError):
pybop.ParameterSet.pybamm("sChen2010s")

Expand Down

0 comments on commit c2423a8

Please sign in to comment.