Skip to content

Commit 2c81964

Browse files
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>
1 parent 23777f0 commit 2c81964

File tree

3 files changed

+50
-49
lines changed

3 files changed

+50
-49
lines changed

tests/test_axes_object.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import numpy as np
22
import pytest
3+
from pytest import approx
34

45
import boost_histogram as bh
56

@@ -39,11 +40,11 @@ def test_axes_centers(h):
3940
full_answers = np.mgrid[0.5:10, 0.5:5, 0.5:2]
4041

4142
for i in range(3):
42-
np.testing.assert_allclose(centers.broadcast()[i], full_answers[i])
43-
np.testing.assert_allclose(centers[i], answers[i])
44-
np.testing.assert_allclose(centers.T[i], answers[i].T)
45-
np.testing.assert_allclose(centers.flatten()[i], answers[i].flatten())
46-
np.testing.assert_allclose(h.axes[i].centers, answers[i].ravel())
43+
assert centers.broadcast()[i] == approx(full_answers[i])
44+
assert centers[i] == approx(answers[i])
45+
assert centers.T[i] == approx(answers[i].T)
46+
assert centers.flatten()[i] == approx(answers[i].flatten())
47+
assert h.axes[i].centers == approx(answers[i].ravel())
4748

4849

4950
def test_axes_edges(h):
@@ -52,11 +53,11 @@ def test_axes_edges(h):
5253
full_answers = np.mgrid[0:11, 0:6, 0:3]
5354

5455
for i in range(3):
55-
np.testing.assert_allclose(edges.broadcast()[i], full_answers[i])
56-
np.testing.assert_allclose(edges[i], answers[i])
57-
np.testing.assert_allclose(edges.T[i], answers[i].T)
58-
np.testing.assert_allclose(edges.ravel()[i], answers[i].ravel())
59-
np.testing.assert_allclose(h.axes[i].edges, answers[i].ravel())
56+
assert edges.broadcast()[i] == approx(full_answers[i])
57+
assert edges[i] == approx(answers[i])
58+
assert edges.T[i] == approx(answers[i].T)
59+
assert edges.ravel()[i] == approx(answers[i].ravel())
60+
assert h.axes[i].edges == approx(answers[i].ravel())
6061

6162

6263
def test_axes_widths(h):
@@ -65,11 +66,11 @@ def test_axes_widths(h):
6566
full_answers = np.mgrid[1:1:10j, 1:1:5j, 1:1:2j]
6667

6768
for i in range(3):
68-
np.testing.assert_allclose(widths.broadcast()[i], full_answers[i])
69-
np.testing.assert_allclose(widths[i], answers[i])
70-
np.testing.assert_allclose(widths.T[i], answers[i].T)
71-
np.testing.assert_allclose(widths.ravel()[i], answers[i].ravel())
72-
np.testing.assert_allclose(h.axes[i].widths, answers[i].ravel())
69+
assert widths.broadcast()[i] == approx(full_answers[i])
70+
assert widths[i] == approx(answers[i])
71+
assert widths.T[i] == approx(answers[i].T)
72+
assert widths.ravel()[i] == approx(answers[i].ravel())
73+
assert h.axes[i].widths == approx(answers[i].ravel())
7374

7475

7576
def test_axis_misconstuct():

tests/test_numpy_interface.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,17 @@ def test_histogram1d(a, opt):
4646
h1, e1 = np.histogram(v, **opt)
4747
h2, e2 = bh.numpy.histogram(v, **opt)
4848

49-
np.testing.assert_array_almost_equal(e1, e2)
50-
np.testing.assert_array_equal(h1, h2)
49+
assert e1 == approx(e2)
50+
assert h1 == approx(h2)
5151

5252
opt = copy.deepcopy(opt)
5353
opt["density"] = True
5454

5555
h1, e1 = np.histogram(v, **opt)
5656
h2, e2 = bh.numpy.histogram(v, **opt)
5757

58-
np.testing.assert_array_almost_equal(e1, e2)
59-
np.testing.assert_array_almost_equal(h1, h2)
58+
assert e1 == approx(e2)
59+
assert h1 == approx(h2)
6060

6161

6262
@pytest.mark.parametrize("a", inputs_1d)
@@ -71,11 +71,11 @@ def test_histogram1d_object(a, opt):
7171
bh_h2 = bh.numpy.histogram(v, **bh_opt)
7272
h2, e2 = bh_h2.to_numpy()
7373

74-
np.testing.assert_array_almost_equal(e1, e2)
75-
np.testing.assert_array_equal(h1, h2)
74+
assert e1 == approx(e2)
75+
assert h1 == approx(h2)
7676

7777
# Ensure reducible
78-
assert bh_h2[:5].values() == pytest.approx(h1[:5])
78+
assert bh_h2[:5].values() == approx(h1[:5])
7979

8080
opt = copy.deepcopy(opt)
8181
opt["density"] = True
@@ -94,16 +94,16 @@ def test_histogram2d():
9494
h1, e1x, e1y = np.histogram2d(x, y)
9595
h2, e2x, e2y = bh.numpy.histogram2d(x, y)
9696

97-
np.testing.assert_array_almost_equal(e1x, e2x)
98-
np.testing.assert_array_almost_equal(e1y, e2y)
99-
np.testing.assert_array_equal(h1, h2)
97+
assert e1x == approx(e2x)
98+
assert e1y == approx(e2y)
99+
assert h1 == approx(h2)
100100

101101
h1, e1x, e1y = np.histogram2d(x, y, density=True)
102102
h2, e2x, e2y = bh.numpy.histogram2d(x, y, density=True)
103103

104-
np.testing.assert_array_almost_equal(e1x, e2x)
105-
np.testing.assert_array_almost_equal(e1y, e2y)
106-
np.testing.assert_array_almost_equal(h1, h2)
104+
assert e1x == approx(e2x)
105+
assert e1y == approx(e2y)
106+
assert h1 == approx(h2)
107107

108108

109109
def test_histogram2d_object():
@@ -114,9 +114,9 @@ def test_histogram2d_object():
114114
bh_h2 = bh.numpy.histogram2d(x, y, histogram=bh.Histogram)
115115
h2, e2x, e2y = bh_h2.to_numpy()
116116

117-
np.testing.assert_array_almost_equal(e1x, e2x)
118-
np.testing.assert_array_almost_equal(e1y, e2y)
119-
np.testing.assert_array_equal(h1, h2)
117+
assert e1x == approx(e2x)
118+
assert e1y == approx(e2y)
119+
assert h1 == approx(h2)
120120

121121
with pytest.raises(KeyError):
122122
bh.numpy.histogram2d(x, y, density=True, histogram=bh.Histogram)
@@ -130,18 +130,18 @@ def test_histogramdd():
130130
h1, (e1x, e1y, e1z) = np.histogramdd([x, y, z])
131131
h2, (e2x, e2y, e2z) = bh.numpy.histogramdd([x, y, z])
132132

133-
np.testing.assert_array_almost_equal(e1x, e2x)
134-
np.testing.assert_array_almost_equal(e1y, e2y)
135-
np.testing.assert_array_almost_equal(e1z, e2z)
136-
np.testing.assert_array_equal(h1, h2)
133+
assert e1x == approx(e2x)
134+
assert e1y == approx(e2y)
135+
assert e1z == approx(e2z)
136+
assert h1 == approx(h2)
137137

138138
h1, (e1x, e1y, e1z) = np.histogramdd([x, y, z], density=True)
139139
h2, (e2x, e2y, e2z) = bh.numpy.histogramdd([x, y, z], density=True)
140140

141-
np.testing.assert_array_almost_equal(e1x, e2x)
142-
np.testing.assert_array_almost_equal(e1y, e2y)
143-
np.testing.assert_array_almost_equal(e1z, e2z)
144-
np.testing.assert_array_almost_equal(h1, h2)
141+
assert e1x == approx(e2x)
142+
assert e1y == approx(e2y)
143+
assert e1z == approx(e2z)
144+
assert h1 == approx(h2)
145145

146146

147147
def test_histogramdd_object():
@@ -153,10 +153,10 @@ def test_histogramdd_object():
153153
bh_h2 = bh.numpy.histogramdd([x, y, z], histogram=bh.Histogram)
154154
h2, (e2x, e2y, e2z) = bh_h2.to_numpy(dd=True)
155155

156-
np.testing.assert_array_almost_equal(e1x, e2x)
157-
np.testing.assert_array_almost_equal(e1y, e2y)
158-
np.testing.assert_array_almost_equal(e1z, e2z)
159-
np.testing.assert_array_equal(h1, h2)
156+
assert e1x == approx(e2x)
157+
assert e1y == approx(e2y)
158+
assert e1z == approx(e2z)
159+
assert h1 == approx(h2)
160160

161161
with pytest.raises(KeyError):
162162
bh.numpy.histogramdd([x, y, z], density=True, histogram=bh.Histogram)

tests/test_pickle.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import numpy as np
88
import pytest
9-
from numpy.testing import assert_almost_equal, assert_array_equal
9+
from pytest import approx
1010

1111
import boost_histogram as bh
1212

@@ -86,7 +86,7 @@ def test_axes(axis, args, opts, copy_fn):
8686
orig = axis(*args, **opts)
8787
new = copy_fn(orig)
8888
assert new == orig
89-
np.testing.assert_array_equal(new.centers, orig.centers)
89+
assert new.centers == approx(orig.centers)
9090

9191

9292
@pytest.mark.parametrize("axis,args,opts", axes_creations)
@@ -97,7 +97,7 @@ def test_metadata_str(axis, args, opts, copy_fn):
9797
assert new.metadata == orig.metadata
9898
new.metadata = orig.metadata
9999
assert new == orig
100-
np.testing.assert_array_equal(new.centers, orig.centers)
100+
assert new.centers == approx(orig.centers)
101101

102102

103103
# Special test: Deepcopy should change metadata id, copy should not
@@ -162,7 +162,7 @@ def test_storage(benchmark, copy_fn, storage, extra):
162162
hist.fill(x, weight=np.arange(2 * n + 4) + 1, sample=np.arange(2 * n + 4) + 1)
163163

164164
new = benchmark(copy_fn, hist)
165-
assert_array_equal(hist.view(True), new.view(True))
165+
assert np.asarray(hist.view(True)) == approx(np.asarray(new.view(True)))
166166
assert new == hist
167167

168168

@@ -213,8 +213,8 @@ def test_pickle_transforms(mod, copy_fn):
213213
ax3 = bh.axis.Regular(100, 1, 100, transform=bh.axis.transform.log)
214214

215215
assert ax1 == ax2
216-
assert_array_equal(ax1.centers, ax2.centers)
217-
assert_almost_equal(ax2.centers, ax3.centers, decimal=10)
216+
assert ax1.centers == approx(ax2.centers)
217+
assert ax2.centers == approx(ax3.centers)
218218

219219

220220
def test_hist_axes_reference(copy_fn):

0 commit comments

Comments
 (0)