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

217

218

219

220

221

222

223

224

# 

# LSST Data Management System 

# Copyright 2008, 2009, 2010 LSST Corporation. 

# 

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

# 

 

""" 

Tests for PCA on Images 

 

Run with: 

python imagePca.py 

or 

python 

>>> import imagePca; imagePca.run() 

""" 

 

import unittest 

import random 

import math 

import itertools 

 

import numpy as np 

 

import lsst.utils.tests 

import lsst.pex.exceptions as pexExcept 

import lsst.geom 

import lsst.afw.image as afwImage 

import lsst.afw.display.utils as displayUtils 

import lsst.afw.display.ds9 as ds9 

 

try: 

type(display) 

except NameError: 

display = False 

 

 

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

"""A test case for ImagePca""" 

 

def setUp(self): 

random.seed(0) 

self.ImageSet = afwImage.ImagePcaF() 

 

def tearDown(self): 

del self.ImageSet 

 

def testInnerProducts(self): 

"""Test inner products""" 

 

width, height = 10, 20 

im1 = afwImage.ImageF(lsst.geom.Extent2I(width, height)) 

val1 = 10 

im1.set(val1) 

 

im2 = im1.Factory(im1.getDimensions()) 

val2 = 20 

im2.set(val2) 

 

self.assertEqual(afwImage.innerProduct(im1, im1), 

width*height*val1*val1) 

self.assertEqual(afwImage.innerProduct(im1, im2), 

width*height*val1*val2) 

 

im2[0, 0, afwImage.LOCAL] = 0 

self.assertEqual(afwImage.innerProduct(im1, im2), 

(width*height - 1)*val1*val2) 

 

im2[0, 0, afwImage.LOCAL] = val2 # reinstate value 

im2[width - 1, height - 1, afwImage.LOCAL] = 1 

self.assertEqual(afwImage.innerProduct(im1, im2), 

(width*height - 1)*val1*val2 + val1) 

 

def testAddImages(self): 

"""Test adding images to a PCA set""" 

 

nImage = 3 

for i in range(nImage): 

im = afwImage.ImageF(lsst.geom.Extent2I(21, 21)) 

val = 1 

im.set(val) 

 

self.ImageSet.addImage(im, 1.0) 

 

vec = self.ImageSet.getImageList() 

self.assertEqual(len(vec), nImage) 

self.assertEqual(vec[nImage - 1][0, 0, afwImage.LOCAL], val) 

 

def tst(): 

"""Try adding an image with no flux""" 

self.ImageSet.addImage(im, 0.0) 

 

self.assertRaises(pexExcept.OutOfRangeError, tst) 

 

def testMean(self): 

"""Test calculating mean image""" 

 

width, height = 10, 20 

 

values = (100, 200, 300) 

meanVal = 0 

for val in values: 

im = afwImage.ImageF(lsst.geom.Extent2I(width, height)) 

im.set(val) 

 

self.ImageSet.addImage(im, 1.0) 

meanVal += val 

 

meanVal = meanVal/len(values) 

 

mean = self.ImageSet.getMean() 

 

self.assertEqual(mean.getWidth(), width) 

self.assertEqual(mean.getHeight(), height) 

self.assertEqual(mean[0, 0, afwImage.LOCAL], meanVal) 

self.assertEqual(mean[width - 1, height - 1, afwImage.LOCAL], meanVal) 

 

def testPca(self): 

"""Test calculating PCA""" 

width, height = 200, 100 

numBases = 3 

numInputs = 3 

 

bases = [] 

for i in range(numBases): 

im = afwImage.ImageF(width, height) 

array = im.getArray() 

x, y = np.indices(array.shape) 

period = 5*(i+1) 

fx = np.sin(2*math.pi/period*x + 2*math.pi/numBases*i) 

fy = np.sin(2*math.pi/period*y + 2*math.pi/numBases*i) 

array[x, y] = fx + fy 

bases.append(im) 

 

150 ↛ 151line 150 didn't jump to line 151, because the condition on line 150 was never true if display: 

mos = displayUtils.Mosaic(background=-10) 

ds9.mtv(mos.makeMosaic(bases), title="Basis functions", frame=1) 

 

inputs = [] 

for i in range(numInputs): 

im = afwImage.ImageF(lsst.geom.Extent2I(width, height)) 

im.set(0) 

for b in bases: 

im.scaledPlus(random.random(), b) 

 

inputs.append(im) 

self.ImageSet.addImage(im, 1.0) 

 

164 ↛ 165line 164 didn't jump to line 165, because the condition on line 164 was never true if display: 

mos = displayUtils.Mosaic(background=-10) 

ds9.mtv(mos.makeMosaic(inputs), title="Inputs", frame=2) 

 

self.ImageSet.analyze() 

 

eImages = [] 

for img in self.ImageSet.getEigenImages(): 

eImages.append(img) 

 

174 ↛ 175line 174 didn't jump to line 175, because the condition on line 174 was never true if display: 

mos = displayUtils.Mosaic(background=-10) 

ds9.mtv(mos.makeMosaic(eImages), title="Eigenimages", frame=3) 

 

self.assertEqual(len(eImages), numInputs) 

 

# Test for orthogonality 

for i1, i2 in itertools.combinations(list(range(len(eImages))), 2): 

inner = afwImage.innerProduct(eImages[i1], eImages[i2]) 

norm1 = eImages[i1].getArray().sum() 

norm2 = eImages[i2].getArray().sum() 

inner /= norm1*norm2 

self.assertAlmostEqual(inner, 0) 

 

def testPcaNaN(self): 

"""Test calculating PCA when the images can contain NaNs""" 

 

width, height = 20, 10 

 

values = (100, 200, 300) 

for i, val in enumerate(values): 

im = afwImage.ImageF(lsst.geom.Extent2I(width, height)) 

im.set(val) 

 

if i == 1: 

im[width//2, height//2, afwImage.LOCAL] = np.nan 

 

self.ImageSet.addImage(im, 1.0) 

 

self.ImageSet.analyze() 

 

eImages = [] 

for img in self.ImageSet.getEigenImages(): 

eImages.append(img) 

 

209 ↛ 210line 209 didn't jump to line 210, because the condition on line 209 was never true if display: 

mos = displayUtils.Mosaic(background=-10) 

ds9.mtv(mos.makeMosaic(eImages), frame=1) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()