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

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

# 

# LSST Data Management System 

# Copyright 2018 University of Washington 

# 

# 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 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 LSST License Statement and 

# the GNU General Public License along with this program. If not, 

# see <http://www.lsstcorp.org/LegalNotices/>. 

# 

 

import unittest 

import unittest.mock 

import numpy as np 

 

import lsst.utils.tests 

import lsst.afw.image 

import lsst.daf.persistence 

import lsst.geom 

import lsst.afw.geom 

from lsst.pipe.tasks import selectImages 

 

 

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

 

def setUp(self): 

self.config = selectImages.BestSeeingWcsSelectImageConfig() 

# define an avgFwhm that is in the config's FWHM range 

self.avgFwhm = np.mean([self.config.minPsfFwhm, self.config.maxPsfFwhm]) 

self.dataId = "visit=mock" 

 

def makeDataId(self, fwhm): 

"""Provide convenience function for dataId mangling. 

Parameters 

---------- 

fwhm: float 

FWHM in arcseconds of image to be mocked. 

""" 

return f'{self.dataId} fwhm={fwhm}' 

 

def localSetUp(self, mockFWHMs=None): 

"""Set up test inputs with mocked variable seeing images. 

 

Parameters 

---------- 

mockFWHMs : iterable or `None` 

FWHM in arcseconds of images to be mocked. 

""" 

if mockFWHMs is not None: 

self.mockFWHMs = mockFWHMs 

else: 

self.mockFWHMs = [] 

 

def mockDeterminantRadii(): 

return [f / np.sqrt(8.*np.log(2.)) for f in self.mockFWHMs] 

 

self.calexp = unittest.mock.Mock(spec=lsst.afw.image.ExposureD) 

self.calexp.getPsf.return_value.computeShape.return_value.getDeterminantRadius.side_effect \ 

= mockDeterminantRadii() 

self.dataRef = unittest.mock.Mock(spec=lsst.daf.persistence.ButlerDataRef, dataId=self.dataId) 

self.dataRef.get.return_value = self.calexp 

 

point1 = lsst.geom.SpherePoint(0, 0, lsst.geom.degrees) 

point2 = lsst.geom.SpherePoint(2, 0, lsst.geom.degrees) 

point3 = lsst.geom.SpherePoint(0, 2, lsst.geom.degrees) 

 

self.coordList = [point1, point2, point3] 

 

center = lsst.geom.SpherePoint(1, 1, lsst.geom.degrees) 

rotateAxis = lsst.geom.SpherePoint(0, 0, lsst.geom.degrees) 

rotateAngle = 0*lsst.geom.degrees 

dims = lsst.geom.Extent2I(3600, 3600) 

scale = 0.5*lsst.geom.arcseconds 

 

crpix = lsst.geom.Point2D(lsst.geom.Extent2D(dims)*0.5) 

center = center.rotated(rotateAxis, rotateAngle) 

cdMatrix = lsst.afw.geom.makeCdMatrix(scale=scale) 

wcs = lsst.afw.geom.makeSkyWcs(crpix=crpix, crval=center, cdMatrix=cdMatrix) 

self.bbox = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), lsst.geom.Point2I(dims[0], dims[1])) 

 

self.selectList = [] 

for fwhm in self.mockFWHMs: 

# encode the FWHM in the dataId 

dataRef = unittest.mock.Mock(spec=lsst.daf.persistence.ButlerDataRef, 

dataId=self.makeDataId(fwhm)) 

dataRef.get.return_value = self.calexp 

# also create a new attribute to store the fwhm since side_effect 

# can only be called once per value 

dataRef.fwhm = fwhm 

selectStruct = selectImages.SelectStruct(dataRef, wcs, self.bbox) 

self.selectList.append(selectStruct) 

 

def testConfigNImagesGTZero(self): 

"""Test that the config requests 0 or more images. 

""" 

self.assertGreater(self.config.nImagesMax, 0) 

 

def testConfigPsfFwhmRangeIsValid(self): 

"""Test that the minimum PSF FWHM is greater than zero 

and less than the maximum. 

""" 

self.assertGreaterEqual(self.config.minPsfFwhm, 0) 

self.assertGreater(self.config.maxPsfFwhm, self.config.minPsfFwhm) 

 

def testNoInputImages(self): 

"""Test that providing no images returns nothing. 

""" 

self.localSetUp(mockFWHMs=[]) 

task = selectImages.BestSeeingWcsSelectImagesTask(config=self.config) 

result = task.runDataRef(self.dataRef, self.coordList, selectDataList=self.selectList) 

self.assertEqual(len(result.exposureInfoList), 0) 

 

def testSelectOne(self): 

"""Test that configs requesting one image return one image. 

""" 

self.config.nImagesMax = 1 

self.localSetUp(mockFWHMs=[self.avgFwhm for i in range(self.config.nImagesMax)]) 

task = selectImages.BestSeeingWcsSelectImagesTask(config=self.config) 

result = task.runDataRef(self.dataRef, self.coordList, selectDataList=self.selectList) 

self.assertEqual(len(result.exposureInfoList), 1) 

self.assertEqual(result.exposureInfoList[0].dataId, self.makeDataId(self.avgFwhm)) 

self.assertEqual(result.dataRefList[0].fwhm, self.avgFwhm) 

 

def testSelectDefault(self): 

"""Test that the default configuration is self-consistent. 

""" 

self.localSetUp(mockFWHMs=[self.avgFwhm for i in range(self.config.nImagesMax)]) 

task = selectImages.BestSeeingWcsSelectImagesTask(config=self.config) 

result = task.runDataRef(self.dataRef, self.coordList, selectDataList=self.selectList) 

self.assertEqual(len(result.exposureInfoList), self.config.nImagesMax) 

for dataRef in result.dataRefList: 

self.assertEqual(dataRef.fwhm, self.avgFwhm) 

 

def testNImagesLessThanNMax(self): 

"""Test case of input selectDataList shorter than nImagesMax. 

""" 

self.config.nImagesMax = 5 

self.localSetUp(mockFWHMs=[self.avgFwhm for i in range(3)]) 

task = selectImages.BestSeeingWcsSelectImagesTask(config=self.config) 

result = task.runDataRef(self.dataRef, self.coordList, selectDataList=self.selectList) 

self.assertEqual(len(result.exposureInfoList), 3) 

for dataRef in result.dataRefList: 

self.assertEqual(dataRef.fwhm, self.avgFwhm) 

 

def testNImagesGreaterThanNMax(self): 

"""Test case of input selectDataList greater than nImagesMax. 

""" 

self.localSetUp(mockFWHMs=[self.avgFwhm for i in range(self.config.nImagesMax+3)]) 

task = selectImages.BestSeeingWcsSelectImagesTask(config=self.config) 

result = task.runDataRef(self.dataRef, self.coordList, selectDataList=self.selectList) 

self.assertEqual(len(result.exposureInfoList), self.config.nImagesMax) 

for dataRef in result.dataRefList: 

self.assertEqual(dataRef.fwhm, self.avgFwhm) 

 

def testFwhmSelection(self): 

"""Test that minimum and maximum FWHM selections work. 

""" 

self.config.minPsfFwhm = 1.0 

self.config.maxPsfFwhm = 1.5 

self.config.nImagesMax = 10 

mockFWHMs = [0.5, 1.0, 1.25, 1.5, 2.0] 

self.localSetUp(mockFWHMs=mockFWHMs) 

task = selectImages.BestSeeingWcsSelectImagesTask(config=self.config) 

result = task.runDataRef(self.dataRef, self.coordList, selectDataList=self.selectList) 

self.assertEqual(len(result.exposureInfoList), 3) 

for i, fwhm in enumerate([1.0, 1.25, 1.5]): 

self.assertEqual(result.exposureInfoList[i].dataId, self.makeDataId(fwhm)) 

self.assertEqual(result.dataRefList[i].fwhm, fwhm) 

 

def testBestSeeingFwhmSelection(self): 

"""Test that the selection picks the best seeing images. 

""" 

self.config.minPsfFwhm = 1.0 

self.config.maxPsfFwhm = 1.5 

self.config.nImagesMax = 3 

mockFWHMs = [0.5, 1.0, 1.1, 1.2, 1.25, 1.3, 1.4, 1.5, 2.0] 

self.localSetUp(mockFWHMs=mockFWHMs) 

task = selectImages.BestSeeingWcsSelectImagesTask(config=self.config) 

result = task.runDataRef(self.dataRef, self.coordList, selectDataList=self.selectList) 

self.assertEqual(len(result.exposureInfoList), self.config.nImagesMax) 

for i, fwhm in enumerate([1.0, 1.1, 1.2]): 

self.assertEqual(result.exposureInfoList[i].dataId, self.makeDataId(fwhm)) 

self.assertEqual(result.dataRefList[i].fwhm, fwhm) 

 

def testSelectDefaultMakeDataRefListIsFalse(self): 

"""Test appropriate behavior for makeDataRefList = False. 

""" 

self.localSetUp(mockFWHMs=[self.avgFwhm for i in range(self.config.nImagesMax)]) 

task = selectImages.BestSeeingWcsSelectImagesTask(config=self.config) 

result = task.runDataRef(self.dataRef, self.coordList, 

selectDataList=self.selectList, makeDataRefList=False) 

self.assertIsNone(result.dataRefList) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()