Skip to content

Commit

Permalink
Most unit tests pass. Ccd assembly still needs work.
Browse files Browse the repository at this point in the history
Also, there are no unit tests for IsrTask methods.
  • Loading branch information
r-owen committed Apr 25, 2012
1 parent 2d7d9d9 commit dd64d97
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 265 deletions.
3 changes: 1 addition & 2 deletions python/lsst/ip/isr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

from .version import *
from .isrLib import *
from .assembleCcdTask import *
from .isr import *
from .assembleCcdTask import *
from .isrTask import *

36 changes: 21 additions & 15 deletions python/lsst/ip/isr/assembleCcdTask.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,26 @@
__all__ = ["AssembleCcdTask"]

class AssembleCcdConfig(pexConfig.Config):
setGain = pexConfig.ConfigField(dtype = bool, default = True,
doc = "set gain?")
doRenorm = pexConfig.ConfigField(dtype = bool, default = True,
doc = "renormalize to a gain of 1? (ignored if setGain false)")
doTrim = pexConfig.ConfigField(dtype = bool, default = True,
doc = "trim out non-data regions?")
keysToRemove = pexConfig.ListField(dtype = str, default = (),
doc = "FITS headers to remove (in addition to DATASEC, BIASSEC, TRIMSEC and perhaps GAIN)")
setGain = pexConfig.Field(
doc = "set gain?",
dtype = bool,
default = True,
)
doRenorm = pexConfig.Field(
doc = "renormalize to a gain of 1? (ignored if setGain false)",
dtype = bool,
default = True,
)
doTrim = pexConfig.Field(
doc = "trim out non-data regions?",
dtype = bool,
default = True,
)
keysToRemove = pexConfig.ListField(
doc = "FITS headers to remove (in addition to DATASEC, BIASSEC, TRIMSEC and perhaps GAIN)",
dtype = str,
default = (),
)


class AssembleCcdTask(pipeBase.Task):
Expand All @@ -47,13 +59,7 @@ class AssembleCcdTask(pipeBase.Task):
def __init__(self, **kwargs):
pipeBase.Task.__init__(self, **kwargs)

if self.config.setGain and self.config.doRenorm:
#Don't want to remove GAIN keyword as it is set by the renormalization.
self.allKeysToRemove = ['DATASEC', 'BIASSEC', 'TRIMSEC']
else:
self.allKeysToRemove = ['DATASEC', 'BIASSEC', 'TRIMSEC', 'GAIN']
for key in self.config.keysToRemove:
self.allKeysToRemove.append(key)
self.allKeysToRemove = ('DATASEC', 'BIASSEC', 'TRIMSEC', 'GAIN') + tuple(self.config.keysToRemove)

def run(self, inExposure):
"""Assemble a CCD by trimming non-data areas
Expand Down
10 changes: 5 additions & 5 deletions python/lsst/ip/isr/isr.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ def calculateSdqaCcdRatings(maskedImage, metadata):
badmaskim = afwImage.ImageU(badmask.getBBox(afwImage.PARENT))
badmaskim <<= badmask
thresh = afwDetection.Threshold(0.5)
fs = afwDetection.makeFootprintSet(satmaskim, thresh)
fs = afwDetection.FootprintSet(satmaskim, thresh)
for f in fs.getFootprints():
metrics['nSaturatePix'] += f.getNpix()
fs = afwDetection.makeFootprintSet(badmaskim, thresh)
fs = afwDetection.FootprintSet(badmaskim, thresh)
for f in fs.getFootprints():
metrics['nBadCalibPix'] += f.getNpix()
stats = afwMath.makeStatistics(maskedImage, afwMath.MEANCLIP | \
Expand Down Expand Up @@ -125,7 +125,7 @@ def calculateSdqaAmpRatings(maskedImage, metadata, biasBBox, dataBBox):
satmaskim = afwImage.ImageU(satmask.getBBox(afwImage.PARENT))
satmaskim <<= satmask
thresh = afwDetection.Threshold(0.5)
fs = afwDetection.makeFootprintSet(satmaskim, thresh)
fs = afwDetection.FootprintSet(satmaskim, thresh)
for f in fs.getFootprints():
metrics['nSaturatePix'] += f.getNpix()
stats = afwMath.makeStatistics(biasmi, afwMath.MEAN | \
Expand Down Expand Up @@ -232,7 +232,7 @@ def getDefectListFromMask(maskedImage, maskName, growFootprints=1):
thresh = afwDetection.Threshold(0.5)
maskimg = afwImage.ImageU(workmask.getBBox(afwImage.PARENT))
maskimg <<= workmask
ds = afwDetection.makeFootprintSet(maskimg, thresh)
ds = afwDetection.FootprintSet(maskimg, thresh)
fpList = ds.getFootprints()
return defectListFromFootprintList(fpList, growFootprints)

Expand All @@ -246,7 +246,7 @@ def makeThresholdMask(maskedImage, threshold, growFootprints=1, maskName = 'SAT'
"""
# find saturated regions
thresh = afwDetection.Threshold(threshold)
ds = afwDetection.makeFootprintSet(maskedImage, thresh)
ds = afwDetection.FootprintSet(maskedImage, thresh)
fpList = ds.getFootprints()
# set mask
mask = maskedImage.getMask()
Expand Down
4 changes: 2 additions & 2 deletions python/lsst/ip/isr/isrTask.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,10 @@ def maskAndInterpNan(self, exposure):
unc.apply(exposure.getMaskedImage())
nnans = unc.getNpix()
self.metadata.set("NUMNANS", nnans)
if not nnans == 0:
raise RuntimeError("There were %i unmasked NaNs"%(nnans))

#get footprints of bad pixels not in the camera class
if nnans > 0:
self.log.log(self.log.WARN, "There were %i unmasked NaNs" % (nnans,))
undefects = isr.getDefectListFromMask(exposure.getMaskedImage(), maskName='UNMASKEDNAN', growFootprints=grow)
#interpolate all bad pixels
if self.transposeForInterpolation:
Expand Down
73 changes: 25 additions & 48 deletions tests/testBiasAndDarkCorrection.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -21,88 +21,66 @@
# the GNU General Public License along with this program. If not,
# see <http://www.lsstcorp.org/LegalNotices/>.
#

import os

import unittest
import lsst.utils.tests as tests

import eups
import lsst.afw.detection as afwDetection
import lsst.utils.tests as tests
import lsst.afw.image as afwImage
import lsst.afw.geom as afwGeom
import lsst.pex.policy as pexPolicy
from lsst.ip.isr import Isr
import lsst.pex.logging as logging

import lsst.afw.display.ds9 as ds9

Verbosity = 4
logging.Trace_setVerbosity('lsst.ip.isr', Verbosity)

isrDir = eups.productDir('ip_isr')


# Policy file
InputIsrPolicy = os.path.join(isrDir, 'pipeline', 'isrPolicy.paf')
import lsst.ip.isr as ipIsr

class IsrTestCases(unittest.TestCase):

def setUp(self):
self.policy = pexPolicy.Policy.createPolicy(InputIsrPolicy)
self.pmin = afwGeom.Point2I(1,1)
self.pmax = afwGeom.Point2I(10,10)
self.isr = Isr()
self.meanCountsKeyword = "IMMODE"
self.filenameKeyword = "filename"

def tearDown(self):
del self.policy
del self.pmin
del self.pmax
del self.isr
del self.meanCountsKeyword
del self.filenameKeyword

def testBias(self):
meanCountsKeyword = self.policy.getString('biasPolicy.meanCountsKeyword')
filenameKeyword = self.policy.getString('filenameKeyword')
mi = afwImage.MaskedImageF(afwGeom.Box2I(self.pmin, self.pmax))
mi.getImage().set(10)
exposure = afwImage.ExposureF(mi, afwImage.Wcs())
maskedImage = afwImage.MaskedImageF(afwGeom.Box2I(self.pmin, self.pmax))
maskedImage.getImage().set(10)
exposure = afwImage.ExposureF(maskedImage, afwImage.Wcs())

bias = afwImage.MaskedImageF(afwGeom.Box2I(self.pmin, self.pmax))
bias.getImage().set(1)
biasexposure = afwImage.ExposureF(bias, afwImage.Wcs())
bmetadata = biasexposure.getMetadata()
bmetadata.setDouble(meanCountsKeyword, 1.)
bmetadata.setString(filenameKeyword, 'Unittest Bias')
bmetadata.setDouble(self.meanCountsKeyword, 1.)
bmetadata.setString(self.filenameKeyword, 'Unittest Bias')

self.isr.biasCorrection(exposure.getMaskedImage(), biasexposure.getMaskedImage())
ipIsr.biasCorrection(maskedImage, biasexposure.getMaskedImage())

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

def doDark(self, scaling):
filenameKeyword = self.policy.getString('filenameKeyword')

mi = afwImage.MaskedImageF(afwGeom.Box2I(self.pmin, self.pmax))
mi.getImage().set(10)
exposure = afwImage.ExposureF(mi, afwImage.Wcs())
maskedImage = afwImage.MaskedImageF(afwGeom.Box2I(self.pmin, self.pmax))
maskedImage.getImage().set(10)
exposure = afwImage.ExposureF(maskedImage, afwImage.Wcs())
metadata = exposure.getMetadata()

dark = afwImage.MaskedImageF(afwGeom.Box2I(self.pmin, self.pmax))
dark.getImage().set(1)
darkexposure = afwImage.ExposureF(dark, afwImage.Wcs())
dmetadata = darkexposure.getMetadata()
dmetadata.setString(filenameKeyword, 'Unittest Dark')
dmetadata.setString(self.filenameKeyword, 'Unittest Dark')

self.isr.darkCorrection(exposure.getMaskedImage(), darkexposure.getMaskedImage(), 1., scaling)
ipIsr.darkCorrection(maskedImage, darkexposure.getMaskedImage(), 1., scaling)

height = mi.getHeight()
width = mi.getWidth()
height = maskedImage.getHeight()
width = maskedImage.getWidth()
for j in range(height):
for i in range(width):
self.assertAlmostEqual(mi.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 @@ -112,9 +90,8 @@ def testDark2(self):

def testDark3(self):
self.doDark(scaling=3.7)

#####



def suite():
"""Returns a suite containing all the test cases in this module."""
tests.init()
Expand Down
15 changes: 8 additions & 7 deletions tests/testCcdAssemble.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
# the GNU General Public License along with this program. If not,
# see <http://www.lsstcorp.org/LegalNotices/>.
#

import os

import unittest
Expand All @@ -46,12 +45,11 @@
afwDir = eups.productDir('afw')

# Policy file
cameraPolicy = os.path.join(afwDir, 'tests', 'TestCameraGeom.paf')
CameraPolicyPath = os.path.join(afwDir, 'tests', 'TestCameraGeom.paf')

class IsrTestCases(unittest.TestCase):

def setUp(self):
self.cameraPolicy = cameraGeomUtils.getGeomPolicy(cameraPolicy)
self.cameraPolicy = cameraGeomUtils.getGeomPolicy(CameraPolicyPath)
afwImage.Filter.reset()
afwImage.FilterProperty.reset()

Expand All @@ -75,8 +73,12 @@ def testCcdAssemble(self):
exp.setFilter(afwImage.Filter("g"))
exp.getCalib().setExptime(15)
exposureList.append(exp)
assembler = ipIsr.CcdAssembler(exposureList, reNorm = False, isTrimmed=True)
aexp = assembler.assembleCcd()

assemblerConfig = ipIsr.AssembleCcdTask.ConfigClass()
assemblerConfig.reNorm = False
assembler = ipIsr.AssembleCcdTask(config=assemblerConfig)

aexp = assembler.run(exposureList).exposure
xpos = [50,150]
ypos = [25,76,137,188]
ind = 0
Expand All @@ -87,7 +89,6 @@ def testCcdAssemble(self):
if display:
ds9.mtv(aexp)

#####

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

0 comments on commit dd64d97

Please sign in to comment.