Coverage for tests/test_measureCrosstalk.py: 23%

60 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2024-05-09 04:05 -0700

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 

27from lsst.cp.pipe.measureCrosstalk import CrosstalkExtractTask, CrosstalkExtractConfig 

28from lsst.cp.pipe.measureCrosstalk import CrosstalkSolveTask, CrosstalkSolveConfig 

29import lsst.ip.isr.isrMock as isrMock 

30 

31 

32class MeasureCrosstalkTaskCases(lsst.utils.tests.TestCase): 

33 

34 def setup_measureCrosstalk(self, isTrimmed=False, nSources=8): 

35 """Generate a simulated set of exposures and test the measured 

36 crosstalk matrix. 

37 

38 Parameters 

39 ---------- 

40 isTrimmed : `bool`, optional 

41 Should the simulation use trimmed or untrimmed raw 

42 exposures? 

43 nSources : `int`, optional 

44 Number of random simulated sources to generate in the 

45 simulated exposures. 

46 

47 Returns 

48 ------- 

49 goodFitMask : `np.ndarray` 

50 Array of booleans indicating if the measured and expected 

51 crosstalk ratios are smaller than the measured uncertainty 

52 in the crosstalk ratio. 

53 """ 

54 mockTask = isrMock.CalibratedRawMock() 

55 mockTask.config.rngSeed = 12345 

56 mockTask.config.doGenerateImage = True 

57 mockTask.config.doAddSky = True 

58 mockTask.config.doAddSource = True 

59 mockTask.config.doAddCrosstalk = True 

60 mockTask.config.doAddBias = True 

61 mockTask.config.doAddFringe = False 

62 

63 mockTask.config.skyLevel = 0.0 

64 mockTask.config.biasLevel = 0.0 

65 mockTask.config.readNoise = 100.0 

66 

67 ctexConfig = CrosstalkExtractConfig() 

68 ctexConfig.threshold = 4000 

69 ctexConfig.isTrimmed = isTrimmed 

70 ctex = CrosstalkExtractTask(config=ctexConfig) 

71 fullResult = [] 

72 

73 mockTask.config.isTrimmed = isTrimmed 

74 # Generate simulated set of exposures. 

75 for idx in range(0, 12): 

76 mockTask.config.rngSeed = 12345 + idx * 1000 

77 

78 # Allow each simulated exposure to have nSources random 

79 # bright sources. 

80 mockTask.config.sourceAmp = (np.random.randint(8, size=nSources)).tolist() 

81 mockTask.config.sourceFlux = ((np.random.random(size=nSources) * 25000.0 + 20000.0).tolist()) 

82 mockTask.config.sourceX = ((np.random.random(size=nSources) * 100.0).tolist()) 

83 mockTask.config.sourceY = ((np.random.random(size=nSources) * 50.0).tolist()) 

84 

85 exposure = mockTask.run() 

86 result = ctex.run(exposure) 

87 fullResult.append(result.outputRatios) 

88 

89 # Generate the final measured CT ratios, uncertainties, pixel counts. 

90 ctsConfig = CrosstalkSolveConfig() 

91 cts = CrosstalkSolveTask(config=ctsConfig) 

92 finalResult = cts.run(fullResult) 

93 calib = finalResult.outputCrosstalk 

94 

95 # Needed because measureCrosstalk cannot find coefficients equal to 0.0 

96 coeff = np.nan_to_num(calib.coeffs) 

97 coeffSig = np.nan_to_num(calib.coeffErr) 

98 

99 # Compare result against expectation used to create the simulation. 

100 expectation = isrMock.CrosstalkCoeffMock().run() 

101 goodFitMask = abs(coeff - expectation) <= coeffSig 

102 

103 if not np.all(goodFitMask): 

104 print("Coeff: ", coeff) 

105 print("Expectation: ", expectation) 

106 print("Good Fits: ", goodFitMask) 

107 return goodFitMask 

108 

109 def testMeasureCrosstalkTaskTrimmed(self): 

110 """Measure crosstalk from a sequence of trimmed mocked images. 

111 """ 

112 goodFitMask = self.setup_measureCrosstalk(isTrimmed=True, nSources=8) 

113 

114 self.assertTrue(np.all(goodFitMask)) 

115 

116 def testMeasureCrosstalkTaskUntrimmed(self): 

117 """Measure crosstalk from a sequence of untrimmed mocked images. 

118 """ 

119 goodFitMask = self.setup_measureCrosstalk(isTrimmed=False, nSources=8) 

120 

121 self.assertTrue(np.all(goodFitMask)) 

122 

123 

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

125 pass 

126 

127 

128def setup_module(module): 

129 lsst.utils.tests.init() 

130 

131 

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

133 lsst.utils.tests.init() 

134 unittest.main()