Coverage for tests/test_imagePsf_trampoline.py: 41%

79 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-06 04:25 -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/>. 

21 

22import pickle 

23import unittest 

24from copy import deepcopy 

25 

26import numpy as np 

27 

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 

33 

34 

35class MyTestImagePsf(ImagePsf): 

36 _factory = StorableHelperFactory(__name__, "MyTestImagePsf") 

37 

38 def __init__(self, image): 

39 ImagePsf.__init__(self) 

40 self.image = image 

41 

42 # "public" virtual overrides 

43 def __deepcopy__(self, meta=None): 

44 return MyTestImagePsf(self.image) 

45 

46 def resized(self, width, height): 

47 raise NotImplementedError("resized not implemented for MyTestImagePsf") 

48 

49 def isPersistable(self): 

50 return True 

51 

52 # "private" virtual overrides are underscored 

53 def _doComputeKernelImage(self, position=None, color=None): 

54 return self.image 

55 

56 def _doComputeBBox(self, position=None, color=None): 

57 return self.image.getBBox() 

58 

59 def _getPersistenceName(self): 

60 return "MyTestImagePsf" 

61 

62 def _getPythonModule(self): 

63 return __name__ 

64 

65 def _write(self): 

66 return pickle.dumps(self.image) 

67 

68 @staticmethod 

69 def _read(pkl): 

70 return MyTestImagePsf(pickle.loads(pkl)) 

71 

72 def __eq__(self, rhs): 

73 if isinstance(rhs, MyTestImagePsf): 

74 return np.array_equal(self.image.array, rhs.image.array) 

75 return False 

76 

77 

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) 

89 self.averagePosition = self.psf.getAveragePosition() 

90 

91 def testImage(self): 

92 self.assertImagesEqual( 

93 self.img, 

94 self.psf.computeImage(self.averagePosition) 

95 ) 

96 self.assertImagesEqual( 

97 self.img, 

98 self.psf.computeKernelImage(self.averagePosition) 

99 ) 

100 

101 def testBBox(self): 

102 self.assertEqual( 

103 self.bbox, 

104 self.psf.computeBBox(self.averagePosition) 

105 ) 

106 

107 def testResized(self): 

108 with self.assertRaises(NotImplementedError): 

109 self.psf.resized(9, 9) 

110 

111 def testClone(self): 

112 clone1 = deepcopy(self.psf) 

113 clone2 = self.psf.clone() 

114 for clone in [clone1, clone2]: 

115 self.assertIsNot(clone, self.psf) 

116 self.assertImagesEqual( 

117 clone.computeImage(clone.getAveragePosition()), 

118 self.psf.computeImage(self.averagePosition) 

119 ) 

120 self.assertEqual( 

121 clone.computeApertureFlux(0.5, self.averagePosition), 

122 self.psf.computeApertureFlux(0.5, self.averagePosition) 

123 ) 

124 self.assertEqual( 

125 clone.computeShape(clone.getAveragePosition()), 

126 self.psf.computeShape(self.averagePosition) 

127 ) 

128 

129 def testPersistence(self): 

130 im = ExposureF(10, 10) 

131 im.setPsf(self.psf) 

132 self.assertEqual(im.getPsf(), self.psf) 

133 with lsst.utils.tests.getTempFilePath(".fits") as tmpFile: 

134 im.writeFits(tmpFile) 

135 newIm = ExposureF(tmpFile) 

136 self.assertEqual(newIm.getPsf(), im.getPsf()) 

137 

138 

139class MemoryTester(lsst.utils.tests.MemoryTestCase): 

140 pass 

141 

142 

143def setup_module(module): 

144 lsst.utils.tests.init() 

145 

146 

147if __name__ == "__main__": 147 ↛ 148line 147 didn't jump to line 148, because the condition on line 147 was never true

148 lsst.utils.tests.init() 

149 unittest.main()