Skip to content

Commit

Permalink
Tidy up tests (#120)
Browse files Browse the repository at this point in the history
- Remove pylint disables in benchmarks
- Move tests out of core and extras
- Update info about tests in CONTRIBUTING
  • Loading branch information
btjanaka committed Apr 4, 2021
1 parent b925995 commit e6add5e
Show file tree
Hide file tree
Showing 63 changed files with 45 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ ENV/
*.pth
MNIST/
dask-worker-space/
!tests/extras/baseline_images/visualize_test/*
!tests/baseline_images/visualize_test/*
!docs/_static/**/*
!examples/tutorials/_static/**/*
!readme_assets/*
Expand Down
29 changes: 17 additions & 12 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ Ready to contribute? Here's how to set up pyribs for local development.
2. Clone the fork locally:

```bash
# If you have SSH set up:
# With SSH:
git clone git@github.com:USERNAME/pyribs.git

# Or, if you do not have SSH set up:
# Without SSH:
git clone https://github.com/USERNAME/pyribs.git
```

Expand Down Expand Up @@ -74,21 +74,26 @@ Ready to contribute? Here's how to set up pyribs for local development.
5. After making changes, check that the changes pass the tests:

```bash
pylint ribs tests
pytest tests/
make test # ^ same as above
tox # Don't worry if this does not run; we will run it in CI/CD
```

To run the tests without benchmarks (benchmarks can take a while), run:
And to run the benchmarks:

```bash
pytest -c pytest_no_benchmark.ini
make test-only # ^ same as above, but shorter
pytest -c pytest_benchmark.ini
make benchmark # ^ same as above
```

To get pytest, pylint, and tox, pip install them into the environment.
However, they should already install with `pip install -e .[dev]`.
Finally, to lint the code:

```bash
pylint ribs tests benchmarks examples
make lint # ^ same as above
```

To get pytest and pylint, pip install them into the environment. However,
they should already install with `pip install -e .[dev]`.

6. Commit the changes and push the branch to GitHub:

Expand Down Expand Up @@ -151,9 +156,9 @@ Tutorials are created in Jupyter notebooks that are stored under
1. Before the main loop of the QD algorithm, include a line like
`total_itrs = 500` (any other integer will work). This line will be replaced
during testing (see `tests/tutorials.sh`) in order to test that the notebook
runs end-to-end. By default, the tests run the notebook with `total_itrs =
5`. If this tutorial needs more (or less), modify the switch-case statement
in `tests/tutorials.sh`.
runs end-to-end. By default, the tests run the notebook with
`total_itrs = 5`. If this tutorial needs more (or less), modify the
switch-case statement in `tests/tutorials.sh`.
1. Make sure that the only level 1 heading (e.g. `# Awesome Tutorial`) is the
title at the top of the notebook. Subsequent titles should be level 2 (e.g.
`## Level 2 Heading`) or higher.
Expand Down
8 changes: 0 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,6 @@ test: ## run tests with the default Python
pytest tests
.PHONY: test

test-core: ## only test the core of ribs
pytest tests/core
.PHONY: test-core

test-extras: ## only test the extras of ribs
pytest tests/extras
.PHONY: test-extras

test-coverage: ## get better test coverage by running without numba on
NUMBA_DISABLE_JIT=1 pytest tests
.PHONY: test-coverage
Expand Down
14 changes: 4 additions & 10 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
# Tests

This directory contains tests and micro-benchmarks for ribs. The tests are
separated into:

1. `core`: tests for the part of ribs that works with `pip install ribs`
2. `extras`: tests for the extra parts of ribs that only work when one installs
the full ribs with `pip install ribs[all]`

To run these tests, install the dev dependencies for ribs with
`pip install ribs[dev]` or `pip install -e .[dev]` (from the root directory of
the repo).
This directory contains tests and micro-benchmarks for ribs. The tests mirror
the directory structure of `ribs`. To run these tests, install the dev
dependencies for ribs with `pip install ribs[dev]` or `pip install -e .[dev]`
(from the root directory of the repo).

For information on running tests, see [CONTRIBUTING.md](../CONTRIBUTING.md).

Expand Down
7 changes: 3 additions & 4 deletions tests/core/archives/README.md → tests/archives/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# Steps for Testing an Archive

1. Add the name of the archive to `conftest.py` under `ARCHIVE_NAMES`.
1. Create data for it in `get_archive_data()`.
1. Create data for it in `get_archive_data()` in `conftest.py`.
1. Parts of the archive should now be automatically tested in
`archive_base_test.py`.
1. To test the rest of the archive, in particular the `add()` and `as_pandas()`
methods, start a new test file for it. For reference, see
`grid_archive_test.py`.
1. To test the rest of the archive, in particular the `add()` method, start a
new test file for it. For reference, see `grid_archive_test.py`.
1. Just like the `_data` fixture in `grid_archive_test.py`, create a
fixture that uses `get_archive_data()` to retrieve test data. Feed that
fixture into all the tests.
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ def test_solution_dim_requires_init(_data):


#
# Test the dtypes of all classes, as some classes use the dtype in their
# constructor.
# Test the dtypes of all classes.
#


Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@

from ribs.archives import CVTArchive

# pylint: disable = unused-variable


def benchmark_init(use_kd_tree, benchmark):
"""Construction includes k-means clustering and building a kd-tree."""

@benchmark
def init():
archive = CVTArchive(1000, [(-1, 1), (-1, 1)],
samples=20_000,
use_kd_tree=use_kd_tree)
archive.initialize(solution_dim=2)

benchmark(init)


def benchmark_add_10k(use_kd_tree, benchmark, benchmark_data_10k):
n, solutions, objective_values, behavior_values = benchmark_data_10k
Expand Down Expand Up @@ -47,11 +46,12 @@ def benchmark_get_10k_random_elites(use_kd_tree, benchmark, benchmark_data_10k):
for i in range(n):
archive.add(solutions[i], objective_values[i], behavior_values[i])

@benchmark
def get_elites():
for i in range(n):
for _ in range(n):
archive.get_random_elite()

benchmark(get_elites)


def benchmark_as_pandas_2000_items(benchmark):
bins = 2000
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

from ribs.archives import GridArchive

# pylint: disable = unused-variable


def benchmark_add_10k(benchmark, benchmark_data_10k):
n, solutions, objective_values, behavior_values = benchmark_data_10k
Expand Down Expand Up @@ -32,11 +30,12 @@ def benchmark_get_10k_random_elites(benchmark, benchmark_data_10k):
for i in range(n):
archive.add(solutions[i], objective_values[i], behavior_values[i])

@benchmark
def get_elites():
for i in range(n):
for _ in range(n):
archive.get_random_elite()

benchmark(get_elites)


def benchmark_as_pandas_2025_items(benchmark):
dim = 45
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

from ribs.archives import SlidingBoundariesArchive

# pylint: disable = unused-variable


def benchmark_add_10k(benchmark, benchmark_data_10k):
n, solutions, objective_values, behavior_values = benchmark_data_10k
Expand Down Expand Up @@ -37,11 +35,12 @@ def benchmark_get_10k_random_elites(benchmark, benchmark_data_10k):
for i in range(n):
archive.add(solutions[i], objective_values[i], behavior_values[i])

@benchmark
def get_elites():
for i in range(n):
for _ in range(n):
archive.get_random_elite()

benchmark(get_elites)


def benchmark_as_pandas_2048_elements(benchmark):
# TODO (btjanaka): Make this size smaller so that we do a remap.
Expand Down
Empty file removed tests/core/optimizers/__init__.py
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

from ribs.emitters import GaussianEmitter

# pylint: disable = unused-variable


def benchmark_ask_tell_100k(benchmark, fake_archive_fixture):
archive, x0 = fake_archive_fixture
Expand All @@ -26,10 +24,11 @@ def benchmark_ask_tell_100k(benchmark, fake_archive_fixture):
obj_vals = np.random.rand(n, batch_size)
behavior_vals = np.random.rand(n, batch_size, 2)

@benchmark
def ask_and_tell():
for i in range(n):
solutions = emitter.ask()
objective_values = obj_vals[i]
behavior_values = behavior_vals[i]
emitter.tell(solutions, objective_values, behavior_values)

benchmark(ask_and_tell)
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

from ribs.emitters import IsoLineEmitter

# pylint: disable = unused-variable


def benchmark_ask_tell_100k(benchmark, fake_archive_fixture):
archive, x0 = fake_archive_fixture
Expand All @@ -25,10 +23,11 @@ def benchmark_ask_tell_100k(benchmark, fake_archive_fixture):
obj_vals = np.random.rand(n, batch_size)
behavior_vals = np.random.rand(n, batch_size, 2)

@benchmark
def ask_and_tell():
for i in range(n):
solutions = emitter.ask()
objective_values = obj_vals[i]
behavior_values = behavior_vals[i]
emitter.tell(solutions, objective_values, behavior_values)

benchmark(ask_and_tell)
File renamed without changes.
Empty file removed tests/extras/__init__.py
Empty file.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ deps =
benchmarks: .[all]
commands =
pip install -U pip
ribs_core: pytest --basetemp={envtmpdir} tests/core
ribs_extras: pytest --basetemp={envtmpdir} tests/extras
pin_core: pytest --basetemp={envtmpdir} tests/core
pin_extras: pytest --basetemp={envtmpdir} tests/extras
ribs_core: pytest --basetemp={envtmpdir} tests/archives tests/emitters tests/optimizers tests/factory_test.py
ribs_extras: pytest --basetemp={envtmpdir} tests/visualize_test.py
pin_core: pytest --basetemp={envtmpdir} tests/archives tests/emitters tests/optimizers tests/factory_test.py
pin_extras: pytest --basetemp={envtmpdir} tests/visualize_test.py
coverage: pytest --basetemp={envtmpdir} tests
benchmarks: pytest --basetemp={envtmpdir} -c pytest_benchmark.ini tests

0 comments on commit e6add5e

Please sign in to comment.