Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assert tests #310

Merged
merged 6 commits into from
Apr 13, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add test tests
jcapriot committed Mar 31, 2023
commit 1debed79d3b6bb082e6ca3e09103850b83a298f1
3 changes: 2 additions & 1 deletion discretize/tests.py
Original file line number Diff line number Diff line change
@@ -466,6 +466,8 @@ class in older versions of `discretize`.
That was easy!
"""
n_cells = np.asarray(n_cells, dtype=int)
if test_type not in ["mean", "min", "last", "all", "mean_at_least"]:
raise ValueError
orders = []
# Do first values:
nc = n_cells[0]
@@ -497,7 +499,6 @@ class in older versions of `discretize`.
np.testing.assert_allclose(orders[-1], expected_order, rtol=rtol)
elif test_type == "all":
np.testing.assert_allclose(orders, expected_order, rtol=rtol)

print(np.random.choice(happiness))
except AssertionError as err:
print(np.random.choice(sadness))
71 changes: 70 additions & 1 deletion tests/base/test_tests.py
Original file line number Diff line number Diff line change
@@ -3,7 +3,8 @@
import discretize
import subprocess
import numpy as np
from discretize.tests import assert_isadjoint
import scipy.sparse as sp
from discretize.tests import assert_isadjoint, checkDerivative, assert_expected_order


class TestAssertIsAdjoint:
@@ -67,6 +68,74 @@ def test_complex_clinear(self):
)


class TestCheckDerivative:
def test_simplePass(self):
def simplePass(x):
return np.sin(x), sp.diags(np.cos(x))

checkDerivative(simplePass, np.random.randn(5), plotIt=False)

def test_simpleFunction(self):
def simpleFunction(x):
return np.sin(x), lambda xi: np.cos(x) * xi

checkDerivative(simpleFunction, np.random.randn(5), plotIt=False)

def test_simpleFail(self):
def simpleFail(x):
return np.sin(x), -sp.diags(np.cos(x))

with pytest.raises(AssertionError):
checkDerivative(simpleFail, np.random.randn(5), plotIt=False)


@pytest.mark.parametrize("test_type", ["mean", "min", "last", "all", "mean_at_least"])
def test_expected_order_pass(test_type):
func = lambda y: np.cos(y)
func_deriv = lambda y: -np.sin(y)

def deriv_error(n):
# The l-inf norm of the average error vector does
# follow second order convergence.
nodes = np.linspace(0, 1, n + 1)
cc = 0.5 * (nodes[1:] + nodes[:-1])
dh = nodes[1] - nodes[0]
node_eval = func(nodes)
num_deriv = (node_eval[1:] - node_eval[:-1]) / dh
true_deriv = func_deriv(cc)
err = np.linalg.norm(num_deriv - true_deriv, ord=np.inf)
return err, dh

assert_expected_order(deriv_error, [10, 20, 30, 40, 50], test_type=test_type)


@pytest.mark.parametrize("test_type", ["mean", "min", "last", "all", "mean_at_least"])
def test_expected_order_failed(test_type):
func = lambda y: np.cos(y)
func_deriv = lambda y: -np.sin(y)

def deriv_error(n):
# The l2 norm of the average error vector does not
# follow second order convergence.
nodes = np.linspace(0, 1, n + 1)
cc = 0.5 * (nodes[1:] + nodes[:-1])
dh = nodes[1] - nodes[0]
node_eval = func(nodes)
num_deriv = (node_eval[1:] - node_eval[:-1]) / dh
true_deriv = func_deriv(cc)
err = np.linalg.norm(num_deriv - true_deriv)
return err, dh

with pytest.raises(AssertionError):
assert_expected_order(deriv_error, [10, 20, 30, 40, 50], test_type=test_type)


def test_expected_order_bad_test_type():
# should fail fast if given a bad test_type
with pytest.raises(ValueError):
assert_expected_order(None, [10, 20, 30, 40, 50], test_type="not_a_test")


@pytest.mark.skipif(not sys.platform.startswith("linux"), reason="Not Linux.")
def test_import_time():
# Relevant for the CLI: How long does it take to import?
22 changes: 0 additions & 22 deletions tests/base/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import unittest
import pytest
import numpy as np
import scipy.sparse as sp
from discretize.utils import (
@@ -30,27 +29,6 @@
TOL = 1e-8


class TestCheckDerivative:
def test_simplePass(self):
def simplePass(x):
return np.sin(x), sdiag(np.cos(x))

checkDerivative(simplePass, np.random.randn(5), plotIt=False)

def test_simpleFunction(self):
def simpleFunction(x):
return np.sin(x), lambda xi: sdiag(np.cos(x)) * xi

checkDerivative(simpleFunction, np.random.randn(5), plotIt=False)

def test_simpleFail(self):
def simpleFail(x):
return np.sin(x), -sdiag(np.cos(x))

with pytest.raises(AssertionError):
checkDerivative(simpleFail, np.random.randn(5), plotIt=False)


class TestSequenceFunctions(unittest.TestCase):
def setUp(self):
self.a = np.array([1, 2, 3])