From a425602391b4e1d7e1b6953be7cf1135ebb789f4 Mon Sep 17 00:00:00 2001 From: MLopez-Ibanez <2620021+MLopez-Ibanez@users.noreply.github.com> Date: Wed, 3 Apr 2024 07:50:14 +0100 Subject: [PATCH] Fix doctests --- python/Makefile | 2 +- python/src/conftest.py | 6 ++++++ python/src/moocore/_moocore.py | 33 +++++++++++++-------------------- python/tests/conftest.py | 2 -- python/tests/test_moocore.py | 18 +++++++++--------- python/tox.ini | 12 +++++++++--- 6 files changed, 38 insertions(+), 35 deletions(-) create mode 100644 python/src/conftest.py diff --git a/python/Makefile b/python/Makefile index 86f7a97d..e0cb3cea 100644 --- a/python/Makefile +++ b/python/Makefile @@ -1,7 +1,7 @@ .PHONY : install build test doc clean install: build - python3 -m pip install -e . + python3 -m pip install -e . --disable-pip-version-check build: python3 -m build diff --git a/python/src/conftest.py b/python/src/conftest.py new file mode 100644 index 00000000..4aeb7261 --- /dev/null +++ b/python/src/conftest.py @@ -0,0 +1,6 @@ +import pytest +import moocore + +@pytest.fixture(autouse=True) +def add_doctest_imports(doctest_namespace) -> None: + doctest_namespace["moocore"] = moocore diff --git a/python/src/moocore/_moocore.py b/python/src/moocore/_moocore.py index 5512a608..f2f5b0e3 100644 --- a/python/src/moocore/_moocore.py +++ b/python/src/moocore/_moocore.py @@ -405,20 +405,17 @@ def filter_dominated_within_sets(data, maximise=False, keep_weakly=False): >>> x = moocore.read_datasets("./doc/examples/input1.dat") >>> pf_per_set = moocore.filter_dominated_within_sets(x) >>> len(pf_per_set) + 42 >>> pf = moocore.filter_dominated(x[:, :-1]) >>> len(pf) - >>> pf # doctest: +ELLIPSIS - array([[2.60764118, 6.31309852, 3. ], - [3.22509709, 6.1522834 , 3. ], - [0.37731545, 9.02211752, 3. ], - [4.61023932, 2.29231998, 3. ], - [0.2901393 , 8.32259412, 4. ], - [1.54506255, 0.38303122, 4. ], - [4.43498452, 4.13150648, 5. ], - [9.78758589, 1.41238277, 5. ], - [7.85344142, 3.02219054, 5. ], - [0.9017068 , 7.49376946, 5. ], - [0.17470556, 8.89066343, 5. ]]) + 6 + >>> pf + array([[0.20816431, 4.62275469], + [0.22997367, 1.11772205], + [0.58799475, 0.73891181], + [1.54506255, 0.38303122], + [0.17470556, 8.89066343], + [8.57911868, 0.35169752]]) See Also -------- @@ -432,15 +429,11 @@ def filter_dominated_within_sets(data, maximise=False, keep_weakly=False): ) nobjs = ncols - 1 uniq_sets, uniq_index = np.unique(data[:, -1], return_index=True) + # FIXME: Is there a more efficient way to do this that just creates views and not copies? x_split = np.vsplit(data[:, :-1], uniq_index[1:]) - is_nondom = np.fromiter( - ( - is_nondominated(g, maximise=maximise, keep_weakly=keep_weakly) - for g in x_split - ), - dtype=float, - count=data.shape[0], - ) + is_nondom = np.concatenate( + [ is_nondominated(g, maximise=maximise, keep_weakly=keep_weakly) for g in x_split ], + dtype=bool, casting = 'no') return data[is_nondom, :] diff --git a/python/tests/conftest.py b/python/tests/conftest.py index 6433ab18..dc1ad1f8 100644 --- a/python/tests/conftest.py +++ b/python/tests/conftest.py @@ -5,8 +5,6 @@ @pytest.fixture(autouse=True, scope="module") def test_datapath(request): """Return the directory of the currently running test script""" - def _(file_path: str): return request.path.parent.joinpath("test_data/" + file_path) - return _ diff --git a/python/tests/test_moocore.py b/python/tests/test_moocore.py index 6734745f..27f4d711 100644 --- a/python/tests/test_moocore.py +++ b/python/tests/test_moocore.py @@ -4,15 +4,15 @@ import moocore - -def test_docstrings(): - import doctest - - doctest.FLOAT_EPSILON = 1e-9 - # Run doctests for "moocore" module and fail if one of the docstring tests is incorrect. - # Pass in the "moocore" module so that the docstrings don't have to import every time - doctest.testmod(moocore, raise_on_error=True, extraglobs={"moocore": moocore}) - +# FIXME: For some reason this stopped working!!! +# def test_docstrings(): +# import doctest +# doctest.FLOAT_EPSILON = 1e-9 +# # Run doctests for "moocore" module and fail if one of the docstring tests is incorrect. +# failed, total = doctest.testmod(moocore, raise_on_error=True) +# assert failed == 0 +# assert total > 0 + def test_read_datasets_data(test_datapath): """ diff --git a/python/tox.ini b/python/tox.ini index c8974838..4ae60462 100644 --- a/python/tox.ini +++ b/python/tox.ini @@ -1,16 +1,22 @@ [tox] min_version = 4.0 -env_list = py{38,39,310,311} +env_list = + py{38,39,310,311} + [testenv] +description = run unit tests package = wheel wheel_build_env = .pkg -description = run unit tests deps = pytest>=7 commands = - pytest --doctest-modules --ignore-glob="*generate-expected-output.py" + pytest --doctest-modules --pyargs moocore + pytest {posargs:{env:_PYTEST_TOX_DEFAULT_POSARGS:}} +[pytest] +addopts = --ignore-glob="*generate-expected-output.py" + [testenv:coverage] description = run coverage deps =