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

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

# 

# LSST Data Management System 

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

# 

 

import unittest 

 

import numpy as np 

 

import lsst.utils.tests 

import lsst.afw.geom.ellipses 

import lsst.shapelet.tests 

import lsst.afw.image 

 

 

class MatrixBuilderTestCase(lsst.shapelet.tests.ShapeletTestCase): 

 

def setUp(self): 

np.random.seed(500) 

self.xD = np.random.randn(50) 

self.yD = np.random.randn(50) 

self.xF = self.xD.astype(np.float32) 

self.yF = self.yD.astype(np.float32) 

 

def checkAccessors(self, obj, basisSize): 

self.assertEqual(obj.getDataSize(), self.xD.size) 

self.assertEqual(obj.getBasisSize(), basisSize) 

 

def testShapeletMatrixBuilder(self): 

function = self.makeRandomShapeletFunction(order=4) 

size = function.getCoefficients().size 

function.getCoefficients()[:] = np.random.randn(size) 

basis = lsst.shapelet.MultiShapeletBasis(size) 

basis.addComponent(1.0, function.getOrder(), np.identity(size)) 

factoryF = lsst.shapelet.MatrixBuilderF.Factory(self.xF, self.yF, function.getOrder()) 

factoryD = lsst.shapelet.MatrixBuilderD.Factory(self.xD, self.yD, function.getOrder()) 

# we should get the same results with an appropriately simple MultiShapeletBasis 

compoundFactoryF = lsst.shapelet.MatrixBuilderF.Factory(self.xF, self.yF, basis) 

compoundFactoryD = lsst.shapelet.MatrixBuilderD.Factory(self.xD, self.yD, basis) 

self.checkAccessors(factoryF, size) 

self.checkAccessors(factoryD, size) 

self.checkAccessors(compoundFactoryF, size) 

self.checkAccessors(compoundFactoryD, size) 

builder1F = factoryF() 

builder1D = factoryD() 

self.checkAccessors(builder1F, size) 

self.checkAccessors(builder1D, size) 

workspaceF = lsst.shapelet.MatrixBuilderF.Workspace(factoryF.computeWorkspace()) 

workspaceD = lsst.shapelet.MatrixBuilderD.Workspace(factoryD.computeWorkspace()) 

builder2F = factoryF(workspaceF) 

builder2D = factoryD(workspaceD) 

self.assertEqual(workspaceF.getRemaining(), 0) 

self.assertEqual(workspaceD.getRemaining(), 0) 

self.checkAccessors(builder2F, size) 

self.checkAccessors(builder2D, size) 

builder3F = compoundFactoryF() 

builder3D = compoundFactoryD() 

self.checkAccessors(builder3F, size) 

self.checkAccessors(builder3D, size) 

matrix1F = builder1F(function.getEllipse()) 

matrix1D = builder1D(function.getEllipse()) 

matrix2F = builder2F(function.getEllipse()) 

matrix2D = builder2D(function.getEllipse()) 

matrix3F = builder3F(function.getEllipse()) 

matrix3D = builder3D(function.getEllipse()) 

# same code, different workspace 

self.assertFloatsAlmostEqual(matrix1D, matrix2D, rtol=0.0, atol=0.0) 

# same code, different workspace 

self.assertFloatsAlmostEqual(matrix1F, matrix2F, rtol=0.0, atol=0.0) 

# same code, different construction pattern 

self.assertFloatsAlmostEqual(matrix1F, matrix3F, rtol=0.0, atol=0.0) 

# same code, different construction pattern 

self.assertFloatsAlmostEqual(matrix1D, matrix3D, rtol=0.0, atol=0.0) 

# same code, different construction pattern 

self.assertFloatsAlmostEqual(matrix1F, matrix2F, rtol=1E-7, atol=0.0) 

# Finally, check against a completely different implementation (which is tested elsewhere) 

checkEvaluator = function.evaluate() 

checkVector = checkEvaluator(self.xD, self.yD) 

self.assertFloatsAlmostEqual(np.dot(matrix1D, function.getCoefficients()), 

checkVector, rtol=1E-13, atol=1E-14) 

 

def testRemappedShapeletMatrixBuilder(self): 

function = self.makeRandomShapeletFunction(order=4) 

size = 6 

radius = 3.2 

remapMatrix = np.random.randn(function.getCoefficients().size, size) 

coefficients = np.random.randn(size) 

function.getCoefficients()[:] = np.dot(remapMatrix, coefficients) 

basis = lsst.shapelet.MultiShapeletBasis(size) 

basis.addComponent(radius, function.getOrder(), remapMatrix) 

factoryF = lsst.shapelet.MatrixBuilderF.Factory(self.xF, self.yF, basis) 

factoryD = lsst.shapelet.MatrixBuilderD.Factory(self.xD, self.yD, basis) 

self.checkAccessors(factoryF, size) 

self.checkAccessors(factoryD, size) 

builder1F = factoryF() 

builder1D = factoryD() 

self.checkAccessors(builder1F, size) 

self.checkAccessors(builder1D, size) 

workspaceF = lsst.shapelet.MatrixBuilderF.Workspace(factoryF.computeWorkspace()) 

workspaceD = lsst.shapelet.MatrixBuilderD.Workspace(factoryD.computeWorkspace()) 

builder2F = factoryF(workspaceF) 

builder2D = factoryD(workspaceD) 

self.assertEqual(workspaceF.getRemaining(), 0) 

self.assertEqual(workspaceD.getRemaining(), 0) 

self.checkAccessors(builder2F, size) 

self.checkAccessors(builder2D, size) 

matrix1F = builder1F(function.getEllipse()) 

matrix1D = builder1D(function.getEllipse()) 

matrix2F = builder2F(function.getEllipse()) 

matrix2D = builder2D(function.getEllipse()) 

# same code, different workspace 

self.assertFloatsAlmostEqual(matrix1D, matrix2D, rtol=0.0, atol=0.0) 

# same code, different workspace 

self.assertFloatsAlmostEqual(matrix1F, matrix2F, rtol=0.0, atol=0.0) 

# same code, different precision 

self.assertFloatsAlmostEqual(matrix1F, matrix2F, rtol=1E-7, atol=0.0) 

# Finally, check against a completely different implementation (which is tested elsewhere) 

function.getEllipse().scale(radius) 

checkEvaluator = function.evaluate() 

checkVector = checkEvaluator(self.xD, self.yD) 

self.assertFloatsAlmostEqual(np.dot(matrix1D, coefficients), checkVector, rtol=1E-13, atol=1E-14) 

 

def testConvolvedShapeletMatrixBuilder(self): 

function = self.makeRandomShapeletFunction(order=4) 

psf = self.makeRandomMultiShapeletFunction(nComponents=1) 

size = function.getCoefficients().size 

function.getCoefficients()[:] = np.random.randn(size) 

basis = lsst.shapelet.MultiShapeletBasis(size) 

basis.addComponent(1.0, function.getOrder(), np.identity(size)) 

factoriesF = [lsst.shapelet.MatrixBuilderF.Factory(self.xF, self.yF, function.getOrder(), 

psf.getComponents()[0]), 

lsst.shapelet.MatrixBuilderF.Factory(self.xF, self.yF, basis, psf), 

] 

factoriesD = [lsst.shapelet.MatrixBuilderD.Factory(self.xD, self.yD, function.getOrder(), 

psf.getComponents()[0]), 

lsst.shapelet.MatrixBuilderD.Factory(self.xD, self.yD, basis, psf), 

] 

lastF = None 

for factory in factoriesF: 

self.checkAccessors(factory, size) 

workspace = lsst.shapelet.MatrixBuilderF.Workspace(factory.computeWorkspace()) 

builder1 = factory() 

builder2 = factory(workspace) 

self.checkAccessors(builder1, size) 

self.checkAccessors(builder2, size) 

self.assertEqual(workspace.getRemaining(), 0) 

matrix1 = builder1(function.getEllipse()) 

matrix2 = builder2(function.getEllipse()) 

# same code, different workspace 

self.assertFloatsAlmostEqual(matrix1, matrix2, rtol=0.0, atol=0.0) 

if lastF is not None: 

# same code, different construction 

self.assertFloatsAlmostEqual(matrix1, lastF, rtol=0.0, atol=0.0) 

lastF = matrix1 

lastD = None 

for factory in factoriesD: 

self.checkAccessors(factory, size) 

workspace = lsst.shapelet.MatrixBuilderD.Workspace(factory.computeWorkspace()) 

builder1 = factory() 

builder2 = factory(workspace) 

self.checkAccessors(builder1, size) 

self.checkAccessors(builder2, size) 

self.assertEqual(workspace.getRemaining(), 0) 

matrix1 = builder1(function.getEllipse()) 

matrix2 = builder2(function.getEllipse()) 

# same code, different workspace 

self.assertFloatsAlmostEqual(matrix1, matrix2, rtol=0.0, atol=0.0) 

if lastD is not None: 

# same code, different construction 

self.assertFloatsAlmostEqual(matrix1, lastD, rtol=0.0, atol=0.0) 

lastD = matrix1 

# same code, different precision 

self.assertFloatsAlmostEqual(lastF, lastD, rtol=1E-6, atol=1E-5) 

 

# Finally, check against a completely different implementation (which is tested elsewhere) 

convolved = function.convolve(psf.getComponents()[0]) 

checkEvaluator = convolved.evaluate() 

checkVector = checkEvaluator(self.xD, self.yD) 

self.assertFloatsAlmostEqual(np.dot(lastD, function.getCoefficients()), checkVector, rtol=1E-13) 

 

def testRemappedConvolvedShapeletMatrixBuilder(self): 

function = self.makeRandomShapeletFunction(order=4) 

psf = self.makeRandomMultiShapeletFunction(nComponents=1) 

size = 6 

radius = 3.2 

remapMatrix = np.random.randn(function.getCoefficients().size, size) 

coefficients = np.random.randn(size) 

function.getCoefficients()[:] = np.dot(remapMatrix, coefficients) 

basis = lsst.shapelet.MultiShapeletBasis(size) 

basis.addComponent(radius, function.getOrder(), remapMatrix) 

factoryF = lsst.shapelet.MatrixBuilderF.Factory(self.xF, self.yF, basis, psf) 

factoryD = lsst.shapelet.MatrixBuilderD.Factory(self.xD, self.yD, basis, psf) 

matrixF = None 

for factory in (factoryF, factoryD): 

self.checkAccessors(factory, size) 

workspace = factory.Workspace(factory.computeWorkspace()) 

builder1 = factory() 

builder2 = factory(workspace) 

self.checkAccessors(builder1, size) 

self.checkAccessors(builder2, size) 

self.assertEqual(workspace.getRemaining(), 0) 

matrix1 = builder1(function.getEllipse()) 

matrix2 = builder2(function.getEllipse()) 

# same code, different workspace 

self.assertFloatsAlmostEqual(matrix1, matrix2, rtol=0.0, atol=0.0) 

if matrixF is None: 

matrixF = matrix1 

matrixD = matrix1 

# same code, different precision 

self.assertFloatsAlmostEqual(matrixF, matrixD, rtol=1E-4, atol=0.0) 

 

# Finally, check against a completely different implementation (which is tested elsewhere) 

function.getEllipse().scale(radius) 

convolved = function.convolve(psf.getComponents()[0]) 

checkEvaluator = convolved.evaluate() 

checkVector = checkEvaluator(self.xD, self.yD) 

self.assertFloatsAlmostEqual(np.dot(matrixD, coefficients), checkVector, rtol=1E-12, atol=1E-12) 

 

def testCompoundMatrixBuilder(self): 

ellipse = lsst.afw.geom.ellipses.Ellipse(lsst.afw.geom.ellipses.Axes(4.0, 3.0, 1.0), 

lsst.afw.geom.Point2D(3.2, 1.0)) 

radii = [0.7, 1.2] 

orders = [4, 3] 

size = 8 

functions = [self.makeRandomShapeletFunction(order=order, ellipse=ellipse, scale=radius) 

for radius, order in zip(radii, orders)] 

remapMatrices = [np.random.randn(f.getCoefficients().size, size) for f in functions] 

coefficients = np.random.randn(size) 

basis = lsst.shapelet.MultiShapeletBasis(size) 

for radius, function, matrix in zip(radii, functions, remapMatrices): 

function.getCoefficients()[:] = np.dot(matrix, coefficients) 

basis.addComponent(radius, function.getOrder(), matrix) 

factoryF = lsst.shapelet.MatrixBuilderF.Factory(self.xF, self.yF, basis) 

factoryD = lsst.shapelet.MatrixBuilderD.Factory(self.xD, self.yD, basis) 

self.checkAccessors(factoryF, size) 

self.checkAccessors(factoryD, size) 

builder1F = factoryF() 

builder1D = factoryD() 

self.checkAccessors(builder1F, size) 

self.checkAccessors(builder1D, size) 

workspaceF = lsst.shapelet.MatrixBuilderF.Workspace(factoryF.computeWorkspace()) 

workspaceD = lsst.shapelet.MatrixBuilderD.Workspace(factoryD.computeWorkspace()) 

builder2F = factoryF(workspaceF) 

builder2D = factoryD(workspaceD) 

self.assertEqual(workspaceF.getRemaining(), 0) 

self.assertEqual(workspaceD.getRemaining(), 0) 

self.checkAccessors(builder2F, size) 

self.checkAccessors(builder2D, size) 

matrix1F = builder1F(ellipse) 

matrix1D = builder1D(ellipse) 

matrix2F = builder2F(ellipse) 

matrix2D = builder2D(ellipse) 

# same code, different workspace 

self.assertFloatsAlmostEqual(matrix1D, matrix2D, rtol=0.0, atol=0.0) 

# same code, different workspace 

self.assertFloatsAlmostEqual(matrix1F, matrix2F, rtol=0.0, atol=0.0) 

# same code, different precision 

self.assertFloatsAlmostEqual(matrix1F, matrix2F, rtol=1E-7, atol=0.0) 

# Finally, check against a completely different implementation (which is tested elsewhere) 

checkVector = np.zeros(self.xD.shape, dtype=float) 

for function in functions: 

checkEvaluator = function.evaluate() 

checkVector += checkEvaluator(self.xD, self.yD) 

self.assertFloatsAlmostEqual(np.dot(matrix1D, coefficients), checkVector, rtol=1E-13, atol=1E-14) 

 

def testConvolvedCompoundMatrixBuilder(self): 

ellipse = lsst.afw.geom.ellipses.Ellipse(lsst.afw.geom.ellipses.Axes(4.0, 3.0, 1.0), 

lsst.afw.geom.Point2D(3.2, 1.0)) 

radii = [0.7, 1.2] 

orders = [4, 3] 

size = 8 

functions = [self.makeRandomShapeletFunction(order=order, ellipse=ellipse, scale=radius) 

for radius, order in zip(radii, orders)] 

psf = self.makeRandomMultiShapeletFunction() 

remapMatrices = [np.random.randn(f.getCoefficients().size, size) for f in functions] 

coefficients = np.random.randn(size) 

basis = lsst.shapelet.MultiShapeletBasis(size) 

for radius, function, matrix in zip(radii, functions, remapMatrices): 

function.getCoefficients()[:] = np.dot(matrix, coefficients) 

basis.addComponent(radius, function.getOrder(), matrix) 

msf = lsst.shapelet.MultiShapeletFunction(functions) 

factoryF = lsst.shapelet.MatrixBuilderF.Factory(self.xF, self.yF, basis, psf) 

factoryD = lsst.shapelet.MatrixBuilderD.Factory(self.xD, self.yD, basis, psf) 

self.checkAccessors(factoryF, size) 

self.checkAccessors(factoryD, size) 

builder1F = factoryF() 

builder1D = factoryD() 

self.checkAccessors(builder1F, size) 

self.checkAccessors(builder1D, size) 

workspaceF = lsst.shapelet.MatrixBuilderF.Workspace(factoryF.computeWorkspace()) 

workspaceD = lsst.shapelet.MatrixBuilderD.Workspace(factoryD.computeWorkspace()) 

builder2F = factoryF(workspaceF) 

builder2D = factoryD(workspaceD) 

self.assertEqual(workspaceF.getRemaining(), 0) 

self.assertEqual(workspaceD.getRemaining(), 0) 

self.checkAccessors(builder2F, size) 

self.checkAccessors(builder2D, size) 

matrix1F = builder1F(ellipse) 

matrix1D = builder1D(ellipse) 

matrix2F = builder2F(ellipse) 

matrix2D = builder2D(ellipse) 

# same code, different workspace 

self.assertFloatsAlmostEqual(matrix1D, matrix2D, rtol=0.0, atol=0.0) 

# same code, different workspace 

self.assertFloatsAlmostEqual(matrix1F, matrix2F, rtol=0.0, atol=0.0) 

# same code, different precision 

self.assertFloatsAlmostEqual(matrix1F, matrix2F, rtol=1E-7, atol=0.0) 

 

# Finally, check against a completely different implementation (which is tested elsewhere) 

convolved = msf.convolve(psf) 

checkEvaluator = convolved.evaluate() 

checkVector = checkEvaluator(self.xD, self.yD) 

self.assertFloatsAlmostEqual(np.dot(matrix1D, coefficients), checkVector, rtol=1E-13, atol=1E-14) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()