Skip to content

Commit

Permalink
TEST: handle pytest 8.3.4 change in bool tests with approx
Browse files Browse the repository at this point in the history
Fix sherpa#2202

In pytest 8.3.3 and earlier

    >>> import numpy as np; import pytest
    >>> pytest.__version__
    '8.3.3'
    >>> np.ones(2, dtype=bool) == pytest.approx([True, True])
    True

However, pytest 8.3.4 now causes this to fail

    >>> import numpy as np; import pytest
    >>> pytest.__version__
    '8.3.4'
    >>> np.ones(2, dtype=bool) == pytest.approx([True, True])
    False

This is because of "pytest.approx considers boolean numeric types"
pytest-dev/pytest#9353

The solution is to make the "expected" value be a ndarray, and so

    >>> np.ones(2, dtype=bool) == pytest.approx(np.asarray([True, True]))
    True

holds with both pytest 8.3.3 and 8.3.4.

So this commit basically goes through and updates the tests so that
we use a ndarray for boolean arrays.

An alternative would be to change from

    assert got == pytest.approx(expected)

to something like

    assert np.all(got == expected)

However, the error message when the array lengths are different or
an element is different are a **lot less** useful, and the change would
be even-more invasive than this change.
  • Loading branch information
DougBurke committed Dec 6, 2024
1 parent c034afa commit 0fafdd4
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 96 deletions.
2 changes: 1 addition & 1 deletion sherpa/astro/io/tests/test_io_pha.py
Original file line number Diff line number Diff line change
Expand Up @@ -1504,7 +1504,7 @@ def test_write_pha_with_bad_quality(tmp_path):
counts = chans * 2
group = [1, -1, -1, 1, -1, 1, 1, 1, -1]
quality = [0, 5, 0, 0, 0, 0, 0, 2, 2]
qfilt = [True, False] + [True] * 5 + [False] * 2
qfilt = np.asarray([True, False] + [True] * 5 + [False] * 2)

pha0 = DataPHA("qual", chans, counts, grouping=group,
quality=quality)
Expand Down
43 changes: 27 additions & 16 deletions sherpa/astro/tests/test_astro_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2367,7 +2367,7 @@ def test_pha_check_limit(ignore, lo, hi, evals):
pha.units = 'energy'

assert pha.mask is True
assert pha.get_mask() == pytest.approx([True] * 10)
assert pha.get_mask() == pytest.approx(np.ones(10, dtype=bool))

func = pha.ignore if ignore else pha.notice
func(lo, hi)
Expand All @@ -2380,7 +2380,7 @@ def test_pha_check_limit(ignore, lo, hi, evals):
vin = True

c1, c2, c3 = evals
expected = [vout] * c1 + [vin] * c2 + [vout] * c3
expected = np.asarray([vout] * c1 + [vin] * c2 + [vout] * c3)
assert pha.mask == pytest.approx(pha.get_mask())
assert pha.mask == pytest.approx(expected)

Expand Down Expand Up @@ -2449,7 +2449,7 @@ def test_pha_check_limit_channel(ignore, lo, hi, evals):
pha.units = 'channel'

assert pha.mask is True
assert pha.get_mask() == pytest.approx([True] * 10)
assert pha.get_mask() == pytest.approx(np.ones(10, dtype=bool))

func = pha.ignore if ignore else pha.notice
func(lo, hi)
Expand All @@ -2462,7 +2462,7 @@ def test_pha_check_limit_channel(ignore, lo, hi, evals):
vin = True

c1, c2, c3 = evals
expected = [vout] * c1 + [vin] * c2 + [vout] * c3
expected = np.asarray([vout] * c1 + [vin] * c2 + [vout] * c3)
assert pha.mask == pytest.approx(pha.get_mask())
assert pha.mask == pytest.approx(expected)

Expand Down Expand Up @@ -2672,7 +2672,9 @@ def test_is_mask_reset_pha(caplog):
# Pick a value somewhere within the independent axis
assert data.mask is True
data.ignore(None, 2)
assert data.mask == pytest.approx([False, False, True])

mask = np.asarray([False, False, True])
assert data.mask == pytest.approx(mask)

# Change the independent axis, but to something of the same
# length.
Expand All @@ -2683,7 +2685,7 @@ def test_is_mask_reset_pha(caplog):
assert len(caplog.records) == 0

# The mask has *not* been cleared
assert data.mask == pytest.approx([False, False, True])
assert data.mask == pytest.approx(mask)


def test_is_mask_reset_pha_channel(caplog):
Expand All @@ -2703,7 +2705,8 @@ def test_is_mask_reset_pha_channel(caplog):
assert len(caplog.records) == 0

# The mask has not been cleared
assert data.mask == pytest.approx([False, False, True])
mask = np.asarray([False, False, True])
assert data.mask == pytest.approx(mask)


@requires_region
Expand Down Expand Up @@ -3376,9 +3379,11 @@ def test_pha_notice_bkg_id_none():

pha.notice(lo=2, bkg_id=None) # the default

assert pha.mask == pytest.approx([False, True])
assert b1.mask == pytest.approx([False, True])
assert bup.mask == pytest.approx([False, True])
bfilt = np.asarray([False, True])

assert pha.mask == pytest.approx(bfilt)
assert b1.mask == pytest.approx(bfilt)
assert bup.mask == pytest.approx(bfilt)


@pytest.mark.parametrize("bkg_id", [1, "up"])
Expand All @@ -3394,13 +3399,15 @@ def test_pha_notice_bkg_id_scalar(bkg_id):

pha.notice(lo=2, bkg_id=bkg_id)

bfilt = np.asarray([False, True])

assert pha.mask is True
if bkg_id == 1:
assert b1.mask == pytest.approx([False, True])
assert b1.mask == pytest.approx(bfilt)
assert bup.mask is True
else:
assert b1.mask is True
assert bup.mask == pytest.approx([False, True])
assert bup.mask == pytest.approx(bfilt)


def test_pha_notice_bkg_id_array_all():
Expand All @@ -3415,9 +3422,11 @@ def test_pha_notice_bkg_id_array_all():

pha.notice(lo=2, bkg_id=["up", 1])

bfilt = np.asarray([False, True])

assert pha.mask is True
assert b1.mask == pytest.approx([False, True])
assert bup.mask == pytest.approx([False, True])
assert b1.mask == pytest.approx(bfilt)
assert bup.mask == pytest.approx(bfilt)


@pytest.mark.parametrize("bkg_id", [1, "up"])
Expand All @@ -3433,13 +3442,15 @@ def test_pha_notice_bkg_id_array_subset(bkg_id):

pha.notice(lo=2, bkg_id=[bkg_id])

bfilt = np.asarray([False, True])

assert pha.mask is True
if bkg_id == 1:
assert b1.mask == pytest.approx([False, True])
assert b1.mask == pytest.approx(bfilt)
assert bup.mask is True
else:
assert b1.mask is True
assert bup.mask == pytest.approx([False, True])
assert bup.mask == pytest.approx(bfilt)


def get_img_spatial_mask():
Expand Down
Loading

0 comments on commit 0fafdd4

Please sign in to comment.