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

# 

# 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 as np 

 

import lsst.geom 

import lsst.afw.image as afwImage 

import lsst.afw.table as afwTable 

from lsst.afw.math import ChebyshevBoundedField 

import lsst.pex.config 

import lsst.meas.algorithms.measureApCorr as measureApCorr 

from lsst.meas.base.apCorrRegistry import addApCorrName 

import lsst.meas.base.tests 

import lsst.utils.tests 

 

 

def apCorrDefaultMap(value=None, bbox=None): 

default_coefficients = np.ones((1, 1), dtype=float) 

default_coefficients /= value 

default_apCorrMap = ChebyshevBoundedField(bbox, default_coefficients) 

default_fill = afwImage.ImageF(bbox) 

default_apCorrMap.fillImage(default_fill) 

return(default_fill) 

 

 

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

 

def makeCatalog(self, apCorrScale=1.0, numSources=5): 

sourceCat = afwTable.SourceCatalog(self.schema) 

fluxName = self.name + "_flux" 

flagName = self.name + "_flag" 

fluxErrName = self.name + "_fluxErr" 

apFluxName = self.apname + "_flux" 

apFlagName = self.apname + "_flag" 

apFluxErrName = self.apname + "_fluxErr" 

fluxKey = self.schema.find(fluxName).key 

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

fluxErrKey = self.schema.find(fluxErrName).key 

apFluxKey = self.schema.find(apFluxName).key 

apFlagKey = self.schema.find(apFlagName).key 

apFluxErrKey = self.schema.find(apFluxErrName).key 

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

inputFilterFlagKey = self.schema.find(self.meas_apCorr_task.config.sourceSelector.active.field).key 

x = np.random.rand(numSources)*self.exposure.getWidth() + self.exposure.getX0() 

y = np.random.rand(numSources)*self.exposure.getHeight() + self.exposure.getY0() 

for _i in range(numSources): 

source_test_flux = 5.1 

source_test_centroid = lsst.geom.Point2D(x[_i], y[_i]) 

source = sourceCat.addNew() 

source.set(fluxKey, source_test_flux) 

source.set(apFluxKey, source_test_flux * apCorrScale) 

source.set(centroidKey, source_test_centroid) 

source.set(fluxErrKey, 0.) 

source.set(apFluxErrKey, 0.) 

source.set(flagKey, False) 

source.set(apFlagKey, False) 

source.set(inputFilterFlagKey, True) 

return(sourceCat) 

 

def setUp(self): 

schema = afwTable.SourceTable.makeMinimalSchema() 

name = "test" 

apname = "testAp" 

calib_flag_name = "cal_source_use" 

addApCorrName(apname) 

schema.addField(name + "_flux", type=float) 

schema.addField(name + "_fluxErr", type=float) 

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

schema.addField(apname + "_flux", type=float) 

schema.addField(apname + "_fluxErr", type=float) 

schema.addField(apname + "_flag", type="Flag") 

schema.addField(calib_flag_name, type="Flag") 

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

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

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

config = measureApCorr.MeasureApCorrTask.ConfigClass() 

config.refFluxName = name 

config.sourceSelector.active.field = calib_flag_name 

self.meas_apCorr_task = measureApCorr.MeasureApCorrTask(schema=schema, config=config) 

self.name = name 

self.apname = apname 

self.schema = schema 

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

 

def tearDown(self): 

del self.schema 

del self.meas_apCorr_task 

del self.exposure 

 

def testAddFields(self): 

"""Instantiating the task should add one field to the schema.""" 

self.assertIn("apcorr_" + self.name + "_used", self.schema.getNames()) 

 

def testReturnApCorrMap(self): 

"""The measureApCorr task should return a structure with a single key 'apCorrMap'.""" 

struct = self.meas_apCorr_task.run(catalog=self.makeCatalog(), exposure=self.exposure) 

self.assertEqual(list(struct.getDict().keys()), ['apCorrMap']) 

 

def testApCorrMapKeys(self): 

"""An apCorrMap structure should have two keys, based on the name supplied to addApCorrName().""" 

apfluxName = self.apname + "_flux" 

apfluxErrName = self.apname + "_fluxErr" 

struct = self.meas_apCorr_task.run(catalog=self.makeCatalog(), exposure=self.exposure) 

key_names = [apfluxName, apfluxErrName] 

self.assertEqual(set(struct.apCorrMap.keys()), set(key_names)) 

 

def testTooFewSources(self): 

""" If there are too few sources, check that an exception is raised.""" 

catalog = afwTable.SourceCatalog(self.schema) 

with self.assertRaises(RuntimeError): 

self.meas_apCorr_task.run(catalog=catalog, exposure=self.exposure) 

# With the measurement algorithm declared as something that might fail, should not get an exception 

self.meas_apCorr_task.config.allowFailure.append(self.apname) 

self.meas_apCorr_task.run(catalog=catalog, exposure=self.exposure) 

 

def testSourceNotUsed(self): 

""" Check that a source outside the bounding box is flagged as not used (False).""" 

fluxName = self.name + "_flux" 

apCorrFlagKey = self.schema.find("apcorr_" + self.name + "_used").key 

sourceCat = self.makeCatalog() 

source = sourceCat.addNew() 

source_test_flux = 5.1 

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

fluxKey = self.schema.find(fluxName).key 

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

source.set(fluxKey, source_test_flux) 

source.set(centroidKey, source_test_centroid) 

self.meas_apCorr_task.run(catalog=sourceCat, exposure=self.exposure) 

self.assertFalse(sourceCat[apCorrFlagKey][-1]) 

 

def testSourceUsed(self): 

"""Check that valid sources inside the bounding box that are used have their flags set to True.""" 

inputFilterFlagKey = self.schema.find(self.meas_apCorr_task.config.sourceSelector.active.field).key 

sourceCat = self.makeCatalog() 

self.meas_apCorr_task.run(catalog=sourceCat, exposure=self.exposure) 

self.assertTrue(sourceCat[inputFilterFlagKey].all()) 

 

def testApertureMeasOnes(self): 

""" Check that sources with aperture fluxes exactly the same as their catalog fluxes 

returns an aperture correction map of 1s""" 

apFluxName = self.apname + "_flux" 

sourceCat = self.makeCatalog() 

struct = self.meas_apCorr_task.run(catalog=sourceCat, exposure=self.exposure) 

default_fill = apCorrDefaultMap(value=1.0, bbox=self.exposure.getBBox()) 

test_fill = afwImage.ImageF(self.exposure.getBBox()) 

struct.apCorrMap[apFluxName].fillImage(test_fill) 

np.testing.assert_allclose(test_fill.getArray(), default_fill.getArray()) 

 

def testApertureMeasTens(self): 

"""Check that aperture correction scales source fluxes in the correct direction.""" 

apCorr_factor = 10. 

sourceCat = self.makeCatalog(apCorrScale=apCorr_factor) 

apFluxName = self.apname + "_flux" 

struct = self.meas_apCorr_task.run(catalog=sourceCat, exposure=self.exposure) 

default_fill = apCorrDefaultMap(value=apCorr_factor, bbox=self.exposure.getBBox()) 

test_fill = afwImage.ImageF(self.exposure.getBBox()) 

struct.apCorrMap[apFluxName].fillImage(test_fill) 

np.testing.assert_allclose(test_fill.getArray(), default_fill.getArray()) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()