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

# 

# LSST Data Management System 

# Copyright 2018 LSST Corporation. 

# 

# 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 

# 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 <http://www.lsstcorp.org/LegalNotices/>. 

# 

 

import numpy as np 

import unittest 

 

import lsst.utils.tests 

from lsst.pex.exceptions import InvalidParameterError 

from lsst.afw.geom import Point2D, Extent2D, Point2I, Box2D, Box2I, SpherePoint, \ 

degrees, makeSkyWcs, makeCdMatrix 

from lsst.afw.image import TransmissionCurve 

from lsst.afw.geom.polygon import Polygon 

from lsst.afw.table import ExposureTable, ExposureCatalog 

from lsst.meas.algorithms import makeCoaddTransmissionCurve 

from lsst.meas.algorithms.testUtils import makeRandomTransmissionCurve 

 

 

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

 

def setUp(self): 

# Test geometry: 

# 

# -100,99 99,99 

# +--------------------+ 

# |AAAAAAAAAACCCCCDDDDD| A == only in epoch A 

# |AAAAAAAAAACCCCCDDDDD| B == only in epoch B 

# |AAAAAAAAAACCCCCDDDDD| C == in both epoch A and epoch B 

# |AAAAAAAAAACCCCCDDDDD| D == in epoch A; in B's bbox but outside its ValidPolygon 

# |AAAAAAAAAACCCCCDDDDD| 

# | BBBBBBBBBB| All WCSs have the same CRVAL and CD. 

# | BBBBBBBBBB| 

# | BBBBBBBBBB| Coadd has CRPIX=(0, 0) 

# | BBBBBBBBBB| Epoch A has CRPIX=(0, -50) 

# | BBBBBBBBBB| Epoch B has CRPIX=(-50, 0) 

# +--------------------+ 

# -100,-100 99,-100 

# 

self.rng = np.random.RandomState(50) 

crval = SpherePoint(45.0, 45.0, degrees) 

cdMatrix = makeCdMatrix(scale=5E-5*degrees, flipX=True) 

self.wcsCoadd = makeSkyWcs(crpix=Point2D(0.0, 0.0), crval=crval, cdMatrix=cdMatrix) 

self.wcsA = makeSkyWcs(crpix=Point2D(0.0, -50.0), crval=crval, cdMatrix=cdMatrix) 

self.wcsB = makeSkyWcs(crpix=Point2D(-50.0, 0.0), crval=crval, cdMatrix=cdMatrix) 

self.bboxCoadd = Box2I(Point2I(-100, -100), Point2I(99, 99)) 

self.bboxA = Box2I(Point2I(-100, -50), Point2I(99, 49)) 

self.bboxB = Box2I(Point2I(-50, -100), Point2I(49, 99)) 

self.polygonA = None 

polygonD = Polygon(Box2D(Box2I(Point2I(0, 0), Point2I(49, 99)))) 

self.polygonB, = polygonD.symDifference(Polygon(Box2D(self.bboxB))) 

self.curveA = makeRandomTransmissionCurve(self.rng) 

self.curveB = makeRandomTransmissionCurve(self.rng) 

self.weightA = 0.6 

self.weightB = 0.2 

schema = ExposureTable.makeMinimalSchema() 

weightKey = schema.addField("weight", type=float, doc="relative weight of image in Coadd") 

catalog = ExposureCatalog(schema) 

recordA = catalog.addNew() 

recordA[weightKey] = self.weightA 

recordA.setWcs(self.wcsA) 

recordA.setValidPolygon(self.polygonA) 

recordA.setBBox(self.bboxA) 

recordA.setTransmissionCurve(self.curveA) 

recordB = catalog.addNew() 

recordB[weightKey] = self.weightB 

recordB.setWcs(self.wcsB) 

recordB.setValidPolygon(self.polygonB) 

recordB.setBBox(self.bboxB) 

recordB.setTransmissionCurve(self.curveB) 

self.curveCoadd = makeCoaddTransmissionCurve(self.wcsCoadd, catalog) 

 

def tearDown(self): 

del self.wcsCoadd 

del self.wcsA 

del self.wcsB 

del self.curveCoadd 

 

def makeRandomPoint(self, *args, **kwds): 

"""Draw a random Point2D within a Box2I. 

 

All arguments are forwarded directly to the Box2I constructor, allowing 

the caller to pass a fully-constructed Box2I, a (Point2I, Point2I) pair, 

or a (Point2I, Extent2I) pair. 

""" 

bboxD = Box2D(Box2I(*args, **kwds)) 

return bboxD.getMin() + Extent2D(bboxD.getWidth()*self.rng.rand(), 

bboxD.getHeight()*self.rng.rand()) 

 

def testSampleAt(self): 

"""Test the behavior of TransmissionCurve.sampleAt on the subclass 

returned by makeCoaddTransmissionCurve. 

""" 

wavelengths = np.linspace(4000, 7000, 200) 

 

# Points in coadd coordinates in each of the distinct regions 

point0 = self.makeRandomPoint(Point2I(-100, -100), Point2I(-1, -1)) 

pointA = self.makeRandomPoint(Point2I(-100, 0), Point2I(-1, 99)) 

pointB = self.makeRandomPoint(Point2I(0, -100), Point2I(99, -1)) 

pointC = self.makeRandomPoint(Point2I(0, 0), Point2I(49, 99)) 

pointD = self.makeRandomPoint(Point2I(50, 0), Point2I(99, 99)) 

points = [point0, pointA, pointB, pointC, pointD] 

 

# The same points, in sky coordinates 

coords = [self.wcsCoadd.pixelToSky(point) for point in points] 

 

# The same points, in Epoch A's coordinates 

point0A, pointAA, pointBA, pointCA, pointDA = [self.wcsA.skyToPixel(coord) for coord in coords] 

self.assertFalse(Box2D(self.bboxA).contains(point0A)) 

self.assertTrue(Box2D(self.bboxA).contains(pointAA)) 

self.assertFalse(Box2D(self.bboxA).contains(pointBA)) 

self.assertTrue(Box2D(self.bboxA).contains(pointCA)) 

self.assertTrue(Box2D(self.bboxA).contains(pointDA)) 

 

# The same points, in Epoch B's coordinates 

point0B, pointAB, pointBB, pointCB, pointDB = [self.wcsB.skyToPixel(coord) for coord in coords] 

self.assertFalse(Box2D(self.bboxB).contains(point0B)) 

self.assertFalse(Box2D(self.bboxB).contains(pointAB)) 

self.assertTrue(Box2D(self.bboxB).contains(pointBB)) 

self.assertTrue(Box2D(self.bboxB).contains(pointCB)) 

self.assertTrue(Box2D(self.bboxB).contains(pointDB)) 

self.assertTrue(self.polygonB.contains(pointBB)) 

self.assertTrue(self.polygonB.contains(pointCB)) 

self.assertFalse(self.polygonB.contains(pointDB)) 

 

# Test that we can't compute throughputs in region 0 (where there are no inputs) 

self.assertRaises(InvalidParameterError, self.curveCoadd.sampleAt, point0, wavelengths) 

 

# Test throughputs in region A (only Epoch A contributes) 

throughputA1 = self.curveCoadd.sampleAt(pointA, wavelengths) 

throughputA2 = self.curveA.sampleAt(pointAA, wavelengths) 

self.assertFloatsAlmostEqual(throughputA1, throughputA2) 

 

# Test throughputs in region B (only Epoch B contributes) 

throughputB1 = self.curveCoadd.sampleAt(pointB, wavelengths) 

throughputB2 = self.curveB.sampleAt(pointBB, wavelengths) 

self.assertFloatsAlmostEqual(throughputB1, throughputB2) 

 

# Test throughputs in region C (both epochs contribute) 

throughputC1 = self.curveCoadd.sampleAt(pointC, wavelengths) 

throughputC2 = self.curveA.sampleAt(pointCA, wavelengths) 

throughputC3 = self.curveB.sampleAt(pointCB, wavelengths) 

self.assertFloatsAlmostEqual(throughputC1, throughputC2*0.75 + throughputC3*0.25) 

 

# Test throughputs in region D (only Epoch A contributes) 

throughputD1 = self.curveCoadd.sampleAt(pointD, wavelengths) 

throughputD2 = self.curveA.sampleAt(pointDA, wavelengths) 

self.assertFloatsAlmostEqual(throughputD1, throughputD2) 

 

def testPersistence(self): 

wavelengths = np.linspace(4000, 7000, 200) 

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

self.curveCoadd.writeFits(filename) 

roundtripped = TransmissionCurve.readFits(filename) 

for i in range(10): 

point = self.makeRandomPoint(self.bboxCoadd) 

try: 

throughput1 = self.curveCoadd.sampleAt(point, wavelengths) 

except InvalidParameterError: 

self.assertRaises(InvalidParameterError, roundtripped.sampleAt, point, wavelengths) 

else: 

throughput2 = roundtripped.sampleAt(point, wavelengths) 

self.assertFloatsAlmostEqual(throughput1, throughput2, atol=1e-10) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()