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

import unittest 

 

 

import lsst.utils.tests 

import lsst.afw.image as afwImage 

import lsst.geom as geom 

import lsst.ip.diffim as ipDiffim 

import lsst.log.utils as logUtils 

import lsst.pex.config as pexConfig 

 

logUtils.traceSetAt("ip.diffim", 4) 

 

# This tests the basics of the BuildSpatialKernelVisitor. E.g. that 

# it makes the right size solution. For more complex behaviors such 

# as reducing a delta function basis set into a Pca basis set, look at 

# FitSpatialKernelFromCandidates.py 

 

 

class DiffimTestCases(unittest.TestCase): 

 

def setUp(self): 

self.config = ipDiffim.ImagePsfMatchTask.ConfigClass() 

self.config.kernel.name = "AL" 

self.subconfig = self.config.kernel.active 

 

self.ps = pexConfig.makePropertySet(self.subconfig) 

self.size = 51 

 

def tearDown(self): 

del self.ps 

 

def makeCandidate(self, kSum, x, y): 

mi1 = afwImage.MaskedImageF(geom.Extent2I(self.size, self.size)) 

mi1.getVariance().set(1.0) # avoid NaNs 

mi1[self.size//2, self.size//2, afwImage.LOCAL] = (1, 0x0, 1) 

mi2 = afwImage.MaskedImageF(geom.Extent2I(self.size, self.size)) 

mi2.getVariance().set(1.0) # avoid NaNs 

mi2[self.size//2, self.size//2, afwImage.LOCAL] = (kSum, 0x0, kSum) 

kc = ipDiffim.makeKernelCandidate(x, y, mi1, mi2, self.ps) 

return kc 

 

def testNoBg(self): 

self.runNoBg(0) 

self.runNoBg(1) 

self.runNoBg(2) 

 

def runNoBg(self, sko): 

basisList = ipDiffim.makeKernelBasisList(self.subconfig) 

self.ps['spatialKernelOrder'] = sko 

self.ps['fitForBackground'] = False 

 

bbox = geom.Box2I(geom.Point2I(0, 0), 

geom.Extent2I(self.size*10, self.size*10)) 

 

bsikv = ipDiffim.BuildSingleKernelVisitorF(basisList, self.ps) 

bspkv = ipDiffim.BuildSpatialKernelVisitorF(basisList, bbox, self.ps) 

 

for x in range(1, self.size, 10): 

for y in range(1, self.size, 10): 

cand = self.makeCandidate(1.0, x, y) 

bsikv.processCandidate(cand) 

bspkv.processCandidate(cand) 

 

bspkv.solveLinearEquation() 

sk, sb = bspkv.getSolutionPair() 

 

# Kernel 

if sko == 0: 

# Specialization for speedup 

spatialKernelSolution = sk.getKernelParameters() 

 

# One term for each basis function 

self.assertEqual(len(spatialKernelSolution), len(basisList)) 

 

else: 

spatialKernelSolution = sk.getSpatialParameters() 

 

nSpatialTerms = int(0.5 * (sko + 1) * (sko + 2)) 

# One model for each basis function 

self.assertEqual(len(spatialKernelSolution), len(basisList)) 

# First basis has no spatial variation 

for i in range(1, nSpatialTerms): 

self.assertEqual(spatialKernelSolution[0][i], 0.) 

# All bases have correct number of terms 

for i in range(len(spatialKernelSolution)): 

self.assertEqual(len(spatialKernelSolution[i]), nSpatialTerms) 

 

# Background 

spatialBgSolution = sb.getParameters() 

nBgTerms = 1 

self.assertEqual(len(spatialBgSolution), nBgTerms) 

 

def testModelType(self): 

bbox = geom.Box2I(geom.Point2I(10, 10), 

geom.Extent2I(10, 10)) 

basisList = ipDiffim.makeKernelBasisList(self.subconfig) 

 

self.ps["spatialModelType"] = "polynomial" 

ipDiffim.BuildSpatialKernelVisitorF(basisList, bbox, self.ps) 

 

self.ps["spatialModelType"] = "chebyshev1" 

ipDiffim.BuildSpatialKernelVisitorF(basisList, bbox, self.ps) 

 

try: 

self.ps["spatialModelType"] = "foo" 

ipDiffim.BuildSpatialKernelVisitorF(basisList, bbox, self.ps) 

except Exception: 

pass 

else: 

self.fail() 

 

def testAlSpatialModel(self): 

self.runAlSpatialModel(0, 0) 

self.runAlSpatialModel(1, 0) 

self.runAlSpatialModel(0, 1) 

self.runAlSpatialModel(1, 1) 

self.runAlSpatialModel(2, 2) 

 

def runAlSpatialModel(self, sko, bgo): 

basisList = ipDiffim.makeKernelBasisList(self.subconfig) 

self.ps['spatialKernelOrder'] = sko 

self.ps['spatialBgOrder'] = bgo 

self.ps['fitForBackground'] = True 

 

bbox = geom.Box2I(geom.Point2I(0, 0), 

geom.Extent2I(self.size*10, self.size*10)) 

 

bsikv = ipDiffim.BuildSingleKernelVisitorF(basisList, self.ps) 

bspkv = ipDiffim.BuildSpatialKernelVisitorF(basisList, bbox, self.ps) 

 

for x in range(1, self.size, 10): 

for y in range(1, self.size, 10): 

cand = self.makeCandidate(1.0, x, y) 

bsikv.processCandidate(cand) 

bspkv.processCandidate(cand) 

 

bspkv.solveLinearEquation() 

sk, sb = bspkv.getSolutionPair() 

 

# Kernel 

if sko == 0: 

# Specialization for speedup 

spatialKernelSolution = sk.getKernelParameters() 

 

# One term for each basis function 

self.assertEqual(len(spatialKernelSolution), len(basisList)) 

 

else: 

spatialKernelSolution = sk.getSpatialParameters() 

 

nSpatialTerms = int(0.5 * (sko + 1) * (sko + 2)) 

# One model for each basis function 

self.assertEqual(len(spatialKernelSolution), len(basisList)) 

# First basis has no spatial variation 

for i in range(1, nSpatialTerms): 

self.assertEqual(spatialKernelSolution[0][i], 0.) 

# All bases have correct number of terms 

for i in range(len(spatialKernelSolution)): 

self.assertEqual(len(spatialKernelSolution[i]), nSpatialTerms) 

 

# Background 

spatialBgSolution = sb.getParameters() 

nBgTerms = int(0.5 * (bgo + 1) * (bgo + 2)) 

self.assertEqual(len(spatialBgSolution), nBgTerms) 

 

 

##### 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()