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 roundtrip(self, ss): 

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

96 """ 

97 with tempfile.NamedTemporaryFile() as f: 

98 ss.writeFits(f.name) 

99 options = PropertyList() 

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

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

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

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

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

105 s2.position.getRa().asDegrees()) 

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

107 s2.position.getDec().asDegrees()) 

108 

109 

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

111 pass 

112 

113 

114def setup_module(module): 

115 lsst.utils.tests.init() 

116 

117 

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

119 lsst.utils.tests.init() 

120 unittest.main()