Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swapped all numpy assert_allclose & assert_array_equal to pytest approx in tests #780

Open
wants to merge 17 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 42 additions & 45 deletions tests/test_axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import numpy as np
import pytest
from numpy.testing import assert_allclose, assert_array_equal
from pytest import approx

import boost_histogram as bh
Expand Down Expand Up @@ -247,8 +246,8 @@ def test_getitem(self):
a = bh.axis.Regular(2, 1.0, 2.0)
ref = [1.0, 1.5, 2.0]
for i in range(2):
assert_allclose(a.bin(i), ref[i : i + 2])
assert_allclose(a[i], ref[i : i + 2])
assert a.bin(i) == approx(ref[i : i + 2])
assert a[i] == approx(ref[i : i + 2])

assert a[-1] == a[1]
with pytest.raises(IndexError):
Expand All @@ -257,8 +256,8 @@ def test_getitem(self):
assert a.bin(-1)[0] == -np.inf
assert a.bin(2)[1] == np.inf

assert_allclose(a[bh.underflow], a.bin(-1))
assert_allclose(a[bh.overflow], a.bin(2))
assert a[bh.underflow] == approx(a.bin(-1))
assert a[bh.overflow] == approx(a.bin(2))

with pytest.raises(IndexError):
a.bin(-2)
Expand All @@ -268,7 +267,7 @@ def test_getitem(self):
def test_iter(self):
a = bh.axis.Regular(2, 1.0, 2.0)
ref = [(1.0, 1.5), (1.5, 2.0)]
assert_allclose(a, ref)
assert a == approx(ref)

def test_index(self):
a = bh.axis.Regular(4, 1.0, 2.0)
Expand Down Expand Up @@ -356,9 +355,9 @@ def test_pow_transform(self):

def test_edges_centers_widths(self):
a = bh.axis.Regular(2, 0, 1)
assert_allclose(a.edges, [0, 0.5, 1])
assert_allclose(a.centers, [0.25, 0.75])
assert_allclose(a.widths, [0.5, 0.5])
assert a.edges == approx([0, 0.5, 1])
assert a.centers == approx([0.25, 0.75])
assert a.widths == approx([0.5, 0.5])


class TestCircular(Axis):
Expand Down Expand Up @@ -412,8 +411,8 @@ def test_getitem(self):
a = bh.axis.Regular(2, 1, 1 + np.pi * 2, circular=True)
ref = [1.0, 1.0 + np.pi, 1.0 + 2.0 * np.pi]
for i in range(2):
assert_allclose(a.bin(i), ref[i : i + 2])
assert_allclose(a[i], ref[i : i + 2])
assert a.bin(i) == approx(ref[i : i + 2])
assert a[i] == approx(ref[i : i + 2])

assert a[-1] == a[1]
with pytest.raises(IndexError):
Expand All @@ -422,7 +421,7 @@ def test_getitem(self):
with pytest.raises(IndexError):
a[bh.underflow]

assert_allclose(a[bh.overflow], a.bin(2))
assert a[bh.overflow] == approx(a.bin(2))

assert a.bin(2)[0] == approx(1 + 2 * np.pi)
assert a.bin(2)[1] == approx(1 + 3 * np.pi)
Expand All @@ -435,7 +434,7 @@ def test_getitem(self):
def test_iter(self):
a = bh.axis.Regular(2, 1, 2, circular=True)
ref = [(1, 1.5), (1.5, 2)]
assert_allclose(a, ref)
assert a == approx(ref)

def test_index(self):
a = bh.axis.Regular(4, 1, 1 + np.pi * 2, circular=True)
Expand All @@ -456,9 +455,9 @@ def test_index(self):

def test_edges_centers_widths(self):
a = bh.axis.Regular(2, 0, 1, circular=True)
assert_allclose(a.edges, [0, 0.5, 1])
assert_allclose(a.centers, [0.25, 0.75])
assert_allclose(a.widths, [0.5, 0.5])
assert a.edges == approx([0, 0.5, 1])
assert a.centers == approx([0.25, 0.75])
assert a.widths == approx([0.5, 0.5])


class TestVariable(Axis):
Expand Down Expand Up @@ -526,15 +525,15 @@ def test_getitem(self):
a = bh.axis.Variable(ref)

for i in range(2):
assert_allclose(a.bin(i), ref[i : i + 2])
assert_allclose(a[i], ref[i : i + 2])
assert a.bin(i) == approx(ref[i : i + 2])
assert a[i] == approx(ref[i : i + 2])

assert a[-1] == a[1]
with pytest.raises(IndexError):
a[2]

assert_allclose(a[bh.underflow], a.bin(-1))
assert_allclose(a[bh.overflow], a.bin(2))
assert a[bh.underflow] == approx(a.bin(-1))
assert a[bh.overflow] == approx(a.bin(2))

assert a.bin(-1)[0] == -np.inf
assert a.bin(-1)[1] == ref[0]
Expand All @@ -551,7 +550,7 @@ def test_iter(self):
ref = [-0.1, 0.2, 0.3]
a = bh.axis.Variable(ref)
for i, bin in enumerate(a):
assert_array_equal(bin, ref[i : i + 2])
assert bin == approx(ref[i : i + 2])

def test_index(self):
a = bh.axis.Variable([-0.1, 0.2, 0.3])
Expand All @@ -569,9 +568,9 @@ def test_index(self):

def test_edges_centers_widths(self):
a = bh.axis.Variable([0, 1, 3])
assert_allclose(a.edges, [0, 1, 3])
assert_allclose(a.centers, [0.5, 2])
assert_allclose(a.widths, [1, 2])
assert a.edges == approx([0, 1, 3])
assert a.centers == approx([0.5, 2])
assert a.widths == approx([1, 2])


class TestInteger:
Expand Down Expand Up @@ -667,13 +666,13 @@ def test_getitem(self):
assert a.bin(-1) == -2
assert a.bin(4) == 3

assert_allclose(a[bh.underflow], a.bin(-1))
assert_allclose(a[bh.overflow], a.bin(4))
assert a[bh.underflow] == approx(a.bin(-1))
assert a[bh.overflow] == approx(a.bin(4))

def test_iter(self):
a = bh.axis.Integer(-1, 3)
ref = (-1, 0, 1, 2)
assert_array_equal(a, ref)
assert a == approx(ref)

def test_index(self):
a = bh.axis.Integer(-1, 3)
Expand All @@ -688,9 +687,9 @@ def test_index(self):

def test_edges_centers_widths(self):
a = bh.axis.Integer(1, 3)
assert_allclose(a.edges, [1, 2, 3])
assert_allclose(a.centers, [1.5, 2.5])
assert_allclose(a.widths, [1, 1])
assert a.edges == approx([1, 2, 3])
assert a.centers == approx([1.5, 2.5])
assert a.widths == approx([1, 1])


class TestCategory(Axis):
Expand Down Expand Up @@ -793,7 +792,7 @@ def test_getitem(self, ref, growth):
def test_iter(self, ref, growth):
Cat = bh.axis.StrCategory if isinstance(ref[0], str) else bh.axis.IntCategory
a = Cat(ref, growth=growth)
assert_array_equal(a, ref)
assert a == approx(ref)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
assert a == approx(ref)
assert np.asarray(a) == approx(ref)

Quick fix. But is it possible to control how pytest treats approx?


@pytest.mark.parametrize(
"ref", ([1, 2, 3, 4], ("A", "B", "C", "D")), ids=("int", "str")
Expand All @@ -803,8 +802,8 @@ def test_index(self, ref, growth):
a = Cat(ref, growth=growth)
for i, r in enumerate(ref):
assert a.index(r) == i
assert_array_equal(a.index(ref), [0, 1, 2, 3])
assert_array_equal(a.index(np.reshape(ref, (2, 2))), [[0, 1], [2, 3]])
assert a.index(ref) == approx([0, 1, 2, 3])
assert a.index(np.reshape(ref, (2, 2))) == approx([[0, 1], [2, 3]])

if isinstance(ref[0], str):
with pytest.raises(KeyError):
Expand All @@ -821,12 +820,10 @@ def test_value(self, ref, growth):
a = Cat(ref, growth=growth)
for i, r in enumerate(ref):
assert a.value(i) == r
assert_array_equal(a.value(range(3)), ref)
assert a.value(range(3)) == approx(ref)
assert a.value(3) is None
assert_array_equal(a.value((0, 3)), [ref[0], None])
assert_array_equal(
a.value(np.array((0, 1, 2, 3))), [ref[0], ref[1], ref[2], None]
)
assert a.value((0, 3)) == approx([ref[0], None])
assert a.value(np.array((0, 1, 2, 3))) == approx([ref[0], ref[1], ref[2], None])
# may be added in the future
with pytest.raises(ValueError):
a.value([[2], [2]])
Expand All @@ -835,9 +832,9 @@ def test_value(self, ref, growth):
def test_edges_centers_widths(self, ref, growth):
Cat = bh.axis.StrCategory if isinstance(ref[0], str) else bh.axis.IntCategory
a = Cat(ref, growth=growth)
assert_allclose(a.edges, [0, 1, 2, 3])
assert_allclose(a.centers, [0.5, 1.5, 2.5])
assert_allclose(a.widths, [1, 1, 1])
assert a.edges == approx([0, 1, 2, 3])
assert a.centers == approx([0.5, 1.5, 2.5])
assert a.widths == approx([1, 1, 1])


class TestBoolean:
Expand Down Expand Up @@ -891,7 +888,7 @@ def test_getitem(self):
def test_iter(self):
a = bh.axis.Boolean()
ref = (False, True)
assert_array_equal(a, ref)
assert a == approx(ref)

def test_index(self):
a = bh.axis.Boolean()
Expand All @@ -900,6 +897,6 @@ def test_index(self):

def test_edges_centers_widths(self):
a = bh.axis.Boolean()
assert_allclose(a.edges, [0.0, 1.0, 2.0])
assert_allclose(a.centers, [0.5, 1.5])
assert_allclose(a.widths, [1, 1])
assert a.edges == approx([0.0, 1.0, 2.0])
assert a.centers == approx([0.5, 1.5])
assert a.widths == approx([1, 1])
6 changes: 3 additions & 3 deletions tests/test_benchmark_1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import numpy as np
import pytest
from numpy.testing import assert_allclose, assert_array_equal
from pytest import approx

import boost_histogram as bh

Expand Down Expand Up @@ -31,7 +31,7 @@
@pytest.mark.parametrize("dtype", vals)
def test_numpy_1d(benchmark, dtype):
result, _ = benchmark(np.histogram, vals[dtype], bins=bins, range=ranges)
assert_array_equal(result, answer[dtype])
assert result == approx(answer[dtype])


def make_and_run_hist(flow, storage, vals):
Expand All @@ -47,4 +47,4 @@ def make_and_run_hist(flow, storage, vals):
@pytest.mark.parametrize("storage", STORAGES)
def test_boost_1d(benchmark, flow, storage, dtype):
result = benchmark(make_and_run_hist, flow, storage, vals[dtype])
assert_allclose(result[:-1], answer[dtype][:-1], atol=2)
assert result[:-1] == approx(answer[dtype][:-1], atol=2)
6 changes: 3 additions & 3 deletions tests/test_benchmark_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import numpy as np
import pytest
from numpy.testing import assert_array_equal
from pytest import approx

import boost_histogram as bh

Expand Down Expand Up @@ -34,7 +34,7 @@
@pytest.mark.parametrize("dtype", vals)
def test_numpy_perf_2d(benchmark, dtype):
result, _, _ = benchmark(np.histogram2d, *vals[dtype], bins=bins, range=ranges)
assert_array_equal(result, answer[dtype])
assert result == approx(answer[dtype])


def make_and_run_hist(flow, storage, vals):
Expand All @@ -53,4 +53,4 @@ def make_and_run_hist(flow, storage, vals):
@pytest.mark.parametrize("storage", STORAGES)
def test_2d(benchmark, flow, storage, dtype):
result = benchmark(make_and_run_hist, flow, storage, vals[dtype])
assert_array_equal(result[:-1, :-1], answer[dtype][:-1, :-1])
assert result[:-1, :-1] == approx(answer[dtype][:-1, :-1])
Loading