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

# This file is part of meas_algorithms. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (https://www.lsst.org). 

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

# for details of code ownership. 

# 

# 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 GNU General Public License 

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

 

import math 

import os 

import sys 

import unittest 

 

import lsst.geom 

import lsst.afw.image as afwImage 

import lsst.afw.math as afwMath 

import lsst.log.utils as logUtils 

import lsst.meas.algorithms as algorithms 

import lsst.pex.config as pexConfig 

import lsst.utils 

import lsst.utils.tests 

 

# Increase the number for more verbose messages 

logUtils.traceSetAt("algorithms.CR", 3) 

 

try: 

display 

except NameError: 

display = False 

else: 

import lsst.afw.display as afwDisplay 

afwDisplay.setDefaultMaskTransparency(75) 

 

try: 

afwdataDir = lsst.utils.getPackageDir('afwdata') 

imageFile0 = os.path.join(afwdataDir, "CFHT", "D4", "cal-53535-i-797722_1.fits") 

except Exception: 

imageFile0 = None 

 

 

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

"""A test case for Cosmic Ray detection.""" 

 

def setUp(self): 

self.FWHM = 5 # pixels 

self.psf = algorithms.DoubleGaussianPsf(29, 29, self.FWHM/(2*math.sqrt(2*math.log(2)))) 

 

self.mi = afwImage.MaskedImageF(imageFile0) 

self.XY0 = lsst.geom.PointI(0, 0) # origin of the subimage we use 

 

if imageFile0: 

if True: # use full image, trimmed to data section 

self.XY0 = lsst.geom.PointI(32, 2) 

self.mi = self.mi.Factory(self.mi, lsst.geom.BoxI(self.XY0, lsst.geom.PointI(2079, 4609)), 

afwImage.LOCAL) 

self.mi.setXY0(lsst.geom.PointI(0, 0)) 

self.nCR = 1076 # number of CRs we should detect 

else: # use sub-image 

if True: 

self.XY0 = lsst.geom.PointI(824, 140) 

self.nCR = 10 

else: 

self.XY0 = lsst.geom.PointI(280, 2750) 

self.nCR = 2 

self.mi = self.mi.Factory(self.mi, lsst.geom.BoxI(self.XY0, lsst.geom.ExtentI(256, 256), 

afwImage.LOCAL)) 

self.mi.setXY0(lsst.geom.PointI(0, 0)) 

else: 

self.nCR = None 

 

self.mi.getMask().addMaskPlane("DETECTED") 

 

def tearDown(self): 

del self.mi 

del self.psf 

 

@unittest.skipUnless(imageFile0, "afwdata not available") 

def testDetection(self): 

"""Test CR detection.""" 

# 

# Subtract background 

# 

bctrl = afwMath.BackgroundControl(afwMath.Interpolate.NATURAL_SPLINE) 

bctrl.setNxSample(int(self.mi.getWidth()/256) + 1) 

bctrl.setNySample(int(self.mi.getHeight()/256) + 1) 

bctrl.getStatisticsControl().setNumSigmaClip(3.0) 

bctrl.getStatisticsControl().setNumIter(2) 

 

im = self.mi.getImage() 

try: 

backobj = afwMath.makeBackground(im, bctrl) 

except Exception as e: 

print(e, file=sys.stderr) 

 

bctrl.setInterpStyle(afwMath.Interpolate.CONSTANT) 

backobj = afwMath.makeBackground(im, bctrl) 

 

im -= backobj.getImageF() 

 

if display: 

frame = 0 

disp = afwDisplay.Display(frame=frame) 

disp.mtv(self.mi, title=self._testMethodName + ": Raw") # raw frame 

if self.mi.getWidth() > 256: 

disp.pan(944 - self.mi.getX0(), 260 - self.mi.getY0()) 

# 

# Mask known bad pixels 

# 

measAlgorithmsDir = lsst.utils.getPackageDir('meas_algorithms') 

badPixels = algorithms.Defects.readText(os.path.join(measAlgorithmsDir, 

"policy", "BadPixels.ecsv")) 

# did someone lie about the origin of the maskedImage? If so, adjust bad pixel list 

if self.XY0.getX() != self.mi.getX0() or self.XY0.getY() != self.mi.getY0(): 

dx = self.XY0.getX() - self.mi.getX0() 

dy = self.XY0.getY() - self.mi.getY0() 

for bp in badPixels: 

bp.shift(-dx, -dy) 

 

algorithms.interpolateOverDefects(self.mi, self.psf, badPixels) 

 

stats = afwMath.makeStatistics(self.mi.getImage(), afwMath.MEANCLIP | afwMath.STDEVCLIP) 

background = stats.getValue(afwMath.MEANCLIP) 

 

crConfig = algorithms.FindCosmicRaysConfig() 

crs = algorithms.findCosmicRays(self.mi, self.psf, background, pexConfig.makePolicy(crConfig)) 

 

if display: 

frame += 1 

disp = afwDisplay.Display(frame=frame) 

disp.mtv(self.mi, title=self._testMethodName + ": CRs removed") 

if self.mi.getWidth() > 256: 

disp.pan(944 - self.mi.getX0(), 260 - self.mi.getY0()) 

 

print("Detected %d CRs" % len(crs)) 

if display and False: 

for cr in crs: 

bbox = cr.getBBox() 

bbox.shift(lsst.geom.ExtentI(-self.mi.getX0(), -self.mi.getY0())) 

disp.line([(bbox.getMinX() - 0.5, bbox.getMinY() - 0.5), 

(bbox.getMaxX() + 0.5, bbox.getMinY() - 0.5), 

(bbox.getMaxX() + 0.5, bbox.getMaxY() + 0.5), 

(bbox.getMinX() - 0.5, bbox.getMaxY() + 0.5), 

(bbox.getMinX() - 0.5, bbox.getMinY() - 0.5)]) 

 

if self.nCR is not None: 

self.assertEqual(len(crs), self.nCR) 

 

 

class CosmicRayNullTestCase(unittest.TestCase): 

"""A test case for no Cosmic Ray detection.""" 

 

def setUp(self): 

self.FWHM = 5 # pixels 

self.size = 128 

 

self.psf = algorithms.DoubleGaussianPsf(29, 29, self.FWHM/(2*math.sqrt(2*math.log(2)))) 

self.mi = afwImage.MaskedImageF(128, 128) 

self.mi.set((0, 0, 1)) 

 

def tearDown(self): 

del self.psf 

del self.mi 

 

def testDetection(self): 

crConfig = algorithms.FindCosmicRaysConfig() 

crs = algorithms.findCosmicRays(self.mi, self.psf, 0.0, pexConfig.makePolicy(crConfig)) 

self.assertEqual(len(crs), 0, "Found %d CRs in empty image" % len(crs)) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()