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-7233: Pytest updates #18

Merged
merged 18 commits into from
Aug 16, 2016
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ doc/*.inc
doc/doxygen.conf
doc/xml
tests/.tests
tests/.cache
version.py
26 changes: 13 additions & 13 deletions tests/testApplyLookupTable.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from lsst.afw.image.testUtils import makeRampImage
from lsst.ip.isr import applyLookupTable

np.random.seed(42)

def referenceApply(image, table, indOffset):
"""!Reference implementation of applyLookupTable
Expand Down Expand Up @@ -38,7 +37,12 @@ def referenceApply(image, table, indOffset):
imArr += table[indArr]
return numBadPoints


class ApplyLookupTableTestCase(lsst.utils.tests.TestCase):

def setUp(self):
np.random.seed(42)

def testBasics(self):
"""!Test basic functionality of applyLookupTable
"""
Expand Down Expand Up @@ -99,21 +103,17 @@ def testKnown(self):
desImArr[0] -= 1
desImArr[-1] += 1
desImArr.shape = imArr.shape
self.assertTrue(np.allclose(desImArr, imArr))
self.assertClose(desImArr, imArr)


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

suites = []
suites += unittest.makeSuite(ApplyLookupTableTestCase)
suites += unittest.makeSuite(lsst.utils.tests.MemoryTestCase)
return unittest.TestSuite(suites)

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


if __name__ == "__main__":
run(True)
lsst.utils.tests.init()
unittest.main()
36 changes: 17 additions & 19 deletions tests/testBiasAndDarkCorrection.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@
#
import unittest

import lsst.utils.tests as tests
import lsst.utils.tests
import lsst.afw.image as afwImage
import lsst.afw.geom as afwGeom
import lsst.ip.isr as ipIsr


class IsrTestCases(unittest.TestCase):

def setUp(self):
self.pmin = afwGeom.Point2I(1,1)
self.pmax = afwGeom.Point2I(10,10)
self.pmin = afwGeom.Point2I(1, 1)
self.pmax = afwGeom.Point2I(10, 10)
self.meanCountsKeyword = "IMMODE"
self.filenameKeyword = "filename"

Expand All @@ -55,10 +57,10 @@ def testBias(self):
ipIsr.biasCorrection(maskedImage, biasexposure.getMaskedImage())

height = maskedImage.getHeight()
width = maskedImage.getWidth()
width = maskedImage.getWidth()
for j in range(height):
for i in range(width):
self.assertEqual(maskedImage.getImage().get(i,j), 9)
self.assertEqual(maskedImage.getImage().get(i, j), 9)

def doDark(self, scaling):
maskedImage = afwImage.MaskedImageF(afwGeom.Box2I(self.pmin, self.pmax))
Expand All @@ -72,11 +74,11 @@ def doDark(self, scaling):

ipIsr.darkCorrection(maskedImage, darkexposure.getMaskedImage(), 1., scaling)

height = maskedImage.getHeight()
width = maskedImage.getWidth()
height = maskedImage.getHeight()
width = maskedImage.getWidth()
for j in range(height):
for i in range(width):
self.assertAlmostEqual(maskedImage.getImage().get(i,j), 10 - 1./scaling, 5)
self.assertAlmostEqual(maskedImage.getImage().get(i, j), 10 - 1./scaling, 5)

def testDark1(self):
self.doDark(scaling=10)
Expand All @@ -88,18 +90,14 @@ def testDark3(self):
self.doDark(scaling=3.7)


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


suites = []
suites += unittest.makeSuite(IsrTestCases)
suites += unittest.makeSuite(tests.MemoryTestCase)
return unittest.TestSuite(suites)
def setup_module(module):
lsst.utils.tests.init()

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

if __name__ == "__main__":
run(True)
lsst.utils.tests.init()
unittest.main()
36 changes: 17 additions & 19 deletions tests/testBrighterFatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@
import unittest
import pickle
import os
import numpy

import lsst.utils.tests as tests
import lsst.utils.tests
import lsst.afw.image as afwImage
import lsst.ip.isr as ipIsr

class BrighterFatterTestCases(unittest.TestCase):

class BrighterFatterTestCases(lsst.utils.tests.TestCase):

def setUp(self):
self.filename = "bf_kernel.pkl"
kernel = afwImage.ImageF(17,17)
kernel.set(9,9,1)
kernel = afwImage.ImageF(17, 17)
kernel.set(9, 9, 1)
kernel.getArray().dump(self.filename)

def tearDown(self):
Expand All @@ -42,7 +43,7 @@ def tearDown(self):
def testBrighterFatterInterface(self):
"""Test brighter fatter correction interface using a delta function kernel on a flat image"""

image = afwImage.ImageF(100,100)
image = afwImage.ImageF(100, 100)
image.set(100)
ref_image = afwImage.ImageF(image, True)

Expand All @@ -51,23 +52,20 @@ def testBrighterFatterInterface(self):

isrTask = ipIsr.IsrTask()
with open(self.filename) as f:
bfKernel = pickle.load(f)
bfKernel = pickle.load(f)

isrTask.brighterFatterCorrection(exp, bfKernel, 5, 100, False)
self.assertTrue(numpy.all(ref_image.getArray() == image.getArray()))
self.assertImagesEqual(ref_image, image)


class MemoryTester(lsst.utils.tests.MemoryTestCase):
pass

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

suites = []
suites += unittest.makeSuite(BrighterFatterTestCases)
suites += unittest.makeSuite(tests.MemoryTestCase)
return unittest.TestSuite(suites)
def setup_module(module):
lsst.utils.tests.init()

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

if __name__ == "__main__":
run(True)
lsst.utils.tests.init()
unittest.main()
45 changes: 22 additions & 23 deletions tests/testDefect.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,22 @@
# see <http://www.lsstcorp.org/LegalNotices/>.
#
import unittest
import numpy as np

import lsst.utils.tests as tests
import lsst.utils.tests
import lsst.afw.image as afwImage
import lsst.meas.algorithms as measAlg
import lsst.afw.geom as afwGeom
import lsst.afw.display.ds9 as ds9
import lsst.afw.display.utils as displayUtils
import lsst.ip.isr as ipIsr

try:
type(display)
except NameError:
display = False
# set to True to display images in ds9
display = False

Choose a reason for hiding this comment

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

Not sure if I like either solution. I assume the former was to allow this to be imported and pick up display if it existed?

Copy link
Contributor

Choose a reason for hiding this comment

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

I have no idea, but it's pretty strange. We don't have a good unified way to set a "display things in tests" variable. lsstDebug is kind of like that, but there are some tickets to rethink that system.



class DefectTestCases(lsst.utils.tests.TestCase):

class DefectTestCases(unittest.TestCase):
def setUp(self):
self.setVal = 10.

Expand All @@ -47,16 +48,16 @@ def testDefectBase(self):
"""Test DefectBases"""

defectList = measAlg.DefectListT()
ccdImage = afwImage.MaskedImageF(250,225)
ccdImage = afwImage.MaskedImageF(250, 225)
ccdImage.set(self.setVal, 0, self.setVal)
#
# Insert some defects into the Ccd
#
for x0, y0, x1, y1 in [
(34, 0, 35, 80 ),
(34, 81, 34, 100),
(34, 0, 35, 80),
(34, 81, 34, 100),
(180, 100, 182, 130),
]:
]:
bbox = afwGeom.Box2I(afwGeom.Point2I(x0, y0), afwGeom.Point2I(x1, y1))
defectList.append(measAlg.Defect(bbox))
bad = ccdImage.Factory(ccdImage, bbox, afwImage.LOCAL)
Expand All @@ -67,7 +68,7 @@ def testDefectBase(self):
bitMask = mask.getPlaneBitMask('BAD')
for d in defectList:
bad = mask.Factory(mask, d.getBBox(), afwImage.LOCAL)
self.assertTrue((bad.getArray()&bitMask == bitMask).all())
self.assertTrue((bad.getArray() & bitMask == bitMask).all())

if display:
ds9.mtv(ccdImage.getImage(), title="Defects")
Expand All @@ -79,7 +80,9 @@ def testDefectBase(self):
im = ccdImage.getImage()
for d in defectList:
intrp = im.Factory(im, d.getBBox())
self.assertTrue((intrp.getArray() == self.setVal).all())
expect = np.empty_like(intrp.getArray())
expect[:] = self.setVal

Choose a reason for hiding this comment

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

Why not expect = np.copy(intrp.getArray())?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think it matters: I'm refilling it with self.setVal. I guess I'd rather just start with an uninitialized array, to ensure it doesn't accidentally use the values it wants to compare against.

self.assertImagesEqual(intrp, expect)

if display:
ds9.mtv(ccdImage.getImage(), title="Defects Interpolated")
Expand Down Expand Up @@ -114,18 +117,14 @@ def testDefectsFromMaskedImage(self):
self.assertEqual(len(defectList), 2)


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


suites = []
suites += unittest.makeSuite(DefectTestCases)
suites += unittest.makeSuite(tests.MemoryTestCase)
return unittest.TestSuite(suites)
def setup_module(module):
lsst.utils.tests.init()

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

if __name__ == "__main__":
run(True)
lsst.utils.tests.init()
unittest.main()
40 changes: 19 additions & 21 deletions tests/testFlatAndIlluminationCorrection.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,17 @@
#
import unittest

import lsst.utils.tests as tests
import lsst.utils.tests
import lsst.afw.image as afwImage
import lsst.afw.geom as afwGeom
import lsst.ip.isr as ipIsr


class IsrTestCases(unittest.TestCase):

def setUp(self):
self.pmin = afwGeom.Point2I(1,1)
self.pmax = afwGeom.Point2I(10,10)
self.pmin = afwGeom.Point2I(1, 1)
self.pmax = afwGeom.Point2I(10, 10)
self.flatScaleKeyword = "IMMODE"
self.filenameKeyword = "filename"

Expand All @@ -42,7 +44,7 @@ def tearDown(self):
del self.filenameKeyword

def doFlat(self, scaling):
maskedImage = afwImage.MaskedImageF(afwGeom.Box2I(self.pmin,self.pmax))
maskedImage = afwImage.MaskedImageF(afwGeom.Box2I(self.pmin, self.pmax))
maskedImage.getImage().set(10)

flat = afwImage.MaskedImageF(afwGeom.Box2I(self.pmin, self.pmax))
Expand All @@ -53,11 +55,11 @@ def doFlat(self, scaling):

ipIsr.flatCorrection(maskedImage, flatexposure.getMaskedImage(), 'USER', scaling)

height = maskedImage.getHeight()
width = maskedImage.getWidth()
height = maskedImage.getHeight()
width = maskedImage.getWidth()
for j in range(height):
for i in range(width):
self.assertAlmostEqual(maskedImage.getImage().get(i,j), 10 / (1./scaling), 5)
self.assertAlmostEqual(maskedImage.getImage().get(i, j), 10 / (1./scaling), 5)

def testFlat1(self):
self.doFlat(scaling=10)
Expand All @@ -80,11 +82,11 @@ def doIllum(self, scaling):

ipIsr.illuminationCorrection(maskedImage, illumexposure.getMaskedImage(), scaling)

height = maskedImage.getHeight()
width = maskedImage.getWidth()
height = maskedImage.getHeight()
width = maskedImage.getWidth()
for j in range(height):
for i in range(width):
self.assertAlmostEqual(maskedImage.getImage().get(i,j), 10 / (1./scaling), 5)
self.assertAlmostEqual(maskedImage.getImage().get(i, j), 10 / (1./scaling), 5)

def testIllum1(self):
self.doIllum(scaling=10)
Expand All @@ -96,18 +98,14 @@ def testIllum3(self):
self.doIllum(scaling=3.7)


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


suites = []
suites += unittest.makeSuite(IsrTestCases)
suites += unittest.makeSuite(tests.MemoryTestCase)
return unittest.TestSuite(suites)
def setup_module(module):
lsst.utils.tests.init()

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

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