Skip to content

Commit

Permalink
MAINT/TEST: Scipy Compatibility and Regression Tests (#5)
Browse files Browse the repository at this point in the history
* Update tests.yml to allow manual execution

* typo

* Set up first regression tests

* remove scipy version requirement -> Python>=3.10 compiles against scipy>=1.14.1 others compile against old version
install requires should take care of making sure the corresponding scipy version is installed

* syntax of the dependency specification

* TEST: Increase tolerance for regressions in incgamma, rename testcases

* TEST: tolerance for problematic cases of incgamma increased to 1e-10

* TEST: Switch to numpy regressions to handle complex numbers properly

* TEST: use old numpy for doctests (format difference) and install gmsh

* MAINT: only deploy coverage report for one python version (3.11)

* TEST: format output in doctests to avoid problems with rounding error. Install dependencies for gmsh

* MAINT: Forgot to update package index

* MAINT: run CI on pull requests
  • Loading branch information
jan-david-fischbach authored Oct 15, 2024
1 parent 8880164 commit 0d8bc2c
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 85 deletions.
10 changes: 7 additions & 3 deletions .github/workflows/doctests.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: doctests

on:
pull_request:
push:
branches:
- main
Expand All @@ -16,8 +17,11 @@ jobs:
with:
python-version: '3.11'
- name: Build treams
run: python -m pip install -e .
run: python -m pip install "numpy<2" -e .[docs,io]
- name: Install requirements for gmsh
run: |
sudo apt-get update
sudo apt-get -y install libglu1 libxcursor-dev libxft2 libxinerama1 libfltk1.3-dev libfreetype6-dev libgl1-mesa-dev
- name: Run doctests
run: |
python -m pip install treams[docs,io]
sphinx-build -b doctest docs docs/_build/doctest
TZ=UTC sphinx-build -b doctest docs docs/_build/doctest
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
name: tests

on:
pull_request:
push:
branches:
- main
workflow_dispatch:

jobs:
tests:
Expand Down Expand Up @@ -43,6 +45,7 @@ jobs:
rm htmlcov/.gitignore
- name: Deploy report
if: ${{ matrix.python-version == '3.11' }}
uses: s0/git-publish-subdir-action@develop
env:
REPO: self
Expand Down
15 changes: 14 additions & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
subpackage.
"""
import pytest

import pathlib

def pytest_addoption(parser):
"""Add option '--runslow' and '--rungmsh'."""
Expand All @@ -14,6 +14,8 @@ def pytest_addoption(parser):
parser.addoption(
"--rungmsh", action="store_true", default=False, help="run tests needing gmsh"
)
parser.addini("datadir", "my own datadir for pytest-regressions")
parser.addini("original_datadir", "my own original_datadir for pytest-regressions")


def pytest_configure(config):
Expand All @@ -36,3 +38,14 @@ def pytest_collection_modifyitems(config, items):
for item in items:
if "gmsh" in item.keywords:
item.add_marker(skip_slow)

@pytest.fixture()
def original_datadir(request) -> pathlib.Path:
config = request.config
return config.rootpath / config.getini('datadir')


@pytest.fixture()
def datadir(request) -> pathlib.Path:
config = request.config
return config.rootpath / config.getini('datadir')
4 changes: 2 additions & 2 deletions docs/gettingstarted.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Import *treams*, create T-matrices and start calculating.

>>> import treams
>>> tm = treams.TMatrix.sphere(1, .6, 1, [4, 1])
>>> tm.xs_ext_avg
0.3072497765576123
>>> f"{tm.xs_ext_avg:.4f}"
'0.3072'

More detailed examples are given in :doc:`intro`.
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ requires = [
"wheel",
"Cython",
"numpy",
"scipy>=1.6",
"scipy",
"setuptools_scm>=6.2"
]
build-backend = "setuptools.build_meta"
Expand All @@ -22,3 +22,6 @@ archs = ["auto64"]
skip = ["pp*", "*musllinux*"]
test-command = "python -m pytest {project}/tests/unit || cd ."
test-extras = ["test", "io"]

[tool.pytest.ini_options]
datadir = "tests/datadir"
10 changes: 5 additions & 5 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ classifiers =
Operating System :: POSIX :: Linux
Natural Language :: English
Programming Language :: Python
Programming Language :: Python :: 3
Programming Language :: Python :: 3 :: Only
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
Programming Language :: Python :: 3.11
Programming Language :: Python :: 3.12
Expand All @@ -40,7 +36,8 @@ packages =

install_requires =
numpy
scipy >= 1.6
scipy >= 1.6;python_version<='3.9'
scipy >= 1.14.1;python_version>='3.10'

[options.extras_require]
coverage =
Expand All @@ -51,8 +48,11 @@ docs =
sphinx
io =
h5py
gmsh
test =
pytest
pytest-regressions
pandas

[sdist]
formats = zip, gztar
Expand Down
1 change: 1 addition & 0 deletions tests/datadir/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.obtained.*
Binary file added tests/datadir/test_many.npz
Binary file not shown.
97 changes: 24 additions & 73 deletions tests/unit/test_special.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import treams.special as sc
import treams.special.cython_special as cs
from pytest_regressions.ndarrays_regression import NDArraysRegressionFixture


def isclose(a, b, rel_tol=1e-09, abs_tol=0.0):
Expand All @@ -15,71 +16,29 @@ def isclose(a, b, rel_tol=1e-09, abs_tol=0.0):


class TestIncgamma:
def test_zero_real(self):
assert isclose(sc.incgamma(0, 1.5), 0.10001958240663263, rel_tol=EPSSQ)

## Failing ##
def test_exp1(self):
assert isclose(
ssc.exp1(2 + 4j),
0.006575211740584215 + 0.0261438237000811j,
rel_tol=EPSSQ,
)

## Failing ##
def test_zero_complex(self):
assert isclose(
sc.incgamma(0, 2 + 4j),
0.006575211740584215 + 0.0261438237000811j,
rel_tol=EPSSQ,
)

def test_zero_negreal(self):
assert isclose(
sc.incgamma(0, -3 + 0.0j),
-9.933832570625414 - 3.141592653589793j,
rel_tol=EPSSQ,
)

def test_zero_negreal_branch(self):
assert isclose(
sc.incgamma(0, complex(-3, -0.0)),
-9.933832570625414 + 3.141592653589793j,
rel_tol=EPSSQ,
)

def test_half_real(self):
assert isclose(sc.incgamma(0.5, 1.5), 0.1475825132040964, rel_tol=EPSSQ)

def test_half_complex(self):
assert isclose(
sc.incgamma(0.5, 2 + 4j),
-0.01415763494202471 + 0.058731665238669344j,
rel_tol=EPSSQ,
)

def test_half_negreal(self):
assert isclose(
sc.incgamma(0.5, -3 + 0.0j),
1.7724538509055152 - 14.626171384019093j,
rel_tol=EPSSQ,
)

def test_half_negreal_branch(self):
assert isclose(
sc.incgamma(0.5, complex(-3, -0.0)),
1.7724538509055152 + 14.626171384019093j,
rel_tol=EPSSQ,
)

def test_one_real(self):
assert isclose(sc.incgamma(1, 1.5), 0.22313016014842982, rel_tol=EPSSQ)

def test_one_complex(self):
assert isclose(
sc.incgamma(1, 2 + 4j),
-0.08846104456538201 + 0.10242208005667372j,
rel_tol=EPSSQ,
def test_many(self, ndarrays_regression: NDArraysRegressionFixture):
relaxed_tolerance = dict(atol=1e-7, rtol=1e-10)
ndarrays_regression.check(
{
'zero_negreal': sc.incgamma(0, -3 + 0.0j),
'zero_negreal_branch': sc.incgamma(0, complex(-3, -0.0)),
'half_real': sc.incgamma(0.5, 1.5),
'half_complex': sc.incgamma(0.5, 2 + 4j),
'half_negreal': sc.incgamma(0.5, -3 + 0.0j),
'half_negreal_branch': sc.incgamma(0.5, complex(-3, -0.0)),
'one_real': sc.incgamma(1, 1.5),
'one_complex': sc.incgamma(1, 2 + 4j),
'zero_real': sc.incgamma(0, 1.5),
'neg_complex': sc.incgamma(-10, 2 + 4j),
'zero_complex': sc.incgamma(0, 2 + 4j),
'exp1': ssc.exp1(2 + 4j),
},
tolerances={
'neg_complex': relaxed_tolerance,
'zero_complex': relaxed_tolerance,
'exp1': relaxed_tolerance,
},
default_tolerance=dict(atol=1e-7, rtol=EPSSQ)
)

def test_one_negreal(self):
Expand Down Expand Up @@ -111,14 +70,6 @@ def test_ten_negreal_branch(self):
def test_neg_real(self):
assert isclose(sc.incgamma(-10, 1.5), 0.0003324561166899859, rel_tol=EPSSQ)

## Failing ##
def test_neg_complex(self):
assert isclose(
sc.incgamma(-10, 2 + 4j),
-3.109457703343637e-9 - 9.73849356067146e-10j,
rel_tol=EPSSQ,
)

def test_neg_negreal(self):
assert isclose(
sc.incgamma(-10, -3 + 0.0j),
Expand Down

0 comments on commit 0d8bc2c

Please sign in to comment.