Skip to content

Commit

Permalink
Add makeRampImage
Browse files Browse the repository at this point in the history
Add lsst.afw.image.testUtils.makeRampImage and a unit test
  • Loading branch information
r-owen committed Jun 3, 2016
1 parent 4ac9cfd commit e09cf82
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
20 changes: 20 additions & 0 deletions python/lsst/afw/image/testUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import numpy as np

import lsst.utils.tests
from .imageLib import ImageF
from .basicUtils import makeMaskedImageFromArrays

# the asserts are automatically imported so unit tests can find them without special imports;
Expand All @@ -47,6 +48,25 @@ def makeGaussianNoiseMaskedImage(dimensions, sigma, variance=1.0):

return makeMaskedImageFromArrays(image, mask, variance)

def makeRampImage(bbox, start=0, stop=None, imageClass=ImageF):
"""!Make an image whose values are a linear ramp
@param[in] bbox bounding box of image (an lsst.afw.geom.Box2I)
@param[in] start starting ramp value, inclusive
@param[in] stop ending ramp value, inclusive; if None, increase by integer values
@param[in] imageClass type of image (e.g. lsst.afw.image.ImageF)
"""
im = imageClass(bbox)
imDim = im.getDimensions()
numPix = imDim[0]*imDim[1]
imArr = im.getArray()
if stop is None:
# increase by integer values
stop = start + numPix - 1
rampArr = np.linspace(start=start, stop=stop, endpoint=True, num=numPix, dtype=imArr.dtype)
imArr[:] = np.reshape(rampArr, (imDim[1], imDim[0])) # numpy arrays are transposed w.r.t. afwImage
return im

@lsst.utils.tests.inTestCase
def assertImagesNearlyEqual(testCase, image0, image1, skipMask=None,
rtol=1.0e-05, atol=1e-08, msg="Images differ"):
Expand Down
62 changes: 62 additions & 0 deletions tests/testImageTestUtils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import unittest

import numpy as np

import lsst.utils.tests
import lsst.afw.geom as afwGeom
import lsst.afw.image as afwImage
from lsst.afw.image.testUtils import makeRampImage

class MakeRampImageTestCase(lsst.utils.tests.TestCase):
"""!Unit tests for makeRampImage"""
def testUnitInterval(self):
"""!Test small ramp images with unit interval for known values
"""
for imageClass in (afwImage.ImageU, afwImage.ImageF, afwImage.ImageD):
dim = afwGeom.Extent2I(7, 9)
box = afwGeom.Box2I(afwGeom.Point2I(-1, 3), dim)
numPix = dim[0]*dim[1]
for start in (-5, 0, 4):
if imageClass == afwImage.ImageU and start < 0:
continue
predStop = start + numPix - 1 # for integer steps
for stop in (None, predStop):
rampImage = makeRampImage(bbox=box, start=start, stop=predStop, imageClass=imageClass)
predArr = np.arange(start, predStop+1)
self.assertEqual(len(predArr), numPix)
predArr.shape = (dim[1], dim[0])
self.assertImagesNearlyEqual(rampImage, predArr)

def testNonUnitIntervals(self):
"""!Test a small ramp image with non-integer increments
"""
for imageClass in (afwImage.ImageU, afwImage.ImageF, afwImage.ImageD):
dim = afwGeom.Extent2I(7, 9)
box = afwGeom.Box2I(afwGeom.Point2I(-1, 3), dim)
numPix = dim[0]*dim[1]
for start in (-5.1, 0, 4.3):
if imageClass == afwImage.ImageU and start < 0:
continue
for stop in (7, 1001.5, 5.4):
rampImage = makeRampImage(bbox=box, start=start, stop=stop, imageClass=imageClass)
dtype = rampImage.getArray().dtype
predArr = np.linspace(start, stop, num=numPix, endpoint=True, dtype=dtype)
predArr.shape = (dim[1], dim[0])
self.assertImagesNearlyEqual(rampImage, predArr)


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

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

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

if __name__ == "__main__":
run(True)

0 comments on commit e09cf82

Please sign in to comment.