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

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

import os 

import unittest 

 

 

import lsst.utils.tests 

import lsst.utils 

import lsst.afw.geom as afwGeom 

import lsst.afw.image as afwImage 

import lsst.afw.math as afwMath 

from lsst.log import Log 

import lsst.log.utils as logUtils 

import lsst.meas.algorithms as measAlg 

import lsst.ip.diffim as ipDiffim 

import lsst.ip.diffim.diffimTools as diffimTools 

 

verbosity = 4 

logUtils.traceSetAt("ip.diffim", verbosity) 

Log.getLogger('psfMatch').setLevel(Log.INFO) 

 

display = False 

 

# known input images 

try: 

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

except Exception: 

defDataDir = None 

 

 

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

 

# D = I - (K.x.T + bg) 

 

def setUp(self): 

self.config = ipDiffim.ImagePsfMatchTask.ConfigClass() 

self.config.kernel.name = "AL" 

self.subconfig = self.config.kernel.active 

 

# Some of the tests are sensitive to the centroids returned by 

# "stdev" vs "pixel_stdev" 

self.subconfig.detectionConfig.detThresholdType = "stdev" 

 

# Impacts some of the test values 

self.subconfig.constantVarianceWeighting = True 

 

if defDataDir: 

 

defTemplatePath = os.path.join(defDataDir, "DC3a-Sim", "sci", "v5-e0", 

"v5-e0-c011-a00.sci.fits") 

defSciencePath = os.path.join(defDataDir, "DC3a-Sim", "sci", "v26-e0", 

"v26-e0-c011-a00.sci.fits") 

 

self.scienceImage = afwImage.ExposureF(defSciencePath) 

self.templateImage = afwImage.ExposureF(defTemplatePath) 

 

bgConfig = self.subconfig.afwBackgroundConfig 

bgConfig.useApprox = False 

bgConfig.binSize = 512 

diffimTools.backgroundSubtract(bgConfig, 

[self.templateImage.getMaskedImage(), 

self.scienceImage.getMaskedImage()]) 

 

self.offset = 1500 

self.bbox = afwGeom.Box2I(afwGeom.Point2I(0, self.offset), 

afwGeom.Point2I(511, 2046)) 

self.subconfig.spatialKernelOrder = 1 

self.subconfig.spatialBgOrder = 0 

 

# Take a stab at a PSF. This is needed to get the KernelCandidateList if you don't provide one. 

ksize = 21 

sigma = 2.0 

self.psf = measAlg.DoubleGaussianPsf(ksize, ksize, sigma) 

self.scienceImage.setPsf(self.psf) 

self.templateImage.setPsf(self.psf) 

 

def tearDown(self): 

del self.config 

if defDataDir: 

del self.scienceImage 

del self.templateImage 

del self.psf 

 

def testModelType(self): 

self.runModelType(fitForBackground=True) 

self.runModelType(fitForBackground=False) 

 

@unittest.skipIf(not defDataDir, "Warning: afwdata is not set up") 

def runModelType(self, fitForBackground): 

self.subconfig.fitForBackground = fitForBackground 

 

templateSubImage = afwImage.ExposureF(self.templateImage, self.bbox) 

scienceSubImage = afwImage.ExposureF(self.scienceImage, self.bbox) 

 

self.subconfig.spatialModelType = 'chebyshev1' 

psfmatch1 = ipDiffim.ImagePsfMatchTask(config=self.config) 

results1 = psfmatch1.subtractExposures(templateSubImage, scienceSubImage, doWarping=True) 

spatialKernel1 = results1.psfMatchingKernel 

backgroundModel1 = results1.backgroundModel 

 

self.subconfig.spatialModelType = 'polynomial' 

psfmatch2 = ipDiffim.ImagePsfMatchTask(config=self.config) 

results2 = psfmatch2.subtractExposures(templateSubImage, scienceSubImage, doWarping=True) 

spatialKernel2 = results2.psfMatchingKernel 

backgroundModel2 = results2.backgroundModel 

 

# Got the types right? 

self.assertTrue( 

spatialKernel1.getSpatialFunctionList()[0].toString().startswith('Chebyshev1Function2') 

) 

self.assertTrue( 

spatialKernel2.getSpatialFunctionList()[0].toString().startswith('PolynomialFunction2') 

) 

 

# First order term has zero spatial variation and sum = kernel sum 

kp1par0 = spatialKernel1.getSpatialFunctionList()[0].getParameters() 

kp2par0 = spatialKernel2.getSpatialFunctionList()[0].getParameters() 

self.assertAlmostEqual(kp1par0[0], kp2par0[0], delta=1e-5) 

for i in range(1, len(kp1par0)): 

self.assertAlmostEqual(kp1par0[i], 0.0, delta=1e-5) 

self.assertAlmostEqual(kp1par0[i], kp2par0[i], delta=1e-5) 

 

if fitForBackground: 

# Nterms (zeroth order model) 

self.assertEqual(backgroundModel1.getNParameters(), 1) 

self.assertEqual(backgroundModel2.getNParameters(), 1) 

 

# Same value of function coefficients (different to 0.001 level) 

self.assertAlmostEqual(backgroundModel1.getParameters()[0], 

backgroundModel2.getParameters()[0], delta=1e-3) 

 

# Functions evaluate to same value at origin (0.001 difference) 

self.assertAlmostEqual(backgroundModel1(0, 0), backgroundModel2(0, 0), delta=1e-3) 

 

# At at different location within image 

self.assertAlmostEqual(backgroundModel1(10, 10), backgroundModel2(10, 10), delta=1e-3) 

 

else: 

# More improtant is the kernel needs to be then same when realized at a coordinate 

kim1 = afwImage.ImageD(spatialKernel1.getDimensions()) 

kim2 = afwImage.ImageD(spatialKernel2.getDimensions()) 

ksum1 = spatialKernel1.computeImage(kim1, False, 0.0, 0.0) 

ksum2 = spatialKernel2.computeImage(kim2, False, 0.0, 0.0) 

self.assertAlmostEqual(ksum1, ksum2, delta=1e-5) 

for y in range(kim1.getHeight()): 

for x in range(kim1.getHeight()): 

self.assertAlmostEqual(kim1[x, y, afwImage.LOCAL], kim2[x, y, afwImage.LOCAL], 

delta=1e-1) 

 

# Nterms (zeroth order) 

self.assertEqual(backgroundModel1.getNParameters(), 1) 

self.assertEqual(backgroundModel2.getNParameters(), 1) 

 

# Zero value in function 

self.assertAlmostEqual(backgroundModel1.getParameters()[0], 0.0, delta=1e-7) 

self.assertAlmostEqual(backgroundModel2.getParameters()[0], 0.0, delta=1e-7) 

 

# Function evaluates to zero 

self.assertAlmostEqual(backgroundModel1(0, 0), 0.0, delta=1e-7) 

self.assertAlmostEqual(backgroundModel2(0, 0), 0.0, delta=1e-7) 

 

# Spatially... 

self.assertAlmostEqual(backgroundModel1(10, 10), 0.0, delta=1e-7) 

self.assertAlmostEqual(backgroundModel2(10, 10), 0.0, delta=1e-7) 

 

@unittest.skipIf(not defDataDir, "Warning: afwdata is not set up") 

def testWarping(self): 

templateSubImage = afwImage.ExposureF(self.templateImage, self.bbox) 

scienceSubImage = afwImage.ExposureF(self.scienceImage, self.bbox) 

psfmatch = ipDiffim.ImagePsfMatchTask(config=self.config) 

try: 

psfmatch.subtractExposures(templateSubImage, scienceSubImage, doWarping=False) 

except Exception: 

pass 

else: 

self.fail() 

 

def testXY0(self): 

self.runXY0('polynomial') 

self.runXY0('chebyshev1') 

 

@unittest.skipIf(not defDataDir, "Warning: afwdata is not set up") 

def runXY0(self, poly, fitForBackground=False): 

self.subconfig.spatialModelType = poly 

self.subconfig.fitForBackground = fitForBackground 

 

templateSubImage = afwImage.ExposureF(self.templateImage.clone(), self.bbox) 

scienceSubImage = afwImage.ExposureF(self.scienceImage.clone(), self.bbox) 

 

# Have an XY0 

psfmatch = ipDiffim.ImagePsfMatchTask(config=self.config) 

fwhm = psfmatch.getFwhmPix(self.psf) 

results1 = psfmatch.subtractExposures(templateSubImage, scienceSubImage, doWarping=True, 

templateFwhmPix=fwhm, scienceFwhmPix=fwhm) 

spatialKernel1 = results1.psfMatchingKernel 

backgroundModel1 = results1.backgroundModel 

kernelCellSet1 = results1.kernelCellSet 

 

# And then take away XY0 

templateSubImage = afwImage.ExposureF(self.templateImage.clone(), self.bbox) 

scienceSubImage = afwImage.ExposureF(self.scienceImage.clone(), self.bbox) 

templateSubImage.setXY0(afwGeom.Point2I(0, 0)) 

scienceSubImage.setXY0(afwGeom.Point2I(0, 0)) 

 

results2 = psfmatch.subtractExposures(templateSubImage, scienceSubImage, doWarping=True, 

templateFwhmPix=fwhm, scienceFwhmPix=fwhm) 

spatialKernel2 = results2.psfMatchingKernel 

backgroundModel2 = results2.backgroundModel 

kernelCellSet2 = results2.kernelCellSet 

 

# need to count up the candidates first, since its a running tally 

count = 0 

for cell in kernelCellSet1.getCellList(): 

for cand1 in cell.begin(False): 

count += 1 

 

for cell in kernelCellSet1.getCellList(): 

for cand1 in cell.begin(False): 

if cand1.getStatus() == afwMath.SpatialCellCandidate.UNKNOWN: 

continue 

if cand1.getStatus() == afwMath.SpatialCellCandidate.BAD: 

continue 

 

cand2 = kernelCellSet2.getCandidateById(cand1.getId()+count) 

 

# positions are nearly the same (different at the 0.01 pixel level) 

self.assertAlmostEqual(cand1.getXCenter(), cand2.getXCenter(), delta=1e-1) 

self.assertAlmostEqual(cand1.getYCenter(), cand2.getYCenter() + self.offset, delta=1e-1) 

 

# kernels are the same 

im1 = cand1.getKernelImage(ipDiffim.KernelCandidateF.RECENT) 

im2 = cand2.getKernelImage(ipDiffim.KernelCandidateF.RECENT) 

for y in range(im1.getHeight()): 

for x in range(im1.getWidth()): 

self.assertAlmostEqual(im1[x, y, afwImage.LOCAL], im2[x, y, afwImage.LOCAL], 

delta=1e-7) 

 

# Spatial fits are the same 

skp1 = spatialKernel1.getSpatialParameters() 

skp2 = spatialKernel2.getSpatialParameters() 

bgp1 = backgroundModel1.getParameters() 

bgp2 = backgroundModel2.getParameters() 

 

# first term = kernel sum, 0, 0 

self.assertAlmostEqual(skp1[0][0], skp2[0][0], delta=1e-6) 

 

# On other terms, the spatial terms are the same, the zpt terms are different 

for nk in range(1, len(skp1)): 

 

# Zeropoint 

if poly == 'polynomial': 

self.assertNotEqual(skp1[nk][0], skp2[nk][0]) 

elif poly == 'chebyshev1': 

# Cheby remaps coords, so model should be the same! 

self.assertAlmostEqual(skp1[nk][0], skp2[nk][0], delta=1e-4) 

else: 

self.fail() 

 

# Spatial terms 

for np in range(1, len(skp1[nk])): 

self.assertAlmostEqual(skp1[nk][np], skp2[nk][np], delta=1e-4) 

 

for np in range(len(bgp1)): 

self.assertAlmostEqual(bgp1[np], bgp2[np], delta=1e-4) 

 

##### 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()