From 2c81964d8f422396963fbbcaece650f5d787eefc Mon Sep 17 00:00:00 2001 From: Jay Gohil Date: Thu, 30 Jun 2022 21:09:35 -0400 Subject: [PATCH] tests: numpy.testing asserts changed to pytest.approx (#760) * Update BoostHistogramHandsOn.ipynb * Updated all numpy.testing asserts to pytest.approx * Updated all numpy.testing asserts to pytest.approx * Updated all numpy.testing asserts to pytest.approx * style: pre-commit fixes * Wrapping this assert to remove test failing Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- tests/test_axes_object.py | 31 +++++++++---------- tests/test_numpy_interface.py | 56 +++++++++++++++++------------------ tests/test_pickle.py | 12 ++++---- 3 files changed, 50 insertions(+), 49 deletions(-) diff --git a/tests/test_axes_object.py b/tests/test_axes_object.py index e67a94d0..4e652729 100644 --- a/tests/test_axes_object.py +++ b/tests/test_axes_object.py @@ -1,5 +1,6 @@ import numpy as np import pytest +from pytest import approx import boost_histogram as bh @@ -39,11 +40,11 @@ def test_axes_centers(h): full_answers = np.mgrid[0.5:10, 0.5:5, 0.5:2] for i in range(3): - np.testing.assert_allclose(centers.broadcast()[i], full_answers[i]) - np.testing.assert_allclose(centers[i], answers[i]) - np.testing.assert_allclose(centers.T[i], answers[i].T) - np.testing.assert_allclose(centers.flatten()[i], answers[i].flatten()) - np.testing.assert_allclose(h.axes[i].centers, answers[i].ravel()) + assert centers.broadcast()[i] == approx(full_answers[i]) + assert centers[i] == approx(answers[i]) + assert centers.T[i] == approx(answers[i].T) + assert centers.flatten()[i] == approx(answers[i].flatten()) + assert h.axes[i].centers == approx(answers[i].ravel()) def test_axes_edges(h): @@ -52,11 +53,11 @@ def test_axes_edges(h): full_answers = np.mgrid[0:11, 0:6, 0:3] for i in range(3): - np.testing.assert_allclose(edges.broadcast()[i], full_answers[i]) - np.testing.assert_allclose(edges[i], answers[i]) - np.testing.assert_allclose(edges.T[i], answers[i].T) - np.testing.assert_allclose(edges.ravel()[i], answers[i].ravel()) - np.testing.assert_allclose(h.axes[i].edges, answers[i].ravel()) + assert edges.broadcast()[i] == approx(full_answers[i]) + assert edges[i] == approx(answers[i]) + assert edges.T[i] == approx(answers[i].T) + assert edges.ravel()[i] == approx(answers[i].ravel()) + assert h.axes[i].edges == approx(answers[i].ravel()) def test_axes_widths(h): @@ -65,11 +66,11 @@ def test_axes_widths(h): full_answers = np.mgrid[1:1:10j, 1:1:5j, 1:1:2j] for i in range(3): - np.testing.assert_allclose(widths.broadcast()[i], full_answers[i]) - np.testing.assert_allclose(widths[i], answers[i]) - np.testing.assert_allclose(widths.T[i], answers[i].T) - np.testing.assert_allclose(widths.ravel()[i], answers[i].ravel()) - np.testing.assert_allclose(h.axes[i].widths, answers[i].ravel()) + assert widths.broadcast()[i] == approx(full_answers[i]) + assert widths[i] == approx(answers[i]) + assert widths.T[i] == approx(answers[i].T) + assert widths.ravel()[i] == approx(answers[i].ravel()) + assert h.axes[i].widths == approx(answers[i].ravel()) def test_axis_misconstuct(): diff --git a/tests/test_numpy_interface.py b/tests/test_numpy_interface.py index da0a6eea..4989557f 100644 --- a/tests/test_numpy_interface.py +++ b/tests/test_numpy_interface.py @@ -46,8 +46,8 @@ def test_histogram1d(a, opt): h1, e1 = np.histogram(v, **opt) h2, e2 = bh.numpy.histogram(v, **opt) - np.testing.assert_array_almost_equal(e1, e2) - np.testing.assert_array_equal(h1, h2) + assert e1 == approx(e2) + assert h1 == approx(h2) opt = copy.deepcopy(opt) opt["density"] = True @@ -55,8 +55,8 @@ def test_histogram1d(a, opt): h1, e1 = np.histogram(v, **opt) h2, e2 = bh.numpy.histogram(v, **opt) - np.testing.assert_array_almost_equal(e1, e2) - np.testing.assert_array_almost_equal(h1, h2) + assert e1 == approx(e2) + assert h1 == approx(h2) @pytest.mark.parametrize("a", inputs_1d) @@ -71,11 +71,11 @@ def test_histogram1d_object(a, opt): bh_h2 = bh.numpy.histogram(v, **bh_opt) h2, e2 = bh_h2.to_numpy() - np.testing.assert_array_almost_equal(e1, e2) - np.testing.assert_array_equal(h1, h2) + assert e1 == approx(e2) + assert h1 == approx(h2) # Ensure reducible - assert bh_h2[:5].values() == pytest.approx(h1[:5]) + assert bh_h2[:5].values() == approx(h1[:5]) opt = copy.deepcopy(opt) opt["density"] = True @@ -94,16 +94,16 @@ def test_histogram2d(): h1, e1x, e1y = np.histogram2d(x, y) h2, e2x, e2y = bh.numpy.histogram2d(x, y) - np.testing.assert_array_almost_equal(e1x, e2x) - np.testing.assert_array_almost_equal(e1y, e2y) - np.testing.assert_array_equal(h1, h2) + assert e1x == approx(e2x) + assert e1y == approx(e2y) + assert h1 == approx(h2) h1, e1x, e1y = np.histogram2d(x, y, density=True) h2, e2x, e2y = bh.numpy.histogram2d(x, y, density=True) - np.testing.assert_array_almost_equal(e1x, e2x) - np.testing.assert_array_almost_equal(e1y, e2y) - np.testing.assert_array_almost_equal(h1, h2) + assert e1x == approx(e2x) + assert e1y == approx(e2y) + assert h1 == approx(h2) def test_histogram2d_object(): @@ -114,9 +114,9 @@ def test_histogram2d_object(): bh_h2 = bh.numpy.histogram2d(x, y, histogram=bh.Histogram) h2, e2x, e2y = bh_h2.to_numpy() - np.testing.assert_array_almost_equal(e1x, e2x) - np.testing.assert_array_almost_equal(e1y, e2y) - np.testing.assert_array_equal(h1, h2) + assert e1x == approx(e2x) + assert e1y == approx(e2y) + assert h1 == approx(h2) with pytest.raises(KeyError): bh.numpy.histogram2d(x, y, density=True, histogram=bh.Histogram) @@ -130,18 +130,18 @@ def test_histogramdd(): h1, (e1x, e1y, e1z) = np.histogramdd([x, y, z]) h2, (e2x, e2y, e2z) = bh.numpy.histogramdd([x, y, z]) - np.testing.assert_array_almost_equal(e1x, e2x) - np.testing.assert_array_almost_equal(e1y, e2y) - np.testing.assert_array_almost_equal(e1z, e2z) - np.testing.assert_array_equal(h1, h2) + assert e1x == approx(e2x) + assert e1y == approx(e2y) + assert e1z == approx(e2z) + assert h1 == approx(h2) h1, (e1x, e1y, e1z) = np.histogramdd([x, y, z], density=True) h2, (e2x, e2y, e2z) = bh.numpy.histogramdd([x, y, z], density=True) - np.testing.assert_array_almost_equal(e1x, e2x) - np.testing.assert_array_almost_equal(e1y, e2y) - np.testing.assert_array_almost_equal(e1z, e2z) - np.testing.assert_array_almost_equal(h1, h2) + assert e1x == approx(e2x) + assert e1y == approx(e2y) + assert e1z == approx(e2z) + assert h1 == approx(h2) def test_histogramdd_object(): @@ -153,10 +153,10 @@ def test_histogramdd_object(): bh_h2 = bh.numpy.histogramdd([x, y, z], histogram=bh.Histogram) h2, (e2x, e2y, e2z) = bh_h2.to_numpy(dd=True) - np.testing.assert_array_almost_equal(e1x, e2x) - np.testing.assert_array_almost_equal(e1y, e2y) - np.testing.assert_array_almost_equal(e1z, e2z) - np.testing.assert_array_equal(h1, h2) + assert e1x == approx(e2x) + assert e1y == approx(e2y) + assert e1z == approx(e2z) + assert h1 == approx(h2) with pytest.raises(KeyError): bh.numpy.histogramdd([x, y, z], density=True, histogram=bh.Histogram) diff --git a/tests/test_pickle.py b/tests/test_pickle.py index a898d0e4..0a90e079 100644 --- a/tests/test_pickle.py +++ b/tests/test_pickle.py @@ -6,7 +6,7 @@ import numpy as np import pytest -from numpy.testing import assert_almost_equal, assert_array_equal +from pytest import approx import boost_histogram as bh @@ -86,7 +86,7 @@ def test_axes(axis, args, opts, copy_fn): orig = axis(*args, **opts) new = copy_fn(orig) assert new == orig - np.testing.assert_array_equal(new.centers, orig.centers) + assert new.centers == approx(orig.centers) @pytest.mark.parametrize("axis,args,opts", axes_creations) @@ -97,7 +97,7 @@ def test_metadata_str(axis, args, opts, copy_fn): assert new.metadata == orig.metadata new.metadata = orig.metadata assert new == orig - np.testing.assert_array_equal(new.centers, orig.centers) + assert new.centers == approx(orig.centers) # Special test: Deepcopy should change metadata id, copy should not @@ -162,7 +162,7 @@ def test_storage(benchmark, copy_fn, storage, extra): hist.fill(x, weight=np.arange(2 * n + 4) + 1, sample=np.arange(2 * n + 4) + 1) new = benchmark(copy_fn, hist) - assert_array_equal(hist.view(True), new.view(True)) + assert np.asarray(hist.view(True)) == approx(np.asarray(new.view(True))) assert new == hist @@ -213,8 +213,8 @@ def test_pickle_transforms(mod, copy_fn): ax3 = bh.axis.Regular(100, 1, 100, transform=bh.axis.transform.log) assert ax1 == ax2 - assert_array_equal(ax1.centers, ax2.centers) - assert_almost_equal(ax2.centers, ax3.centers, decimal=10) + assert ax1.centers == approx(ax2.centers) + assert ax2.centers == approx(ax3.centers) def test_hist_axes_reference(copy_fn):