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

# 

# LSST Data Management System 

# 

# Copyright 2008-2017 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 

 

import numpy as np 

 

import lsst.utils.tests 

import lsst.geom 

import lsst.afw.geom.ellipses as el 

import lsst.afw.image 

from lsst.meas.extensions.simpleShape import SimpleShape 

 

 

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

 

def setUp(self): 

self.ellipseCores = [ 

el.Quadrupole(25.0, 25.0, 0.0), 

el.Quadrupole(27.0, 22.0, -5.0), 

el.Quadrupole(23.0, 28.0, 2.0), 

] 

self.centers = [ 

lsst.geom.Point2D(0.0, 0.0), 

lsst.geom.Point2D(2.0, 3.0), 

lsst.geom.Point2D(-1.0, 2.5), 

] 

for ellipseCore in self.ellipseCores: 

ellipseCore.scale(2) 

self.bbox = lsst.geom.Box2I(lsst.geom.Point2I(-500, -500), lsst.geom.Point2I(50, 50)) 

self.xg, self.yg = np.meshgrid( 

np.arange(self.bbox.getBeginX(), self.bbox.getEndX(), dtype=float), 

np.arange(self.bbox.getBeginY(), self.bbox.getEndY(), dtype=float) 

) 

 

def evaluateGaussian(self, ellipse): 

''' 

Create an elliptical Guassian as a Numpy array. Does not normalize the Gaussian 

''' 

gt = ellipse.getGridTransform() 

xt = gt[gt.XX] * self.xg + gt[gt.XY] * self.yg + gt[gt.X] 

yt = gt[gt.YX] * self.xg + gt[gt.YY] * self.yg + gt[gt.Y] 

return np.exp(-0.5 * (xt**2 + yt**2)) 

 

def buildImageAndMoments(self, dEllipseCore, dCenter, wEllipseCore, wCenter): 

''' 

Generates a elliptical Gaussian image from input ellipse (dEllipse) 

and uses an elliptical Gaussian (wEllipse) to calculate the shape 

of the generated image. 

''' 

dEllipse = el.Ellipse(dEllipseCore, dCenter) 

image = lsst.afw.image.MaskedImageF(self.bbox) 

image.getImage().getArray()[:, :] = self.evaluateGaussian(dEllipse) 

wEllipse = el.Ellipse(wEllipseCore, wCenter) 

result = SimpleShape.computeMoments(wEllipse, image) 

return result 

 

def testCorrectWeightedMoments(self): 

''' 

Test that the measured moments can be corrected for the fact that the measurement 

contains information on the moments of the weight function used to make the 

measurement. The results should only contain the objects shape and not the moments 

used to make the measurement. This is a subset of the functionality in testNoNoiseGaussians. 

Because the other test tests a broader scope, it is useful to have this test happen under 

more restrictive conditions. 

''' 

for dEllipseCore in self.ellipseCores: 

for dCenter in self.centers: 

dEllipse = el.Ellipse(dEllipseCore, dCenter) 

dArray = self.evaluateGaussian(dEllipse) 

for wEllipseCore in self.ellipseCores: 

for wCenter in self.centers: 

wEllipse = el.Ellipse(wEllipseCore, wCenter) 

wArray = self.evaluateGaussian(wEllipse) 

product = dArray * wArray 

i0 = np.sum(product) 

ix = np.sum(product * self.xg) / i0 

iy = np.sum(product * self.yg) / i0 

ixx = np.sum(product * (self.xg - ix)**2) / i0 

iyy = np.sum(product * (self.yg - iy)**2) / i0 

ixy = np.sum(product * (self.xg - ix) * (self.yg - iy)) / i0 

mEllipseCore = el.Quadrupole(ixx, iyy, ixy) 

mCenter = lsst.geom.Point2D(ix, iy) 

SimpleShape.correctWeightedMoments(wEllipseCore, mEllipseCore, mCenter) 

self.assertFloatsAlmostEqual(mEllipseCore.getParameterVector(), 

dEllipseCore.getParameterVector(), 

rtol=1E-8, atol=1E-11) 

 

def testNoNoiseGaussians(self): 

''' 

Test that shape moments can be actuately determined for Gaussian images with no noise 

''' 

for ellipseCore in self.ellipseCores: 

for center in self.centers: 

result = self.buildImageAndMoments(ellipseCore, center, ellipseCore, center) 

self.assertFloatsAlmostEqual(result.ellipse.getParameterVector(), 

ellipseCore.getParameterVector(), 

rtol=3E-3, atol=1E-15) 

self.assertFloatsAlmostEqual(np.array(result.center), 

np.array(center), 

rtol=1E-8, atol=1E-15) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()