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

# 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 

 

from lsst.afw.geom import makeSkyWcs 

import lsst.afw.image as afwImage 

import lsst.afw.table as afwTable 

import lsst.daf.base as dafBase 

import lsst.meas.base as measBase 

import lsst.pex.config as pexConfig 

import lsst.pex.exceptions as pexExcept 

import lsst.utils.tests 

 

import testLib 

 

try: 

type(verbose) 

except NameError: 

verbose = 0 

 

 

def makeWcs(): 

"""Provide a simple WCS for use in testing. 

""" 

# The parameters given are placeholders; their values are unimportant 

md = dafBase.PropertySet() 

md.set("NAXIS", 2) 

md.set("CTYPE1", "RA---TAN") 

md.set("CTYPE2", "DEC--TAN") 

md.set("CRPIX1", 0) 

md.set("CRPIX2", 0) 

md.set("CRVAL1", 0) 

md.set("CRVAL2", 0) 

md.set("RADESYS", "FK5") 

md.set("EQUINOX", 2000.0) 

return makeSkyWcs(md) 

 

 

@pexConfig.wrap(testLib.SillyCentroidControl) 

class SillyCentroidConfig(pexConfig.Config): 

pass 

 

 

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

pluginName = "base_SillyCentroid" 

centroidPosition = (1.0, -1.0) 

 

def _generateCatalog(self): 

"""Returns a SourceCatalog with one entry""" 

schema = afwTable.SourceTable.makeMinimalSchema() 

schema.addField(self.pluginName + "_x", type=np.float64) 

schema.addField(self.pluginName + "_y", type=np.float64) 

cat = afwTable.SourceCatalog(schema) 

source = cat.addNew() 

source.set(self.pluginName + "_x", self.centroidPosition[0]) 

source.set(self.pluginName + "_y", self.centroidPosition[1]) 

return cat 

 

def _performTransform(self, transformClass, inCat, doExtend=True): 

"""Operate on ``inCat`` with a transform of class ``transformClass``. 

""" 

mapper = afwTable.SchemaMapper(inCat.schema) 

config = SillyCentroidConfig() 

transform = transformClass(config, self.pluginName, mapper) 

outCat = afwTable.BaseCatalog(mapper.getOutputSchema()) 

if doExtend: 

outCat.extend(inCat, mapper=mapper) 

transform(inCat, outCat, makeWcs(), afwImage.Calib()) 

return outCat 

 

def _checkSillyOutputs(self, inCat, outCat): 

"""Check that ``outCat`` matches ``inCat`` under `SillyTransform`. 

 

SillyTransform looks for fields named ``name_x`` and ``name_y``; it 

copies them to the output, and adds ``name_reverse_x`` and ``_y`` 

which are equal to the corresponding inputs multiplied by -1. 

""" 

for inSrc, outSrc in zip(inCat, outCat): 

# The source x, y should have been copied to the output table 

self.assertEqual(outSrc[self.pluginName + "_x"], inSrc[self.pluginName + "_x"]) 

self.assertEqual(outSrc[self.pluginName + "_y"], inSrc[self.pluginName + "_y"]) 

 

# And the reversed position added 

self.assertEqual(outSrc[self.pluginName + "_reverse_x"], -1.0 * inSrc[self.pluginName + "_x"]) 

self.assertEqual(outSrc[self.pluginName + "_reverse_y"], -1.0 * inSrc[self.pluginName + "_y"]) 

 

# Other entries from the source table have not been copied 

for name in ("id", "coord_ra", "coord_dec", "parent"): 

self.assertIn(name, inCat.schema) 

self.assertNotIn(name, outCat.schema) 

 

def testNullTransform(self): 

"""The `NullTransform` passes through nothing. 

""" 

inCat = self._generateCatalog() 

with self.assertRaises(pexExcept.LengthError): 

self._performTransform(measBase.NullTransform, inCat, False) 

outCat = self._performTransform(measBase.NullTransform, inCat) 

self.assertEqual(len(inCat), len(outCat)) 

self.assertEqual(outCat.schema.getFieldCount(), 0) 

 

def testPassThroughTransform(self): 

"""Copies all fields starting with the plugin name. 

""" 

inCat = self._generateCatalog() 

with self.assertRaises(pexExcept.LengthError): 

self._performTransform(measBase.PassThroughTransform, inCat, False) 

outCat = self._performTransform(measBase.PassThroughTransform, inCat) 

self.assertEqual(len(inCat), len(outCat)) 

for inSrc, outSrc in zip(inCat, outCat): 

for fieldname in inCat.schema.extract(self.pluginName + "*"): 

self.assertEqual(inSrc.get(fieldname), outSrc.get(fieldname)) 

 

def testPythonConfig(self): 

"""Python configs are converted to Control when using a C++ transform. 

""" 

inCat = self._generateCatalog() 

with self.assertRaises(pexExcept.LengthError): 

self._performTransform(testLib.SillyTransform, inCat, False) 

outCat = self._performTransform(testLib.SillyTransform, inCat) 

self._checkSillyOutputs(inCat, outCat) 

 

def testApplyCppTransform(self): 

"""Test that we can apply a simple C++ transform. 

""" 

inCat = self._generateCatalog() 

sillyControl = testLib.SillyCentroidControl() 

mapper = afwTable.SchemaMapper(inCat.schema) 

sillyTransform = testLib.SillyTransform(sillyControl, self.pluginName, mapper) 

outCat = afwTable.BaseCatalog(mapper.getOutputSchema()) 

outCat.extend(inCat, mapper=mapper) 

self.assertEqual(len(inCat), len(outCat)) 

sillyTransform(inCat, outCat, makeWcs(), afwImage.Calib()) 

self._checkSillyOutputs(inCat, outCat) 

 

 

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

 

def testDefaultTransform(self): 

"""By default, we perform no transformations. 

""" 

self.assertEqual(measBase.BasePlugin.getTransformClass(), measBase.PassThroughTransform) 

 

def testWrapAlgorithm(self): 

"""Test the appropriate transform is provided for wrapped algorithms. 

""" 

# By default, we inherit from BasePlugin 

# NB the choice of algorithm and executionOrder is arbitrary 

singleFrame, forced = measBase.wrapSimpleAlgorithm(measBase.PsfFluxAlgorithm, 

Control=measBase.PsfFluxControl, 

executionOrder=measBase.BasePlugin.FLUX_ORDER, 

doRegister=False) 

self.assertEqual(singleFrame.getTransformClass(), measBase.BasePlugin.getTransformClass()) 

# Unless we override 

singleFrame, forced = measBase.wrapSimpleAlgorithm(measBase.PsfFluxAlgorithm, 

Control=measBase.PsfFluxControl, 

executionOrder=measBase.BasePlugin.FLUX_ORDER, 

doRegister=False, 

TransformClass=measBase.PassThroughTransform) 

self.assertEqual(singleFrame.getTransformClass(), measBase.PassThroughTransform) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()