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

DM-12061: Eliminate test warnings in test_methods.py #279

Merged
merged 5 commits into from
Sep 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions tests/test_maskedImageIO.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

from builtins import object
import numpy as np
import pyfits
import astropy.io
Copy link
Member

Choose a reason for hiding this comment

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

Many people do this as import astropy.io.fits as pyfits to minimize code impact but that is probably more confusing in the long term since it leaves the pyfits string in the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed. I considered from astropy.io import fits but felt it was too ambiguous, and in at least one case fits was already used as a variable.

Copy link
Member

Choose a reason for hiding this comment

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

Looking at other AFW code this should be plain import astropy.io.fits (we don't want to import all the other astropy IO plugins).


import lsst.utils
import lsst.utils.tests
Expand Down Expand Up @@ -204,9 +204,9 @@ def testReadWriteXY0(self):
def tmpFits(*hdus):
# Given a list of numpy arrays, create a temporary FITS file that
# contains them as consecutive HDUs. Yield it, then remove it.
hdus = [pyfits.PrimaryHDU(hdus[0])] + [pyfits.ImageHDU(hdu)
for hdu in hdus[1:]]
hdulist = pyfits.HDUList(hdus)
hdus = [astropy.io.fits.PrimaryHDU(hdus[0])] + \
[astropy.io.fits.ImageHDU(hdu) for hdu in hdus[1:]]
hdulist = astropy.io.fits.HDUList(hdus)
tempdir = tempfile.mkdtemp()
try:
filename = os.path.join(tempdir, 'test.fits')
Expand Down
50 changes: 36 additions & 14 deletions tests/test_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# see <http://www.lsstcorp.org/LegalNotices/>.
#
from __future__ import absolute_import, division, print_function
import contextlib
import math
import unittest
import re
Expand All @@ -39,6 +40,20 @@
class TestTestUtils(lsst.utils.tests.TestCase):
"""Test test methods added to lsst.utils.tests.TestCase
"""
def setUp(self):
# unittest did not get `assertWarns` until Python 3.2
self._addedAssertWarns = False
if not hasattr(self, "assertWarns"):
self._addedAssertWarns = True

@contextlib.contextmanager
def nullContextManger(dummy):
yield
self.assertWarns = nullContextManger

def tearDown(self):
if self._addedAssertWarns:
del self.assertWarns

def testAssertAnglesAlmostEqual(self):
"""Test assertAnglesAlmostEqual"""
Expand All @@ -50,11 +65,12 @@ def testAssertAnglesAlmostEqual(self):
maxDiff=0.010001*afwGeom.arcseconds,
)
# sanity-check deprecated version
self.assertAnglesNearlyEqual(
ang0,
ang0 + 0.01*afwGeom.arcseconds,
maxDiff=0.010001*afwGeom.arcseconds,
)
with self.assertWarns(DeprecationWarning):
self.assertAnglesNearlyEqual(
ang0,
ang0 + 0.01*afwGeom.arcseconds,
maxDiff=0.010001*afwGeom.arcseconds,
)
with self.assertRaises(AssertionError):
self.assertAnglesAlmostEqual(
ang0,
Expand Down Expand Up @@ -128,8 +144,9 @@ def testAssertBoxesAlmostEqual(self):
self.assertBoxesAlmostEqual(
box0, box1, maxDiff=radDiff*1.00001)
# sanity-check deprecated version
self.assertBoxesNearlyEqual(
box0, box1, maxDiff=radDiff*1.00001)
with self.assertWarns(DeprecationWarning):
self.assertBoxesNearlyEqual(
box0, box1, maxDiff=radDiff*1.00001)
with self.assertRaises(AssertionError):
self.assertBoxesAlmostEqual(
box0, box1, maxDiff=radDiff*0.99999)
Expand Down Expand Up @@ -158,8 +175,9 @@ def testAssertCoordsAlmostEqual(self):
self.assertCoordsAlmostEqual(
coord0, coord0, maxDiff=1e-7*afwGeom.arcseconds)
# sanity-check deprecated version
self.assertCoordsNearlyEqual(
coord0, coord0, maxDiff=1e-7*afwGeom.arcseconds)
with self.assertWarns(DeprecationWarning):
self.assertCoordsNearlyEqual(
coord0, coord0, maxDiff=1e-7*afwGeom.arcseconds)

for offAng in (0, 45, 90):
offAng = offAng*afwGeom.degrees
Expand Down Expand Up @@ -246,7 +264,8 @@ def testAssertPairsAlmostEqual(self):
for pair0 in ((-5, 4), (-5, 0.001), (0, 0), (49, 0.1)):
self.assertPairsAlmostEqual(pair0, pair0, maxDiff=1e-7)
# sanity-check deprecated version
self.assertPairsNearlyEqual(pair0, pair0, maxDiff=1e-7)
with self.assertWarns(DeprecationWarning):
self.assertPairsNearlyEqual(pair0, pair0, maxDiff=1e-7)
self.assertPairsAlmostEqual(afwGeom.Point2D(*pair0),
afwGeom.Extent2D(*pair0), maxDiff=1e-7)
for diff in ((0.001, 0), (-0.01, 0.03)):
Expand Down Expand Up @@ -285,8 +304,9 @@ def testAssertWcssAlmostEqualOverBBox(self):
self.assertWcsAlmostEqualOverBBox(wcs0, wcs0, bbox,
maxDiffSky=1e-7*afwGeom.arcseconds, maxDiffPix=1e-7)
# sanity-check deprecated version
self.assertWcsNearlyEqualOverBBox(wcs0, wcs0, bbox,
maxDiffSky=1e-7*afwGeom.arcseconds, maxDiffPix=1e-7)
with self.assertWarns(DeprecationWarning):
self.assertWcsNearlyEqualOverBBox(wcs0, wcs0, bbox,
maxDiffSky=1e-7*afwGeom.arcseconds, maxDiffPix=1e-7)
self.assertTrue(afwImage.wcsAlmostEqualOverBBox(wcs0, wcs0, bbox,
maxDiffSky=1e-7*afwGeom.arcseconds, maxDiffPix=1e-7))

Expand Down Expand Up @@ -335,7 +355,8 @@ def checkMaskedImage(self, mi):
self.assertMaskedImagesAlmostEqual(mi0, mi1, atol=0, rtol=0)
self.assertMaskedImagesAlmostEqual(mi0, mi1, atol=0, rtol=0)
# sanity-check deprecated version
self.assertMaskedImagesNearlyEqual(mi1, mi0, atol=0, rtol=0)
with self.assertWarns(DeprecationWarning):
self.assertMaskedImagesNearlyEqual(mi1, mi0, atol=0, rtol=0)
self.assertMaskedImagesAlmostEqual(
mi0.getArrays(), mi1, atol=0, rtol=0)
self.assertMaskedImagesAlmostEqual(
Expand All @@ -349,7 +370,8 @@ def checkMaskedImage(self, mi):
self.assertImagesEqual(plane1, plane0)
self.assertImagesAlmostEqual(plane0, plane1, atol=0, rtol=0)
# sanity-check deprecated version
self.assertImagesNearlyEqual(plane0, plane1, atol=0, rtol=0)
with self.assertWarns(DeprecationWarning):
self.assertImagesNearlyEqual(plane0, plane1, atol=0, rtol=0)
self.assertImagesAlmostEqual(plane1, plane0, atol=0, rtol=0)
self.assertImagesAlmostEqual(
plane0.getArray(), plane1, atol=0, rtol=0)
Expand Down
6 changes: 0 additions & 6 deletions tests/test_simpleTable.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@
from builtins import range
import numpy as np

try:
import pyfits
except ImportError:
pyfits = None
print("WARNING: pyfits not available; some tests will not be run")

import lsst.utils.tests
import lsst.pex.exceptions
import lsst.daf.base
Expand Down
11 changes: 5 additions & 6 deletions tests/test_skyWcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import sys
import unittest

import lsst.afw.coord # needed for assertCoordsNearlyEqual
import lsst.utils.tests
from lsst.afw.coord import IcrsCoord
from lsst.afw.geom import SkyWcs, Extent2D, Point2D, degrees, \
Expand Down Expand Up @@ -151,7 +150,7 @@ def setUp(self):
IcrsCoord(0.00001 * degrees, 45 * degrees),
IcrsCoord(359.99999 * degrees, 45 * degrees),
IcrsCoord(30 * degrees, 89.99999 * degrees),
]
]
orientationList = [
0 * degrees,
0.00001 * degrees,
Expand Down Expand Up @@ -185,10 +184,10 @@ def testGenericWcs(self):
transform = makeWcsPairTransform(wcs1, wcs2)
for point1 in self.points():
point2 = transform.applyForward(point1)
self.assertPairsNearlyEqual(
self.assertPairsAlmostEqual(
transform.applyInverse(point2),
point1)
self.assertCoordsNearlyEqual(
self.assertCoordsAlmostEqual(
wcs1.pixelToSky(point1),
wcs2.pixelToSky(point2))

Expand All @@ -200,8 +199,8 @@ def testSameWcs(self):
for point in self.points():
outPoint1 = transform.applyForward(point)
outPoint2 = transform.applyInverse(outPoint1)
self.assertPairsNearlyEqual(point, outPoint1)
self.assertPairsNearlyEqual(outPoint1, outPoint2)
self.assertPairsAlmostEqual(point, outPoint1)
self.assertPairsAlmostEqual(outPoint1, outPoint2)


class TestMemory(lsst.utils.tests.MemoryTestCase):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_wcsFitsTable.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from __future__ import absolute_import, division, print_function
import unittest

import pyfits
import astropy.io

import lsst.afw.image
import lsst.afw.geom
Expand Down Expand Up @@ -167,7 +167,7 @@ def testExposure(self):
# Manually mess up the headers, so we'd know if we were loading the Wcs from that;
# when there is a WCS in the header and a WCS in the FITS table, we should use the
# latter, because the former might just be an approximation.
fits = pyfits.open(fileName)
fits = astropy.io.fits.open(fileName)
fits[1].header.remove("CTYPE1")
fits[1].header.remove("CTYPE2")
fits.writeto(fileName, clobber=True)
Expand Down
2 changes: 1 addition & 1 deletion ups/afw.table
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ setupRequired(python_future)
setupRequired(log)
setupRequired(pybind11)
setupRequired(astshim)
setupOptional(pyfits)
setupOptional(astropy)
setupOptional(matplotlib)

setupOptional(afwdata)
Expand Down