Coverage for tests/test_linearizeSquared.py: 23%
104 statements
« prev ^ index » next coverage.py v6.4.2, created at 2022-07-20 03:12 -0700
« prev ^ index » next coverage.py v6.4.2, created at 2022-07-20 03:12 -0700
1#
2# LSST Data Management System
3# Copyright 2017 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 unittest
23import pickle
24import logging
26import numpy as np
28import lsst.utils.tests
29import lsst.geom
30import lsst.afw.image as afwImage
31import lsst.afw.cameraGeom as cameraGeom
32from lsst.afw.geom.testUtils import BoxGrid
33from lsst.afw.image.testUtils import makeRampImage
34from lsst.ip.isr import Linearizer
37def refLinearizeSquared(image, detector):
38 """!Basic implementation of squared non-linearization correction
40 corr = uncorr + coeff[0]*uncorr^2
42 @param[in,out] image image to correct in place (an lsst.afw.image.Image of
43 some type)
44 @param[in] detector detector info (an lsst.afw.cameraGeom.Detector)
45 """
46 ampInfoCat = detector.getAmplifiers()
47 for ampInfo in ampInfoCat:
48 bbox = ampInfo.getBBox()
49 sqCoeff = ampInfo.getLinearityCoeffs()[0]
50 viewArr = image.Factory(image, bbox).getArray()
51 viewArr[:] = viewArr + sqCoeff*viewArr**2
54class LinearizeSquaredTestCase(lsst.utils.tests.TestCase):
55 """!Unit tests for LinearizeSquared"""
57 def setUp(self):
58 # the following values are all arbitrary, but sane and varied
59 self.bbox = lsst.geom.Box2I(lsst.geom.Point2I(-31, 22), lsst.geom.Extent2I(100, 85))
60 self.numAmps = (2, 3)
61 self.sqCoeffs = np.array([[0, 5e-6, 2.5e-5], [1e-5, 1.1e-6, 2.1e-5]], dtype=float)
62 self.detector = self.makeDetector()
64 def tearDown(self):
65 # destroy LSST objects so memory test passes
66 self.bbox = None
67 self.detector = None
69 def testBasics(self):
70 """!Test basic functionality of LinearizeSquared
71 """
72 for imageClass in (afwImage.ImageF, afwImage.ImageD):
73 inImage = makeRampImage(bbox=self.bbox, start=-5, stop=2500, imageClass=imageClass)
75 measImage = inImage.Factory(inImage, True)
76 linCorr = Linearizer(detector=self.detector)
77 linRes = linCorr.applyLinearity(image=measImage, detector=self.detector)
78 desNumLinearized = np.sum(self.sqCoeffs.flatten() > 0)
79 self.assertEqual(linRes.numLinearized, desNumLinearized)
80 self.assertEqual(linRes.numAmps, len(self.detector.getAmplifiers()))
82 refImage = inImage.Factory(inImage, True)
83 refLinearizeSquared(image=refImage, detector=self.detector)
85 self.assertImagesAlmostEqual(refImage, measImage)
87 # make sure logging is accepted
88 log = logging.getLogger("lsst.ip.isr.LinearizeSquared")
89 linRes = linCorr.applyLinearity(image=measImage, detector=self.detector, log=log)
91 def testKnown(self):
92 """!Test a few known values
93 """
94 numAmps = (2, 2)
95 bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.Extent2I(4, 4))
96 # make a 4x4 image with 4 identical 2x2 subregions that flatten
97 # to -1, 0, 1, 2
98 im = afwImage.ImageF(bbox)
99 imArr = im.getArray()
100 imArr[:, :] = np.array(((-1, 0, -1, 0),
101 (1, 2, 1, 2),
102 (-1, 0, -1, 0),
103 (1, 2, 1, 2)), dtype=imArr.dtype)
105 sqCoeffs = np.array(((0, 0.11), (-0.15, -12)))
106 detector = self.makeDetector(bbox=bbox, numAmps=numAmps, sqCoeffs=sqCoeffs)
107 ampInfoCat = detector.getAmplifiers()
109 linSq = Linearizer(detector=detector)
110 linSq.applyLinearity(im, detector=detector)
112 # amp 0 has 0 squared coefficient and so makes no correction
113 imArr0 = im.Factory(im, ampInfoCat[0].getBBox()).getArray()
114 linCoeff0 = ampInfoCat[0].getLinearityCoeffs()[0]
115 self.assertEqual(0, linCoeff0)
116 self.assertFloatsAlmostEqual(imArr0.flatten(), (-1, 0, 1, 2))
118 # test all amps
119 for ampInfo in ampInfoCat:
120 imArr = im.Factory(im, ampInfo.getBBox()).getArray()
121 linCoeff = ampInfo.getLinearityCoeffs()[0]
122 expect = np.array((-1 + linCoeff, 0, 1 + linCoeff, 2 + 4*linCoeff), dtype=imArr.dtype)
123 self.assertFloatsAlmostEqual(imArr.flatten(), expect)
125 def testPickle(self):
126 """!Test that a LinearizeSquared can be pickled and unpickled
127 """
128 inImage = makeRampImage(bbox=self.bbox, start=-5, stop=2500)
129 linSq = Linearizer(detector=self.detector)
131 refImage = inImage.Factory(inImage, True)
132 refNumOutOfRange = linSq.applyLinearity(refImage, detector=self.detector)
134 pickledStr = pickle.dumps(linSq)
135 restoredLlt = pickle.loads(pickledStr)
137 measImage = inImage.Factory(inImage, True)
138 measNumOutOfRange = restoredLlt.applyLinearity(measImage, detector=self.detector)
140 self.assertEqual(refNumOutOfRange, measNumOutOfRange)
141 self.assertImagesAlmostEqual(refImage, measImage)
143 def makeDetector(self, bbox=None, numAmps=None, sqCoeffs=None, linearityType="Squared"):
144 """!Make a detector
146 @param[in] bbox bounding box for image
147 @param[n] numAmps x,y number of amplifiers (pair of int)
148 @param[in] sqCoeffs square coefficient for each amplifier (2D array of
149 float)
150 @param[in] detName detector name (a str)
151 @param[in] detID detector ID (an int)
152 @param[in] detSerial detector serial numbe (a str)
153 @param[in] linearityType name of linearity type (a str)
155 @return a detector (an lsst.afw.cameraGeom.Detector)
156 """
157 bbox = bbox if bbox is not None else self.bbox
158 numAmps = numAmps if numAmps is not None else self.numAmps
159 sqCoeffs = sqCoeffs if sqCoeffs is not None else self.sqCoeffs
161 detName = "det_a"
162 detId = 1
163 detSerial = "123"
164 orientation = cameraGeom.Orientation()
165 pixelSize = lsst.geom.Extent2D(1, 1)
167 camBuilder = cameraGeom.Camera.Builder("fakeCam")
168 detBuilder = camBuilder.add(detName, detId)
169 detBuilder.setSerial(detSerial)
170 detBuilder.setBBox(bbox)
171 detBuilder.setOrientation(orientation)
172 detBuilder.setPixelSize(pixelSize)
174 boxArr = BoxGrid(box=bbox, numColRow=numAmps)
175 for i in range(numAmps[0]):
176 for j in range(numAmps[1]):
177 ampInfo = cameraGeom.Amplifier.Builder()
178 ampInfo.setName("amp %d_%d" % (i + 1, j + 1))
179 ampInfo.setBBox(boxArr[i, j])
180 ampInfo.setLinearityType(linearityType)
181 ampInfo.setLinearityCoeffs([sqCoeffs[i, j]])
182 detBuilder.append(ampInfo)
184 return detBuilder
187class MemoryTester(lsst.utils.tests.MemoryTestCase):
188 pass
191def setup_module(module):
192 lsst.utils.tests.init()
195if __name__ == "__main__": 195 ↛ 196line 195 didn't jump to line 196, because the condition on line 195 was never true
196 lsst.utils.tests.init()
197 unittest.main()