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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

# 

# LSST Data Management System 

# Copyright 2017 LSST Corporation. 

# 

# This product includes software developed by the 

# LSST Project (http://www.lsst.org/). 

# 

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

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

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

# (at your option) any later version. 

# 

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

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

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the LSST License Statement and 

# the GNU General Public License along with this program. If not, 

# see <http://www.lsstcorp.org/LegalNotices/>. 

# 

 

import unittest 

import itertools 

 

import numpy as np 

 

import lsst.utils.tests 

import lsst.afw.image 

import lsst.afw.table 

import lsst.afw.cameraGeom 

 

from lsst.afw.table import LL, LR, UL, UR 

from lsst.pipe.base import Struct 

from lsst.ip.isr import (IsrTask, subtractCrosstalk, extractCrosstalkRatios, measureCrosstalkCoefficients, 

MeasureCrosstalkTask) 

 

try: 

debug 

except NameError: 

debug = False 

else: 

import lsst.afw.display 

display = lsst.afw.display.Display(backend="ds9", frame=1) 

 

 

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

def setUp(self): 

width, height = 250, 500 

self.numAmps = 4 

numPixelsPerAmp = 1000 

# crosstalk[i][j] is the fraction of the j-th amp present on the i-th amp. 

self.crosstalk = [[0.0, 1e-4, 2e-4, 3e-4], 

[3e-4, 0.0, 2e-4, 1e-4], 

[4e-4, 5e-4, 0.0, 6e-4], 

[7e-4, 8e-4, 9e-4, 0.0]] 

self.value = 12345 

self.crosstalkStr = "XTLK" 

 

# A bit of noise is important, because otherwise the pixel distributions are razor-thin 

# and then rejection doesn't work 

rng = np.random.RandomState(12345) 

self.noise = rng.normal(0.0, 0.1, (2*height, 2*width)) 

 

# Create amp images 

withoutCrosstalk = [lsst.afw.image.ImageF(width, height) for _ in range(self.numAmps)] 

for image in withoutCrosstalk: 

image.set(0) 

xx = rng.randint(0, width, numPixelsPerAmp) 

yy = rng.randint(0, height, numPixelsPerAmp) 

image.getArray()[yy, xx] = self.value 

 

# Add in crosstalk 

withCrosstalk = [image.Factory(image, True) for image in withoutCrosstalk] 

for ii, iImage in enumerate(withCrosstalk): 

for jj, jImage in enumerate(withoutCrosstalk): 

value = self.crosstalk[ii][jj] 

iImage.scaledPlus(value, jImage) 

 

# Put amp images together 

def construct(imageList): 

image = lsst.afw.image.ImageF(2*width, 2*height) 

image.getArray()[:height, :width] = imageList[0].getArray() 

image.getArray()[:height, width:] = imageList[1].getArray()[:, ::-1] # flip in x 

image.getArray()[height:, :width] = imageList[2].getArray()[::-1, :] # flip in y 

image.getArray()[height:, width:] = imageList[3].getArray()[::-1, ::-1] # flip in x and y 

image.getArray()[:] += self.noise 

return image 

 

# Create amp info 

schema = lsst.afw.table.AmpInfoTable.makeMinimalSchema() 

amplifiers = lsst.afw.table.AmpInfoCatalog(schema) 

for ii, (xx, yy, corner) in enumerate([(0, 0, LL), 

(width, 0, LR), 

(0, height, UL), 

(width, height, UR)]): 

amp = amplifiers.addNew() 

amp.setName("amp %d" % ii) 

amp.setBBox(lsst.afw.geom.Box2I(lsst.afw.geom.Point2I(xx, yy), 

lsst.afw.geom.Extent2I(width, height))) 

amp.setRawDataBBox(lsst.afw.geom.Box2I(lsst.afw.geom.Point2I(xx, yy), 

lsst.afw.geom.Extent2I(width, height))) 

amp.setReadoutCorner(corner) 

 

# Put everything together 

ccd = lsst.afw.cameraGeom.Detector("detector", 123, lsst.afw.cameraGeom.SCIENCE, "serial", 

lsst.afw.geom.Box2I(lsst.afw.geom.Point2I(0, 0), 

lsst.afw.geom.Extent2I(2*width, 2*height)), 

amplifiers, lsst.afw.cameraGeom.Orientation(), 

lsst.afw.geom.Extent2D(1, 1), {}, 

np.array(self.crosstalk, dtype=np.float32)) 

 

self.exposure = lsst.afw.image.makeExposure(lsst.afw.image.makeMaskedImage(construct(withCrosstalk))) 

self.exposure.setDetector(ccd) 

 

self.corrected = construct(withoutCrosstalk) 

 

118 ↛ 119line 118 didn't jump to line 119, because the condition on line 118 was never true if debug: 

display.mtv(self.exposure) 

display.incrDefaultFrame() 

display.mtv(self.corrected) 

display.incrDefaultFrame() 

 

def tearDown(self): 

del self.exposure 

del self.corrected 

 

def checkCoefficients(self, coeff, coeffErr, coeffNum): 

"""Check that coefficients are as expected 

 

Parameters 

---------- 

coeff : `numpy.ndarray` 

Crosstalk coefficients. 

coeffErr : `numpy.ndarray` 

Crosstalk coefficient errors. 

coeffNum : `numpy.ndarray` 

Number of pixels to produce each coefficient. 

""" 

for matrix in (coeff, coeffErr, coeffNum): 

self.assertEqual(matrix.shape, (self.numAmps, self.numAmps)) 

for ii in range(self.numAmps): 

self.assertEqual(matrix[ii, ii], 0) 

self.assertFloatsAlmostEqual(coeff, np.array(self.crosstalk), atol=1.0e-6) 

145 ↛ exitline 145 didn't run the generator expression on line 145 self.assertTrue(np.all(coeffErr[ii, jj] > 0 for ii, jj in 

itertools.product(range(self.numAmps), range(self.numAmps)) if ii != jj)) 

147 ↛ exitline 147 didn't run the generator expression on line 147 self.assertTrue(np.all(coeffNum[ii, jj] > 0 for ii, jj in 

itertools.product(range(self.numAmps), range(self.numAmps)) if ii != jj)) 

 

def checkSubtracted(self, exposure): 

"""Check that the subtracted image is as expected 

 

Parameters 

---------- 

exposure : `lsst.afw.image.Exposure` 

Crosstalk-subtracted exposure. 

""" 

image = exposure.getMaskedImage().getImage() 

mask = exposure.getMaskedImage().getMask() 

self.assertFloatsAlmostEqual(image.getArray(), self.corrected.getArray(), atol=2.0e-2) 

self.assertIn(self.crosstalkStr, mask.getMaskPlaneDict()) 

self.assertGreater((mask.getArray() & mask.getPlaneBitMask(self.crosstalkStr) > 0).sum(), 0) 

 

def testDirectAPI(self): 

"""Test that individual function calls work""" 

ratios = extractCrosstalkRatios(self.exposure, threshold=self.value - 1) 

coeff, coeffErr, coeffNum = measureCrosstalkCoefficients(ratios) 

self.checkCoefficients(coeff, coeffErr, coeffNum) 

subtractCrosstalk(self.exposure, minPixelToMask=self.value - 1, 

crosstalkStr=self.crosstalkStr) 

self.checkSubtracted(self.exposure) 

 

def testTaskAPI(self): 

"""Test that the Tasks work 

 

Checks both MeasureCrosstalkTask and the CrosstalkTask. 

""" 

# make exposure available to NullIsrTask 

# without NullIsrTask's `self` hiding this test class's `self` 

exposure = self.exposure 

 

class NullIsrTask(IsrTask): 

def runDataRef(self, dataRef): 

return Struct(exposure=exposure) 

 

config = MeasureCrosstalkTask.ConfigClass() 

config.isr.retarget(NullIsrTask) 

config.threshold = self.value - 1 

measure = MeasureCrosstalkTask(config=config) 

fakeDataRef = Struct(dataId={'fake': 1}) 

coeff, coeffErr, coeffNum = measure.reduce([measure.run(fakeDataRef)]) 

self.checkCoefficients(coeff, coeffErr, coeffNum) 

 

config = IsrTask.ConfigClass() 

config.crosstalk.minPixelToMask = self.value - 1 

config.crosstalk.crosstalkMaskPlane = self.crosstalkStr 

isr = IsrTask(config=config) 

isr.crosstalk.run(self.exposure) 

self.checkSubtracted(self.exposure) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

210 ↛ 211line 210 didn't jump to line 211, because the condition on line 210 was never trueif __name__ == "__main__": 

import sys 

setup_module(sys.modules[__name__]) 

unittest.main()