From 1fad506a5e287bda491f2f597b455289541074e2 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Thu, 13 Oct 2022 12:53:52 +1100 Subject: [PATCH] Use pytest.approx in unit tests --- tests/test_beta_oddsratio.py | 10 ++++++---- tests/test_beta_ratio.py | 16 ++++++---------- tests/test_hdi.py | 8 ++++---- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/tests/test_beta_oddsratio.py b/tests/test_beta_oddsratio.py index 223394a..b75cb8b 100644 --- a/tests/test_beta_oddsratio.py +++ b/tests/test_beta_oddsratio.py @@ -14,6 +14,8 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +from pytest import approx + import numpy as np from scipy import stats @@ -38,7 +40,7 @@ def test_beta_ratio_cdf_vs_empirical(): y2 = [(sample < xx).sum()/sample.size for xx in x] # Allow 0.01 tolerance - assert (np.abs(y1 - y2) < 0.01).all() + assert y1 == approx(y2, abs=0.01) def test_beta_ratio_ppf_vs_empirical(): """Compare beta_ratio.ppf with empirical distribution""" @@ -59,7 +61,7 @@ def test_beta_ratio_ppf_vs_empirical(): y2 = np.quantile(sample, x) # Allow 0.01 tolerance - assert (np.abs(y1 - y2) < 0.01).all() + assert y1 == approx(y2, abs=0.01) def test_beta_ratio_mean_vs_empirical(): """Compare beta_ratio.mean (vs _munp) with empirical mean""" @@ -75,7 +77,7 @@ def test_beta_ratio_mean_vs_empirical(): sample = (samples_p1 / (1 - samples_p1)) / (samples_p2 / (1 - samples_p2)) # Allow 0.01 tolerance - assert np.abs(dist.mean() - sample.mean()) < 0.01 + assert dist.mean() == approx(sample.mean(), abs=0.01) def test_beta_ratio_var_vs_empirical(): """Compare beta_ratio.var (vs _munp) with empirical variance""" @@ -91,4 +93,4 @@ def test_beta_ratio_var_vs_empirical(): sample = (samples_p1 / (1 - samples_p1)) / (samples_p2 / (1 - samples_p2)) # Allow 0.01 tolerance - assert np.abs(dist.var() - sample.var()) < 0.01 + assert dist.var() == approx(sample.var(), abs=0.01) diff --git a/tests/test_beta_ratio.py b/tests/test_beta_ratio.py index ad4baf8..8391f11 100644 --- a/tests/test_beta_ratio.py +++ b/tests/test_beta_ratio.py @@ -14,6 +14,8 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +from pytest import approx + import numpy as np from scipy import stats @@ -32,10 +34,7 @@ def test_beta_ratio_vs_jsaffer_pdf(): # Compare with expected values from jsaffer implementation expected = np.load('tests/beta_ratio_vs_jsaffer.npy', allow_pickle=False)[0] - - # Allow 1e-10 tolerance - diff = np.abs(y - expected) - assert (diff < 1e-10).all() + assert y == approx(expected) def test_beta_ratio_vs_jsaffer_cdf(): """Compare beta_ratio.cdf with result from https://github.com/jsaffer/beta_quotient_distribution""" @@ -50,10 +49,7 @@ def test_beta_ratio_vs_jsaffer_cdf(): # Compare with expected values from jsaffer implementation expected = np.load('tests/beta_ratio_vs_jsaffer.npy', allow_pickle=False)[1] - - # Allow 1e-10 tolerance - diff = np.abs(y - expected) - assert (diff < 1e-10).all() + assert y == approx(expected) def _gen_beta_ratio_vs_jsaffer(): """Generate beta_ratio_vs_jsaffer.npy for test_beta_ratio_vs_jsaffer_pdf/cdf""" @@ -82,7 +78,7 @@ def test_beta_ratio_mean_vs_empirical(): sample = samples_p1 / samples_p2 # Allow 0.01 tolerance - assert np.abs(dist.mean() - sample.mean()) < 0.01 + assert dist.mean() == approx(sample.mean(), abs=0.01) def test_beta_ratio_var_vs_empirical(): """Compare beta_ratio.var (via beta_ratio._munp) with empirical variance""" @@ -98,4 +94,4 @@ def test_beta_ratio_var_vs_empirical(): sample = samples_p1 / samples_p2 # Allow 0.01 tolerance - assert np.abs(dist.var() - sample.var()) < 0.01 + assert dist.var() == approx(sample.var(), abs=0.01) diff --git a/tests/test_hdi.py b/tests/test_hdi.py index 42b775c..4d2d841 100644 --- a/tests/test_hdi.py +++ b/tests/test_hdi.py @@ -14,11 +14,13 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -import yli +from pytest import approx import numpy as np from scipy import stats +import yli + def test_hdi_beta_vs_r(): """Compare yli.hdi for beta distribution with R binom.bayes""" @@ -37,6 +39,4 @@ def test_hdi_beta_vs_r(): #1 bayes 12 250 13 239 0.0515873 0.02594348 0.0793006 0.05 expected = np.array([0.02594348, 0.0793006]) - # Allow 1e-5 tolerance - diff = np.abs(np.array(hdi) - expected) - assert (diff < 1e-5).all() + assert hdi == approx(expected)