From 637d9c4f207509052b5abbecf39bc48ff5ac05e8 Mon Sep 17 00:00:00 2001 From: Steven Cordwell Date: Tue, 6 Jan 2015 12:01:04 +1300 Subject: [PATCH] [tests] Spacing based on max values rather than 1 Base the comparison that two arrays are almost equal based on the spacing of the maximum value of the array rather than one. Using one will give incorrect resulsts when the magintude of the values is significantly different to one. --- src/tests/test_examples.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/tests/test_examples.py b/src/tests/test_examples.py index f0f11de..3d74e5f 100644 --- a/src/tests/test_examples.py +++ b/src/tests/test_examples.py @@ -12,10 +12,13 @@ import scipy.sparse as sp import mdptoolbox.example -## example.forest +def assert_equal_numpy_spacing(A, B): + A = np.array(A) + B = np.array(B) + x = np.amax(np.maximum(np.abs(A), np.abs(B))) + assert_true((np.abs(A - B) <= np.spacing(x)).all()) -def assert_spacing_equal(A, B): - return(assert_true((np.abs(A - B) <= np.spacing(1)).all())) +## example.forest class TestExampleForest(object): P = np.array( @@ -34,8 +37,8 @@ class TestExampleForest(object): P, R = mdptoolbox.example.forest() assert_equal(P.shape, self.P.shape) assert_equal(R.shape, self.R.shape) - assert_spacing_equal(P, self.P) - assert_spacing_equal(R, self.R) + assert_equal_numpy_spacing(P, self.P) + assert_equal_numpy_spacing(R, self.R) def test_sparse_PR(self): P, R = mdptoolbox.example.forest(is_sparse=True) @@ -97,8 +100,8 @@ class TestExampleRand(object): P, R = mdptoolbox.example.rand(self.S, self.A) assert_equal(P.shape, self.P.shape) assert_equal(R.shape, self.R.shape) - assert_spacing_equal(P, self.P) - assert_spacing_equal(R, self.R) + assert_equal_numpy_spacing(P, self.P) + assert_equal_numpy_spacing(R, self.R) def test_sparse_PR(self): P, R = mdptoolbox.example.rand(self.S, self.A, is_sparse=True) -- GitLab