Skip to content

Commit

Permalink
pytest 8 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
larrybradley committed Jul 14, 2023
1 parent 70b270f commit 091fed9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 43 deletions.
26 changes: 11 additions & 15 deletions photutils/detection/tests/test_irafstarfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@

import itertools
import os.path as op
from contextlib import nullcontext

import numpy as np
import pytest
from astropy.table import Table
from astropy.utils import minversion
from numpy.testing import assert_allclose

from photutils.datasets import make_100gaussians_image
from photutils.detection.irafstarfinder import IRAFStarFinder
from photutils.tests.helper import PYTEST_LT_80
from photutils.utils._optional_deps import HAS_SCIPY
from photutils.utils.exceptions import NoDetectionsWarning

Expand Down Expand Up @@ -82,22 +83,17 @@ def test_irafstarfind_sky(self):
assert len(tbl) == 4

def test_irafstarfind_largesky(self):
if not minversion(pytest, '8.0.0.dev0'):
match1 = 'Sources were found, but none pass'
with pytest.warns(NoDetectionsWarning, match=match1):
starfinder = IRAFStarFinder(threshold=25.0, fwhm=2.0,
sky=100.0)
tbl = starfinder(DATA)
assert tbl is None
match1 = 'Sources were found, but none pass'
ctx1 = pytest.warns(NoDetectionsWarning, match=match1)
if PYTEST_LT_80:
ctx2 = nullcontext()
else:
match1 = 'Sources were found, but none pass'
match2 = 'invalid value encountered in divide'
with pytest.warns(NoDetectionsWarning, match=match1):
with pytest.warns(RuntimeWarning, match=match2):
starfinder = IRAFStarFinder(threshold=25.0, fwhm=2.0,
sky=100.0)
tbl = starfinder(DATA)
assert tbl is None
ctx2 = pytest.warns(RuntimeWarning, match=match2)
with ctx1, ctx2:
starfinder = IRAFStarFinder(threshold=25.0, fwhm=2.0, sky=100.0)
tbl = starfinder(DATA)
assert tbl is None

def test_irafstarfind_peakmax_filtering(self):
"""
Expand Down
27 changes: 14 additions & 13 deletions photutils/isophote/tests/test_ellipse.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@
"""

import math
from contextlib import nullcontext

import numpy as np
import pytest
from astropy.io import fits
from astropy.modeling.models import Gaussian2D
from astropy.utils import minversion
from astropy.utils.exceptions import AstropyUserWarning

from photutils.datasets import get_path, make_noise_image
from photutils.isophote.ellipse import Ellipse
from photutils.isophote.geometry import EllipseGeometry
from photutils.isophote.isophote import Isophote, IsophoteList
from photutils.isophote.tests.make_test_data import make_test_image
from photutils.tests.helper import PYTEST_LT_80
from photutils.utils._optional_deps import HAS_SCIPY

# define an off-center position and a tilted sma
Expand Down Expand Up @@ -94,22 +95,22 @@ def test_offcenter_fail(self):
# image is off-center by a large offset.
ellipse = Ellipse(OFFSET_GALAXY)

if not minversion(pytest, '8.0.0.dev0'):
match1 = 'Degrees of freedom'
with pytest.warns(RuntimeWarning, match=match1):
isophote_list = ellipse.fit_image()
match1 = 'Degrees of freedom'
ctx1 = pytest.warns(RuntimeWarning, match=match1)
if PYTEST_LT_80:
ctx2 = nullcontext()
ctx3 = nullcontext()
ctx4 = nullcontext()
else:
match1 = 'Degrees of freedom'
match2 = 'Mean of empty slice'
match3 = 'invalid value encountered'
match4 = 'No meaningful fit was possible'
with pytest.warns(RuntimeWarning, match=match1):
with pytest.warns(RuntimeWarning, match=match2):
with pytest.warns(RuntimeWarning, match=match3):
with pytest.warns(AstropyUserWarning, match=match4):
isophote_list = ellipse.fit_image()

assert len(isophote_list) == 0
ctx2 = pytest.warns(RuntimeWarning, match=match2)
ctx3 = pytest.warns(RuntimeWarning, match=match3)
ctx4 = pytest.warns(AstropyUserWarning, match=match4)
with ctx1, ctx2, ctx3, ctx4:
isophote_list = ellipse.fit_image()
assert len(isophote_list) == 0

def test_offcenter_fit(self):
# A first guess ellipse that is roughly centered on the
Expand Down
26 changes: 12 additions & 14 deletions photutils/isophote/tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
"""

import warnings
from contextlib import nullcontext

import numpy as np
import pytest
from astropy.io import fits
from astropy.utils import minversion
from astropy.utils.data import get_pkg_data_filename

from photutils.datasets import get_path
from photutils.isophote.ellipse import Ellipse
from photutils.isophote.geometry import EllipseGeometry
from photutils.isophote.model import build_ellipse_model
from photutils.isophote.tests.make_test_data import make_test_image
from photutils.tests.helper import PYTEST_LT_80
from photutils.utils._optional_deps import HAS_SCIPY


Expand Down Expand Up @@ -78,22 +79,19 @@ def test_model_minimum_radius():
g.find_center(data)
ellipse = Ellipse(data, geometry=g)

if not minversion(pytest, '8.0.0.dev0'):
match1 = 'Degrees of freedom'
with pytest.warns(RuntimeWarning, match=match1):
isophote_list = ellipse.fit_image(sma0=40, minsma=0,
maxsma=350.0, step=0.4,
nclip=3)
match1 = 'Degrees of freedom'
ctx1 = pytest.warns(RuntimeWarning, match=match1)
if PYTEST_LT_80:
ctx2 = nullcontext()
ctx3 = nullcontext()
else:
match1 = 'Degrees of freedom'
match2 = 'Mean of empty slice'
match3 = 'invalid value encountered'
with pytest.warns(RuntimeWarning, match=match1):
with pytest.warns(RuntimeWarning, match=match2):
with pytest.warns(RuntimeWarning, match=match3):
isophote_list = ellipse.fit_image(sma0=40, minsma=0,
maxsma=350.0,
step=0.4, nclip=3)
ctx2 = pytest.warns(RuntimeWarning, match=match2)
ctx3 = pytest.warns(RuntimeWarning, match=match3)
with ctx1, ctx2, ctx3:
isophote_list = ellipse.fit_image(sma0=40, minsma=0,
maxsma=350.0, step=0.4, nclip=3)

model = build_ellipse_model(data.shape, isophote_list,
fill=np.mean(data[0:50, 0:50]))
Expand Down
4 changes: 3 additions & 1 deletion photutils/psf/tests/test_photometry-depr.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,12 +839,14 @@ def test_re_use_result_as_initial_guess():
if PYTEST_LT_80:
ctx2 = nullcontext()
ctx3 = nullcontext()
ctx4 = nullcontext()
else:
match2 = 'init_guesses contains a "group_id" column'
match3 = 'The fit may be unsuccessful; check fit_info'
ctx2 = pytest.warns(AstropyUserWarning, match=match2)
ctx3 = pytest.warns(AstropyUserWarning, match=match3)
with ctx1, ctx2, ctx3:
ctx4 = pytest.warns(AstropyDeprecationWarning)
with ctx1, ctx2, ctx3, ctx4:
second_result = dao_phot_obj(image, init_guesses=result_table)
assert second_result

Expand Down

0 comments on commit 091fed9

Please sign in to comment.