Skip to content

Commit

Permalink
Other platform tests (#1113)
Browse files Browse the repository at this point in the history
* blind attempt mac

* brew installs

* try python 3.11 numba

* try windows

* fix cache

* stuff

* stuff

* stuff

* stuff

* minimal test

* add basic workflow test

* Update .github/workflows/ci.yml

Co-authored-by: Daniel Weindl <dweindl@users.noreply.github.com>

* close figures

---------

Co-authored-by: Daniel Weindl <dweindl@users.noreply.github.com>
  • Loading branch information
yannikschaelte and dweindl authored Aug 25, 2023
1 parent 787028d commit b624087
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 6 deletions.
62 changes: 60 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
# update to 3.11 when numba allows (needed in umap)
python-version: ['3.9', '3.10']
python-version: ['3.9', '3.11']

steps:
- name: Check out repository
Expand Down Expand Up @@ -48,6 +47,65 @@ jobs:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.xml

mac:
runs-on: macos-latest
strategy:
matrix:
python-version: ['3.11']

steps:
- name: Check out repository
uses: actions/checkout@v3

- name: Prepare python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Cache
uses: actions/cache@v3
with:
path: ~/.cache
key: ${{ runner.os }}-${{ matrix.python-version }}-ci

- name: Install dependencies
run: .github/workflows/install_deps.sh amici

- name: Run tests
timeout-minutes: 30
run: tox -e base

- name: Coverage
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.xml

windows:
runs-on: windows-latest
strategy:
matrix:
python-version: ['3.11']

steps:
- name: Check out repository
uses: actions/checkout@v3

- name: Prepare python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
pip install --upgrade pip
pip install tox
- name: Run tests
shell: bash
timeout-minutes: 10
run: tox -e windows

petab:
runs-on: ubuntu-latest
strategy:
Expand Down
18 changes: 14 additions & 4 deletions .github/workflows/install_deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ pip install wheel setuptools
# Used to create local test environments
pip install tox

# Update apt
sudo apt-get update
# Update package lists
if [ "$(uname)" == "Darwin" ]; then
# MacOS
brew update
else
# Linux
sudo apt-get update
fi

# Check arguments
for par in "$@"; do
Expand All @@ -22,8 +28,12 @@ for par in "$@"; do

amici)
# for amici
sudo apt-get install \
swig libatlas-base-dev libhdf5-serial-dev
if [ "$(uname)" == "Darwin" ]; then
brew install swig hdf5 libomp
else
sudo apt-get install \
swig libatlas-base-dev libhdf5-serial-dev
fi
;;

ipopt)
Expand Down
142 changes: 142 additions & 0 deletions test/base/test_workflow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
"""Test a basic workflow, with minimum dependencies.
These tests are not for correctness, but for basic functionality.
"""

from functools import wraps

import matplotlib.pyplot as plt

import pypesto
import pypesto.optimize as optimize
import pypesto.profile as profile
import pypesto.sample as sample
import pypesto.visualize as visualize

from ..util import CRProblem


def close_fig(fun):
"""Close figure."""

@wraps(fun)
def wrapped_fun(*args, **kwargs):
ret = fun(*args, **kwargs)
plt.close('all')
return ret

return wrapped_fun


def test_objective():
"""Test a simple objective function."""
crproblem = CRProblem()
obj = crproblem.get_objective()
p = crproblem.p_true

assert obj(p) == crproblem.get_fnllh()(p)
assert obj(p, sensi_orders=(0,)) == crproblem.get_fnllh()(p)
assert (obj(p, sensi_orders=(1,)) == crproblem.get_fsnllh()(p)).all()
assert (obj(p, sensi_orders=(2,)) == crproblem.get_fs2nllh()(p)).all()
fval, grad = obj(p, sensi_orders=(0, 1))
assert fval == crproblem.get_fnllh()(p)
assert (grad == crproblem.get_fsnllh()(p)).all()


@close_fig
def test_optimize():
"""Test a simple multi-start optimization."""
crproblem = CRProblem()
problem = pypesto.Problem(
objective=crproblem.get_objective(),
lb=crproblem.lb,
ub=crproblem.ub,
)
optimizer = optimize.ScipyOptimizer()
n_start = 20
result = optimize.minimize(
problem=problem,
optimizer=optimizer,
n_starts=n_start,
)

# check basic sanity
assert len(result.optimize_result.list) == n_start
assert len(result.optimize_result.fval) == n_start
assert len(result.optimize_result.x) == n_start

# check that the results are sorted
fvals = result.optimize_result.fval
assert fvals == sorted(fvals)

# check that optimization was successful
assert fvals[0] < crproblem.get_fnllh()(crproblem.p_true)

# visualize the results
visualize.waterfall(result)


@close_fig
def test_profile():
"""Test a simple profile calculation."""
crproblem = CRProblem()
problem = pypesto.Problem(
objective=crproblem.get_objective(),
lb=crproblem.lb,
ub=crproblem.ub,
)
optimizer = optimize.ScipyOptimizer()
n_starts = 5
result = optimize.minimize(
problem=problem,
optimizer=optimizer,
n_starts=n_starts,
)
profile_result = profile.parameter_profile(
problem=problem,
result=result,
optimizer=optimizer,
profile_index=[0],
)

# check basic sanity
assert len(profile_result.profile_result.list) == 1

# visualize the results
visualize.profiles(profile_result)


@close_fig
def test_sample():
"""Test a simple sampling."""
crproblem = CRProblem()
problem = pypesto.Problem(
objective=crproblem.get_objective(),
lb=crproblem.lb,
ub=crproblem.ub,
)
optimizer = optimize.ScipyOptimizer()
n_start = 5
result = optimize.minimize(
problem=problem,
optimizer=optimizer,
n_starts=n_start,
)
sampler = sample.AdaptiveMetropolisSampler()
n_samples = 500
sample_result = sample.sample(
problem=problem,
result=result,
sampler=sampler,
n_samples=n_samples,
)

# check basic sanity
assert sample_result.sample_result.trace_x.shape == (
1,
n_samples + 1,
len(crproblem.p_true),
)

# visualize the results
visualize.sampling_1d_marginals(sample_result)
10 changes: 10 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ commands =
description =
Test basic functionality

[testenv:windows]
extras = test
commands =
pytest \
test/base/test_prior.py \
test/base/test_problem.py \
test/base/test_workflow.py
description =
Test basic functionality on Windows

[testenv:petab]
extras = test,amici,petab,pyswarm
deps =
Expand Down

0 comments on commit b624087

Please sign in to comment.