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

# 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 statisticsMasked 

 

Run with: 

python test_statisticsMasked.py 

or 

pytest test_statisticsMasked.py 

""" 

 

import unittest 

 

import numpy as np 

 

import lsst.utils.tests 

import lsst.geom 

import lsst.afw.image as afwImage 

import lsst.afw.math as afwMath 

 

 

class StatisticsTestCase(unittest.TestCase): 

 

"""A test case to check that special values (NaN and Masks) are begin handled in Statistics""" 

 

def setUp(self): 

self.valL, self.valR = 10, 20 

self.nRow, self.nCol = 100, 200 

self.n = self.nRow*self.nCol 

 

self.bboxL = lsst.geom.Box2I(lsst.geom.Point2I(0, 0), 

lsst.geom.Point2I(self.nRow//2 - 1, self.nCol - 1)) 

self.bboxR = lsst.geom.Box2I(lsst.geom.Point2I(self.nRow//2, 0), 

lsst.geom.Point2I(self.nRow - 1, self.nCol - 1)) 

 

# create masked images and set the left side to valL, and right to valR 

self.mimg = afwImage.MaskedImageF( 

lsst.geom.Extent2I(self.nRow, self.nCol)) 

self.mimg.set(0.0, 0x0, 0.0) 

self.mimgL = afwImage.MaskedImageF( 

self.mimg, self.bboxL, afwImage.LOCAL) 

self.mimgL.set(self.valL, 0x0, self.valL) 

self.mimgR = afwImage.MaskedImageF( 

self.mimg, self.bboxR, afwImage.LOCAL) 

self.mimgR.set(self.valR, 0x0, self.valR) 

 

def tearDown(self): 

del self.mimg 

del self.mimgL 

del self.mimgR 

 

# Verify that NaN values are being ignored 

# (by default, StatisticsControl.useNanSafe = True) 

# We'll set the L and R sides of an image to two different values and verify mean and stdev 

# ... then set R-side to NaN and try again ... we should get mean,stdev for L-side 

def testNaN(self): 

 

# get the stats for the image with two values 

stats = afwMath.makeStatistics( 

self.mimg, afwMath.NPOINT | afwMath.MEAN | afwMath.STDEV) 

mean = 0.5*(self.valL + self.valR) 

nL, nR = self.mimgL.getWidth()*self.mimgL.getHeight(), self.mimgR.getWidth() * \ 

self.mimgR.getHeight() 

stdev = ((nL*(self.valL - mean)**2 + nR * 

(self.valR - mean)**2)/(nL + nR - 1))**0.5 

 

self.assertEqual(stats.getValue(afwMath.NPOINT), self.n) 

self.assertEqual(stats.getValue(afwMath.MEAN), mean) 

self.assertEqual(stats.getValue(afwMath.STDEV), stdev) 

 

# set the right side to NaN and stats should be just for the left side 

self.mimgR.set(np.nan, 0x0, self.valR) 

 

statsNaN = afwMath.makeStatistics( 

self.mimg, afwMath.NPOINT | afwMath.MEAN | afwMath.STDEV) 

mean = self.valL 

stdev = 0.0 

self.assertEqual(statsNaN.getValue(afwMath.NPOINT), nL) 

self.assertEqual(statsNaN.getValue(afwMath.MEAN), mean) 

self.assertEqual(statsNaN.getValue(afwMath.STDEV), stdev) 

 

# Verify that Masked pixels are being ignored according to the andMask 

# (by default, StatisticsControl.andMask = 0x0) 

# We'll set the L and R sides of an image to two different values and verify mean and stdev 

# ... then set R-side Mask and the andMask to 0x1 and try again ... we should get mean,stdev for L-side 

def testMasked(self): 

 

# get the stats for the image with two values 

self.mimgR.set(self.valR, 0x0, self.valR) 

stats = afwMath.makeStatistics( 

self.mimg, afwMath.NPOINT | afwMath.MEAN | afwMath.STDEV) 

mean = 0.5*(self.valL + self.valR) 

nL, nR = self.mimgL.getWidth()*self.mimgL.getHeight(), self.mimgR.getWidth() * \ 

self.mimgR.getHeight() 

stdev = ((nL*(self.valL - mean)**2 + nR * 

(self.valR - mean)**2)/(nL + nR - 1))**0.5 

 

self.assertEqual(stats.getValue(afwMath.NPOINT), self.n) 

self.assertEqual(stats.getValue(afwMath.MEAN), mean) 

self.assertEqual(stats.getValue(afwMath.STDEV), stdev) 

 

# set the right side Mask and the StatisticsControl andMask to 0x1 

# Stats should be just for the left side! 

maskBit = 0x1 

self.mimgR.getMask().set(maskBit) 

 

sctrl = afwMath.StatisticsControl() 

sctrl.setAndMask(maskBit) 

statsNaN = afwMath.makeStatistics( 

self.mimg, afwMath.NPOINT | afwMath.MEAN | afwMath.STDEV, sctrl) 

 

mean = self.valL 

stdev = 0.0 

 

self.assertEqual(statsNaN.getValue(afwMath.NPOINT), nL) 

self.assertEqual(statsNaN.getValue(afwMath.MEAN), mean) 

self.assertEqual(statsNaN.getValue(afwMath.STDEV), stdev) 

 

# Verify that pixels are being weighted according to the variance plane (1/var) 

# We'll set the L and R sides of an image to two different values and verify mean 

# ... then set R-side Variance to equal the Image value, and set 'weighted' and try again ... 

def testWeighted(self): 

 

self.mimgR.set(self.valR, 0x0, self.valR) 

sctrl = afwMath.StatisticsControl() 

sctrl.setWeighted(True) 

stats = afwMath.makeStatistics( 

self.mimg, afwMath.NPOINT | afwMath.MEAN | afwMath.STDEV, sctrl) 

nL, nR = self.mimgL.getWidth()*self.mimgL.getHeight(), self.mimgR.getWidth() * \ 

self.mimgR.getHeight() 

 

mean = 1.0*(nL + nR)/(nL/self.valL + nR/self.valR) 

 

# get the stats for the image with two values 

self.assertEqual(stats.getValue(afwMath.NPOINT), self.n) 

self.assertAlmostEqual(stats.getValue(afwMath.MEAN), mean, 10) 

 

def testWeightedSimple(self): 

mimg = afwImage.MaskedImageF(lsst.geom.Extent2I(1, 2)) 

mimg[0, 0, afwImage.LOCAL] = (self.valR, 0x0, self.valR) 

mimg[0, 1, afwImage.LOCAL] = (self.valL, 0x0, self.valL) 

 

sctrl = afwMath.StatisticsControl() 

sctrl.setWeighted(True) 

stats = afwMath.makeStatistics( 

mimg, afwMath.NPOINT | afwMath.MEAN | afwMath.STDEV, sctrl) 

vsum = 2.0 

vsum2 = self.valR + self.valL 

wsum = 1.0/self.valR + 1.0/self.valL 

wwsum = 1.0/self.valR**2 + 1.0/self.valL**2 

mean = vsum/wsum 

variance = vsum2/wsum - mean**2 # biased variance 

 

n = 2 

# original estimate; just a rewrite of the usual n/(n - 1) correction 

stddev = (1.0*(vsum2)/(wsum*(1.0-1.0/n)) - 

(vsum**2)/(wsum**2*(1.0-1.0/n)))**0.5 

self.assertAlmostEqual(stddev, np.sqrt(variance*n/(n - 1))) 

# 

# The correct formula: 

stddev = np.sqrt(variance*wsum**2/(wsum**2 - wwsum)) 

 

# get the stats for the image with two values 

self.assertAlmostEqual(stats.getValue(afwMath.MEAN), mean, 10) 

self.assertAlmostEqual(stats.getValue(afwMath.STDEV), stddev, 10) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()