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

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

 

""" 

Tests for detection.GaussianPsf 

 

Run with: 

python test_gaussianPsf.py 

or 

pytest test_gaussianPsf.py 

""" 

 

import unittest 

 

import numpy as np 

 

import lsst.utils.tests 

import lsst.pex.exceptions 

import lsst.geom 

import lsst.afw.table 

import lsst.afw.fits 

import lsst.afw.detection 

 

 

def makeGaussianImage(bbox, sigma, xc=0.0, yc=0.0): 

image = lsst.afw.image.ImageD(bbox) 

array = image.getArray() 

for yi, yv in enumerate(range(bbox.getBeginY(), bbox.getEndY())): 

for xi, xv in enumerate(range(bbox.getBeginX(), bbox.getEndX())): 

array[yi, xi] = np.exp(-0.5*((xv - xc)**2 + (yv - yc)**2)/sigma**2) 

array /= array.sum() 

return image 

 

 

def computeNaiveApertureFlux(image, radius, xc=0.0, yc=0.0): 

bbox = image.getBBox() 

array = image.getArray() 

s = 0.0 

for yi, yv in enumerate(range(bbox.getBeginY(), bbox.getEndY())): 

for xi, xv in enumerate(range(bbox.getBeginX(), bbox.getEndX())): 

if (xv - xc)**2 + (yv - yc)**2 < radius**2: 

s += array[yi, xi] 

return s 

 

 

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

 

def setUp(self): 

self.kernelSize = 51 

self.psf = lsst.afw.detection.GaussianPsf( 

self.kernelSize, self.kernelSize, 4.0) 

 

def tearDown(self): 

del self.psf 

 

def testKernelImage(self): 

image = self.psf.computeKernelImage() 

check = makeGaussianImage(image.getBBox(), self.psf.getSigma()) 

self.assertFloatsAlmostEqual(image.getArray(), check.getArray()) 

self.assertFloatsAlmostEqual(image.getArray().sum(), 1.0, atol=1E-14) 

 

def testOffsetImage(self): 

image = self.psf.computeImage(lsst.geom.Point2D(0.25, 0.25)) 

check = makeGaussianImage( 

image.getBBox(), self.psf.getSigma(), 0.25, 0.25) 

self.assertFloatsAlmostEqual(image.getArray(), check.getArray(), atol=1E-4, rtol=1E-4, 

plotOnFailure=True) 

 

def testApertureFlux(self): 

image = self.psf.computeKernelImage(lsst.geom.Point2D(0.0, 0.0)) 

# test aperture implementation is very crude; can only test to about 10% 

self.assertFloatsAlmostEqual(self.psf.computeApertureFlux(5.0), computeNaiveApertureFlux(image, 5.0), 

rtol=0.1) 

self.assertFloatsAlmostEqual(self.psf.computeApertureFlux(7.0), computeNaiveApertureFlux(image, 7.0), 

rtol=0.1) 

 

def testShape(self): 

self.assertFloatsAlmostEqual( 

self.psf.computeShape().getDeterminantRadius(), 4.0) 

 

def testPersistence(self): 

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

self.psf.writeFits(filename) 

psf = lsst.afw.detection.GaussianPsf.readFits(filename) 

self.assertEqual(self.psf.getSigma(), psf.getSigma()) 

self.assertEqual(self.psf.getDimensions(), psf.getDimensions()) 

 

def testBBox(self): 

 

self.assertEqual(self.psf.computeKernelImage().getBBox(), 

self.psf.computeBBox()) 

 

self.assertEqual(self.psf.computeBBox().getWidth(), self.kernelSize) 

 

# Test interface. GaussianPsf does not vary spatially 

self.assertEqual(self.psf.computeKernelImage(lsst.geom.Point2D(0.0, 0.0)).getBBox(), 

self.psf.computeBBox(lsst.geom.Point2D(0.0, 0.0))) 

 

def testResized(self): 

for pad in [0, -10, 10]: 

newLen = self.kernelSize - pad 

resizedPsf = self.psf.resized(newLen, newLen) 

newBBox = resizedPsf.computeBBox() 

self.assertEqual(newBBox.getWidth(), newLen) 

self.assertEqual(newBBox.getHeight(), newLen) 

self.assertEqual(resizedPsf.getSigma(), self.psf.getSigma()) 

 

image = resizedPsf.computeKernelImage() 

check = makeGaussianImage(newBBox, self.psf.getSigma()) 

self.assertFloatsAlmostEqual(image.getArray(), check.getArray()) 

# tolerance same as in self.testKernelImage 

self.assertFloatsAlmostEqual(image.getArray().sum(), 1.0, atol=1E-14) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()