Coverage for tests / test_isrMockLSST.py: 23%

75 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-24 08:28 +0000

1# 

2# LSST Data Management System 

3# Copyright 2008-2017 AURA/LSST. 

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 <https://www.lsstcorp.org/LegalNotices/>. 

21# 

22 

23import unittest 

24import numpy as np 

25 

26import lsst.utils.tests 

27 

28import lsst.afw.image as afwImage 

29import lsst.ip.isr.isrMockLSST as isrMockLSST 

30from lsst.ip.isr.brighterFatterKernel import BrighterFatterKernel 

31 

32 

33class IsrMockLSSTCases(lsst.utils.tests.TestCase): 

34 """Test the generation of IsrMockLSST data. 

35 """ 

36 def setUp(self): 

37 self.inputExp = isrMockLSST.TrimmedRawMockLSST().run() 

38 self.mi = self.inputExp.maskedImage 

39 

40 self.defects = isrMockLSST.DefectMockLSST().run() 

41 self.nonDefectPixels = self.getNonDefectPixels(self.mi.mask) 

42 

43 def test_simple(self): 

44 """Check trimmed raw data are generated as expected, 

45 taking the same approach as in test_isrMock. 

46 """ 

47 initialMean = np.median(self.mi.image.array[self.nonDefectPixels]) 

48 initialStd = np.std(self.mi.image.array[self.nonDefectPixels]) 

49 

50 # Build and subtract a bias calibration 

51 biasAdu = isrMockLSST.BiasMockLSST(adu=True).run() 

52 self.mi.image.array[:] = (self.mi.image.array[:] - biasAdu.image.array[:]) 

53 newMean = np.median(self.mi.image.array[self.nonDefectPixels]) 

54 newStd = np.std(self.mi.image.array[self.nonDefectPixels]) 

55 

56 self.assertFloatsAlmostEqual(newStd, initialStd, rtol=1e-5) 

57 

58 initialMean = newMean 

59 initialStd = newStd 

60 

61 darkAdu = isrMockLSST.DarkMockLSST(adu=True).run() 

62 self.mi.image.array[:] = (self.mi.image.array[:] 

63 - darkAdu.image.array[:]) 

64 newMean = np.median(self.mi.image.array[self.nonDefectPixels]) 

65 newStd = np.std(self.mi.image.array[self.nonDefectPixels]) 

66 

67 self.assertLess(newMean, initialMean) 

68 

69 initialMean = newMean 

70 initialStd = newStd 

71 

72 flatAdu = isrMockLSST.FlatMockLSST(adu=True).run() 

73 self.mi.image.array[:] = (self.mi.image.array[:] 

74 - flatAdu.image.array[:]) 

75 newMean = np.median(self.mi.image.array[self.nonDefectPixels]) 

76 newStd = np.std(self.mi.image.array[self.nonDefectPixels]) 

77 

78 self.assertAlmostEqual(newMean, initialMean, -2) 

79 self.assertLess(newStd, initialStd) 

80 

81 initialMean = newMean 

82 initialStd = newStd 

83 

84 fringeAdu = isrMockLSST.FringeMockLSST(adu=True).run() 

85 self.mi.image.array[:] = (self.mi.image.array[:] 

86 - fringeAdu.image.array[:]) 

87 newMean = np.median(self.mi.image.array[self.nonDefectPixels]) 

88 newStd = np.std(self.mi.image.array[self.nonDefectPixels]) 

89 

90 self.assertAlmostEqual(newMean, initialMean, -1) 

91 self.assertAlmostEqual(newStd, initialStd, -1) 

92 

93 # TODO: add tests that bias is consistent, etc. 

94 

95 def test_untrimmedSimple(self): 

96 """Test untrimmed mocks are genetared. 

97 """ 

98 exposureLowNoise = isrMockLSST.RawMockLSST().run() 

99 

100 rawMock = isrMockLSST.RawMockLSST() 

101 rawMock.config.readNoise = 100. 

102 exposureHighNoise = rawMock.run() 

103 

104 lowNoiseStd = np.std(exposureLowNoise.image.array[:]) 

105 highNoiseStd = np.std(exposureHighNoise.image.array[:]) 

106 

107 self.assertLess(lowNoiseStd, highNoiseStd) 

108 

109 def test_productTypes(self): 

110 """Tests non-image data are returned as the expected type, 

111 taking the same approach as in test_isrMock. 

112 """ 

113 self.assertIsInstance(isrMockLSST.BfKernelMockLSST().run(), BrighterFatterKernel) 

114 self.assertIsInstance(isrMockLSST.CrosstalkCoeffMockLSST().run(), np.ndarray) 

115 

116 self.assertIsInstance(isrMockLSST.DefectMockLSST().run()[0], lsst.meas.algorithms.Defect) 

117 self.assertIsInstance(isrMockLSST.TransmissionMockLSST().run(), afwImage.TransmissionCurve) 

118 

119 def test_edgeCases(self): 

120 """Tests that improperly specified configurations do not return data, 

121 taking the same approach as in test_isrMock 

122 """ 

123 config = isrMockLSST.IsrMockLSSTConfig() 

124 self.assertIsNone(isrMockLSST.IsrMockLSST(config=config).run()) 

125 

126 with self.assertRaises(RuntimeError): 

127 config.doGenerateData = True 

128 isrMockLSST.IsrMockLSST(config=config).run() 

129 

130 def getNonDefectPixels(self, maskOrigin): 

131 """Get the non-defect pixels to compare. 

132 

133 Parameters 

134 ---------- 

135 maskOrigin : `lsst.afw.image.MaskX` 

136 The origin mask (for shape and type). 

137 

138 Returns 

139 ------- 

140 pix_x, pix_y : `tuple` [`np.ndarray`] 

141 x and y values of good pixels. 

142 """ 

143 maskTemp = maskOrigin.clone() 

144 maskTemp[:, :] = 0 

145 

146 for defect in self.defects: 

147 maskTemp[defect.getBBox()] = 1 

148 

149 return np.where(maskTemp.array == 0) 

150 

151 

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

153 pass 

154 

155 

156def setup_module(module): 

157 lsst.utils.tests.init() 

158 

159 

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

161 lsst.utils.tests.init() 

162 unittest.main()