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-7346: Pytest support #16

Merged
merged 12 commits into from
Sep 1, 2016
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ doc/*.tag
doc/*.inc
doc/doxygen.conf
tests/.tests
tests/.cache
version.py
bin/genCameraRegistry.py
bin/genCoaddRegistry.py
Expand Down
35 changes: 15 additions & 20 deletions tests/getId.py → tests/testGetId.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
#!/usr/bin/env python

#
#
# LSST Data Management System
# Copyright 2008-2015 AURA/LSST.
#
#
# This product includes software developed by the
# LSST Project (http://www.lsst.org/).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the LSST License Statement and
# the GNU General Public License along with this program. If not,
#
# You should have received a copy of the LSST License Statement and
# the GNU General Public License along with this program. If not,
# see <https://www.lsstcorp.org/LegalNotices/>.
#

import unittest
import lsst.utils.tests as utilsTests
import lsst.utils.tests

import lsst.daf.persistence as dafPersist
from lsst.obs.sdss import SdssMapper

class GetIdTestCase(unittest.TestCase):

class GetIdTestCase(lsst.utils.tests.TestCase):
"""Testing butler exposure id retrieval"""

def setUp(self):
Expand Down Expand Up @@ -62,20 +63,14 @@ def testId(self):
self.assertEqual(bits, 34)
self.assertEqual(id, (((1L * 8192) + 2) * 8192) + 3)

#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

def suite():
"""Returns a suite containing all the test cases in this module."""
class TestMemory(lsst.utils.tests.MemoryTestCase):
pass

utilsTests.init()
suites = []
suites += unittest.makeSuite(GetIdTestCase)
suites += unittest.makeSuite(utilsTests.MemoryTestCase)
return unittest.TestSuite(suites)

def run(shouldExit = False):
"""Run the tests"""
utilsTests.run(suite(), shouldExit)
def setup_module(module):
lsst.utils.tests.init()

if __name__ == "__main__":
run(True)
lsst.utils.tests.init()
unittest.main()
24 changes: 12 additions & 12 deletions tests/testSdss.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
import unittest

import lsst.utils
import lsst.utils.tests as utilsTests
import lsst.utils.tests
import lsst.daf.persistence as dafPersist
import lsst.afw.image
import lsst.afw.detection

class SdssMapperTestCase(unittest.TestCase):

class SdssMapperTestCase(lsst.utils.tests.TestCase):
"""A test case for the SdssMapper."""

def testGetDR7(self):
Expand Down Expand Up @@ -72,19 +73,18 @@ def testGetDR7(self):

calib, gain = ref.get("tsField")
self.assertAlmostEqual(calib.getMidTime().get(),
53664.2260706 + 0.5 * 53.907456/3600/24, 7)
53664.2260706 + 0.5 * 53.907456/3600/24, 7)
self.assertAlmostEqual(calib.getExptime(), 53.907456, 6)
self.assertAlmostEqual(gain, 4.72, 2)

def suite():
utilsTests.init()
suites = []
suites += unittest.makeSuite(SdssMapperTestCase)
suites += unittest.makeSuite(utilsTests.MemoryTestCase)
return unittest.TestSuite(suites)

def run(shouldExit=False):
utilsTests.run(suite(), shouldExit)
class TestMemory(lsst.utils.tests.MemoryTestCase):
pass


def setup_module(module):
lsst.utils.tests.init()

if __name__ == "__main__":
run(True)
lsst.utils.tests.init()
unittest.main()
74 changes: 36 additions & 38 deletions tests/testSelectFluxMag0.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,36 @@
import lsst.afw.coord as afwCoord
from lsst.obs.sdss.scaleSdssZeroPoint import ScaleSdssZeroPointTask
from lsst.obs.sdss.selectFluxMag0 import SelectSdssFluxMag0Task
import lsst.log

logger = lsst.log.Log.getLogger("obs_sdss.testSelectFluxMag0")

config = ScaleSdssZeroPointTask.ConfigClass()

# Some of the tests require loading SDSS images from "lsst-db.ncsa.illinois.edu" and
# require a login name and password. If the test is unable to connect to the external data,
# some of the tests are skipped.
noConnection = False
try:
DbAuth.username(config.selectFluxMag0.host, str(config.selectFluxMag0.port))
except Exception, e:
Copy link
Member

Choose a reason for hiding this comment

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

If you are going to write new code please use the modern variant of as e.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, I just missed this one when I copied it.

Copy link
Member

Choose a reason for hiding this comment

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

copied from where? 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The try...except block was originally in the run function before I updated the boilerplate. I modified it slightly but didn't pay attention to the except syntax.

logger.warn("Did not find host={0}, port={1} in your db-auth file; \nWarning generated: {2} ".format(
config.selectFluxMag0.host, str(config.selectFluxMag0.port), e))
noConnection = True


#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
class WrapDataId():
"""A container for dataId that looks like dataRef to computeImageScaler()
"""

def __init__(self, dataId):
self.dataId = dataId

class ScaleSdssZeroPointTaskTestCase(unittest.TestCase):

class ScaleSdssZeroPointTaskTestCase(lsst.utils.tests.TestCase):
"""A test case for ScaleSdssZeroPointTask
"""

def makeTestExposure(self, xNumPix=2060, yNumPix=1967):
"""
Create and return an exposure with wcs. Wcs is chosen such that the exposure is
Expand All @@ -70,7 +88,7 @@ def makeTestExposure(self, xNumPix=2060, yNumPix=1967):
metadata.set("CUNIT2", "deg")
metadata.set("LTV1", -341970)
metadata.set("LTV2", -11412)
#exposure needs a wcs and a bbox
# exposure needs a wcs and a bbox
wcs = afwImage.makeWcs(metadata)
bbox = afwGeom.Box2I(afwGeom.Point2I(341970, 11412), afwGeom.Extent2I(xNumPix, yNumPix))
exposure = afwImage.ExposureF(bbox, wcs)
Expand All @@ -79,6 +97,7 @@ def makeTestExposure(self, xNumPix=2060, yNumPix=1967):
mi.getVariance().set(1.0)
return exposure

@unittest.skipIf(noConnection, "No remote connection to SDSS image database")
def testSelectFluxMag0(self):
"""Test SelectFluxMag0"""
config = SelectSdssFluxMag0Task.ConfigClass()
Expand All @@ -95,7 +114,7 @@ def testSelectFluxMag0(self):
fmInfoList = fmInfoStruct.fluxMagInfoList
self.assertEqual(2, len(fmInfoList))


@unittest.skipIf(noConnection, "No remote connection to SDSS image database")
def testScaleZeroPoint(self):
ZEROPOINT = 27
self.sctrl = afwMath.StatisticsControl()
Expand All @@ -107,66 +126,45 @@ def testScaleZeroPoint(self):
config.selectFluxMag0.database = "test_select_sdss_images"
zpScaler = ScaleSdssZeroPointTask(config=config)


outCalib = zpScaler.getCalib()
self.assertAlmostEqual(outCalib.getMagnitude(1.0), ZEROPOINT)

exposure = self.makeTestExposure()
#create dataId for exposure. Visit is only field needed. Others ignored.
# create dataId for exposure. Visit is only field needed. Others ignored.
exposureId = {'ignore_fake_key': 1234, 'run': 4192, 'filter': 'i'}

#test methods: computeImageScale(), scaleMaskedImage(), getInterpImage()
# test methods: computeImageScale(), scaleMaskedImage(), getInterpImage()
dataRef = WrapDataId(exposureId)
imageScaler = zpScaler.computeImageScaler(exposure,dataRef)
imageScaler = zpScaler.computeImageScaler(exposure, dataRef)
scaleFactorIm = imageScaler.getInterpImage(exposure.getBBox())

predScale = 0.402867736 #image mean for "NATURAL_SPLINE"
predScale = 0.402867736 # image mean for "NATURAL_SPLINE"
self.assertAlmostEqual(afwMath.makeStatistics(scaleFactorIm, afwMath.MEAN, self.sctrl).getValue(),
predScale)

mi = exposure.getMaskedImage()
imageScaler.scaleMaskedImage(mi)
pixel11 = scaleFactorIm.getArray()[1,1]
self.assertAlmostEqual(mi.get(1,1)[0], pixel11) #check image plane scaled
self.assertAlmostEqual(mi.get(1,1)[2], pixel11**2) #check variance plane scaled
pixel11 = scaleFactorIm.getArray()[1, 1]
self.assertAlmostEqual(mi.get(1, 1)[0], pixel11) # check image plane scaled
self.assertAlmostEqual(mi.get(1, 1)[2], pixel11**2) # check variance plane scaled

exposure.setCalib(zpScaler.getCalib())
self.assertAlmostEqual(exposure.getCalib().getFlux(ZEROPOINT), 1.0)


def makeCalib(self, zeroPoint):
calib = afwImage.Calib()
fluxMag0 = 10**(0.4 * zeroPoint)
calib.setFluxMag0(fluxMag0, 1.0)
return calib


def suite():
"""Return a suite containing all the test cases in this module.
"""
utilsTests.init()

suites = [
unittest.makeSuite(ScaleSdssZeroPointTaskTestCase),
unittest.makeSuite(utilsTests.MemoryTestCase),
]

return unittest.TestSuite(suites)


def run(shouldExit=False):
"""Run the tests"""
config = ScaleSdssZeroPointTask.ConfigClass()
try:
DbAuth.username(config.selectFluxMag0.host, str(config.selectFluxMag0.port)),
except Exception, e:
print "Warning: did not find host=%s, port=%s in your db-auth file; or %s " \
"skipping unit tests" % \
(config.selectFluxMag0.host, str(config.selectFluxMag0.port), e)
return
class TestMemory(lsst.utils.tests.MemoryTestCase):
pass

utilsTests.run(suite(), shouldExit)

def setup_module(module):
lsst.utils.tests.init()

if __name__ == "__main__":
run(True)
lsst.utils.tests.init()
unittest.main()