Coverage for tests/test_empiricalVariance.py: 17%
103 statements
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-12 10:46 +0000
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-12 10:46 +0000
1#
2# LSST Data Management System
3# Copyright 2018 LSST Corporation.
4#
5# This product includes software developed by the
6# LSST Project (http://www.lsst.org/).
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the LSST License Statement and
19# the GNU General Public License along with this program. If not,
20# see <http://www.lsstcorp.org/LegalNotices/>.
21#
22import numpy as np
23import unittest
25import lsst.utils.tests
27from lsst.daf.base import PropertyList
28import lsst.afw.cameraGeom as cameraGeom
29from lsst.geom import Point2I, Extent2I, Box2I, Extent2D
30from lsst.afw.image import ExposureF, VisitInfo
32from lsst.ip.isr.isrTask import IsrTask
35def makeAmplifier(name, bbox, rawImageBox, overscanBox, gain, readNoise, saturation):
36 amp = cameraGeom.Amplifier.Builder()
37 amp.setName(name)
38 amp.setBBox(bbox)
39 amp.setRawDataBBox(rawImageBox)
40 amp.setRawHorizontalOverscanBBox(overscanBox)
41 amp.setGain(gain)
42 amp.setReadNoise(readNoise)
43 amp.setSaturation(saturation)
44 amp.setSuspectLevel(np.nan)
45 return amp
48class EmpiricalVarianceTestCast(lsst.utils.tests.TestCase):
49 def setUp(self):
50 """Constructs a CCD with two amplifiers and prepares for ISR"""
51 np.random.seed(12345)
52 baseValue = 100.0
53 gain = 1.0
54 readNoise = 123456789.0
55 saturation = 987654321.0
56 height = 234
57 imageSize = Extent2I(123, height)
58 overscanSize = Extent2I(16, height)
59 self.sigma = 1.234
61 # Set up the various regions
62 overscan1 = Box2I(Point2I(0, 0), overscanSize)
63 image1 = Box2I(Point2I(overscanSize[0], 0), imageSize)
64 image2 = Box2I(Point2I(overscanSize[0] + imageSize[0], 0), imageSize)
65 overscan2 = Box2I(Point2I(overscanSize[0] + 2*imageSize[0], 0), overscanSize)
67 leftBox = Box2I(overscan1.getMin(), Extent2I(overscan1.getWidth() + image1.getWidth(), height))
68 rightBox = Box2I(image2.getMin(), Extent2I(image2.getWidth() + overscan2.getWidth(), height))
70 target1 = Box2I(Point2I(0, 0), imageSize)
71 target2 = Box2I(Point2I(image1.getWidth(), 0), imageSize)
73 # Set the pixels
74 exposure = ExposureF(Box2I(Point2I(0, 0), Extent2I(imageSize[0]*2 + overscanSize[0]*2, height)))
75 yy = np.arange(0, height, 1, dtype=np.float32)
76 leftImage = ExposureF(exposure, leftBox)
77 leftImage.image.array[:] = baseValue + yy[:, np.newaxis]
78 rightImage = ExposureF(exposure, rightBox)
79 rightImage.image.array[:] = baseValue - yy[:, np.newaxis]
81 leftOverscan = ExposureF(exposure, overscan1)
82 leftOverscan.image.array += np.random.normal(0.0, self.sigma, leftOverscan.image.array.shape)
83 rightOverscan = ExposureF(exposure, overscan2)
84 rightOverscan.image.array += np.random.normal(0.0, self.sigma, leftOverscan.image.array.shape)
85 exposure.mask.array[:] = 0.0
86 exposure.variance.array[:] = np.nan
88 # Construct the detectors
89 amp1 = makeAmplifier("left", target1, image1, overscan1, gain, readNoise, saturation)
90 amp2 = makeAmplifier("right", target2, image2, overscan2, gain, readNoise, saturation)
91 ccdBox = Box2I(Point2I(0, 0), Extent2I(image1.getWidth() + image2.getWidth(), height))
92 camBuilder = cameraGeom.Camera.Builder("fakeCam")
93 detBuilder = camBuilder.add("detector", 1)
94 detBuilder.setSerial("det1")
95 detBuilder.setBBox(ccdBox)
96 detBuilder.setPixelSize(Extent2D(1.0, 1.0))
97 detBuilder.setOrientation(cameraGeom.Orientation())
98 detBuilder.append(amp1)
99 detBuilder.append(amp2)
100 cam = camBuilder.finish()
101 exposure.setDetector(cam.get('detector'))
103 header = PropertyList()
104 header.add("EXPTIME", 0.0)
105 exposure.getInfo().setVisitInfo(VisitInfo(header))
107 self.exposure = exposure
108 self.config = IsrTask.ConfigClass()
110 # Disable everything we don't care about
111 self.config.doBias = False
112 self.config.doDark = False
113 self.config.doFlat = False
114 self.config.doFringe = False
115 self.config.doDefect = False
116 self.config.doWrite = False
117 self.config.expectWcs = False
118 self.config.doLinearize = False
119 self.config.doCrosstalk = False
120 self.config.doBrighterFatter = False
121 self.config.doAttachTransmissionCurve = False
122 self.config.doAssembleCcd = False
123 self.config.doNanMasking = False
124 self.config.doInterpolate = False
126 self.config.maskNegativeVariance = False # This runs on mocks.
127 # Set the things that match our test setup
128 self.config.overscan.fitType = "CHEB"
129 self.config.overscan.order = 1
130 self.config.doEmpiricalReadNoise = True
132 self.task = IsrTask(config=self.config)
134 def tearDown(self):
135 del self.exposure
137 def testEmpiricalVariance(self):
138 results = self.task.run(self.exposure)
139 postIsr = results.exposure
141 self.assertFloatsEqual(postIsr.mask.array, 0)
142 # Image is not exactly zero because the noise in the overscan (required
143 # to be able to set the empirical variance) leads to a slight
144 # misestimate in the polynomial fit.
145 self.assertFloatsAlmostEqual(np.median(postIsr.image.array), 0.0, atol=5.0e-2)
146 self.assertFloatsAlmostEqual(np.nanmedian(postIsr.variance.array), self.sigma**2, rtol=5.0e-2)
149class MemoryTester(lsst.utils.tests.MemoryTestCase):
150 pass
153def setup_module(module):
154 lsst.utils.tests.init()
157if __name__ == "__main__": 157 ↛ 158line 157 didn't jump to line 158, because the condition on line 157 was never true
158 import sys
159 setup_module(sys.modules[__name__])
160 unittest.main()