Skip to content

Commit

Permalink
Fix doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
MLopez-Ibanez committed Apr 3, 2024
1 parent 295b204 commit a425602
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 35 deletions.
2 changes: 1 addition & 1 deletion python/Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 6 additions & 0 deletions python/src/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import pytest
import moocore

@pytest.fixture(autouse=True)
def add_doctest_imports(doctest_namespace) -> None:
doctest_namespace["moocore"] = moocore
33 changes: 13 additions & 20 deletions python/src/moocore/_moocore.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------
Expand All @@ -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, :]


Expand Down
2 changes: 0 additions & 2 deletions python/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 _
18 changes: 9 additions & 9 deletions python/tests/test_moocore.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
12 changes: 9 additions & 3 deletions python/tox.ini
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down

0 comments on commit a425602

Please sign in to comment.