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

# This file is part of afw. 

# 

# 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/>. 

 

""" 

Tests for math.TransformBoundedField 

 

Run with: 

python test_transformBoundedField.py 

or 

pytest test_transformBoundedField.py 

""" 

 

import unittest 

 

import astshim 

import numpy as np 

from numpy.testing import assert_allclose 

 

import lsst.utils.tests 

import lsst.pex.exceptions 

import lsst.geom 

import lsst.afw.geom 

import lsst.afw.image 

from lsst.afw.math import TransformBoundedField 

 

 

45 ↛ exit,   45 ↛ exit,   45 ↛ exit,   45 ↛ exit,   45 ↛ exit,   45 ↛ exit6 missed branches: 1) line 51 didn't finish the lambda on line 51, 2) line 50 didn't finish the lambda on line 50, 3) line 49 didn't finish the lambda on line 49, 4) line 48 didn't finish the lambda on line 48, 5) line 47 didn't finish the lambda on line 47, 6) line 46 didn't finish the lambda on line 46CHEBYSHEV_T = [ 

lambda x: x**0, 

lambda x: x, 

lambda x: 2*x**2 - 1, 

lambda x: (4*x**2 - 3)*x, 

lambda x: (8*x**2 - 8)*x**2 + 1, 

lambda x: ((16*x**2 - 20)*x**2 + 5)*x, 

] 

 

 

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

 

def setUp(self): 

self.longMessage = True 

 

# an arbitrary bounding box (not that this kind of field cares) 

self.bbox = lsst.geom.Box2I(lsst.geom.Point2I(-3, 4), 

lsst.geom.Extent2I(5, 30)) 

 

# a list of points contained in the bbox 

self.pointList = lsst.geom.Box2D(self.bbox).getCorners() 

self.pointList.append(lsst.geom.Box2D(self.bbox).getCenter()) 

self.xList = np.array([p[0] for p in self.pointList]) 

self.yList = np.array([p[1] for p in self.pointList]) 

 

# a simple polynomial mapping 

coeff_f = np.array([ 

[1.5, 1, 0, 0], 

[-0.5, 1, 1, 0], 

[1.0, 1, 0, 1], 

]) 

polyMap = astshim.PolyMap(coeff_f, 1) 

self.transform = lsst.afw.geom.TransformPoint2ToGeneric(polyMap) 

self.boundedField = TransformBoundedField(self.bbox, self.transform) 

 

def tearDown(self): 

del self.transform 

 

def testEvaluate(self): 

"""Test the various overloads of `evaluate` 

""" 

for point in self.pointList: 

# applylForward returns a vector with one entry per axis 

# and in this case there is just one axis 

predRes = self.transform.applyForward(point)[0] 

 

res = self.boundedField.evaluate(point) 

self.assertFloatsAlmostEqual(res, predRes) 

 

x, y = point 

res2 = self.boundedField.evaluate(x, y) 

self.assertFloatsAlmostEqual(res2, predRes) 

 

resArr = self.boundedField.evaluate(self.xList, self.yList) 

# applylForward returns an array with one row of values per axis 

# and in this case there is just one axis 

predResArr = self.transform.applyForward(self.pointList)[0] 

assert_allclose(resArr, predResArr) 

 

def testMultiplyOperator(self): 

"""Test operator* 

""" 

maxVal = np.max(np.abs(self.transform.applyForward(self.pointList)[0])) 

for multFactor in (-9e99, -1.5e-7, 3.6e-7, 1.5, 9.23e99): 

atol = abs(maxVal*multFactor*1e-15) 

predResult = self.transform.applyForward(self.pointList)[0]*multFactor 

 

scaledField1 = self.boundedField*multFactor 

assert_allclose(scaledField1.evaluate(self.xList, self.yList), predResult, atol=atol) 

 

scaledField2 = multFactor*self.boundedField 

assert_allclose(scaledField2.evaluate(self.xList, self.yList), predResult, atol=atol) 

 

def testBBox(self): 

"""The BBox should have no effect on the kind of transform being tested 

 

Use an empty bbox as an extreme test of this 

""" 

self.assertEqual(self.boundedField.getBBox(), self.bbox) 

 

emptyBBox = lsst.geom.Box2I() 

noBBoxField = TransformBoundedField(emptyBBox, self.transform) 

self.assertEqual(noBBoxField.getBBox(), emptyBBox) 

 

resArr = self.boundedField.evaluate(self.xList, self.yList) 

resArrNoBBox = noBBoxField.evaluate(self.xList, self.yList) 

assert_allclose(resArr, resArrNoBBox) 

 

def testPersistenceAndEquality(self): 

"""Test persistence using writeFits and readFits 

 

Also test operator== 

""" 

with lsst.utils.tests.getTempFilePath(".fits") as filename: 

self.boundedField.writeFits(filename) 

readField = TransformBoundedField.readFits(filename) 

 

self.assertTrue(self.boundedField == readField) 

self.assertFalse(self.boundedField != readField) 

self.assertEqual(self.boundedField, readField) 

 

resArr = self.boundedField.evaluate(self.xList, self.yList) 

readResArr = readField.evaluate(self.xList, self.yList) 

assert_allclose(resArr, readResArr) 

self.assertEqual(readField.getBBox(), self.bbox) 

 

def testComplexPersistence(self): 

"""Test persistence of a TransformBoundedField whose string representation is huge 

""" 

# DM-11964 shows that CFITSIO cannot handle string fields 

# in binary tables that have more than 28799 characters 

# make sure the test has plenty of margin 

minChars = 10*28799 

degree = 100 # make large enough that len(transform.writeString()) > minChars 

n_coeffs = (degree + 1)*(degree + 2)//2 

coeffs = np.zeros((n_coeffs, 4), dtype=float) 

k = 0 

for j in range(degree + 1): 

for i in range(degree - j + 1): 

coeffs[k][0] = np.random.random() 

coeffs[k][1] = 1 

coeffs[k][2] = i 

coeffs[k][3] = j 

k += 1 

chebyMap = astshim.PolyMap(coeffs, 1) 

transform = lsst.afw.geom.TransformPoint2ToGeneric(chebyMap) 

lenTransformStr = len(transform.writeString()) 

print(f"len transform str={lenTransformStr}; minChars={minChars}") 

self.assertGreater(lenTransformStr, minChars) 

complexBoundedField = TransformBoundedField(self.bbox, transform) 

with lsst.utils.tests.getTempFilePath(".fits") as filename: 

complexBoundedField.writeFits(filename) 

readField = TransformBoundedField.readFits(filename) 

 

self.assertTrue(complexBoundedField == readField) 

self.assertFalse(complexBoundedField != readField) 

self.assertEqual(complexBoundedField, readField) 

 

resArr = complexBoundedField.evaluate(self.xList, self.yList) 

readResArr = readField.evaluate(self.xList, self.yList) 

assert_allclose(resArr, readResArr) 

self.assertEqual(readField.getBBox(), self.bbox) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()