Coverage for tests/test_crosstalk.py: 28%

78 statements  

« prev     ^ index     » next       coverage.py v6.4.1, created at 2022-06-28 03:00 -0700

1# 

2# This file is part of obs_decam. 

3# 

4# Developed for the LSST Data Management System. 

5# This product includes software developed by the LSST Project 

6# (http://www.lsst.org). 

7# See the COPYRIGHT file at the top-level directory of this distribution 

8# for details of code ownership. 

9# 

10# This program is free software: you can redistribute it and/or modify 

11# it under the terms of the GNU General Public License as published by 

12# the Free Software Foundation, either version 3 of the License, or 

13# (at your option) any later version. 

14# 

15# This program is distributed in the hope that it will be useful, 

16# but WITHOUT ANY WARRANTY; without even the implied warranty of 

17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

18# GNU General Public License for more details. 

19# 

20# You should have received a copy of the GNU General Public License 

21# along with this program. If not, see <http://www.gnu.org/licenses/>. 

22# 

23 

24import os 

25import warnings 

26import unittest 

27import lsst.utils.tests 

28 

29from lsst.utils import getPackageDir 

30import lsst.geom as geom 

31import lsst.daf.persistence as dafPersist 

32from lsst.ip.isr.isrTask import IsrTask 

33 

34obsDecamDir = getPackageDir('obs_decam') 

35displayDiffs = False 

36 

37 

38def getObsDecamConfig(TaskClass): 

39 """Helper function to get a command-line task config customized by obs_decam. 

40 

41 Borrowed from test_processCcd.py. 

42 """ 

43 config = TaskClass.ConfigClass() 

44 filename = os.path.join(obsDecamDir, 'config', TaskClass._DefaultName + '.py') 

45 if os.path.exists(filename): 

46 config.load(filename) 

47 return config 

48 

49 

50class CrosstalkTestCase(lsst.utils.tests.TestCase): 

51 """A set of tests for DECam crosstalk correction. 

52 """ 

53 

54 def setUp(self): 

55 try: 

56 datadir = getPackageDir('testdata_decam') 

57 except LookupError: 

58 message = "testdata_decam not setup. Skipping." 

59 warnings.warn(message) 

60 raise unittest.SkipTest(message) 

61 

62 self.repoPath = os.path.join(datadir, 'rawData') 

63 self.calibPath = os.path.join(datadir, 'rawData/cpCalib') 

64 self.butler = dafPersist.Butler( 

65 inputs={'root': self.repoPath, 'mapperArgs': {'calibRoot': self.calibPath}}, 

66 outputs=None 

67 ) 

68 self.dataId = {'visit': 229388, 'ccdnum': 1} 

69 # Location of pixels where crosstalk is appreciable in the test image 

70 self.xtalkX = 1810 

71 self.xtalkY = 2970 

72 self.xtalkRad = 100 

73 

74 def tearDown(self): 

75 del self.butler 

76 

77 def runIsr(self, doCrosstalk): 

78 """Run IsrTask with or without crosstalk correction 

79 """ 

80 dataRef = self.butler.dataRef('raw', dataId=self.dataId) 

81 rawExposure = self.butler.get('raw', dataId=self.dataId) 

82 camera = dataRef.get('camera') 

83 config = getObsDecamConfig(IsrTask) 

84 config.doCrosstalk = doCrosstalk 

85 # These configs are necessary because the testdata bias and flat data 

86 # products are from the DECam Community Pipeline. 

87 config.biasDataProductName = 'cpBias' 

88 config.flatDataProductName = 'cpFlat' 

89 decamCpIsrTask = IsrTask(config=config) 

90 isrData = decamCpIsrTask.readIsrData(dataRef, rawExposure) 

91 isrResult = decamCpIsrTask.run(rawExposure, camera=camera, **isrData.getDict()) 

92 return isrResult 

93 

94 def testCrosstalk(self): 

95 """Compare DECam postISR images with and without crosstalk removal. 

96 

97 A region with known crosstalk from the neighbor amp is inspected to 

98 verify the crosstalk is removed, and we also test to see that the 

99 image statistics are altered as expected by crosstalk removal. 

100 This test requires running DecamCpIsrTask twice. 

101 """ 

102 # Run ISR with and without crosstalk correction 

103 expWithoutCrosstalkCorr = self.runIsr(doCrosstalk=False).exposure 

104 expWithCrosstalkCorr = self.runIsr(doCrosstalk=True).exposure 

105 image1 = expWithoutCrosstalkCorr.getMaskedImage().getImage() 

106 image2 = expWithCrosstalkCorr.getMaskedImage().getImage() 

107 

108 # Work with a small image chunk only 

109 pmin = geom.Point2I(self.xtalkX - self.xtalkRad, self.xtalkY - self.xtalkRad) 

110 pmax = geom.Point2I(self.xtalkX + self.xtalkRad, self.xtalkY + self.xtalkRad) 

111 box = geom.Box2I(pmin, pmax) 

112 chunk1 = image1.Factory(image1, box) 

113 chunk2 = image2.Factory(image2, box) 

114 chunkDiff = chunk1.clone() 

115 chunkDiff -= chunk2 

116 

117 # Check that the difference of the two image chunks is nonzero 

118 # (the non-crosstalk-corrected and crosstalk-corrected images should differ) 

119 all_zeros = not chunkDiff.getArray().any() 

120 self.assertFalse(all_zeros) 

121 

122 # Check that the standard deviation with crosstalk signatures still 

123 # present is larger than when it has been removed 

124 self.assertGreater(chunk1.getArray().std(), chunk2.getArray().std()) 

125 

126 # More specific tests for the exact image statistics expected 

127 self.assertAlmostEqual(chunk1.getArray().mean(), 3730.9856, places=2) 

128 self.assertAlmostEqual(chunk2.getArray().mean(), 3727.5760, places=2) 

129 self.assertAlmostEqual(chunkDiff.getArray().mean(), 3.41, places=2) 

130 self.assertAlmostEqual(chunk1.getArray().std(), 33.802116, places=2) 

131 self.assertAlmostEqual(chunk2.getArray().std(), 33.309788, places=2) 

132 self.assertAlmostEqual(chunkDiff.getArray().std(), 5.86, places=2) 

133 

134 # Option to display the portions of the image with/without crosstalk 

135 if displayDiffs: 

136 import lsst.afw.display.ds9 as ds9 

137 ds9.mtv(chunk1, frame=1) 

138 ds9.mtv(chunk2, frame=2) 

139 ds9.mtv(chunkDiff, frame=3) 

140 

141 

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

143 pass 

144 

145 

146def setup_module(module): 

147 lsst.utils.tests.init() 

148 

149 

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

151 lsst.utils.tests.init() 

152 unittest.main()