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

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

 

import unittest 

 

import numpy as np 

 

import lsst.utils.tests 

import lsst.afw.display as afwDisplay 

import lsst.geom 

import lsst.afw.image as afwImage 

import lsst.afw.math as afwMath 

import lsst.pex.exceptions as pexExcept 

 

afwDisplay.setDefaultMaskTransparency(75) 

# Set to True to display things 

display = False 

 

 

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

def makeRamp(self, binsize=1): 

"""Make a linear ramp""" 

ramp = afwImage.MaskedImageF(20, 40) 

 

x = [] 

for i in range(ramp.getWidth()): 

x.append((i + 0.5)*binsize - 0.5) 

 

y = [] 

for j in range(ramp.getHeight()): 

y.append((j + 0.5)*binsize - 0.5) 

 

var = 1 

rampCoeffs = (1000, 1, 1) 

for i in range(ramp.getHeight()): 

for j in range(ramp.getWidth()): 

ramp[j, i, afwImage.LOCAL] = (rampCoeffs[0] + rampCoeffs[1]*x[j] + rampCoeffs[2]*y[i], 

0x0, var) 

 

return ramp, rampCoeffs, x, y 

 

def testLinearRamp(self): 

"""Fit a ramp""" 

 

binsize = 1 

ramp, rampCoeffs, xVec, yVec = self.makeRamp(binsize) 

# Add a (labelled) bad value 

ramp[ramp.getWidth()//2, ramp.getHeight()//2, afwImage.LOCAL] = (0, 0x1, np.nan) 

 

if display: 

afwDisplay.Display(frame=0).mtv(ramp, title=self._testMethodName + ": Input") 

# Here's the range that the approximation should be valid (and also the 

# bbox of the image returned by getImage) 

bbox = lsst.geom.BoxI(lsst.geom.PointI(0, 0), 

lsst.geom.PointI(binsize*ramp.getWidth() - 1, 

binsize*ramp.getHeight() - 1)) 

 

order = 3 # 1 would be enough to fit the ramp 

actrl = afwMath.ApproximateControl( 

afwMath.ApproximateControl.CHEBYSHEV, order) 

approx = afwMath.makeApproximate(xVec, yVec, ramp, bbox, actrl) 

 

for i, aim in enumerate([approx.getImage(), 

approx.getMaskedImage().getImage(), 

]): 

if i == 0 and display: 

disp = afwDisplay.Display(frame=1) 

disp.mtv(aim, title=self._testMethodName + ": Interpolated") 

with disp.Buffering(): 

for x in xVec: 

for y in yVec: 

disp.dot('+', x, y, size=0.4) 

 

for x, y in aim.getBBox().getCorners(): 

self.assertEqual( 

aim[x, y, afwImage.LOCAL], rampCoeffs[0] + rampCoeffs[1]*x + rampCoeffs[1]*y) 

 

def testChebyshevEqualOrder(self): 

"""Check that we enforce the condition orderX == orderY""" 

 

self.assertRaises(pexExcept.InvalidParameterError, 

lambda: 

afwMath.ApproximateControl(afwMath.ApproximateControl.CHEBYSHEV, 1, 2)) 

 

def testNoFinitePoints(self): 

"""Check that makeApproximate throws a RuntimeError if grid has no finite points and weights to fit 

""" 

binsize = 1 

for badValue in [(3, 0x1, 0), (np.nan, 0x1, 1)]: 

ramp, rampCoeffs, xVec, yVec = self.makeRamp(binsize) 

ramp.set(badValue) 

bbox = lsst.geom.BoxI(lsst.geom.PointI(0, 0), lsst.geom.PointI(binsize*ramp.getWidth() - 1, 

binsize*ramp.getHeight() - 1)) 

order = 2 

actrl = afwMath.ApproximateControl( 

afwMath.ApproximateControl.CHEBYSHEV, order) 

self.assertRaises(pexExcept.RuntimeError, 

lambda: afwMath.makeApproximate(xVec, yVec, ramp, bbox, actrl)) 

 

def testLinearRampAsBackground(self): 

"""Fit a ramp""" 

 

ramp, rampCoeffs = self.makeRamp()[0:2] 

 

if display: 

afwDisplay.Display(frame=0).mtv(ramp, title=self._testMethodName + ": Input") 

# Here's the range that the approximation should be valid (and also the 

# bbox of the image returned by getImage) 

bkgd = afwMath.makeBackground(ramp, afwMath.BackgroundControl(10, 10)) 

 

orderMax = 3 # 1 would be enough to fit the ramp 

for order in range(orderMax + 1): 

actrl = afwMath.ApproximateControl( 

afwMath.ApproximateControl.CHEBYSHEV, order) 

 

approx = bkgd.getApproximate(actrl) 

# Get the Image, the MaskedImage, and the Image with a truncated 

# expansion 

for i, aim in enumerate([approx.getImage(), 

approx.getMaskedImage().getImage(), 

approx.getImage( 

order - 1 if order > 1 else -1), 

]): 

if display and (i == 0 and order == 1): 

afwDisplay.Display(frame=1).mtv(aim, title=self._testMethodName + ": Interpolated") 

 

for x, y in aim.getBBox().getCorners(): 

val = np.mean(aim.getArray()) if order == 0 else \ 

rampCoeffs[0] + rampCoeffs[1]*x + rampCoeffs[1]*y 

 

self.assertEqual(aim[x, y, afwImage.LOCAL], val) 

# Check that we can't "truncate" the expansion to a higher order than 

# we requested 

self.assertRaises(pexExcept.InvalidParameterError, 

lambda: approx.getImage(orderMax + 1, orderMax + 1)) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()