Skip to content

Commit

Permalink
Clean up global constants in unit tests
Browse files Browse the repository at this point in the history
Use AfwdataDir as the path to the afwData package.
Add another constant for the path to the desired data file(s),
relative to AfwdataDir (instead of absolute and only defined
if afwdata is setup).
Build the path to the final desired image(s) using os.path.join
when needed.
  • Loading branch information
r-owen committed Aug 20, 2016
1 parent b5ccb6b commit 295d23f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
13 changes: 8 additions & 5 deletions tests/testAddToCoadd.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@
pexLog.Trace_setVerbosity("lsst.coadd.utils", Verbosity)

try:
dataDir = lsst.utils.getPackageDir('afwdata')
medMIPath = os.path.join(dataDir, "data", "med.fits")
AfwdataDir = lsst.utils.getPackageDir('afwdata')
except Exception:
dataDir = None
AfwdataDir = None
# path to a medium-sized MaskedImage, relative to afwdata package root
MedMiSubpath = os.path.join("data", "med.fits")


def slicesFromBox(box, image):
Expand Down Expand Up @@ -230,24 +231,26 @@ def referenceTest(self, coadd, weightMap, image, badPixelMask, weight):
self.fail(errMsg)
return overlapBBox

@unittest.skipUnless(dataDir, "afwdata not available")
@unittest.skipUnless(AfwdataDir, "afwdata not available")
def testMed(self):
"""Test addToCoadd by adding an image with known bad pixels using varying masks
"""
medBBox = afwGeom.Box2I(afwGeom.Point2I(130, 315), afwGeom.Extent2I(20, 21))
medMIPath = os.path.join(AfwdataDir, MedMiSubpath)
maskedImage = afwImage.MaskedImageF(afwImage.MaskedImageF(medMIPath), medBBox)
coadd = afwImage.MaskedImageF(medBBox)
weightMap = afwImage.ImageF(medBBox)
weight = 0.9
for badPixelMask in (0x00, 0xFF):
self.referenceTest(coadd, weightMap, maskedImage, badPixelMask, weight)

@unittest.skipUnless(dataDir, "afwdata not available")
@unittest.skipUnless(AfwdataDir, "afwdata not available")
def testMultSizes(self):
"""Test addToCoadd by adding various subregions of the med image
to a coadd that's a slightly different shape
"""
bbox = afwGeom.Box2I(afwGeom.Point2I(130, 315), afwGeom.Extent2I(30, 31))
medMIPath = os.path.join(AfwdataDir, MedMiSubpath)
fullMaskedImage = afwImage.MaskedImageF(medMIPath)
maskedImage = afwImage.MaskedImageF(fullMaskedImage, bbox)
coaddBBox = afwGeom.Box2I(
Expand Down
22 changes: 13 additions & 9 deletions tests/testCoadd.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,23 @@
pexLog.Trace_setVerbosity("lsst.coadd.utils", Verbosity)

try:
AfwDataDir = lsst.utils.getPackageDir('afwdata')
ImSimFile = os.path.join(AfwDataDir, "ImSim/calexp/v85408556-fr/R23/S11.fits")
AfwdataDir = lsst.utils.getPackageDir('afwdata')
except Exception:
AfwDataDir = None
AfwdataDir = None
# path to LsstSim calexp relative to afwData package root
SimCalexpSubpath = os.path.join("ImSim", "calexp", "v85408556-fr", "R23", "S11.fits")


class CoaddTestCase(lsst.utils.tests.TestCase):
"""A test case for Coadd
"""

@unittest.skipUnless(AfwDataDir, "afwdata not available")
@unittest.skipUnless(AfwdataDir, "afwdata not available")
def testAddOne(self):
"""Add a single exposure; make sure coadd = input, appropriately scaled
"""
inExp = afwImage.ExposureF(ImSimFile)
calexpPath = os.path.join(AfwdataDir, SimCalexpSubpath)
inExp = afwImage.ExposureF(calexpPath)
inMaskedImage = inExp.getMaskedImage()
for badMaskPlanes in (
(),
Expand Down Expand Up @@ -101,11 +103,12 @@ def assertWcsSame(self, wcs1, wcs2):
self.fail("wcs do not match at fromPixPos=%s, sky1=%s: toPixPos1=%s != toPixPos2=%s" %
(fromPixPos, sky1, toPixPos1, toPixPos2))

@unittest.skipUnless(AfwDataDir, "afwdata not available")
@unittest.skipUnless(AfwdataDir, "afwdata not available")
def testGetters(self):
"""Test getters for coadd
"""
inExp = afwImage.ExposureF(ImSimFile)
calexpPath = os.path.join(AfwdataDir, SimCalexpSubpath)
inExp = afwImage.ExposureF(calexpPath)
bbox = inExp.getBBox()
wcs = inExp.getWcs()
for badMaskPlanes, bbox in (
Expand All @@ -126,7 +129,7 @@ def testGetters(self):
self.assertEqual(badPixelMask, coadd.getBadPixelMask())
self.assertWcsSame(wcs, coadd.getWcs())

@unittest.skipUnless(AfwDataDir, "afwdata not available")
@unittest.skipUnless(AfwdataDir, "afwdata not available")
def testFilters(self):
"""Test that the coadd filter is set correctly
"""
Expand All @@ -139,7 +142,8 @@ def testFilters(self):
gFilter = afwImage.Filter("g")
rFilter = afwImage.Filter("r")

inExp = afwImage.ExposureF(ImSimFile, afwGeom.Box2I(afwGeom.Point2I(0, 0), afwGeom.Extent2I(10, 10)),
calexpPath = os.path.join(AfwdataDir, SimCalexpSubpath)
inExp = afwImage.ExposureF(calexpPath, afwGeom.Box2I(afwGeom.Point2I(0, 0), afwGeom.Extent2I(10, 10)),
afwImage.PARENT)
coadd = coaddUtils.Coadd(
bbox=inExp.getBBox(),
Expand Down

0 comments on commit 295d23f

Please sign in to comment.