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

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

# 

# LSST Data Management System 

# Copyright 2016-2017 AURA/LSST. 

# 

# 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 <https://www.lsstcorp.org/LegalNotices/>. 

import unittest 

 

import numpy as np 

 

import lsst.utils.tests 

import lsst.afw.image as afwImage 

import lsst.afw.math as afwMath 

import lsst.geom as geom 

import lsst.meas.algorithms as measAlg 

 

from test_imageDecorrelation import makeFakeImages 

 

from lsst.ip.diffim.zogy import ZogyTask, ZogyConfig, ZogyMapReduceConfig, \ 

ZogyImagePsfMatchConfig, ZogyImagePsfMatchTask 

from lsst.ip.diffim.imageMapReduce import ImageMapReduceTask 

 

try: 

type(verbose) 

except NameError: 

verbose = False 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

"""A test case for the Zogy task. 

""" 

 

def setUp(self): 

self.psf1_sigma = 3.3 # sigma of psf of science image 

self.psf2_sigma = 2.2 # sigma of psf of template image 

 

self.statsControl = afwMath.StatisticsControl() 

self.statsControl.setNumSigmaClip(3.) 

self.statsControl.setNumIter(3) 

self.statsControl.setAndMask(afwImage.Mask 

.getPlaneBitMask(["INTRP", "EDGE", "SAT", "CR", 

"DETECTED", "BAD", 

"NO_DATA", "DETECTED_NEGATIVE"])) 

 

def _setUpImages(self, svar=100., tvar=100., varyPsf=0.): 

"""Generate a fake aligned template and science image. 

""" 

self.svar = svar # variance of noise in science image 

self.tvar = tvar # variance of noise in template image 

 

seed = 666 

self.im1ex, self.im2ex \ 

= makeFakeImages(size=(255, 257), svar=self.svar, tvar=self.tvar, 

psf1=self.psf1_sigma, psf2=self.psf2_sigma, 

n_sources=10, psf_yvary_factor=varyPsf, 

seed=seed, verbose=False) 

# Create an array corresponding to the "expected" subtraction (noise only) 

np.random.seed(seed) 

self.expectedSubtraction = np.random.normal(scale=np.sqrt(svar), size=self.im1ex.getDimensions()) 

self.expectedSubtraction -= np.random.normal(scale=np.sqrt(tvar), size=self.im2ex.getDimensions()) 

self.expectedVar = np.var(self.expectedSubtraction) 

self.expectedMean = np.mean(self.expectedSubtraction) 

 

def _computeVarianceMean(self, maskedIm): 

statObj = afwMath.makeStatistics(maskedIm.getVariance(), 

maskedIm.getMask(), afwMath.MEANCLIP, 

self.statsControl) 

mn = statObj.getValue(afwMath.MEANCLIP) 

return mn 

 

def _computePixelVariance(self, maskedIm): 

statObj = afwMath.makeStatistics(maskedIm, afwMath.VARIANCECLIP, 

self.statsControl) 

var = statObj.getValue(afwMath.VARIANCECLIP) 

return var 

 

def _computePixelMean(self, maskedIm): 

statObj = afwMath.makeStatistics(maskedIm, afwMath.MEANCLIP, 

self.statsControl) 

var = statObj.getValue(afwMath.MEANCLIP) 

return var 

 

def tearDown(self): 

del self.im1ex 

del self.im2ex 

 

def _compareExposures(self, D_F, D_R, Scorr=False, tol=0.02): 

"""Tests to compare the two images (diffim's or Scorr's). 

 

See below. Also compare the diffim pixels with the "expected" 

pixels statistics. Only do the latter if Scorr==False. 

""" 

D_F.getMaskedImage().getMask()[:, :] = D_R.getMaskedImage().getMask() 

varMean_F = self._computeVarianceMean(D_F.getMaskedImage()) 

varMean_R = self._computeVarianceMean(D_R.getMaskedImage()) 

pixMean_F = self._computePixelMean(D_F.getMaskedImage()) 

pixMean_R = self._computePixelMean(D_R.getMaskedImage()) 

pixVar_F = self._computePixelVariance(D_F.getMaskedImage()) 

pixVar_R = self._computePixelVariance(D_R.getMaskedImage()) 

 

if not Scorr: 

self.assertFloatsAlmostEqual(varMean_F, varMean_R, rtol=tol) 

self.assertFloatsAlmostEqual(pixMean_F, self.expectedMean, atol=tol*2.) 

self.assertFloatsAlmostEqual(pixMean_R, self.expectedMean, atol=tol*2.) 

self.assertFloatsAlmostEqual(pixVar_F, pixVar_R, rtol=tol) 

self.assertFloatsAlmostEqual(pixVar_F, self.expectedVar, rtol=tol*2.) 

self.assertFloatsAlmostEqual(pixVar_R, self.expectedVar, rtol=tol*2.) 

else: 

self.assertFloatsAlmostEqual(varMean_F, varMean_R, atol=tol) # nearly zero so need to use atol 

self.assertFloatsAlmostEqual(pixVar_F, pixVar_R, atol=tol) 

 

self.assertFloatsAlmostEqual(pixMean_F, pixMean_R, atol=tol*2.) # nearly zero so need to use atol 

 

def testZogyDiffim(self): 

"""Compute Zogy diffims using Fourier- and Real-space methods. 

 

Compare the images. They are not identical but should be 

similar (within ~2%). 

""" 

self._setUpImages() 

config = ZogyConfig() 

task = ZogyTask(templateExposure=self.im2ex, scienceExposure=self.im1ex, config=config) 

D_F = task.computeDiffim(inImageSpace=False) 

D_R = task.computeDiffim(inImageSpace=True) 

# Fourier-space and image-space versions are not identical, so up the tolerance. 

# This is a known issue with the image-space version. 

self._compareExposures(D_F.D, D_R.D, tol=0.03) 

 

def _testZogyScorr(self, varAst=0.): 

"""Compute Zogy likelihood images (Scorr) using Fourier- and Real-space methods. 

 

Compare the images. They are not identical but should be similar (within ~2%). 

""" 

config = ZogyConfig() 

task = ZogyTask(templateExposure=self.im2ex, scienceExposure=self.im1ex, config=config) 

D_F = task.computeScorr(inImageSpace=False, xVarAst=varAst, yVarAst=varAst) 

D_R = task.computeScorr(inImageSpace=True, xVarAst=varAst, yVarAst=varAst) 

self._compareExposures(D_F.S, D_R.S, Scorr=True) 

 

def testZogyScorr(self): 

"""Compute Zogy likelihood images (Scorr) using Fourier- and Real-space methods. 

 

Do the computation with "astrometric variance" both zero and non-zero. 

Compare the images. They are not identical but should be similar (within ~2%). 

""" 

self._setUpImages() 

self._testZogyScorr() 

self._testZogyScorr(varAst=0.1) 

 

def _testZogyDiffimMapReduced(self, inImageSpace=False, doScorr=False, **kwargs): 

"""Test running Zogy using ImageMapReduceTask framework. 

 

Compare map-reduced version with non-map-reduced version. 

Do it for pure Fourier-based calc. and also for real-space. 

Also for computing pure diffim D and corrected likelihood image Scorr. 

""" 

config = ZogyMapReduceConfig() 

config.gridStepX = config.gridStepY = 9 

config.borderSizeX = config.borderSizeY = 3 

if inImageSpace: 

config.gridStepX = config.gridStepY = 8 

config.borderSizeX = config.borderSizeY = 6 # need larger border size for image-space run 

config.reducer.reduceOperation = 'average' 

task = ImageMapReduceTask(config=config) 

D_mapReduced = task.run(self.im1ex, template=self.im2ex, inImageSpace=inImageSpace, 

doScorr=doScorr, forceEvenSized=False, **kwargs).exposure 

 

config = ZogyConfig() 

task = ZogyTask(templateExposure=self.im2ex, scienceExposure=self.im1ex, config=config) 

if not doScorr: 

D = task.computeDiffim(inImageSpace=inImageSpace, **kwargs).D 

else: 

D = task.computeScorr(inImageSpace=inImageSpace, **kwargs).S 

 

self._compareExposures(D_mapReduced, D, tol=0.04, Scorr=doScorr) 

 

def testZogyDiffimMapReduced(self): 

"""Test running Zogy using ImageMapReduceTask framework. 

 

Compare map-reduced version with non-map-reduced version. 

Do it for pure Fourier-based calc. and also for real-space. 

Do it for ZOGY diffim and corrected likelihood image Scorr. 

For Scorr, do it for zero and non-zero astrometric variance. 

""" 

self._setUpImages() 

self._testZogyDiffimMapReduced(inImageSpace=False) 

self._testZogyDiffimMapReduced(inImageSpace=True) 

self._testZogyDiffimMapReduced(inImageSpace=False, doScorr=True) 

self._testZogyDiffimMapReduced(inImageSpace=True, doScorr=True) 

self._testZogyDiffimMapReduced(inImageSpace=False, doScorr=True, xVarAst=0.1, yVarAst=0.1) 

self._testZogyDiffimMapReduced(inImageSpace=True, doScorr=True, xVarAst=0.1, yVarAst=0.1) 

 

def _testZogyImagePsfMatchTask(self, spatiallyVarying=False, inImageSpace=False, 

doScorr=False, **kwargs): 

"""Test running Zogy using ZogyImagePsfMatchTask framework. 

 

Compare resulting diffim version with original, non-spatially-varying version. 

""" 

config = ZogyImagePsfMatchConfig() 

config.zogyMapReduceConfig.gridStepX = config.zogyMapReduceConfig.gridStepY = 9 

config.zogyMapReduceConfig.borderSizeX = config.zogyMapReduceConfig.borderSizeY = 3 

if inImageSpace: # need larger border size for image-space run 

config.zogyMapReduceConfig.gridStepX = config.zogyMapReduceConfig.gridStepY = 8 

config.zogyMapReduceConfig.borderSizeX = config.zogyMapReduceConfig.borderSizeY = 6 

task = ZogyImagePsfMatchTask(config=config) 

result = task.subtractExposures(self.im2ex, self.im1ex, inImageSpace=inImageSpace, 

doWarping=False, spatiallyVarying=spatiallyVarying) 

D_fromTask = result.subtractedExposure 

 

config = ZogyConfig() 

task = ZogyTask(templateExposure=self.im2ex, scienceExposure=self.im1ex, config=config) 

D = task.computeDiffim(inImageSpace=inImageSpace, **kwargs).D 

self._compareExposures(D_fromTask, D, tol=0.04, Scorr=doScorr) 

 

def testZogyImagePsfMatchTask(self): 

"""Test running ZogyTask both with and without the spatiallyVarying option. 

""" 

self._setUpImages() 

self._testZogyImagePsfMatchTask(inImageSpace=False) 

self._testZogyImagePsfMatchTask(inImageSpace=True) 

self._testZogyImagePsfMatchTask(inImageSpace=False, spatiallyVarying=True) 

self._testZogyImagePsfMatchTask(inImageSpace=True, spatiallyVarying=True) 

 

def testZogyImagePsfMatchTaskDifferentPsfSizes(self): 

"""Test running ZogyTask both with and without the spatiallyVarying option. 

 

Here we artificially set the two images to have PSFs with different dimensions 

to ensure this edge case passes. This also tests cases where one of the PSFs 

is not square. 

""" 

 

# All this to grow the PSF of im1ex by a few pixels: 

def _growPsf(exp, extraPix=(2, 3)): 

bbox = exp.getBBox() 

center = ((bbox.getBeginX() + bbox.getEndX()) // 2., (bbox.getBeginY() + bbox.getEndY()) // 2.) 

center = geom.Point2D(center[0], center[1]) 

kern = exp.getPsf().computeKernelImage(center).convertF() 

kernSize = kern.getDimensions() 

paddedKern = afwImage.ImageF(kernSize[0] + extraPix[0], kernSize[1] + extraPix[1]) 

bboxToPlace = geom.Box2I(geom.Point2I((kernSize[0] + extraPix[0] - kern.getWidth()) // 2, 

(kernSize[1] + extraPix[1] - kern.getHeight()) // 2), 

kern.getDimensions()) 

paddedKern.assign(kern, bboxToPlace) 

fixedKern = afwMath.FixedKernel(paddedKern.convertD()) 

psfNew = measAlg.KernelPsf(fixedKern, center) 

exp.setPsf(psfNew) 

return exp 

 

def _runAllTests(): 

self._testZogyImagePsfMatchTask(inImageSpace=False) 

self._testZogyImagePsfMatchTask(inImageSpace=True) 

self._testZogyImagePsfMatchTask(inImageSpace=False, spatiallyVarying=True) 

self._testZogyImagePsfMatchTask(inImageSpace=True, spatiallyVarying=True) 

 

# Try a range of PSF size combinations... 

self._setUpImages() 

self.im1ex = _growPsf(self.im1ex, (2, 3)) 

_runAllTests() 

 

self.im2ex = _growPsf(self.im2ex, (3, 2)) 

_runAllTests() 

 

self._setUpImages() 

self.im2ex = _growPsf(self.im2ex, (1, 0)) 

_runAllTests() 

 

self.im2ex = _growPsf(self.im2ex, (3, 6)) 

_runAllTests() 

 

self.im1ex = _growPsf(self.im1ex, (5, 6)) 

_runAllTests() 

 

 

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

pass 

 

 

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

lsst.utils.tests.init() 

unittest.main()