Coverage for tests/test_imagePsf_trampoline.py: 44%
78 statements
« prev ^ index » next coverage.py v6.4.4, created at 2022-09-20 02:34 -0700
« prev ^ index » next coverage.py v6.4.4, created at 2022-09-20 02:34 -0700
1# This file is part of meas_algorithms.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <https://www.gnu.org/licenses/>.
22import pickle
23import unittest
24from copy import deepcopy
26import numpy as np
28import lsst.utils.tests
29from lsst.afw.image import Image, ExposureF
30from lsst.afw.typehandling import StorableHelperFactory
31from lsst.geom import Box2I, Point2I, Extent2I
32from lsst.meas.algorithms import ImagePsf
35class MyTestImagePsf(ImagePsf):
36 _factory = StorableHelperFactory(__name__, "MyTestImagePsf")
38 def __init__(self, image):
39 ImagePsf.__init__(self)
40 self.image = image
42 # "public" virtual overrides
43 def __deepcopy__(self, meta=None):
44 return MyTestImagePsf(self.image)
46 def resized(self, width, height):
47 raise NotImplementedError("resized not implemented for MyTestImagePsf")
49 def isPersistable(self):
50 return True
52 # "private" virtual overrides are underscored
53 def _doComputeKernelImage(self, position=None, color=None):
54 return self.image
56 def _doComputeBBox(self, position=None, color=None):
57 return self.image.getBBox()
59 def _getPersistenceName(self):
60 return "MyTestImagePsf"
62 def _getPythonModule(self):
63 return __name__
65 def _write(self):
66 return pickle.dumps(self.image)
68 @staticmethod
69 def _read(pkl):
70 return MyTestImagePsf(pickle.loads(pkl))
72 def __eq__(self, rhs):
73 if isinstance(rhs, MyTestImagePsf):
74 return np.array_equal(self.image.array, rhs.image.array)
75 return False
78class ImagePsfTrampolineTestSuite(lsst.utils.tests.TestCase):
79 def setUp(self):
80 dimensions = Extent2I(7, 7)
81 self.bbox = Box2I(Point2I(-dimensions/2), dimensions)
82 self.img = Image(self.bbox, dtype=np.float64)
83 x, y = np.ogrid[-3:4, -3:4]
84 rsqr = x**2 + y**2
85 # Some arbitrary circular double Gaussian
86 self.img.array[:] = np.exp(-0.5*rsqr**2) + np.exp(-0.5*rsqr**2/4)
87 self.img.array /= np.sum(self.img.array)
88 self.psf = MyTestImagePsf(self.img)
90 def testImage(self):
91 self.assertImagesEqual(
92 self.img,
93 self.psf.computeImage(self.psf.getAveragePosition())
94 )
95 self.assertImagesEqual(
96 self.img,
97 self.psf.computeKernelImage(self.psf.getAveragePosition())
98 )
100 def testBBox(self):
101 self.assertEqual(
102 self.bbox,
103 self.psf.computeBBox()
104 )
106 def testResized(self):
107 with self.assertRaises(NotImplementedError):
108 self.psf.resized(9, 9)
110 def testClone(self):
111 clone1 = deepcopy(self.psf)
112 clone2 = self.psf.clone()
113 for clone in [clone1, clone2]:
114 self.assertIsNot(clone, self.psf)
115 self.assertImagesEqual(
116 clone.computeImage(clone.getAveragePosition()),
117 self.psf.computeImage(self.psf.getAveragePosition())
118 )
119 self.assertEqual(
120 clone.computeApertureFlux(0.5),
121 self.psf.computeApertureFlux(0.5)
122 )
123 self.assertEqual(
124 clone.computeShape(clone.getAveragePosition()),
125 self.psf.computeShape(self.psf.getAveragePosition())
126 )
128 def testPersistence(self):
129 im = ExposureF(10, 10)
130 im.setPsf(self.psf)
131 self.assertEqual(im.getPsf(), self.psf)
132 with lsst.utils.tests.getTempFilePath(".fits") as tmpFile:
133 im.writeFits(tmpFile)
134 newIm = ExposureF(tmpFile)
135 self.assertEqual(newIm.getPsf(), im.getPsf())
138class MemoryTester(lsst.utils.tests.MemoryTestCase):
139 pass
142def setup_module(module):
143 lsst.utils.tests.init()
146if __name__ == "__main__": 146 ↛ 147line 146 didn't jump to line 147, because the condition on line 146 was never true
147 lsst.utils.tests.init()
148 unittest.main()