Hide keyboard shortcuts

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

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 unittest 

23import numpy as np 

24import tempfile 

25 

26from lsst.meas.algorithms import stamps 

27from lsst.afw import image as afwImage 

28from lsst.daf.base import PropertyList 

29import lsst.geom as geom 

30import lsst.utils.tests 

31 

32np.random.seed(90210) 

33 

34 

35def make_stamps(n_stamps=3): 

36 stampSize = 25 

37 # create dummy stamp stamps 

38 stampImages = [afwImage.maskedImage.MaskedImageF(stampSize, stampSize) 

39 for _ in range(n_stamps)] 

40 for stampIm in stampImages: 

41 stampImArray = stampIm.image.array 

42 stampImArray += np.random.rand(stampSize, stampSize) 

43 stampMaskArray = stampIm.mask.array 

44 stampMaskArray += 10 

45 stampVarArray = stampIm.variance.array 

46 stampVarArray += 1000. 

47 ras = np.random.rand(n_stamps)*360. 

48 decs = np.random.rand(n_stamps)*180 - 90 

49 stamp_list = [stamps.Stamp(stamp_im=stampIm, 

50 position=geom.SpherePoint(geom.Angle(ra, geom.degrees), 

51 geom.Angle(dec, geom.degrees))) 

52 for stampIm, ra, dec in zip(stampImages, ras, decs)] 

53 metadata = PropertyList() 

54 metadata['RA_DEG'] = ras 

55 metadata['DEC_DEG'] = decs 

56 

57 return stamps.Stamps(stamp_list, metadata=metadata) 

58 

59 

60class StampsTestCase(lsst.utils.tests.TestCase): 

61 """Test Stamps. 

62 """ 

63 def testAppend(self): 

64 """Test ability to append to a Stamps object 

65 """ 

66 ss = make_stamps() 

67 s = ss[-1] 

68 ss.append(s) 

69 self.roundtrip(ss) 

70 # check if appending something other than a Stamp raises 

71 with self.assertRaises(ValueError): 

72 ss.append('hello world') 

73 

74 def testExtend(self): 

75 ss = make_stamps() 

76 ss2 = make_stamps() 

77 ss.extend([s for s in ss2]) 

78 # check if extending with something other than a Stamps 

79 # object raises 

80 with self.assertRaises(ValueError): 

81 ss.extend(['hello', 'world']) 

82 

83 def testIO(self): 

84 """Test the class' write and readFits methods. 

85 """ 

86 self.roundtrip(make_stamps()) 

87 

88 def testIOone(self): 

89 """Test the class' write and readFits methods for the special case of 

90 one stamp. 

91 """ 

92 self.roundtrip(make_stamps(1)) 

93 

94 def testIOsub(self): 

95 """Test the class' write and readFits when passing on a bounding box. 

96 """ 

97 bbox = geom.Box2I(geom.Point2I(3, 9), geom.Extent2I(11, 7)) 

98 ss = make_stamps() 

99 with tempfile.NamedTemporaryFile() as f: 

100 ss.writeFits(f.name) 

101 options = {'bbox': bbox} 

102 subStamps = stamps.Stamps.readFitsWithOptions(f.name, options) 

103 for s1, s2 in zip(ss, subStamps): 

104 self.assertEqual(bbox.getDimensions(), s2.stamp_im.getDimensions()) 

105 self.assertMaskedImagesAlmostEqual(s1.stamp_im[bbox], s2.stamp_im) 

106 

107 def roundtrip(self, ss): 

108 """Round trip a Stamps object to disk and check values 

109 """ 

110 with tempfile.NamedTemporaryFile() as f: 

111 ss.writeFits(f.name) 

112 options = PropertyList() 

113 ss2 = stamps.Stamps.readFitsWithOptions(f.name, options) 

114 self.assertEqual(len(ss), len(ss2)) 

115 for s1, s2 in zip(ss, ss2): 

116 self.assertMaskedImagesAlmostEqual(s1.stamp_im, s2.stamp_im) 

117 self.assertAlmostEqual(s1.position.getRa().asDegrees(), 

118 s2.position.getRa().asDegrees()) 

119 self.assertAlmostEqual(s1.position.getDec().asDegrees(), 

120 s2.position.getDec().asDegrees()) 

121 

122 

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

124 pass 

125 

126 

127def setup_module(module): 

128 lsst.utils.tests.init() 

129 

130 

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

132 lsst.utils.tests.init() 

133 unittest.main()