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

# This file is part of meas_base. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (https://www.lsst.org). 

# See the COPYRIGHT file at the top-level directory of this distribution 

# for details of code ownership. 

# 

# 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 GNU General Public License 

# along with this program. If not, see <https://www.gnu.org/licenses/>. 

 

import unittest 

 

import numpy as np 

 

import lsst.geom 

import lsst.afw.image 

import lsst.afw.table 

import lsst.utils.tests 

 

from lsst.meas.base.tests import (AlgorithmTestCase, FluxTransformTestCase, 

SingleFramePluginTransformSetupHelper) 

 

 

class PsfFluxTestCase(AlgorithmTestCase, lsst.utils.tests.TestCase): 

 

def setUp(self): 

self.center = lsst.geom.Point2D(50.1, 49.8) 

self.bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), 

lsst.geom.Extent2I(100, 100)) 

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

self.dataset.addSource(100000.0, self.center) 

 

def tearDown(self): 

del self.center 

del self.bbox 

del self.dataset 

 

def makeAlgorithm(self, ctrl=None): 

"""Construct an algorithm and return both it and its schema. 

""" 

if ctrl is None: 

ctrl = lsst.meas.base.PsfFluxControl() 

schema = lsst.meas.base.tests.TestDataset.makeMinimalSchema() 

algorithm = lsst.meas.base.PsfFluxAlgorithm(ctrl, "base_PsfFlux", schema) 

return algorithm, schema 

 

def testMasking(self): 

algorithm, schema = self.makeAlgorithm() 

# Results are RNG dependent; we choose a seed that is known to pass. 

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

record = catalog[0] 

badPoint = lsst.geom.Point2I(self.center) + lsst.geom.Extent2I(3, 4) 

imageArray = exposure.getMaskedImage().getImage().getArray() 

maskArray = exposure.getMaskedImage().getMask().getArray() 

badMask = exposure.getMaskedImage().getMask().getPlaneBitMask("BAD") 

imageArray[badPoint.getY() - exposure.getY0(), badPoint.getX() - exposure.getX0()] = np.inf 

maskArray[badPoint.getY() - exposure.getY0(), badPoint.getX() - exposure.getX0()] |= badMask 

# Should get an infinite value exception, because we didn't mask that 

# one pixel 

with self.assertRaises(lsst.meas.base.PixelValueError): 

algorithm.measure(record, exposure) 

# If we do mask it, we should get a reasonable result 

ctrl = lsst.meas.base.PsfFluxControl() 

ctrl.badMaskPlanes = ["BAD"] 

algorithm, schema = self.makeAlgorithm(ctrl) 

algorithm.measure(record, exposure) 

self.assertFloatsAlmostEqual(record.get("base_PsfFlux_instFlux"), 

record.get("truth_instFlux"), 

atol=3*record.get("base_PsfFlux_instFluxErr")) 

# If we mask the whole image, we should get a MeasurementError 

maskArray[:, :] |= badMask 

with self.assertRaises(lsst.meas.base.MeasurementError) as context: 

algorithm.measure(record, exposure) 

self.assertEqual(context.exception.getFlagBit(), 

lsst.meas.base.PsfFluxAlgorithm.NO_GOOD_PIXELS.number) 

 

def testSubImage(self): 

"""Test measurement on sub-images. 

 

Specifically, checks that we don't get confused by images with nonzero 

``xy0``, and that the ``EDGE`` flag is set when it should be. 

""" 

 

algorithm, schema = self.makeAlgorithm() 

# Results are RNG dependent; we choose a seed that is known to pass. 

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

record = catalog[0] 

psfImage = exposure.getPsf().computeImage(record.getCentroid()) 

bbox = psfImage.getBBox() 

bbox.grow(-1) 

subExposure = exposure.Factory(exposure, bbox, lsst.afw.image.LOCAL) 

algorithm.measure(record, subExposure) 

self.assertFloatsAlmostEqual(record.get("base_PsfFlux_instFlux"), record.get("truth_instFlux"), 

atol=3*record.get("base_PsfFlux_instFluxErr")) 

self.assertTrue(record.get("base_PsfFlux_flag_edge")) 

 

def testNoPsf(self): 

"""Test that we raise `FatalAlgorithmError` when there's no PSF. 

""" 

algorithm, schema = self.makeAlgorithm() 

# Results are RNG dependent; we choose a seed that is known to pass. 

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

exposure.setPsf(None) 

with self.assertRaises(lsst.meas.base.FatalAlgorithmError): 

algorithm.measure(catalog[0], exposure) 

 

def testMonteCarlo(self): 

"""Test an ideal simulation, with no noise. 

 

Demonstrate that: 

 

- We get exactly the right answer, and 

- The reported uncertainty agrees with a Monte Carlo test of the noise. 

""" 

algorithm, schema = self.makeAlgorithm() 

# Results are RNG dependent; we choose a seed that is known to pass. 

exposure, catalog = self.dataset.realize(0.0, schema, randomSeed=3) 

record = catalog[0] 

instFlux = record.get("truth_instFlux") 

algorithm.measure(record, exposure) 

self.assertFloatsAlmostEqual(record.get("base_PsfFlux_instFlux"), instFlux, rtol=1E-3) 

self.assertFloatsAlmostEqual(record.get("base_PsfFlux_instFluxErr"), 0.0, rtol=1E-3) 

for noise in (0.001, 0.01, 0.1): 

instFluxes = [] 

instFluxErrs = [] 

nSamples = 1000 

for repeat in range(nSamples): 

# By using ``repeat`` to seed the RNG, we get results which 

# fall within the tolerances defined below. If we allow this 

# test to be truly random, passing becomes RNG-dependent. 

exposure, catalog = self.dataset.realize(noise*instFlux, schema, randomSeed=repeat) 

record = catalog[0] 

algorithm.measure(record, exposure) 

instFluxes.append(record.get("base_PsfFlux_instFlux")) 

instFluxErrs.append(record.get("base_PsfFlux_instFluxErr")) 

instFluxMean = np.mean(instFluxes) 

instFluxErrMean = np.mean(instFluxErrs) 

instFluxStandardDeviation = np.std(instFluxes) 

self.assertFloatsAlmostEqual(instFluxErrMean, instFluxStandardDeviation, rtol=0.10) 

self.assertLess(instFluxMean - instFlux, 2.0*instFluxErrMean / nSamples**0.5) 

 

def testSingleFramePlugin(self): 

task = self.makeSingleFrameMeasurementTask("base_PsfFlux") 

# Results are RNG dependent; we choose a seed that is known to pass. 

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

task.run(catalog, exposure) 

record = catalog[0] 

self.assertFalse(record.get("base_PsfFlux_flag")) 

self.assertFalse(record.get("base_PsfFlux_flag_noGoodPixels")) 

self.assertFalse(record.get("base_PsfFlux_flag_edge")) 

self.assertFloatsAlmostEqual(record.get("base_PsfFlux_instFlux"), record.get("truth_instFlux"), 

atol=3*record.get("base_PsfFlux_instFluxErr")) 

 

def testForcedPlugin(self): 

task = self.makeForcedMeasurementTask("base_PsfFlux") 

# Results of this test are RNG dependent: we choose seeds that are 

# known to pass. 

measWcs = self.dataset.makePerturbedWcs(self.dataset.exposure.getWcs(), randomSeed=5) 

measDataset = self.dataset.transform(measWcs) 

exposure, truthCatalog = measDataset.realize(10.0, measDataset.makeMinimalSchema(), randomSeed=5) 

refCat = self.dataset.catalog 

refWcs = self.dataset.exposure.getWcs() 

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

task.attachTransformedFootprints(measCat, refCat, exposure, refWcs) 

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

measRecord = measCat[0] 

truthRecord = truthCatalog[0] 

# Centroid tolerances set to ~ single precision epsilon 

self.assertFloatsAlmostEqual(measRecord.get("slot_Centroid_x"), 

truthRecord.get("truth_x"), rtol=1E-7) 

self.assertFloatsAlmostEqual(measRecord.get("slot_Centroid_y"), 

truthRecord.get("truth_y"), rtol=1E-7) 

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

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

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

self.assertFloatsAlmostEqual(measRecord.get("base_PsfFlux_instFlux"), 

truthCatalog.get("truth_instFlux"), rtol=1E-3) 

self.assertLess(measRecord.get("base_PsfFlux_instFluxErr"), 500.0) 

 

 

class PsfFluxTransformTestCase(FluxTransformTestCase, SingleFramePluginTransformSetupHelper, 

lsst.utils.tests.TestCase): 

controlClass = lsst.meas.base.PsfFluxControl 

algorithmClass = lsst.meas.base.PsfFluxAlgorithm 

transformClass = lsst.meas.base.PsfFluxTransform 

flagNames = ('flag', 'flag_noGoodPixels', 'flag_edge') 

singleFramePlugins = ('base_PsfFlux',) 

forcedPlugins = ('base_PsfFlux',) 

 

 

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()