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

# 

# LSST Data Management System 

# 

# Copyright 2008-2016 AURA/LSST. 

# 

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

# 

import unittest 

 

from lsst.meas.algorithms import GaussianPsfFactory, SigmaPerFwhm, SingleGaussianPsf, DoubleGaussianPsf 

from lsst.pex.config import Config, FieldValidationError 

import lsst.utils.tests 

 

 

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

 

def testApply(self): 

"""Test apply and computeSizeAndSigma methods.""" 

factory = GaussianPsfFactory() 

for fixedSize in (None, 5, 6): 

factory.size = fixedSize 

for fwhm in (None, 1, 5, 10): 

if fwhm is None: 

predFwhm = factory.defaultFwhm 

else: 

predFwhm = fwhm 

for sizeFactor in (1, 3, 5): 

factory.sizeFactor = sizeFactor 

if fixedSize is None: 

# predicted size without applying limits = fwhm * sizeFactor, increased to be odd 

naiveSize = int(0.5 + (predFwhm * sizeFactor)) 

if naiveSize % 2 == 0: 

naiveSize += 1 

else: 

naiveSize = fixedSize 

for minSize in (max(1, naiveSize-1), naiveSize, naiveSize + 2): 

52 ↛ 53line 52 didn't jump to line 53, because the condition on line 52 was never true if minSize <= 0: 

continue 

factory.minSize = minSize 

for maxSize in (naiveSize-1, naiveSize): 

if maxSize <= 0: 

continue 

if maxSize < minSize: 

continue 

factory.maxSize = maxSize 

for addWing in (False, True): 

factory.addWing = addWing 

size, sigma = factory.computeSizeAndSigma(predFwhm) 

self.assertAlmostEqual(sigma, SigmaPerFwhm * predFwhm) 

 

if fixedSize is not None: 

self.assertEqual(fixedSize, size) 

else: 

69 ↛ 71line 69 didn't jump to line 71, because the condition on line 69 was never false if minSize is not None: 

self.assertLessEqual(minSize, size) 

71 ↛ 73line 71 didn't jump to line 73, because the condition on line 71 was never false if maxSize is not None: 

self.assertGreaterEqual(maxSize, size) 

73 ↛ 74line 73 didn't jump to line 74, because the condition on line 73 was never true if None not in (minSize, maxSize) and minSize < size < maxSize: 

self.assertEqual(naiveSize, size) 

self.assertEqual(size % 2, 1) 

 

psfModel = factory.apply(fwhm) 

if addWing: 

self.assertIsInstance(psfModel, DoubleGaussianPsf) 

self.assertAlmostEqual(psfModel.getSigma1(), sigma) 

self.assertAlmostEqual( 

psfModel.getSigma2(), sigma * factory.wingFwhmFactor) 

self.assertAlmostEqual(psfModel.getB(), factory.wingAmplitude) 

else: 

self.assertIsInstance(psfModel, SingleGaussianPsf) 

self.assertAlmostEqual(psfModel.getSigma(), sigma) 

 

psfKernel = psfModel.getKernel() 

self.assertEqual(size, psfKernel.getHeight()) 

self.assertEqual(size, psfKernel.getWidth()) 

 

def testValidate(self): 

"""Test the validate method and field-by-field validation.""" 

# test field-by-field validation 

for fieldName in ( 

"size", 

"sizeFactor", 

"minSize", 

"maxSize", 

"defaultFwhm", 

"wingFwhmFactor", 

"wingAmplitude", 

): 

for value in (-1, 0): 

factory = GaussianPsfFactory() 

with self.assertRaises(FieldValidationError): 

setattr(factory, fieldName, value) 

 

# test the validate method 

for fieldName in ("sizeFactor", "defaultFwhm", "addWing", "wingFwhmFactor", "wingAmplitude"): 

factory = GaussianPsfFactory() 

setattr(factory, fieldName, None) 

with self.assertRaises(Exception): 

factory.validate() 

 

for minSize in (None, 5, 9): 

for maxSize in (None, 3, 7): 

factory = GaussianPsfFactory() 

factory.minSize = minSize 

factory.maxSize = maxSize 

if None not in (minSize, maxSize) and maxSize < minSize: 

with self.assertRaises(Exception): 

factory.validate() 

else: 

factory.validate() # should not raise 

 

def testMakeField(self): 

"""Test the makeField method.""" 

for addWing in (False, True): 

psfConfig = PsfConfig() 

psfConfig.psfModel.defaultFwhm = 2.7 

psfConfig.psfModel.sizeFactor = 3.0 

psfConfig.psfModel.minSize = 1 

psfConfig.psfModel.maxSize = None 

psfConfig.psfModel.addWing = addWing 

psfModel = psfConfig.psfModel.apply() 

if addWing: 

self.assertIsInstance(psfModel, DoubleGaussianPsf) 

self.assertAlmostEqual(psfModel.getSigma1(), SigmaPerFwhm * psfConfig.psfModel.defaultFwhm) 

else: 

self.assertIsInstance(psfModel, SingleGaussianPsf) 

self.assertAlmostEqual(psfModel.getSigma(), SigmaPerFwhm * psfConfig.psfModel.defaultFwhm) 

 

for fwhm in (None, 2.5): 

predFwhm = psfConfig.psfModel.defaultFwhm if fwhm is None else fwhm 

psfModel = psfConfig.psfModel.apply(fwhm=fwhm) 

if addWing: 

self.assertIsInstance(psfModel, DoubleGaussianPsf) 

self.assertAlmostEqual(psfModel.getSigma1(), SigmaPerFwhm * predFwhm) 

else: 

self.assertIsInstance(psfModel, SingleGaussianPsf) 

self.assertAlmostEqual(psfModel.getSigma(), SigmaPerFwhm * predFwhm) 

 

 

class PsfConfig(Config): 

psfModel = GaussianPsfFactory.makeField("PSF model factory") 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()