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

# 

# LSST Data Management System 

# 

# Copyright 2008-2016 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 

 

import lsst.utils.tests 

import lsst.shapelet.tests 

import lsst.afw.geom 

import lsst.afw.geom.ellipses 

import lsst.afw.image 

import lsst.afw.math 

import lsst.afw.detection 

import lsst.meas.modelfit 

 

 

ASSERT_CLOSE_KWDS = dict(plotOnFailure=False, printFailures=False) 

 

 

def makeGaussianFunction(ellipse, flux=1.0): 

"""Create a single-Gaussian MultiShapeletFunction 

 

ellipse may be an afw.geom.ellipses.Ellipse or a float radius for a circle 

""" 

s = lsst.shapelet.ShapeletFunction(0, lsst.shapelet.HERMITE, ellipse) 

s.getCoefficients()[0] = 1.0 

s.normalize() 

s.getCoefficients()[0] *= flux 

msf = lsst.shapelet.MultiShapeletFunction() 

msf.addComponent(s) 

return msf 

 

 

def addGaussian(exposure, ellipse, flux, psf=None): 

s = makeGaussianFunction(ellipse, flux) 

if psf is not None: 

s = s.convolve(psf) 

imageF = exposure.getMaskedImage().getImage() 

imageD = lsst.afw.image.ImageD(imageF.getBBox()) 

s.evaluate().addToImage(imageD) 

imageF += imageD.convertF() 

 

 

def scaleExposure(exposure, factor): 

mi = exposure.getMaskedImage() 

mi *= factor 

 

 

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

 

def setUp(self): 

numpy.random.seed(500) 

self.position = lsst.afw.geom.SpherePoint(45.0, 45.0, lsst.afw.geom.degrees) 

self.model = lsst.meas.modelfit.Model.makeGaussian(lsst.meas.modelfit.Model.FIXED_CENTER) 

self.ellipse = lsst.afw.geom.ellipses.Ellipse(lsst.afw.geom.ellipses.Axes(6.0, 5.0, numpy.pi/6)) 

self.flux = 50.0 

ev = self.model.makeEllipseVector() 

ev[0].setCore(self.ellipse.getCore()) 

ev[0].setCenter(self.ellipse.getCenter()) 

self.nonlinear = numpy.zeros(self.model.getNonlinearDim(), dtype=lsst.meas.modelfit.Scalar) 

self.fixed = numpy.zeros(self.model.getFixedDim(), dtype=lsst.meas.modelfit.Scalar) 

self.model.readEllipses(ev, self.nonlinear, self.fixed) 

self.amplitudes = numpy.zeros(self.model.getAmplitudeDim(), dtype=lsst.meas.modelfit.Scalar) 

self.amplitudes[:] = self.flux 

# setup ideal exposure0: uses fit Wcs and Calib, has delta function PSF 

scale0 = 0.2*lsst.afw.geom.arcseconds 

self.crpix0 = lsst.afw.geom.Point2D(0, 0) 

wcs0 = lsst.afw.geom.makeSkyWcs(crpix=self.crpix0, 

crval=self.position, 

cdMatrix=lsst.afw.geom.makeCdMatrix(scale=scale0)) 

calib0 = lsst.afw.image.Calib() 

calib0.setFluxMag0(10000) 

self.psf0 = makeGaussianFunction(0.0) 

self.bbox0 = lsst.afw.geom.Box2I(lsst.afw.geom.Point2I(-100, -100), lsst.afw.geom.Point2I(100, 100)) 

self.spanSet0 = lsst.afw.geom.SpanSet(self.bbox0) 

self.footprint0 = lsst.afw.detection.Footprint(self.spanSet0) 

self.exposure0 = lsst.afw.image.ExposureF(self.bbox0) 

self.exposure0.setWcs(wcs0) 

self.exposure0.setCalib(calib0) 

self.sys0 = lsst.meas.modelfit.UnitSystem(self.exposure0) 

addGaussian(self.exposure0, self.ellipse, self.flux, psf=self.psf0) 

self.exposure0.getMaskedImage().getVariance().set(1.0) 

# setup secondary exposure: 2x pixel scale, 3x gain, Gaussian PSF with sigma=2.5pix 

scale1 = 0.4 * lsst.afw.geom.arcseconds 

wcs1 = lsst.afw.geom.makeSkyWcs(crpix=lsst.afw.geom.Point2D(), 

crval=self.position, 

cdMatrix=lsst.afw.geom.makeCdMatrix(scale=scale1)) 

calib1 = lsst.afw.image.Calib() 

calib1.setFluxMag0(30000) 

self.sys1 = lsst.meas.modelfit.UnitSystem(wcs1, calib1) 

# transform object that maps between exposures (not including PSF) 

self.t01 = lsst.meas.modelfit.LocalUnitTransform(self.sys0.wcs.getPixelOrigin(), self.sys0, self.sys1) 

self.bbox1 = lsst.afw.geom.Box2I(self.bbox0) 

self.bbox1.grow(-60) 

self.spanSet1 = lsst.afw.geom.SpanSet(self.bbox1) 

self.footprint1 = lsst.afw.detection.Footprint(self.spanSet1) 

self.psfSigma1 = 2.5 

self.psf1 = makeGaussianFunction(self.psfSigma1) 

 

def tearDown(self): 

del self.position 

del self.model 

del self.ellipse 

del self.bbox0 

del self.spanSet0 

del self.footprint0 

del self.exposure0 

del self.sys0 

del self.sys1 

del self.t01 

del self.bbox1 

del self.spanSet1 

del self.footprint1 

del self.psf1 

 

def checkLikelihood(self, likelihood, data): 

self.assertFloatsAlmostEqual(likelihood.getData().reshape(data.shape), data, rtol=1E-6, 

**ASSERT_CLOSE_KWDS) 

matrix = numpy.zeros((1, likelihood.getDataDim()), dtype=lsst.meas.modelfit.Pixel).transpose() 

likelihood.computeModelMatrix(matrix, self.nonlinear) 

model = numpy.dot(matrix, self.amplitudes) 

self.assertFloatsAlmostEqual(model.reshape(data.shape), data, rtol=1E-6, atol=1E-7, 

**ASSERT_CLOSE_KWDS) 

 

def testModel(self): 

"""Test that when we use a Model to create a MultiShapeletFunction from our parameter vectors 

it agrees with the reimplementation here.""" 

msf = self.model.makeShapeletFunction(self.nonlinear, self.amplitudes, self.fixed) 

image0a = lsst.afw.image.ImageD(self.bbox0) 

msf.evaluate().addToImage(image0a) 

self.assertFloatsAlmostEqual(image0a.getArray(), 

self.exposure0.getMaskedImage().getImage().getArray(), 

rtol=1E-6, atol=1E-7, **ASSERT_CLOSE_KWDS) 

 

def testWarp(self): 

"""Test that transforming ellipses and fluxes with LocalUnitTransform agrees with warping 

""" 

warpCtrl = lsst.afw.math.WarpingControl("lanczos5") 

# exposure1: check image; just a transform and scaling of exposure0 (no PSF...yet) 

exposure1 = lsst.afw.image.ExposureF(self.bbox1) 

addGaussian(exposure1, self.ellipse.transform(self.t01.geometric), self.flux * self.t01.flux) 

# exposure1a: warp exposure0 using warpExposure with WCS arguments 

exposure1a = lsst.afw.image.ExposureF(self.bbox1) 

exposure1a.setWcs(self.sys1.wcs) 

lsst.afw.math.warpExposure(exposure1a, self.exposure0, warpCtrl) 

exposure1a.setCalib(self.sys1.calib) 

scaleExposure(exposure1a, self.t01.flux) 

self.assertFloatsAlmostEqual(exposure1.getMaskedImage().getImage().getArray(), 

exposure1a.getMaskedImage().getImage().getArray(), 

rtol=1E-6, **ASSERT_CLOSE_KWDS) 

# exposure1b: warp exposure0 using warpImage with AffineTransform arguments 

exposure1b = lsst.afw.image.ExposureF(self.bbox1) 

exposure1b.setWcs(self.sys1.wcs) 

srcToDest = lsst.afw.geom.makeTransform(self.t01.geometric) 

lsst.afw.math.warpImage(exposure1b.getMaskedImage(), self.exposure0.getMaskedImage(), 

srcToDest, warpCtrl) 

exposure1b.setCalib(self.sys1.calib) 

scaleExposure(exposure1b, self.t01.flux) 

self.assertFloatsAlmostEqual(exposure1.getMaskedImage().getImage().getArray(), 

exposure1b.getMaskedImage().getImage().getArray(), 

rtol=1E-6, **ASSERT_CLOSE_KWDS) 

# now we rebuild exposure1 with the PSF convolution included, and convolve 1a->1c using an 

# afw::math::Kernel. Since 1a is the same as 1b, there's no need to convolve 1b too. 

exposure1 = lsst.afw.image.ExposureF(self.bbox1) 

addGaussian(exposure1, self.ellipse.transform(self.t01.geometric), self.flux * self.t01.flux, 

psf=self.psf1) 

kernel = lsst.afw.math.AnalyticKernel( 

int(self.psfSigma1*16)+1, int(self.psfSigma1*16)+1, 

lsst.afw.math.GaussianFunction2D(self.psfSigma1, self.psfSigma1) 

) 

exposure1c = lsst.afw.image.ExposureF(self.bbox1) 

ctrl = lsst.afw.math.ConvolutionControl() 

ctrl.setDoCopyEdge(True) 

lsst.afw.math.convolve(exposure1c.getMaskedImage(), exposure1a.getMaskedImage(), kernel, ctrl) 

self.assertFloatsAlmostEqual(exposure1.getMaskedImage().getImage().getArray(), 

exposure1c.getMaskedImage().getImage().getArray(), 

rtol=1E-5, atol=1E-6, **ASSERT_CLOSE_KWDS) 

 

def testDirect(self): 

"""Test likelihood evaluation when the fit system is the same as the data system. 

""" 

ctrl = lsst.meas.modelfit.UnitTransformedLikelihoodControl() 

var = numpy.random.rand(self.bbox0.getHeight(), self.bbox0.getWidth()) + 2.0 

self.exposure0.getMaskedImage().getVariance().getArray()[:, :] = var 

efv = [lsst.meas.modelfit.EpochFootprint(self.footprint0, self.exposure0, self.psf0)] 

# test with per-pixel weights, using both ctors 

ctrl.usePixelWeights = True 

data = self.exposure0.getMaskedImage().getImage().getArray() / var**0.5 

l0a = lsst.meas.modelfit.UnitTransformedLikelihood(self.model, self.fixed, self.sys0, self.position, 

self.exposure0, self.footprint0, self.psf0, ctrl) 

self.checkLikelihood(l0a, data) 

l0b = lsst.meas.modelfit.UnitTransformedLikelihood(self.model, self.fixed, self.sys0, self.position, 

efv, ctrl) 

self.checkLikelihood(l0b, data) 

# test with constant weights, using both ctors 

ctrl.usePixelWeights = False 

data = self.exposure0.getMaskedImage().getImage().getArray() 

l0c = lsst.meas.modelfit.UnitTransformedLikelihood(self.model, self.fixed, self.sys0, self.position, 

self.exposure0, self.footprint0, self.psf0, ctrl) 

weights = numpy.exp((-0.5*numpy.log(l0c.getVariance())/l0c.getDataDim()).sum()) 

self.checkLikelihood(l0c, data*weights) 

l0d = lsst.meas.modelfit.UnitTransformedLikelihood(self.model, self.fixed, self.sys0, self.position, 

efv, ctrl) 

self.checkLikelihood(l0d, data*weights) 

 

def testProjected(self): 

"""Test likelihood evaluation when the fit system is not the same as the data system. 

""" 

# Start by building the data exposure 

exposure1 = lsst.afw.image.ExposureF(self.bbox1) 

addGaussian(exposure1, self.ellipse.transform(self.t01.geometric), self.flux * self.t01.flux, 

psf=self.psf1) 

exposure1.setWcs(self.sys1.wcs) 

exposure1.setCalib(self.sys1.calib) 

var = numpy.random.rand(self.bbox1.getHeight(), self.bbox1.getWidth()) + 2.0 

exposure1.getMaskedImage().getVariance().getArray()[:, :] = var 

ctrl = lsst.meas.modelfit.UnitTransformedLikelihoodControl() 

efv = [lsst.meas.modelfit.EpochFootprint(self.footprint1, exposure1, self.psf1)] 

# test with per-pixel weights, using both ctors 

ctrl.usePixelWeights = True 

data = exposure1.getMaskedImage().getImage().getArray() / var**0.5 

l1a = lsst.meas.modelfit.UnitTransformedLikelihood(self.model, self.fixed, self.sys0, self.position, 

exposure1, self.footprint1, self.psf1, ctrl) 

self.checkLikelihood(l1a, data) 

l1b = lsst.meas.modelfit.UnitTransformedLikelihood(self.model, self.fixed, self.sys0, self.position, 

efv, ctrl) 

self.checkLikelihood(l1b, data) 

# test with constant weights, using both ctors 

ctrl.usePixelWeights = False 

data = exposure1.getMaskedImage().getImage().getArray() 

l1c = lsst.meas.modelfit.UnitTransformedLikelihood(self.model, self.fixed, self.sys0, self.position, 

exposure1, self.footprint1, self.psf1, ctrl) 

weights = numpy.exp((-0.5*numpy.log(l1c.getVariance())/l1c.getDataDim()).sum()) 

self.checkLikelihood(l1c, data*weights) 

l1d = lsst.meas.modelfit.UnitTransformedLikelihood(self.model, self.fixed, self.sys0, self.position, 

efv, ctrl) 

self.checkLikelihood(l1d, data*weights) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()