Skip to content

Commit

Permalink
tests: numpy.testing asserts changed to pytest.approx (#760)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
gohil-jay and pre-commit-ci[bot] committed Jul 1, 2022
1 parent 23777f0 commit 2c81964
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 49 deletions.
31 changes: 16 additions & 15 deletions tests/test_axes_object.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
import pytest
from pytest import approx

import boost_histogram as bh

Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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():
Expand Down
56 changes: 28 additions & 28 deletions tests/test_numpy_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ 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

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)
Expand All @@ -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
Expand All @@ -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():
Expand All @@ -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)
Expand All @@ -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():
Expand All @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions tests/test_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 2c81964

Please sign in to comment.