Skip to content

Commit

Permalink
Merge 8e7215f into 9a09cd1
Browse files Browse the repository at this point in the history
  • Loading branch information
pgkirsch committed Oct 27, 2021
2 parents 9a09cd1 + 8e7215f commit 28d58fa
Show file tree
Hide file tree
Showing 35 changed files with 66 additions and 88 deletions.
1 change: 0 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
include docs/source/*.rst
include docs/source/examples/*.txt
include docs/source/examples/*.py
include gpfit/tests/artifacts/*.txt
10 changes: 10 additions & 0 deletions gpfit/tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
## Plot unit tests

Plot unit tests are run with [pytest-mpl](https://github.com/matplotlib/pytest-mpl)

To generate the baseline images:
`pytest --mpl-generate-path=baseline t_plot_fit.py`

To test against the baseline images:
`pytest --mpl t_plot_fit.py`

9 changes: 3 additions & 6 deletions gpfit/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
"""
To run all GPfit unit tests, execute:
from gpfit.tests import run
run()
"""
"""GPfit tests"""
from .run_tests import run

__all__ = ['run']
Binary file removed gpfit/tests/artifacts/isma_test.png
Binary file not shown.
Binary file removed gpfit/tests/artifacts/isma_test_slices.png
Binary file not shown.
Binary file removed gpfit/tests/artifacts/isma_test_surface.png
Binary file not shown.
Binary file removed gpfit/tests/artifacts/ma_test.png
Binary file not shown.
Binary file removed gpfit/tests/artifacts/ma_test_slices.png
Binary file not shown.
Binary file removed gpfit/tests/artifacts/ma_test_surface.png
Binary file not shown.
Binary file removed gpfit/tests/artifacts/sma_test.png
Binary file not shown.
Binary file removed gpfit/tests/artifacts/sma_test_slices.png
Binary file not shown.
Binary file removed gpfit/tests/artifacts/sma_test_surface.png
Binary file not shown.
Binary file added gpfit/tests/baseline/isma_1d.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gpfit/tests/baseline/isma_2d_slices.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gpfit/tests/baseline/isma_2d_surface.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gpfit/tests/baseline/ma_1d.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gpfit/tests/baseline/ma_2d_slices.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gpfit/tests/baseline/ma_2d_surface.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gpfit/tests/baseline/sma_1d.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gpfit/tests/baseline/sma_2d_slices.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gpfit/tests/baseline/sma_2d_surface.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
File renamed without changes.
48 changes: 5 additions & 43 deletions gpfit/tests/run_tests.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,10 @@
"Runs all tests"
from gpkit.tests.helpers import run_tests
"""Run unit tests"""
import pytest

TESTS = []

from gpfit.tests import t_least_squares
TESTS += t_least_squares.TESTS

from gpfit.tests import t_logsumexp
TESTS += t_logsumexp.TESTS

from gpfit.tests import t_initialize
TESTS += t_initialize.TESTS

from gpfit.tests import t_evaluate
TESTS += t_evaluate.TESTS

from gpfit.tests import t_print_fit
TESTS += t_print_fit.TESTS

from gpfit.tests import t_plot_fit
TESTS += t_plot_fit.TESTS

from gpfit.tests import t_examples
TESTS += t_examples.TESTS

from gpfit.tests import t_fit
TESTS += t_fit.TESTS

from gpfit.tests import t_constraint_set
TESTS += t_constraint_set.TESTS


def run(xmloutput=False):
"""Run all gpfit unit tests.
Arguments
---------
xmloutput: bool
If true, generate xml output files for continuous integration
"""
if xmloutput:
run_tests(TESTS, xmloutput="test_reports")
else:
run_tests(TESTS)
def run():
"""Run tests"""
pytest.main(["--mpl"])


if __name__ == "__main__":
Expand Down
1 change: 1 addition & 0 deletions gpfit/tests/run_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest --mpl
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions gpfit/tests/t_fit.py → gpfit/tests/test_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,18 @@ def test_incorrect_inputs(self):

def test_save_and_load(self):
f1 = ImplicitSoftmaxAffine(self.x, self.y, self.K, seed=SEED)
f1.save("artifacts/fit.pkl")
f1.save("fit.pkl")
strings1 = f1.__repr__()
with open("artifacts/fit.pkl", "rb") as picklefile:
with open("fit.pkl", "rb") as picklefile:
f2 = pickle.load(picklefile)
self.assertTrue(f2.errors["rms_rel"] < 1e-5)
strings2 = f2.__repr__()
self.assertEqual(strings1, strings2)

def test_savetxt(self):
f = ImplicitSoftmaxAffine(self.x, self.y, self.K, seed=SEED)
f.savetxt("artifacts/fit.txt")
with open("artifacts/fit.txt", "r", encoding="utf-8") as textfile:
f.savetxt("fit.txt")
with open("fit.txt", "r", encoding="utf-8") as textfile:
fitstring = textfile.read()
self.assertEqual(fitstring, (
"1 = (0.947385/w**0.0920329)*(u_1)**0.0176859\n"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
65 changes: 33 additions & 32 deletions gpfit/tests/t_plot_fit.py → gpfit/tests/test_plot_fit.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"unit tests for plot fit methods"
import unittest
import pytest
import numpy as np
from gpfit.fit import MaxAffine, SoftmaxAffine, ImplicitSoftmaxAffine

SEED = 33404

class TestPlot(unittest.TestCase):

class TestPlot:
"Unit tests for plot methods"

N = 51
Expand All @@ -14,23 +16,26 @@ class TestPlot(unittest.TestCase):
y = np.log(w)
K = 2

@pytest.mark.mpl_image_compare(filename='ma_1d.png')
def test_max_affine(self):
f = MaxAffine(self.x, self.y, self.K)
f = MaxAffine(self.x, self.y, self.K, seed=SEED)
fig, _ = f.plot()
fig.savefig("artifacts/ma_test.png")
return fig

@pytest.mark.mpl_image_compare(filename='sma_1d.png')
def test_softmax_affine(self):
f = SoftmaxAffine(self.x, self.y, self.K)
f = SoftmaxAffine(self.x, self.y, self.K, seed=SEED)
fig, _ = f.plot()
fig.savefig("artifacts/sma_test.png")
return fig

@pytest.mark.mpl_image_compare(filename='isma_1d.png')
def test_implicit_softmax_affine(self):
f = ImplicitSoftmaxAffine(self.x, self.y, self.K)
f = ImplicitSoftmaxAffine(self.x, self.y, self.K, seed=SEED)
fig, _ = f.plot()
fig.savefig("artifacts/isma_test.png")
return fig


class TestPlotSurface(unittest.TestCase):
class TestPlotSurface:
"Unit tests for plot_surface methods"

rng = np.random.RandomState(33404)
Expand All @@ -42,23 +47,26 @@ class TestPlotSurface(unittest.TestCase):
y = np.log(P)
K = 3

@pytest.mark.mpl_image_compare(filename='ma_2d_surface.png')
def test_max_affine(self):
f = MaxAffine(self.x, self.y, self.K)
f = MaxAffine(self.x, self.y, self.K, seed=SEED)
fig, _ = f.plot_surface(azim=135)
fig.savefig("artifacts/ma_test_surface.png")
return fig

@pytest.mark.mpl_image_compare(filename='sma_2d_surface.png')
def test_softmax_affine(self):
f = SoftmaxAffine(self.x, self.y, self.K)
f = SoftmaxAffine(self.x, self.y, self.K, seed=SEED)
fig, _ = f.plot_surface(azim=135)
fig.savefig("artifacts/sma_test_surface.png")
return fig

@pytest.mark.mpl_image_compare(filename='isma_2d_surface.png')
def test_implicit_softmax_affine(self):
f = ImplicitSoftmaxAffine(self.x, self.y, self.K)
f = ImplicitSoftmaxAffine(self.x, self.y, self.K, seed=SEED)
fig, _ = f.plot_surface(azim=135)
fig.savefig("artifacts/isma_test_surface.png")
return fig


class TestPlotSlices(unittest.TestCase):
class TestPlotSlices:
"Unit tests for plot_slices method"

Vdd = np.linspace(1, 2, 10)
Expand All @@ -71,33 +79,26 @@ class TestPlotSlices(unittest.TestCase):
y = np.log(P)
K = 3

@pytest.mark.mpl_image_compare(filename='ma_2d_slices.png')
def test_max_affine(self):
f = MaxAffine(self.x, self.y, self.K)
f = MaxAffine(self.x, self.y, self.K, seed=SEED)
fig, _ = f.plot_slices()
fig.savefig("artifacts/ma_test_slices.png")
return fig

@pytest.mark.mpl_image_compare(filename='sma_2d_slices.png')
def test_softmax_affine(self):
f = SoftmaxAffine(self.x, self.y, self.K)
f = SoftmaxAffine(self.x, self.y, self.K, seed=SEED)
fig, _ = f.plot_slices()
fig.savefig("artifacts/sma_test_slices.png")
return fig

@pytest.mark.mpl_image_compare(filename='isma_2d_slices.png')
def test_implicit_softmax_affine(self):
f = ImplicitSoftmaxAffine(self.x, self.y, self.K)
f = ImplicitSoftmaxAffine(self.x, self.y, self.K, seed=SEED)
fig, _ = f.plot_slices()
fig.savefig("artifacts/isma_test_slices.png")

return fig

TESTS = [
TestPlot,
TestPlotSurface,
TestPlotSlices,
]

if __name__ == '__main__':
SUITE = unittest.TestSuite()
LOADER = unittest.TestLoader()

for t in TESTS:
SUITE.addTests(LOADER.loadTestsFromTestCase(t))

unittest.TextTestRunner(verbosity=2).run(SUITE)
File renamed without changes.
12 changes: 10 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,17 @@
author_email="gpkit@mit.edu",
url="https://github.com/convexengineering/gpfit",
python_requires=">=3.4",
install_requires=["numpy", "scipy", "gpkit", "matplotlib"],
install_requires=[
"numpy",
"scipy",
"gpkit",
"matplotlib>=3.3.2",
"pytest>=6.2.5",
"pytest-mpl>=0.13",
],
version="0.2.0",
packages=["gpfit", "gpfit.maths", "gpfit.tests", "gpfit.xfoil"],
packages=["gpfit", "gpfit.maths", "gpfit.tests", "gpfit.tests.baseline",
"gpfit.xfoil"],
include_package_data=True,
license=LICENSE,
)

0 comments on commit 28d58fa

Please sign in to comment.