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

# 

# LSST Data Management System 

# Copyright 2008-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.geom 

import lsst.afw.geom 

import lsst.meas.base 

import lsst.meas.base.tests 

import lsst.utils.tests 

 

 

class SdssShapeTestCase(lsst.meas.base.tests.AlgorithmTestCase, lsst.utils.tests.TestCase): 

 

def setUp(self): 

self.bbox = lsst.geom.Box2I(lsst.geom.Point2I(-20, -30), 

lsst.geom.Extent2I(240, 160)) 

self.dataset = lsst.meas.base.tests.TestDataset(self.bbox) 

# first source is a point 

self.dataset.addSource(100000.0, lsst.geom.Point2D(50.1, 49.8)) 

# second source is extended 

self.dataset.addSource(100000.0, lsst.geom.Point2D(149.9, 50.3), 

lsst.afw.geom.Quadrupole(8, 9, 3)) 

self.config = self.makeSingleFrameMeasurementConfig("base_SdssShape") 

 

def tearDown(self): 

del self.bbox 

del self.dataset 

del self.config 

 

def assertFinite(self, value): 

self.assertTrue(np.isfinite(value), msg="%s is not finite" % (value,)) 

 

def _runMeasurementTask(self): 

task = self.makeSingleFrameMeasurementTask("base_SdssShape", config=self.config) 

exposure, catalog = self.dataset.realize(10.0, task.schema, randomSeed=0) 

task.run(catalog, exposure) 

return exposure, catalog 

 

def _checkShape(self, result, record): 

self.assertFloatsAlmostEqual(result.x, record.get("truth_x"), rtol=1E-2) 

self.assertFloatsAlmostEqual(result.y, record.get("truth_y"), rtol=1E-2) 

self.assertFloatsAlmostEqual(result.xx, record.get("truth_xx"), rtol=1E-2) 

self.assertFloatsAlmostEqual(result.yy, record.get("truth_yy"), rtol=1E-2) 

self.assertFloatsAlmostEqual(result.xy, record.get("truth_xy"), rtol=1E-1, atol=2E-1) 

self.assertFinite(result.xxErr) 

self.assertFinite(result.yyErr) 

self.assertFinite(result.xyErr) 

self.assertFinite(result.flux_xx_Cov) 

self.assertFinite(result.flux_yy_Cov) 

self.assertFinite(result.flux_xy_Cov) 

self.assertFinite(result.xx_yy_Cov) 

self.assertFinite(result.xx_xy_Cov) 

self.assertFinite(result.yy_xy_Cov) 

self.assertFalse(result.getFlag(lsst.meas.base.SdssShapeAlgorithm.FAILURE.number)) 

self.assertFalse(result.getFlag(lsst.meas.base.SdssShapeAlgorithm.UNWEIGHTED_BAD.number)) 

self.assertFalse(result.getFlag(lsst.meas.base.SdssShapeAlgorithm.UNWEIGHTED.number)) 

self.assertFalse(result.getFlag(lsst.meas.base.SdssShapeAlgorithm.SHIFT.number)) 

self.assertFalse(result.getFlag(lsst.meas.base.SdssShapeAlgorithm.MAXITER.number)) 

 

def _checkPsfShape(self, result, psfResult, psfTruth): 

self.assertFloatsAlmostEqual(psfResult.getIxx(), psfTruth.getIxx(), rtol=1E-4) 

self.assertFloatsAlmostEqual(psfResult.getIyy(), psfTruth.getIyy(), rtol=1E-4) 

self.assertFloatsAlmostEqual(psfResult.getIxy(), psfTruth.getIxy(), rtol=1E-4) 

self.assertFalse(result.getFlag(lsst.meas.base.SdssShapeAlgorithm.PSF_SHAPE_BAD.number)) 

 

def testMeasureGoodPsf(self): 

"""Test that we measure shapes and record the PSF shape correctly 

 

Note: Given that the PSF model here is constant over the entire image, this test 

would not catch an error in the potition at which base_SdssShape_psf is computed. 

Such a test requires a spatially varying PSF model such that different locations 

can be distinguished by their different PSF model shapes. Such a test exists in 

meas_algorithms (tests/testSdssShapePsf.py), making use of the PcaPsf algorithm 

to build the spatially varying PSF. 

""" 

exposure, catalog = self._runMeasurementTask() 

key = lsst.meas.base.SdssShapeResultKey(catalog.schema["base_SdssShape"]) 

psfTruth = exposure.getPsf().computeShape() 

for record in catalog: 

result = record.get(key) 

self._checkShape(result, record) 

psfResult = key.getPsfShape(record) 

self._checkPsfShape(result, psfResult, psfTruth) 

 

def testMeasureWithoutPsf(self): 

"""Test that we measure shapes correctly and do not record the PSF shape when not needed.""" 

self.config.plugins["base_SdssShape"].doMeasurePsf = False 

_, catalog = self._runMeasurementTask() 

key = lsst.meas.base.SdssShapeResultKey(catalog.schema["base_SdssShape"]) 

for record in catalog: 

result = record.get(key) 

self._checkShape(result, record) 

self.assertNotIn("base_SdssShape_psf_xx", catalog.schema) 

self.assertNotIn("base_SdssShape_psf_yy", catalog.schema) 

self.assertNotIn("base_SdssShape_psf_xy", catalog.schema) 

self.assertNotIn("base_SdssShape_flag_psf", catalog.schema) 

 

def testMeasureBadPsf(self): 

"""Test that we measure shapes correctly and set a flag with the PSF is unavailable.""" 

self.config.plugins["base_SdssShape"].doMeasurePsf = True 

task = self.makeSingleFrameMeasurementTask("base_SdssShape", config=self.config) 

exposure, catalog = self.dataset.realize(10.0, task.schema, randomSeed=1) 

exposure.setPsf(None) # Set PSF to None to test no PSF case 

task.run(catalog, exposure) 

key = lsst.meas.base.SdssShapeResultKey(catalog.schema["base_SdssShape"]) 

for record in catalog: 

result = record.get(key) 

self._checkShape(result, record) 

self.assertTrue(result.getFlag(lsst.meas.base.SdssShapeAlgorithm.PSF_SHAPE_BAD.number)) 

 

 

class SdssShapeTransformTestCase(lsst.meas.base.tests.FluxTransformTestCase, 

lsst.meas.base.tests.CentroidTransformTestCase, 

lsst.meas.base.tests.SingleFramePluginTransformSetupHelper, 

lsst.utils.tests.TestCase): 

 

name = "sdssShape" 

controlClass = lsst.meas.base.SdssShapeControl 

algorithmClass = lsst.meas.base.SdssShapeAlgorithm 

transformClass = lsst.meas.base.SdssShapeTransform 

flagNames = ("flag", "flag_unweighted", "flag_unweightedBad", "flag_shift", "flag_maxIter") 

singleFramePlugins = ('base_SdssShape',) 

forcedPlugins = ('base_SdssShape',) 

testPsf = True 

 

def _setFieldsInRecords(self, records, name): 

lsst.meas.base.tests.FluxTransformTestCase._setFieldsInRecords(self, records, name) 

lsst.meas.base.tests.CentroidTransformTestCase._setFieldsInRecords(self, records, name) 

for record in records: 

for field in ('xx', 'yy', 'xy', 'xxErr', 'yyErr', 'xyErr', 'psf_xx', 'psf_yy', 'psf_xy'): 

if record.schema.join(name, field) in record.schema: 

record[record.schema.join(name, field)] = np.random.random() 

 

def _compareFieldsInRecords(self, inSrc, outSrc, name): 

lsst.meas.base.tests.FluxTransformTestCase._compareFieldsInRecords(self, inSrc, outSrc, name) 

lsst.meas.base.tests.CentroidTransformTestCase._compareFieldsInRecords(self, inSrc, outSrc, name) 

 

inShape = lsst.meas.base.ShapeResultKey(inSrc.schema[name]).get(inSrc) 

outShape = lsst.meas.base.ShapeResultKey(outSrc.schema[name]).get(outSrc) 

 

centroid = lsst.meas.base.CentroidResultKey(inSrc.schema[name]).get(inSrc).getCentroid() 

xform = self.calexp.getWcs().linearizePixelToSky(centroid, lsst.geom.radians) 

 

trInShape = inShape.getShape().transform(xform.getLinear()) 

self.assertEqual(trInShape.getIxx(), outShape.getShape().getIxx()) 

self.assertEqual(trInShape.getIyy(), outShape.getShape().getIyy()) 

self.assertEqual(trInShape.getIxy(), outShape.getShape().getIxy()) 

 

m = lsst.meas.base.makeShapeTransformMatrix(xform.getLinear()) 

np.testing.assert_array_almost_equal( 

np.dot(np.dot(m, inShape.getShapeErr()), m.transpose()), outShape.getShapeErr() 

) 

if self.testPsf: 

inPsfShape = lsst.meas.base.ShapeResultKey( 

inSrc.schema[inSrc.schema.join(name, "psf")] 

).get(inSrc) 

outPsfShape = lsst.meas.base.ShapeResultKey( 

outSrc.schema[outSrc.schema.join(name, "psf")] 

).get(outSrc) 

trInPsfShape = inPsfShape.getShape().transform(xform.getLinear()) 

 

self.assertEqual(trInPsfShape.getIxx(), outPsfShape.getShape().getIxx()) 

self.assertEqual(trInPsfShape.getIyy(), outPsfShape.getShape().getIyy()) 

self.assertEqual(trInPsfShape.getIxy(), outPsfShape.getShape().getIxy()) 

else: 

self.assertNotIn(inSrc.schema.join(name, "psf", "xx"), inSrc.schema) 

self.assertNotIn(inSrc.schema.join(name, "flag", "psf"), inSrc.schema) 

 

 

class PsfSdssShapeTransformTestCase(SdssShapeTransformTestCase, lsst.utils.tests.TestCase): 

testPsf = False 

 

def makeSdssShapeControl(self): 

ctrl = lsst.meas.base.SdssShapeControl() 

ctrl.doMeasurePsf = False 

return ctrl 

controlClass = makeSdssShapeControl 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()