Coverage for tests/test_isrTask.py : 13%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# # LSST Data Management System # Copyright 2008-2017 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, # see <https://www.lsstcorp.org/LegalNotices/>. #
"""Function to count the number of masked pixels of a given type.
Parameters ---------- maskedImage : `lsst.afw.image.MaskedImage` Image to measure the mask on. maskPlane : `str` Name of the mask plane to count
Returns ------- nMask : `int` Number of masked pixels. """ bitMask = maskedImage.getMask().getPlaneBitMask(maskPlane) isBit = maskedImage.getMask().getArray() & bitMask > 0 numBit = np.sum(isBit) return numBit
"""Function to calculate median and std of image data.
Parameters ---------- image : `lsst.afw.image.Image` Image to measure statistics on.
Returns ------- median : `float` Image median. std : `float` Image stddev. """ median = np.nanmedian(image.getArray()) std = np.nanstd(image.getArray()) return (median, std)
"""Test IsrTask methods with trimmed raw data. """ self.config = IsrTaskConfig() self.config.qa = IsrQaConfig() self.task = IsrTask(config=self.config) self.dataRef = isrMock.DataRefMock() self.camera = isrMock.IsrMock().getCamera()
self.inputExp = isrMock.TrimmedRawMock().run() self.amp = self.inputExp.getDetector()[0] self.mi = self.inputExp.getMaskedImage()
"""results should be a struct with components that are not None if included in the configuration file. """ self.assertIsInstance(results, Struct) if self.config.doBias is True: self.assertIsNotNone(results.bias) if self.config.doDark is True: self.assertIsNotNone(results.dark) if self.config.doFlat is True: self.assertIsNotNone(results.flat) if self.config.doFringe is True: self.assertIsNotNone(results.fringes) if self.config.doDefect is True: self.assertIsNotNone(results.defects) if self.config.doBrighterFatter is True: self.assertIsNotNone(results.bfKernel) if self.config.doAttachTransmissionCurve is True: self.assertIsNotNone(results.opticsTransmission) self.assertIsNotNone(results.filterTransmission) self.assertIsNotNone(results.sensorTransmission) self.assertIsNotNone(results.atmosphereTransmission)
"""Test that all necessary calibration frames are retrieved. """ self.config.doAttachTransmissionCurve = False self.task = IsrTask(config=self.config) results = self.task.readIsrData(self.dataRef, self.inputExp) self.validateIsrData(results)
"""Test that all necessary calibration frames are retrieved. """ self.config.doAttachTransmissionCurve = True self.task = IsrTask(config=self.config) results = self.task.readIsrData(self.dataRef, self.inputExp) self.validateIsrData(results)
"""Test that an exposure has a usable instance class. """ self.assertIsInstance(self.task.ensureExposure(self.inputExp, self.camera, 0), afwImage.Exposure)
"""Test conversion from integer to floating point pixels. """ result = self.task.convertIntToFloat(self.inputExp) self.assertEqual(result.getImage().getArray().dtype, np.dtype("float32")) self.assertEqual(result, self.inputExp)
"""Expect The variance image should have a larger median value after this operation. """ statBefore = computeImageMedianAndStd(self.inputExp.variance[self.amp.getBBox()]) self.task.updateVariance(self.inputExp, self.amp) statAfter = computeImageMedianAndStd(self.inputExp.variance[self.amp.getBBox()]) self.assertGreater(statAfter[0], statBefore[0]) self.assertFloatsAlmostEqual(statBefore[0], 0.0, atol=1e-2) self.assertFloatsAlmostEqual(statAfter[0], 8175.6885, atol=1e-2)
"""Expect the median image value should decrease after this operation. """ darkIm = isrMock.DarkMock().run()
statBefore = computeImageMedianAndStd(self.inputExp.image[self.amp.getBBox()]) self.task.darkCorrection(self.inputExp, darkIm) statAfter = computeImageMedianAndStd(self.inputExp.image[self.amp.getBBox()]) self.assertLess(statAfter[0], statBefore[0]) self.assertFloatsAlmostEqual(statBefore[0], 8075.6885, atol=1e-2) self.assertFloatsAlmostEqual(statAfter[0], 8051.6206, atol=1e-2)
"""Expect the median image value should decrease after this operation. """ darkIm = isrMock.DarkMock().run() darkIm.getInfo().setVisitInfo(None)
statBefore = computeImageMedianAndStd(self.inputExp.image[self.amp.getBBox()]) self.task.darkCorrection(self.inputExp, darkIm) statAfter = computeImageMedianAndStd(self.inputExp.image[self.amp.getBBox()]) self.assertLess(statAfter[0], statBefore[0]) self.assertFloatsAlmostEqual(statBefore[0], 8075.6885, atol=1e-2) self.assertFloatsAlmostEqual(statAfter[0], 8051.6206, atol=1e-2)
"""Expect the image median should increase (divide by < 1). """ flatIm = isrMock.FlatMock().run()
statBefore = computeImageMedianAndStd(self.inputExp.image[self.amp.getBBox()]) self.task.flatCorrection(self.inputExp, flatIm) statAfter = computeImageMedianAndStd(self.inputExp.image[self.amp.getBBox()]) self.assertGreater(statAfter[1], statBefore[1]) self.assertFloatsAlmostEqual(statAfter[1], 147417.36, atol=1e-2) self.assertFloatsAlmostEqual(statBefore[1], 148.28436, atol=1e-2)
"""Expect the saturation level detection/masking to scale with threshold. """ self.amp.setSaturation(9000.0) self.task.saturationDetection(self.inputExp, self.amp) countBefore = countMaskedPixels(self.mi, "SAT")
self.amp.setSaturation(8250.0) self.task.saturationDetection(self.inputExp, self.amp) countAfter = countMaskedPixels(self.mi, "SAT")
self.assertLessEqual(countBefore, countAfter) self.assertEqual(countBefore, 43) self.assertEqual(countAfter, 141)
"""Expect the background measurement runs successfully and to save metadata values. """ self.config.qa.flatness.meshX = 20 self.config.qa.flatness.meshY = 20 self.task.measureBackground(self.inputExp, self.config.qa) self.assertIsNotNone(self.inputExp.getMetadata().getScalar('SKYLEVEL'))
"""Expect the flat context manager runs successfully (applying both flat and dark within the context), and results in the same image data after completion. """ darkExp = isrMock.DarkMock().run() flatExp = isrMock.FlatMock().run()
mi = self.inputExp.getMaskedImage().clone() self.inputExp.writeFits("/tmp/czwBefore.fits") with self.task.flatContext(self.inputExp, flatExp, darkExp): contextStat = computeImageMedianAndStd(self.inputExp.getMaskedImage().getImage()) self.assertFloatsAlmostEqual(contextStat[0], 37269.914, atol=1e-2)
self.assertMaskedImagesAlmostEqual(mi, self.inputExp.getMaskedImage())
"""Test IsrTask methods using untrimmed raw data. """ self.config = IsrTaskConfig() self.config.qa = IsrQaConfig() self.task = IsrTask(config=self.config)
self.mockConfig = isrMock.IsrMockConfig() self.mockConfig.isTrimmed = False self.doGenerateImage = True self.dataRef = isrMock.DataRefMock(config=self.mockConfig) self.camera = isrMock.IsrMock(config=self.mockConfig).getCamera()
self.inputExp = isrMock.RawMock(config=self.mockConfig).run() self.amp = self.inputExp.getDetector()[0] self.mi = self.inputExp.getMaskedImage()
"""Set the configuration state to a consistent value.
Disable options we do not need as well.
Parameters ---------- value : `bool` Value to switch common ISR configuration options to. """ self.config.qa.flatness.meshX = 20 self.config.qa.flatness.meshY = 20 self.config.doWrite = False self.config.doLinearize = False self.config.doCrosstalk = False
self.config.doConvertIntToFloat = value self.config.doSaturation = value self.config.doSuspect = value self.config.doSetBadRegions = value self.config.doOverscan = value self.config.doBias = value self.config.doVariance = value self.config.doWidenSaturationTrails = value self.config.doBrighterFatter = value self.config.doDefect = value self.config.doSaturationInterpolation = value self.config.doDark = value self.config.doStrayLight = value self.config.doFlat = value self.config.doFringe = value self.config.doAddDistortionModel = value self.config.doMeasureBackground = value self.config.doVignette = value self.config.doAttachTransmissionCurve = value self.config.doUseOpticsTransmission = value self.config.doUseFilterTransmission = value self.config.doUseSensorTransmission = value self.config.doUseAtmosphereTransmission = value self.config.qa.saveStats = value self.config.qa.doThumbnailOss = value self.config.qa.doThumbnailFlattened = value
self.config.doApplyGains = not value self.config.doCameraSpecificMasking = value self.config.vignette.doWriteVignettePolygon = value
"""results should be a struct with components that are not None if included in the configuration file.
Returns ------- results : `pipeBase.Struct` Results struct generated from the current ISR configuration. """ self.task = IsrTask(config=self.config) results = self.task.run(self.inputExp, camera=self.camera, bias=self.dataRef.get("bias"), dark=self.dataRef.get("dark"), flat=self.dataRef.get("flat"), bfKernel=self.dataRef.get("bfKernel"), defects=self.dataRef.get("defects"), fringes=Struct(fringes=self.dataRef.get("fringe"), seed=1234), opticsTransmission=self.dataRef.get("transmission_"), filterTransmission=self.dataRef.get("transmission_"), sensorTransmission=self.dataRef.get("transmission_"), atmosphereTransmission=self.dataRef.get("transmission_") )
self.assertIsInstance(results, Struct) self.assertIsInstance(results.exposure, afwImage.Exposure) return results
"""Expect that this should reduce the image variance with a full fit. The default fitType of MEDIAN will reduce the median value.
This needs to operate on a RawMock() to have overscan data to use.
The output types may be different when fitType != MEDIAN. """ statBefore = computeImageMedianAndStd(self.inputExp.image[self.amp.getRawDataBBox()])
oscanResults = self.task.overscanCorrection(self.inputExp, self.amp) self.assertIsInstance(oscanResults, Struct) self.assertIsInstance(oscanResults.imageFit, float) self.assertIsInstance(oscanResults.overscanFit, float) self.assertIsInstance(oscanResults.overscanImage, afwImage.MaskedImageF)
statAfter = computeImageMedianAndStd(self.inputExp.image[self.amp.getRawDataBBox()]) self.assertLess(statAfter[0], statBefore[0])
"""Expect a dataRef to be handled correctly. """ self.config.doLinearize = False self.config.doWrite = False self.task = IsrTask(config=self.config) results = self.task.runDataRef(self.dataRef)
self.assertIsInstance(results, Struct) self.assertIsInstance(results.exposure, afwImage.Exposure)
"""Expect successful run with expected outputs when all non-exclusive configuration options are on.
Output results should be tested more precisely by the individual function tests.
""" self.batchSetConfiguration(True) self.validateIsrResults()
"""Expect successful run with expected outputs when all non-exclusive configuration options are off.
Output results should be tested more precisely by the individual function tests.
""" self.batchSetConfiguration(False) self.validateIsrResults()
"""Expect failure with crosstalk enabled.
Output results should be tested more precisely by the individual function tests. """ self.batchSetConfiguration(True)
# This breaks it self.config.doCrosstalk = True
with self.assertRaises(RuntimeError): self.validateIsrResults()
"""Test masking cases of configuration parameters. """ self.batchSetConfiguration(True) self.config.overscanFitType = "POLY" self.config.overscanOrder = 1
self.config.doSaturation = False self.config.doWidenSaturationTrails = False self.config.doSaturationInterpolation = False self.config.doSuspect = False self.config.doSetBadRegions = False self.config.doDefect = False self.config.doBrighterFatter = False
results = self.validateIsrResults()
self.assertEqual(countMaskedPixels(results.exposure, "SAT"), 0) self.assertEqual(countMaskedPixels(results.exposure, "INTRP"), 0) self.assertEqual(countMaskedPixels(results.exposure, "SUSPECT"), 0) self.assertEqual(countMaskedPixels(results.exposure, "BAD"), 0)
"""Test masking cases of configuration parameters. """ self.batchSetConfiguration(True) self.config.overscanFitType = "POLY" self.config.overscanOrder = 1
self.config.saturation = 20000.0 self.config.doSaturation = True self.config.doWidenSaturationTrails = True
self.config.doSaturationInterpolation = False self.config.doSuspect = False self.config.doSetBadRegions = False self.config.doDefect = False self.config.doBrighterFatter = False
results = self.validateIsrResults()
self.assertEqual(countMaskedPixels(results.exposure, "SAT"), 0) self.assertEqual(countMaskedPixels(results.exposure, "INTRP"), 0) self.assertEqual(countMaskedPixels(results.exposure, "SUSPECT"), 0) self.assertEqual(countMaskedPixels(results.exposure, "BAD"), 0)
"""Test masking cases of configuration parameters. """ self.batchSetConfiguration(True) self.config.overscanFitType = "POLY" self.config.overscanOrder = 1
self.config.saturation = 20000.0 self.config.doSaturation = True self.config.doWidenSaturationTrails = True self.config.doSaturationInterpolation = True
self.config.doSuspect = False self.config.doSetBadRegions = False self.config.doDefect = False self.config.doBrighterFatter = False
results = self.validateIsrResults()
self.assertEqual(countMaskedPixels(results.exposure, "SAT"), 0) self.assertEqual(countMaskedPixels(results.exposure, "INTRP"), 0) self.assertEqual(countMaskedPixels(results.exposure, "SUSPECT"), 0) self.assertEqual(countMaskedPixels(results.exposure, "BAD"), 0)
"""Test masking cases of configuration parameters. """ self.batchSetConfiguration(True) self.config.overscanFitType = "POLY" self.config.overscanOrder = 1
self.config.saturation = 20000.0 self.config.doSaturation = True self.config.doWidenSaturationTrails = True self.config.doSaturationInterpolation = True self.config.numEdgeSuspect = 5 self.config.doSuspect = True
self.config.doSetBadRegions = False self.config.doDefect = False self.config.doBrighterFatter = False
results = self.validateIsrResults()
self.assertEqual(countMaskedPixels(results.exposure, "SAT"), 0) self.assertEqual(countMaskedPixels(results.exposure, "INTRP"), 0) self.assertEqual(countMaskedPixels(results.exposure, "SUSPECT"), 0) self.assertEqual(countMaskedPixels(results.exposure, "BAD"), 0)
"""Test masking cases of configuration parameters. """ self.batchSetConfiguration(True) self.config.overscanFitType = "POLY" self.config.overscanOrder = 1
self.config.saturation = 20000.0 self.config.doSaturation = True self.config.doWidenSaturationTrails = True self.config.doSaturationInterpolation = True self.config.numEdgeSuspect = 5 self.config.doSuspect = True self.config.doDefect = True
self.config.doSetBadRegions = False self.config.doBrighterFatter = False
results = self.validateIsrResults()
self.assertEqual(countMaskedPixels(results.exposure, "SAT"), 0) self.assertEqual(countMaskedPixels(results.exposure, "INTRP"), 2000) self.assertEqual(countMaskedPixels(results.exposure, "SUSPECT"), 3940) self.assertEqual(countMaskedPixels(results.exposure, "BAD"), 2000)
"""Test masking cases of configuration parameters. """ self.batchSetConfiguration(True) self.config.overscanFitType = "POLY" self.config.overscanOrder = 1
self.config.saturation = 20000.0 self.config.doSaturation = True self.config.doWidenSaturationTrails = True self.config.doSaturationInterpolation = True
self.config.doSuspect = True self.config.doDefect = True self.config.doSetBadRegions = True self.config.doBrighterFatter = False
results = self.validateIsrResults()
self.assertEqual(countMaskedPixels(results.exposure, "SAT"), 0) self.assertEqual(countMaskedPixels(results.exposure, "INTRP"), 2000) self.assertEqual(countMaskedPixels(results.exposure, "SUSPECT"), 0) self.assertEqual(countMaskedPixels(results.exposure, "BAD"), 2000)
lsst.utils.tests.init() unittest.main() |