Skip to content

Commit

Permalink
Migrate tests to py.test standard
Browse files Browse the repository at this point in the history
RFC-69 mandates that all python unittests be runnable under py.test
to allow for proper collection of test statistics etc... This commit
updates the testColorterm.py and testPhotoCal.py tests to the py.test
standard as outlined in SQR-012.
  • Loading branch information
AstroVPK committed Jun 13, 2016
1 parent c9aab8e commit a10e97a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 48 deletions.
29 changes: 12 additions & 17 deletions tests/testColorterm.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import unittest
import pickle

import lsst.utils.tests as utilsTests
import lsst.utils.tests
from lsst.pipe.tasks.colorterms import Colorterm, ColortermDict, ColortermLibrary, ColortermNotFoundError

# From the last page of http://www.naoj.org/staff/nakata/suprime/illustration/colorterm_report_ver3.pdf
Expand All @@ -51,12 +51,17 @@

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

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

class ColortermTestCase(unittest.TestCase):
"""A test case for MaskedImage"""
def setUp(self):
# A list of simple fake sources. The values are chosen so that the colorterm corrections are
# predictable.
self.sources = (dict(g=0.0, r=0.0, fluxErr_g=0.0, fluxErr_r=0.0, true_g=-0.00928, true_fluxErr_g=0.0),
dict(g=0.0, r=-1.0, fluxErr_g=1.0, fluxErr_r=1.0, true_g=-0.09168,
true_fluxErr_g=0.92129230974756315))
true_fluxErr_g=0.92129230974756315))
self.colorterms = hamamatsu

def tearDown(self):
Expand Down Expand Up @@ -116,21 +121,11 @@ def testPickle(self):
colorterms = pickle.loads(pickle.dumps(self.colorterms))
self.assertEqual(colorterms, self.colorterms)

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

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

utilsTests.init()

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

def run(shouldExit=False):
"""Run the tests"""
utilsTests.run(suite(), shouldExit)
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

if __name__ == "__main__":
run(True)
lsst.utils.tests.init()
unittest.main()
42 changes: 11 additions & 31 deletions tests/testPhotoCal.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@

import os
import unittest

import numpy as np

import lsst.meas.astrom as measAstrom
import lsst.afw.geom as afwGeom
import lsst.afw.table as afwTable
import lsst.afw.image as afwImage
import lsst.utils.tests as utilsTests
import lsst.utils.tests
from lsst.pex.logging import Log
from lsst.pipe.tasks.photoCal import PhotoCalTask, PhotoCalConfig
from lsst.pipe.tasks.colorterms import Colorterm, ColortermDict, ColortermLibrary, ColortermNotFoundError
from lsst.pipe.tasks.colorterms import Colorterm, ColortermDict, ColortermLibrary

import testFindAstrometryNetDataDir as helper

Expand All @@ -56,6 +56,9 @@ def makeRefLoader():
config = measAstrom.LoadAstrometryNetObjectsTask.ConfigClass()
return measAstrom.LoadAstrometryNetObjectsTask(config=config)

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

class PhotoCalTest(unittest.TestCase):

def setUp(self):
Expand All @@ -78,25 +81,20 @@ def setUp(self):

# Set up local astrometry_net_data
helper.setupAstrometryNetDataDir('photocal', rootDir=testDir)

self.refObjLoader = makeRefLoader()

self.res = self.getAstrometrySolution(loglvl=Log.DEBUG)
self.matches = self.res.matches

logLevel = Log.DEBUG
self.log = Log(Log.getDefaultLog(),
'testPhotoCal',
logLevel)

self.schema = self.matches[0].second.schema

self.config = PhotoCalConfig()

# The test and associated data have been prepared on the basis that we
# use the PsfFlux to perform photometry.
self.config.fluxField = "base_PsfFlux_flux"

self.config.doWriteOutput = False # schema is fixed because we already loaded the data

def tearDown(self):
Expand Down Expand Up @@ -149,7 +147,6 @@ def _runTask(self):

def testZeroPoint(self):
""" Test to see if we can compute a photometric zeropoint given a reference task"""
print 'testZeroPoint'
self._runTask()
self.assertGreater(len(self.diff), 50)
self.log.info('%i magnitude differences; mean difference %g; mean abs diff %g' %
Expand All @@ -170,7 +167,6 @@ def testZeroPoint(self):
# mean abs(diff): 0.0516589

self.assertLess(abs(self.zp - 31.3145), 0.05)

self.assertGreater(len(self.fitdiff), 50)
# Tolerances are somewhat arbitrary; they're set simply to avoid regressions, and
# are not based on we'd expect to get given the data quality.
Expand All @@ -179,43 +175,27 @@ def testZeroPoint(self):

def testColorTerms(self):
""" Test to see if we can apply colorterm corrections while computing photometric zeropoints"""
print 'testColorTerms'

# Turn colorterms on. The colorterm library used here is simple - we just apply a 1 mag
#color-independentcolorterm correction to everything. This should change the photometric zeropoint.
# by 1 mag.
self.config.applyColorTerms = True
self.config.colorterms = testColorterms
self.config.photoCatName = "test"
self.config.photoCatName = "testglob" # Check glo expansion
# zerPointOffset is the offset in the zeropoint that we expect from a uniform (i.e. color-independent)
# colorterm correction.
zeroPointOffset = testColorterms.data['test*'].data['i'].c0

self._runTask()

self.assertLess(np.mean(self.diff), 0.6 + zeroPointOffset)

self.log.logdebug('zeropoint: %g' % self.zp)

# zeropoint: 32.3145

self.assertLess(abs(self.zp - (31.3145 + zeroPointOffset)), 0.05)

#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
class MemoryTester(lsst.utils.tests.MemoryTestCase):
pass

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

suites = []
suites += unittest.makeSuite(PhotoCalTest)
suites += unittest.makeSuite(utilsTests.MemoryTestCase)

return unittest.TestSuite(suites)

def run(exit=False):
"""Run the tests"""
utilsTests.run(suite(), exit)
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

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

0 comments on commit a10e97a

Please sign in to comment.