Skip to content

Commit

Permalink
Merge pull request #784 from pllim/deprecation-deprecated
Browse files Browse the repository at this point in the history
MNT: Stop using deprecated astropy.tests stuff
  • Loading branch information
mwcraig committed Jan 12, 2022
2 parents 2ee7021 + 8ed01b7 commit 84c0a94
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 88 deletions.
27 changes: 2 additions & 25 deletions ccdproc/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,9 @@
def pytest_configure(config):
config.option.astropy_header = True
except ImportError:
# TODO: Remove this when astropy 2.x and 3.x support is dropped.
# Probably an old pytest-astropy package where the pytest_astropy_header
# is not a dependency.
try:
from astropy.tests.plugins.display import (pytest_report_header,
PYTEST_HEADER_MODULES,
TESTED_VERSIONS)
except ImportError:
# TODO: Remove this when astropy 2.x support is dropped.
# If that also did not work we're probably using astropy 2.0
from astropy.tests.pytest_plugins import (pytest_report_header,
PYTEST_HEADER_MODULES,
TESTED_VERSIONS)
PYTEST_HEADER_MODULES = {}
TESTED_VERSIONS = {}

try:
# TODO: Remove this when astropy 2.x support is dropped.
# This is the way to get plugins in astropy 2.x
from astropy.tests.pytest_plugins import *
except ImportError:
# Otherwise they are installed as separate packages that pytest
# automagically finds.
pass

from .tests.pytest_fixtures import *

Expand All @@ -48,10 +29,6 @@ def pytest_configure(config):
packagename = os.path.basename(os.path.dirname(__file__))
TESTED_VERSIONS[packagename] = version

# Uncomment the following line to treat all DeprecationWarnings as
# exceptions
# enable_deprecations_as_exceptions()

# Add astropy to test header information and remove unused packages.

try:
Expand Down
2 changes: 1 addition & 1 deletion ccdproc/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ def transform_image(ccd, transform_func, **kwargs):

if nccd.wcs is not None:
warn = 'WCS information may be incorrect as no transformation was applied to it'
logging.warning(warn)
warnings.warn(warn, UserWarning)

return nccd

Expand Down
4 changes: 0 additions & 4 deletions ccdproc/tests/pytest_fixtures.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst

import gzip
from tempfile import mkdtemp
import os
from shutil import rmtree

import numpy as np

import pytest
from astropy import units as u
from astropy.utils import NumpyRNGContext
from astropy.io import fits
from astropy.nddata import CCDData

from ..utils.sample_directory import directory_for_testing
Expand Down
3 changes: 3 additions & 0 deletions ccdproc/tests/run_for_memory_profile.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import pytest
pytest.importorskip('memory_profiler')

from argparse import ArgumentParser
from tempfile import TemporaryDirectory
from pathlib import Path
Expand Down
5 changes: 1 addition & 4 deletions ccdproc/tests/test_bitfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import numpy as np
import pytest

from astropy.tests.helper import catch_warnings

from ccdproc.core import bitfield_to_boolean_mask


Expand Down Expand Up @@ -69,9 +67,8 @@ def test_bitfield_flag_non_integer():

def test_bitfield_duplicate_flag_throws_warning():
bm = np.random.randint(0, 10, (10, 10))
with catch_warnings(UserWarning) as w:
with pytest.warns(UserWarning):
bitfield_to_boolean_mask(bm, [1, 1])
assert len(w)


def test_bitfield_none_identical_to_strNone():
Expand Down
34 changes: 18 additions & 16 deletions ccdproc/tests/test_ccdproc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from astropy.units.quantity import Quantity
import astropy.units as u
from astropy.wcs import WCS
from astropy.tests.helper import catch_warnings
from astropy.utils.exceptions import AstropyUserWarning

from astropy.nddata import StdDevUncertainty, CCDData
Expand Down Expand Up @@ -80,7 +79,7 @@ def test_create_deviation_from_negative():
np.isnan(ccd_var.uncertainty.array))


def test_create_deviation_from_negative():
def test_create_deviation_from_negative_2():
ccd_data = ccd_data_func(data_mean=0, data_scale=10)
ccd_data.unit = u.electron
readnoise = 5 * u.electron
Expand Down Expand Up @@ -619,8 +618,13 @@ def test_catch_transform_wcs_warning():
def tran(arr):
return 10 * arr

with catch_warnings() as w:
tran = transform_image(ccd_data, tran)
# No warning.
transform_image(ccd_data, tran)

# Issue warning when data has WCS.
ccd_data.wcs = wcs_for_testing(ccd_data.shape)
with pytest.warns(UserWarning, match='WCS information may be incorrect'):
transform_image(ccd_data, tran)


@pytest.mark.parametrize('mask_data, uncertainty', [
Expand Down Expand Up @@ -661,7 +665,7 @@ def test_block_reduce():
mask=np.zeros((4, 4), dtype=bool),
uncertainty=StdDevUncertainty(np.ones((4, 4)))
)
with catch_warnings(AstropyUserWarning) as w:
with pytest.warns(AstropyUserWarning) as w:
ccd_summed = block_reduce(ccd, (2, 2))
assert len(w) == 1
assert 'following attributes were set' in str(w[0].message)
Expand Down Expand Up @@ -690,7 +694,7 @@ def test_block_average():
mask=np.zeros((4, 4), dtype=bool),
uncertainty=StdDevUncertainty(np.ones((4, 4))))
ccd.data[::2, ::2] = 2
with catch_warnings(AstropyUserWarning) as w:
with pytest.warns(AstropyUserWarning) as w:
ccd_avgd = block_average(ccd, (2, 2))
assert len(w) == 1
assert 'following attributes were set' in str(w[0].message)
Expand All @@ -716,7 +720,7 @@ def test_block_replicate():
ccd = CCDData(np.ones((4, 4)), unit='adu', meta={'testkw': 1},
mask=np.zeros((4, 4), dtype=bool),
uncertainty=StdDevUncertainty(np.ones((4, 4))))
with catch_warnings(AstropyUserWarning) as w:
with pytest.warns(AstropyUserWarning) as w:
ccd_repl = block_replicate(ccd, (2, 2))
assert len(w) == 1
assert 'following attributes were set' in str(w[0].message)
Expand Down Expand Up @@ -772,7 +776,7 @@ def test__overscan_schange():
def test_create_deviation_does_not_change_input():
ccd_data = ccd_data_func()
original = ccd_data.copy()
ccd = create_deviation(ccd_data, gain=5 * u.electron / u.adu, readnoise=10 * u.electron)
_ = create_deviation(ccd_data, gain=5 * u.electron / u.adu, readnoise=10 * u.electron)
np.testing.assert_array_equal(original.data, ccd_data.data)
assert original.unit == ccd_data.unit

Expand All @@ -790,8 +794,7 @@ def test_cosmicray_median_does_not_change_input():
def test_cosmicray_lacosmic_does_not_change_input():
ccd_data = ccd_data_func()
original = ccd_data.copy()
error = np.zeros_like(ccd_data)
ccd = cosmicray_lacosmic(ccd_data)
_ = cosmicray_lacosmic(ccd_data)
np.testing.assert_array_equal(original.data, ccd_data.data)
assert original.unit == ccd_data.unit

Expand All @@ -809,7 +812,7 @@ def test_flat_correct_does_not_change_input():
def test_gain_correct_does_not_change_input():
ccd_data = ccd_data_func()
original = ccd_data.copy()
ccd = gain_correct(ccd_data, gain=1, gain_unit=ccd_data.unit)
_ = gain_correct(ccd_data, gain=1, gain_unit=ccd_data.unit)
np.testing.assert_array_equal(original.data, ccd_data.data)
assert original.unit == ccd_data.unit

Expand All @@ -818,15 +821,15 @@ def test_subtract_bias_does_not_change_input():
ccd_data = ccd_data_func()
original = ccd_data.copy()
master_frame = CCDData(np.zeros_like(ccd_data), unit=ccd_data.unit)
ccd = subtract_bias(ccd_data, master=master_frame)
_ = subtract_bias(ccd_data, master=master_frame)
np.testing.assert_array_equal(original.data, ccd_data.data)
assert original.unit == ccd_data.unit


def test_trim_image_does_not_change_input():
ccd_data = ccd_data_func()
original = ccd_data.copy()
ccd = trim_image(ccd_data, fits_section=None)
_ = trim_image(ccd_data, fits_section=None)
np.testing.assert_array_equal(original.data, ccd_data.data)
assert original.unit == ccd_data.unit

Expand Down Expand Up @@ -879,7 +882,6 @@ def test_wcs_project_onto_same_wcs_remove_headers():
# Remove an example WCS keyword from the header
target_wcs = wcs_for_testing(ccd_data.shape)
ccd_data.wcs = wcs_for_testing(ccd_data.shape)
print(ccd_data.header)
ccd_data.header = ccd_data.wcs.to_header()

new_ccd = wcs_project(ccd_data, target_wcs)
Expand Down Expand Up @@ -988,8 +990,8 @@ def test_wcs_project_onto_scale_wcs():
def test_ccd_process_does_not_change_input():
ccd_data = ccd_data_func()
original = ccd_data.copy()
ccd = ccd_process(ccd_data, gain=5 * u.electron / u.adu,
readnoise=10 * u.electron)
_ = ccd_process(ccd_data, gain=5 * u.electron / u.adu,
readnoise=10 * u.electron)
np.testing.assert_array_equal(original.data, ccd_data.data)
assert original.unit == ccd_data.unit

Expand Down
35 changes: 10 additions & 25 deletions ccdproc/tests/test_image_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
from astropy.table import Table
import numpy as np

from astropy.tests.helper import catch_warnings
from astropy.utils.data import get_pkg_data_filename
from astropy.utils import minversion
from astropy.utils.exceptions import AstropyUserWarning
from astropy.io.fits.verify import VerifyWarning

Expand All @@ -25,8 +23,6 @@
_filters = []
_original_dir = ''

_ASTROPY_LT_1_3 = not minversion("astropy", "1.3")


def test_fits_summary(triage_setup):
keywords = ['imagetyp', 'filter']
Expand Down Expand Up @@ -361,7 +357,7 @@ def test_generator_ccds_without_unit(self, triage_setup):
location=triage_setup.test_dir, keywords=['imagetyp'])

with pytest.raises(ValueError):
ccd = next(collection.ccds())
_ = next(collection.ccds())

def test_generator_ccds(self, triage_setup):
collection = ImageFileCollection(
Expand Down Expand Up @@ -433,7 +429,7 @@ def test_dir_with_no_fits_files(self, tmpdir):
empty_dir = tmpdir.mkdtemp()
some_file = empty_dir.join('some_file.txt')
some_file.dump('words')
with catch_warnings() as w:
with pytest.warns(Warning) as w:
collection = ImageFileCollection(location=empty_dir.strpath,
keywords=['imagetyp'])
assert len(w) == 1
Expand Down Expand Up @@ -571,10 +567,6 @@ def test_setting_write_location_to_bad_dest_raises_error(self, tmpdir,
for hdr in ic.headers(save_location=bad_directory.strpath):
pass

def test_no_fits_files_in_collection(self):
with catch_warnings(AstropyUserWarning) as warning_lines:
assert "no FITS files in the collection."

def test_initialization_with_no_keywords(self, triage_setup):
# This test is primarily historical -- the old default for
# keywords was an empty list (it is now the wildcard '*').
Expand Down Expand Up @@ -707,10 +699,6 @@ def test_sorting(self, triage_setup):
for i in range(len(collection.summary)):
assert(collection.summary['file'][i] == collection.files[i])

@pytest.mark.skipif(
_ASTROPY_LT_1_3,
reason="It seems to fail with a TypeError there but because of "
"different reasons (something to do with NumPy).")
def test_sorting_without_key_fails(self, triage_setup):
ic = ImageFileCollection(location=triage_setup.test_dir)
with pytest.raises(ValueError):
Expand All @@ -726,7 +714,7 @@ def test_duplicate_keywords(self, triage_setup):

hdu.writeto(os.path.join(triage_setup.test_dir, 'duplicated.fits'))

with catch_warnings(UserWarning) as w:
with pytest.warns(UserWarning) as w:
ic = ImageFileCollection(triage_setup.test_dir, keywords='*')
assert len(w) == 1
assert 'stupid' in str(w[0].message)
Expand Down Expand Up @@ -977,9 +965,8 @@ def test_less_strict_verify_option(self, triage_setup):
TESTVERI= '2017/02/13-16:51:38 / Test VerifyWarning
"""

with catch_warnings(VerifyWarning) as warning_lines:
with pytest.warns(VerifyWarning):
testh = fits.Header.fromstring(bad_header)
print(testh)

testfits = fits.PrimaryHDU(data=np.ones((10, 10)), header=testh)

Expand All @@ -990,7 +977,6 @@ def test_less_strict_verify_option(self, triage_setup):
overwrite=True)

ic = ImageFileCollection(location=str(path))
print(ic.summary.colnames)

assert bad_fits_name in ic.files

Expand All @@ -1002,8 +988,8 @@ def test_less_strict_verify_option(self, triage_setup):
assert '17/02/13' in ic.summary.colnames

# Try making the summary as in the original bug report
ic = ImageFileCollection(location=str(path),
glob_include='*warnA*')
with pytest.warns(AstropyUserWarning, match='The following header keyword is invalid'):
ic = ImageFileCollection(location=str(path), glob_include='*warnA*')

def test_type_of_empty_collection(self, triage_setup):
# Test for implementation of the suggestion in
Expand All @@ -1015,9 +1001,7 @@ def test_type_of_empty_collection(self, triage_setup):
# with no keys and no files returns None.

# Make a dummy keyword that we then delete.
with catch_warnings(AstropyUserWarning) as warning_lines:
ic = ImageFileCollection(triage_setup.test_dir, keywords=['fafa'])
assert "no FITS files in the collection."
ic = ImageFileCollection(triage_setup.test_dir, keywords=['fafa'])
ic.keywords = []
assert set(ic.summary.colnames) == set(['file'])

Expand All @@ -1027,7 +1011,8 @@ def test_type_of_empty_collection(self, triage_setup):
p.unlink()

# Now the summary should be none
ic = ImageFileCollection(triage_setup.test_dir)
with pytest.warns(AstropyUserWarning, match='no FITS files in the collection'):
ic = ImageFileCollection(triage_setup.test_dir)
assert ic.summary is None
assert ic.keywords == []

Expand Down Expand Up @@ -1114,7 +1099,7 @@ def test_filter_on_regex_escape_characters(self, triage_setup):
# keyword containing these

special_kwds = ['CO+', 'GG420 (1)', 'V|R|I', 'O[III]', 'NaD^2']
for i,kw in enumerate(special_kwds,1):
for i, kw in enumerate(special_kwds, 1):
hdu = fits.PrimaryHDU()
hdu.data = np.zeros((5, 5))
hdu.header['REGEX_FL'] = kw
Expand Down

0 comments on commit 84c0a94

Please sign in to comment.