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

# 

# LSST Data Management System 

# Copyright 2008-2017 LSST Corporation. 

# 

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

# 

 

import unittest 

 

import numpy as np 

 

import lsst.utils.tests 

import lsst.geom 

import lsst.meas.base.tests 

import lsst.afw.image as afwImage 

import lsst.afw.table as afwTable 

import lsst.meas.base.applyApCorr as applyApCorr 

from lsst.afw.math import ChebyshevBoundedField 

from lsst.meas.base.apCorrRegistry import addApCorrName 

 

 

def initializeSourceCatalog(schema=None, name=None, instFlux=None, sigma=None, centroid=None): 

instFluxName = name + "_instFlux" 

instFluxErrName = name + "_instFluxErr" 

instFluxKey = schema.find(instFluxName).key 

centroidKey = afwTable.Point2DKey(schema["slot_Centroid"]) 

sourceCat = afwTable.SourceCatalog(schema) 

source = sourceCat.addNew() 

source.set(instFluxKey, instFlux) 

source.set(instFluxErrName, sigma) 

source.set(centroidKey, centroid) 

return(sourceCat) 

 

 

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

 

def setUp(self): 

schema = afwTable.SourceTable.makeMinimalSchema() 

names = ["test2", "test"] 

for name in names: 

addApCorrName(name) 

schema.addField(name + "_instFlux", type=np.float64) 

schema.addField(name + "_instFluxErr", type=np.float64) 

schema.addField(name + "_flag", type=np.float64) 

schema.addField(name + "_Centroid_x", type=np.float64) 

schema.addField(name + "_Centroid_y", type=np.float64) 

schema.getAliasMap().set('slot_Centroid', name + '_Centroid') 

self.ap_corr_task = applyApCorr.ApplyApCorrTask(schema=schema) 

self.name = name # just use 'test' prefix for most of the tests 

self.schema = schema 

 

def tearDown(self): 

del self.schema 

del self.ap_corr_task 

 

def testAddFields(self): 

# Check that the required fields have been added to the schema 

self.assertIn(self.name + "_apCorr", self.schema.getNames()) 

self.assertIn(self.name + "_apCorrErr", self.schema.getNames()) 

self.assertIn(self.name + "_flag_apCorr", self.schema.getNames()) 

self.assertLess(self.schema.find("test_apCorr").key.getOffset(), 

self.schema.find("test2_apCorr").key.getOffset()) 

 

def testSuccessUnflagged(self): 

# Check that the aperture correction flag is set to False if aperture correction was successfully run 

flagName = self.name + "_flag_apCorr" 

flagKey = self.schema.find(flagName).key 

source_test_instFlux = 5.1 

source_test_centroid = lsst.geom.Point2D(5, 7.1) 

sourceCat = initializeSourceCatalog(schema=self.schema, name=self.name, instFlux=source_test_instFlux, 

sigma=0, centroid=source_test_centroid) 

instFluxName = self.name + "_instFlux" 

instFluxErrName = self.name + "_instFluxErr" 

 

apCorrMap = afwImage.ApCorrMap() 

bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.ExtentI(10, 10)) 

coefficients = np.ones((1, 1), dtype=np.float64) 

coefficients_sigma = np.zeros((1, 1), dtype=np.float64) 

apCorrMap[instFluxName] = ChebyshevBoundedField(bbox, coefficients) 

apCorrMap[instFluxErrName] = ChebyshevBoundedField(bbox, coefficients_sigma) 

self.ap_corr_task.run(sourceCat, apCorrMap) 

self.assertFalse(sourceCat[flagKey]) 

 

def testFailureFlagged(self): 

# Check that aperture correction flag is set to True if aperture correction is invalid (negative) 

flagName = self.name + "_flag_apCorr" 

flagKey = self.schema.find(flagName).key 

source_test_instFlux = 5.2 

source_test_centroid = lsst.geom.Point2D(5, 7.1) 

sourceCat = initializeSourceCatalog(schema=self.schema, name=self.name, instFlux=source_test_instFlux, 

sigma=0, centroid=source_test_centroid) 

instFluxName = self.name + "_instFlux" 

instFluxErrName = self.name + "_instFluxErr" 

 

apCorrMap = afwImage.ApCorrMap() 

bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.ExtentI(10, 10)) 

coefficients = -(np.ones((1, 1), dtype=np.float64)) 

coefficients_sigma = np.zeros((1, 1), dtype=np.float64) 

apCorrMap[instFluxName] = ChebyshevBoundedField(bbox, coefficients) 

apCorrMap[instFluxErrName] = ChebyshevBoundedField(bbox, coefficients_sigma) 

self.ap_corr_task.run(sourceCat, apCorrMap) 

self.assertTrue(sourceCat[flagKey]) 

 

def testCatFluxUnchanged(self): 

# Pick arbitrary but unique values for the test case 

source_test_instFlux = 5.3 

source_test_centroid = lsst.geom.Point2D(5, 7.1) 

sourceCat = initializeSourceCatalog(schema=self.schema, name=self.name, instFlux=source_test_instFlux, 

sigma=0, centroid=source_test_centroid) 

instFluxName = self.name + "_instFlux" 

instFluxErrName = self.name + "_instFluxErr" 

instFluxKey = self.schema.find(instFluxName).key 

 

apCorrMap = afwImage.ApCorrMap() 

bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.ExtentI(10, 10)) 

coefficients = np.ones((1, 1), dtype=np.float64) 

coefficients_sigma = np.zeros((1, 1), dtype=np.float64) 

apCorrMap[instFluxName] = ChebyshevBoundedField(bbox, coefficients) 

apCorrMap[instFluxErrName] = ChebyshevBoundedField(bbox, coefficients_sigma) 

self.ap_corr_task.run(sourceCat, apCorrMap) 

 

self.assertEqual(sourceCat[instFluxKey], source_test_instFlux) 

 

def testCatFluxHalf(self): 

# Pick arbitrary but unique values for the test case 

source_test_instFlux = 5.4 

source_test_centroid = lsst.geom.Point2D(5, 7.1) 

sourceCat = initializeSourceCatalog(schema=self.schema, name=self.name, instFlux=source_test_instFlux, 

sigma=0, centroid=source_test_centroid) 

instFluxName = self.name + "_instFlux" 

instFluxErrName = self.name + "_instFluxErr" 

instFluxKey = self.schema.find(instFluxName).key 

 

apCorrMap = afwImage.ApCorrMap() 

bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.ExtentI(10, 10)) 

coefficients = np.ones((1, 1), dtype=np.float64) 

coefficients /= 2. 

coefficients_sigma = np.zeros((1, 1), dtype=np.float64) 

apCorrMap[instFluxName] = ChebyshevBoundedField(bbox, coefficients) 

apCorrMap[instFluxErrName] = ChebyshevBoundedField(bbox, coefficients_sigma) 

self.ap_corr_task.run(sourceCat, apCorrMap) 

 

self.assertAlmostEqual(sourceCat[instFluxKey], source_test_instFlux / 2) 

 

def testCatFluxErr(self): 

""" 

Important note! This test will break if UseNaiveFluxErr = False 

The alternate method significantly overestimates noise, causing this test to fail. 

It is likely that this test will need to be modified if the noise calculation is updated. 

""" 

# Pick arbitrary but unique values for the test case 

source_test_instFlux = 5.5 

source_test_sigma = 0.23 

source_test_centroid = lsst.geom.Point2D(5, 7.3) 

sourceCat = initializeSourceCatalog(schema=self.schema, name=self.name, instFlux=source_test_instFlux, 

sigma=source_test_sigma, centroid=source_test_centroid) 

 

instFluxName = self.name + "_instFlux" 

instFluxErrName = self.name + "_instFluxErr" 

instFluxErrKey = self.schema.find(instFluxErrName).key 

 

apCorrMap = afwImage.ApCorrMap() 

bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.ExtentI(10, 10)) 

coefficients = np.ones((1, 1), dtype=np.float64) 

coefficients_sigma = np.ones((1, 1), dtype=np.float64) 

apCorrMap[instFluxName] = ChebyshevBoundedField(bbox, coefficients) 

apCorrMap[instFluxErrName] = ChebyshevBoundedField(bbox, coefficients_sigma) 

self.ap_corr_task.run(sourceCat, apCorrMap) 

 

self.assertAlmostEqual(sourceCat[instFluxErrKey], source_test_sigma) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()