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

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

# 

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

import unittest 

 

import lsst.afw.table as afwTable 

import lsst.log 

from lsst.meas.algorithms import LoadReferenceObjectsTask, getRefFluxField, getRefFluxKeys 

from lsst.meas.algorithms.loadReferenceObjects import hasNanojanskyFluxUnits, convertToNanojansky 

import lsst.utils.tests 

 

 

class TrivialLoader(LoadReferenceObjectsTask): 

"""Minimal subclass of LoadReferenceObjectsTask to allow instantiation 

""" 

 

def loadSkyCircle(self, ctrCoord, radius, filterName): 

pass 

 

 

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

"""Test case for LoadReferenceObjectsTask abstract base class 

 

Only methods with concrete implementations are tested (hence not loadSkyCircle) 

""" 

 

def testMakeMinimalSchema(self): 

"""Make a schema and check it.""" 

for filterNameList in (["r"], ["foo", "_bar"]): 

for (addIsPhotometric, addIsResolved, addIsVariable, 

coordErrDim, addProperMotion, properMotionErrDim, 

addParallax, addParallaxErr) in itertools.product( 

(False, True), (False, True), (False, True), 

(-1, 0, 1, 2, 3, 4), (False, True), (-1, 0, 1, 2, 3, 4), 

(False, True), (False, True)): 

argDict = dict( 

filterNameList=filterNameList, 

addIsPhotometric=addIsPhotometric, 

addIsResolved=addIsResolved, 

addIsVariable=addIsVariable, 

coordErrDim=coordErrDim, 

addProperMotion=addProperMotion, 

properMotionErrDim=properMotionErrDim, 

addParallax=addParallax, 

addParallaxErr=addParallaxErr, 

) 

if coordErrDim not in (0, 2, 3) or \ 

(addProperMotion and properMotionErrDim not in (0, 2, 3)): 

with self.assertRaises(ValueError): 

LoadReferenceObjectsTask.makeMinimalSchema(**argDict) 

else: 

refSchema = LoadReferenceObjectsTask.makeMinimalSchema(**argDict) 

self.assertTrue("coord_ra" in refSchema) 

self.assertTrue("coord_dec" in refSchema) 

self.assertTrue("centroid_x" in refSchema) 

self.assertTrue("centroid_y" in refSchema) 

self.assertTrue("hasCentroid" in refSchema) 

for filterName in filterNameList: 

fluxField = filterName + "_flux" 

self.assertIn(fluxField, refSchema) 

self.assertNotIn("x" + fluxField, refSchema) 

fluxErrField = fluxField + "Err" 

self.assertIn(fluxErrField, refSchema) 

self.assertEqual(getRefFluxField(refSchema, filterName), filterName + "_flux") 

self.assertEqual("resolved" in refSchema, addIsResolved) 

self.assertEqual("variable" in refSchema, addIsVariable) 

self.assertEqual("photometric" in refSchema, addIsPhotometric) 

self.assertEqual("photometric" in refSchema, addIsPhotometric) 

self.assertEqual("epoch" in refSchema, addProperMotion or addParallax) 

self.assertEqual("coord_raErr" in refSchema, coordErrDim > 0) 

self.assertEqual("coord_decErr" in refSchema, coordErrDim > 0) 

self.assertEqual("coord_ra_dec_Cov" in refSchema, coordErrDim == 3) 

self.assertEqual("pm_ra" in refSchema, addProperMotion) 

self.assertEqual("pm_dec" in refSchema, addProperMotion) 

self.assertEqual("pm_raErr" in refSchema, addProperMotion and properMotionErrDim > 0) 

self.assertEqual("pm_decErr" in refSchema, addProperMotion and properMotionErrDim > 0) 

self.assertEqual("pm_flag" in refSchema, addProperMotion) 

self.assertEqual("pm_ra_dec_Cov" in refSchema, 

addProperMotion and properMotionErrDim == 3) 

self.assertEqual("parallax" in refSchema, addParallax) 

self.assertEqual("parallaxErr" in refSchema, addParallax and addParallaxErr) 

self.assertEqual("parallax_flag" in refSchema, addParallax) 

 

def testFilterAliasMap(self): 

"""Make a schema with filter aliases.""" 

for defaultFilter in ("", "r", "camr"): 

for filterMap in ({}, {"camr": "r"}): 

config = TrivialLoader.ConfigClass() 

config.defaultFilter = defaultFilter 

config.filterMap = filterMap 

loader = TrivialLoader(config=config) 

refSchema = TrivialLoader.makeMinimalSchema(filterNameList="r") 

try: 

loader._addFluxAliases(refSchema) 

self.assertNotEqual(defaultFilter, "camr") 

except Exception: 

# only reference filters are allowed as default filters 

self.assertEqual(defaultFilter, "camr") 

continue 

 

self.assertIn("r_flux", refSchema) 

self.assertIn("r_fluxErr", refSchema) 

 

# camera filters aliases are named <filter>_camFlux 

if "camr" in filterMap: 

self.assertEqual(getRefFluxField(refSchema, "camr"), "camr_camFlux") 

else: 

with self.assertRaises(RuntimeError): 

getRefFluxField(refSchema, "camr") 

 

# if a non-empty default filter is specified then camFlux 

# and camFluxErr should be present 

hasDefault = bool(defaultFilter) 

self.assertEqual("camFlux" in refSchema, hasDefault) 

self.assertEqual("camFluxErr" in refSchema, hasDefault) 

 

refCat = afwTable.SimpleCatalog(refSchema) 

refObj = refCat.addNew() 

refObj["r_flux"] = 1.23 

self.assertAlmostEqual(refCat[0].get(getRefFluxField(refSchema, "r")), 1.23) 

if "camr" in filterMap: 

self.assertAlmostEqual(refCat[0].get(getRefFluxField(refSchema, "camr")), 1.23) 

if hasDefault: 

self.assertEqual(getRefFluxField(refSchema, ""), "camFlux") 

self.assertAlmostEqual(refCat[0].get(getRefFluxField(refSchema, "")), 1.23) 

refObj["r_fluxErr"] = 0.111 

if "camr" in filterMap: 

self.assertEqual(refCat[0].get("camr_camFluxErr"), 0.111) 

fluxKey, fluxErrKey = getRefFluxKeys(refSchema, "r") 

self.assertEqual(refCat[0].get(fluxKey), 1.23) 

self.assertEqual(refCat[0].get(fluxErrKey), 0.111) 

if "camr" in filterMap: 

fluxKey, fluxErrKey = getRefFluxKeys(refSchema, "camr") 

self.assertEqual(refCat[0].get(fluxErrKey), 0.111) 

else: 

with self.assertRaises(RuntimeError): 

getRefFluxKeys(refSchema, "camr") 

 

def testCheckFluxUnits(self): 

"""Test that we can identify old style fluxes in a schema.""" 

schema = LoadReferenceObjectsTask.makeMinimalSchema(['r', 'z']) 

# the default schema should pass 

self.assertTrue(hasNanojanskyFluxUnits(schema)) 

schema.addField('bad_fluxSigma', doc='old flux units', type=float, units='') 

self.assertFalse(hasNanojanskyFluxUnits(schema)) 

 

schema = LoadReferenceObjectsTask.makeMinimalSchema(['r', 'z']) 

schema.addField('bad_flux', doc='old flux units', type=float, units='') 

self.assertFalse(hasNanojanskyFluxUnits(schema)) 

 

schema = LoadReferenceObjectsTask.makeMinimalSchema(['r', 'z']) 

schema.addField('bad_flux', doc='old flux units', type=float, units='Jy') 

self.assertFalse(hasNanojanskyFluxUnits(schema)) 

 

schema = LoadReferenceObjectsTask.makeMinimalSchema(['r', 'z']) 

schema.addField('bad_fluxErr', doc='old flux units', type=float, units='') 

self.assertFalse(hasNanojanskyFluxUnits(schema)) 

 

schema = LoadReferenceObjectsTask.makeMinimalSchema(['r', 'z']) 

schema.addField('bad_fluxErr', doc='old flux units', type=float, units='Jy') 

self.assertFalse(hasNanojanskyFluxUnits(schema)) 

 

schema = LoadReferenceObjectsTask.makeMinimalSchema(['r', 'z']) 

schema.addField('bad_fluxSigma', doc='old flux units', type=float, units='') 

self.assertFalse(hasNanojanskyFluxUnits(schema)) 

 

def testConvertOldFluxes(self): 

"""Check that we can convert old style fluxes in a catalog.""" 

flux = 1.234 

fluxErr = 5.678 

log = lsst.log.Log() 

 

def make_catalog(): 

schema = LoadReferenceObjectsTask.makeMinimalSchema(['r', 'z']) 

schema.addField('bad_flux', doc='old flux units', type=float, units='') 

schema.addField('bad_fluxErr', doc='old flux units', type=float, units='Jy') 

refCat = afwTable.SimpleCatalog(schema) 

refObj = refCat.addNew() 

refObj["bad_flux"] = flux 

refObj["bad_fluxErr"] = fluxErr 

return refCat 

 

oldRefCat = make_catalog() 

newRefCat = convertToNanojansky(oldRefCat, log) 

self.assertEqual(newRefCat['bad_flux'], [flux*1e9, ]) 

self.assertEqual(newRefCat['bad_fluxErr'], [fluxErr*1e9, ]) 

self.assertEqual(newRefCat.schema['bad_flux'].asField().getUnits(), 'nJy') 

self.assertEqual(newRefCat.schema['bad_fluxErr'].asField().getUnits(), 'nJy') 

 

# check that doConvert=False returns None (it also logs a summary) 

oldRefCat = make_catalog() 

newRefCat = convertToNanojansky(oldRefCat, log, doConvert=False) 

self.assertIsNone(newRefCat) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()