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

# 

# 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 os 

 

import lsst.utils.tests 

import lsst.shapelet 

import lsst.afw.geom.ellipses 

import lsst.afw.table 

import lsst.afw.detection 

import lsst.log 

import lsst.log.utils 

import lsst.meas.modelfit 

import lsst.meas.base 

import lsst.meas.algorithms 

 

# Set trace to 0-5 to view debug messages. Level 5 enables all traces. 

lsst.log.utils.traceSetAt("meas.modelfit.optimizer.Optimizer", -1) 

lsst.log.utils.traceSetAt("meas.modelfit.optimizer.solveTrustRegion", -1) 

 

 

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

 

def makeBlankConfig(self): 

config = lsst.meas.base.SingleFrameMeasurementTask.ConfigClass() 

config.slots.centroid = None 

config.slots.shape = None 

config.slots.psfFlux = None 

config.slots.apFlux = None 

config.slots.gaussianFlux = None 

config.slots.modelFlux = None 

config.slots.calibFlux = None 

config.doReplaceWithNoise = False 

return config 

 

def setUp(self): 

numpy.random.seed(500) 

self.psfSigma = 2.0 

self.exposure = lsst.afw.image.ExposureF(41, 41) 

self.psf = lsst.afw.detection.GaussianPsf(19, 19, self.psfSigma) 

self.schema = lsst.afw.table.SourceTable.makeMinimalSchema() 

self.centroidKey = lsst.afw.table.Point2DKey.addFields(self.schema, "centroid", "centroid", "pixel") 

self.schema.getAliasMap().set("slot_Centroid", "centroid") 

self.psfDir = os.path.join(os.environ["MEAS_MODELFIT_DIR"], "tests", "data", "psfs") 

 

def tearDown(self): 

del self.exposure 

del self.psf 

del self.schema 

del self.centroidKey 

del self.psfDir 

 

def checkResult(self, msf): 

# Because we're fitting multiple shapelets to a single Gaussian (a single 0th-order shapelet) 

# we should be able to fit with zero residuals, aside from (single-precision) round-off error. 

dataImage = self.exposure.getPsf().computeImage() 

modelImage = dataImage.Factory(dataImage.getBBox()) 

modelImage.getArray()[:, :] *= -1 

msf.evaluate().addToImage(modelImage) 

self.assertFloatsAlmostEqual(dataImage.getArray(), modelImage.getArray(), atol=1E-6, 

plotOnFailure=False) 

 

def testSingleFrame(self): 

self.exposure.setPsf(self.psf) 

config = self.makeBlankConfig() 

config.plugins.names = ["modelfit_GeneralShapeletPsfApprox"] 

config.plugins["modelfit_GeneralShapeletPsfApprox"].sequence = ["SingleGaussian"] 

task = lsst.meas.base.SingleFrameMeasurementTask(config=config, schema=self.schema) 

measCat = lsst.afw.table.SourceCatalog(self.schema) 

measRecord = measCat.addNew() 

measRecord.set(self.centroidKey, lsst.afw.geom.Point2D(20.0, 20.0)) 

task.run(measCat, self.exposure) 

keySingleGaussian = lsst.shapelet.MultiShapeletFunctionKey( 

self.schema["modelfit"]["GeneralShapeletPsfApprox"]["SingleGaussian"] 

) 

msfSingleGaussian = measRecord.get(keySingleGaussian) 

self.assertEqual(len(msfSingleGaussian.getComponents()), 1) 

self.checkResult(msfSingleGaussian) 

 

def testForced(self): 

self.exposure.setPsf(self.psf) 

config = lsst.meas.base.ForcedMeasurementTask.ConfigClass() 

config.slots.centroid = "base_TransformedCentroid" 

config.slots.shape = None 

config.slots.psfFlux = None 

config.slots.apFlux = None 

config.slots.gaussianFlux = None 

config.slots.modelFlux = None 

config.doReplaceWithNoise = False 

config.slots.centroid = "base_TransformedCentroid" 

config.plugins.names = ["base_TransformedCentroid", "modelfit_GeneralShapeletPsfApprox"] 

config.plugins["modelfit_GeneralShapeletPsfApprox"].sequence = ["SingleGaussian"] 

config.copyColumns = {"id": "objectId", "parent": "parentObjectId"} 

refCat = lsst.afw.table.SourceCatalog(self.schema) 

refRecord = refCat.addNew() 

refRecord.set(self.centroidKey, lsst.afw.geom.Point2D(20.0, 20.0)) 

refWcs = self.exposure.getWcs() # same as measurement Wcs 

task = lsst.meas.base.ForcedMeasurementTask(config=config, refSchema=self.schema) 

measCat = task.generateMeasCat(self.exposure, refCat, refWcs) 

task.run(measCat, self.exposure, refCat, refWcs) 

measRecord = measCat[0] 

measSchema = measCat.schema 

keySingleGaussian = lsst.shapelet.MultiShapeletFunctionKey( 

measSchema["modelfit"]["GeneralShapeletPsfApprox"]["SingleGaussian"] 

) 

msfSingleGaussian = measRecord.get(keySingleGaussian) 

self.assertEqual(len(msfSingleGaussian.getComponents()), 1) 

self.checkResult(msfSingleGaussian) 

 

def testNanFlag(self): 

config = self.makeBlankConfig() 

config.plugins.names = ["modelfit_GeneralShapeletPsfApprox"] 

config.plugins["modelfit_GeneralShapeletPsfApprox"].sequence = ["Full"] 

task = lsst.meas.base.SingleFrameMeasurementTask(config=config, schema=self.schema) 

measCat = lsst.afw.table.SourceCatalog(self.schema) 

measRecord = measCat.addNew() 

psfImage = lsst.afw.image.ImageD(os.path.join(self.psfDir, "galsimPsf_0.9.fits")) 

psfImage.getArray()[0, 0] = numpy.nan 

psfImage.setXY0(lsst.afw.geom.Point2I(0, 0)) 

kernel = lsst.afw.math.FixedKernel(psfImage) 

psf = lsst.meas.algorithms.KernelPsf(kernel) 

self.exposure.setPsf(psf) 

center = lsst.afw.geom.Point2D(psfImage.getArray().shape[0]/2, psfImage.getArray().shape[1]/2) 

measRecord.set(self.centroidKey, center) 

task.run(measCat, self.exposure) 

self.assertTrue(measRecord.get("modelfit_GeneralShapeletPsfApprox_Full_flag")) 

self.assertTrue(measRecord.get("modelfit_GeneralShapeletPsfApprox_Full_flag_contains_nan")) 

self.assertFalse(measRecord.get("modelfit_GeneralShapeletPsfApprox_Full_flag_max_inner_iterations")) 

self.assertFalse(measRecord.get("modelfit_GeneralShapeletPsfApprox_Full_flag_max_outer_iterations")) 

self.assertFalse(measRecord.get("modelfit_GeneralShapeletPsfApprox_Full_flag_exception")) 

 

def testInnerIterationsFlag(self): 

config = self.makeBlankConfig() 

config.plugins.names = ["modelfit_GeneralShapeletPsfApprox"] 

config.plugins["modelfit_GeneralShapeletPsfApprox"].sequence = ["Full"] 

config.plugins["modelfit_GeneralShapeletPsfApprox"].models["Full"].optimizer.maxInnerIterations = 1 

task = lsst.meas.base.SingleFrameMeasurementTask(config=config, schema=self.schema) 

measCat = lsst.afw.table.SourceCatalog(self.schema) 

measRecord = measCat.addNew() 

psfImage = lsst.afw.image.ImageD(os.path.join(self.psfDir, "galsimPsf_0.9.fits")) 

psfImage.setXY0(lsst.afw.geom.Point2I(0, 0)) 

kernel = lsst.afw.math.FixedKernel(psfImage) 

psf = lsst.meas.algorithms.KernelPsf(kernel) 

self.exposure.setPsf(psf) 

center = lsst.afw.geom.Point2D(psfImage.getArray().shape[0]/2, psfImage.getArray().shape[1]/2) 

measRecord.set(self.centroidKey, center) 

task.run(measCat, self.exposure) 

self.assertTrue(measRecord.get("modelfit_GeneralShapeletPsfApprox_Full_flag")) 

self.assertFalse(measRecord.get("modelfit_GeneralShapeletPsfApprox_Full_flag_contains_nan")) 

self.assertTrue(measRecord.get("modelfit_GeneralShapeletPsfApprox_Full_flag_max_inner_iterations")) 

self.assertFalse(measRecord.get("modelfit_GeneralShapeletPsfApprox_Full_flag_max_outer_iterations")) 

self.assertFalse(measRecord.get("modelfit_GeneralShapeletPsfApprox_Full_flag_exception")) 

 

def testOuterIterationsFlag(self): 

config = self.makeBlankConfig() 

config.plugins.names = ["modelfit_GeneralShapeletPsfApprox"] 

config.plugins["modelfit_GeneralShapeletPsfApprox"].sequence = ["Full"] 

config.plugins["modelfit_GeneralShapeletPsfApprox"].models["Full"].optimizer.maxOuterIterations = 1 

task = lsst.meas.base.SingleFrameMeasurementTask(config=config, schema=self.schema) 

measCat = lsst.afw.table.SourceCatalog(self.schema) 

measRecord = measCat.addNew() 

psfImage = lsst.afw.image.ImageD(os.path.join(self.psfDir, "galsimPsf_0.9.fits")) 

psfImage.setXY0(lsst.afw.geom.Point2I(0, 0)) 

kernel = lsst.afw.math.FixedKernel(psfImage) 

psf = lsst.meas.algorithms.KernelPsf(kernel) 

self.exposure.setPsf(psf) 

center = lsst.afw.geom.Point2D(psfImage.getArray().shape[0]/2, psfImage.getArray().shape[1]/2) 

measRecord.set(self.centroidKey, center) 

task.run(measCat, self.exposure) 

self.assertTrue(measRecord.get("modelfit_GeneralShapeletPsfApprox_Full_flag")) 

self.assertFalse(measRecord.get("modelfit_GeneralShapeletPsfApprox_Full_flag_contains_nan")) 

self.assertFalse(measRecord.get("modelfit_GeneralShapeletPsfApprox_Full_flag_max_inner_iterations")) 

self.assertTrue(measRecord.get("modelfit_GeneralShapeletPsfApprox_Full_flag_max_outer_iterations")) 

self.assertFalse(measRecord.get("modelfit_GeneralShapeletPsfApprox_Full_flag_exception")) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()