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

# This file is part of jointcal. 

# 

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

 

import abc 

 

import numpy as np 

 

import unittest 

import lsst.utils.tests 

 

import lsst.afw.geom 

from lsst.jointcal import photometryTransfo 

 

 

33 ↛ exit,   33 ↛ exit,   33 ↛ exit,   33 ↛ exit,   33 ↛ exit,   33 ↛ exit6 missed branches: 1) line 39 didn't finish the lambda on line 39, 2) line 38 didn't finish the lambda on line 38, 3) line 37 didn't finish the lambda on line 37, 4) line 36 didn't finish the lambda on line 36, 5) line 35 didn't finish the lambda on line 35, 6) line 34 didn't finish the lambda on line 34CHEBYSHEV_T = [ 

lambda x: 1, 

lambda x: x, 

lambda x: 2*x**2 - 1, 

lambda x: (4*x**2 - 3)*x, 

lambda x: (8*x**2 - 8)*x**2 + 1, 

lambda x: ((16*x**2 - 20)*x**2 + 5)*x, 

] 

 

 

class PhotometryTransfoTestBase: 

def setUp(self): 

self.value = 5.0 

self.valueError = 0.3 

self.point = [1., 5.] 

 

 

class SpatiallyInvariantTestBase(PhotometryTransfoTestBase): 

"""Tests for PhotometryTransfoSpatiallyInvariant. 

Subclasses need to call setUp to define: 

self.transfo1 == a default initalized PhotometryTransfoSpatiallyInvariant. 

self.transfo2 == a transfo initialized with self.t2InitValue. 

""" 

def setUp(self): 

super().setUp() 

# initial values for self.transfo2 

self.t2InitValue = 1000.0 

self.t2InitError = 70.0 

 

def _test_transform(self, transfo, expect): 

result = transfo.transform(self.point[0], self.point[1], self.value) 

self.assertEqual(result, expect) # yes, I really mean exactly equal 

 

def _test_transformError(self, transfo, expect): 

result = transfo.transformError(self.point[0], self.point[1], self.value, self.valueError) 

self.assertFloatsAlmostEqual(result, expect) 

 

def _offsetParams(self, delta, value, expect): 

self.transfo1.offsetParams(delta) 

result = self.transfo1.transform(self.point[0], self.point[1], value) 

self.assertFloatsAlmostEqual(result, expect) 

 

def _test_offsetParams(self, expect): 

"""Test offsetting; note that offsetParams offsets by +1.""" 

# check that offset by 0 doesn't change anything. 

delta = np.zeros(1, dtype=float) 

self._offsetParams(delta, self.value, self.value) 

 

# offset by +1 should result in `expect` 

delta -= 1 

self._offsetParams(delta, self.value, expect) 

 

def test_clone(self): 

clone1 = self.transfo1.clone() 

self.assertEqual(self.transfo1.getParameters(), clone1.getParameters()) 

clone2 = self.transfo2.clone() 

self.assertEqual(self.transfo2.getParameters(), clone2.getParameters()) 

self.assertNotEqual(clone1.getParameters(), clone2.getParameters()) 

 

def _test_computeParameterDerivatives(self, expect): 

"""The derivative of a spatially invariant transform is always the same. 

Should be indepdendent of position 

""" 

result = self.transfo1.computeParameterDerivatives(1, 2, self.value) 

self.assertEqual(expect, result) 

result = self.transfo1.computeParameterDerivatives(-5, -100, self.value) 

self.assertEqual(expect, result) 

result = self.transfo2.computeParameterDerivatives(-1000, 150, self.value) 

self.assertEqual(expect, result) 

 

 

class FluxTransfoSpatiallyInvariantTestCase(SpatiallyInvariantTestBase, lsst.utils.tests.TestCase): 

def setUp(self): 

super().setUp() 

self.transfo1 = photometryTransfo.FluxTransfoSpatiallyInvariant() 

self.transfo2 = photometryTransfo.FluxTransfoSpatiallyInvariant(self.t2InitValue) 

 

def test_transform(self): 

self._test_transform(self.transfo1, self.value) 

self._test_transform(self.transfo2, self.value*self.t2InitValue) 

 

def test_transformError(self): 

expect = (self.valueError*1) 

self._test_transformError(self.transfo1, expect) 

expect = (self.valueError*self.t2InitValue) 

self._test_transformError(self.transfo2, expect) 

 

def test_offsetParams(self): 

"""Offset by +1 means transform by 2.""" 

self._test_offsetParams(self.value*2) 

 

def test_computeParameterDerivatives(self): 

"""Should be indepdendent of position, and equal to the flux.""" 

self._test_computeParameterDerivatives(self.value) 

 

 

class MagnitudeTransfoSpatiallyInvariantTestCase(SpatiallyInvariantTestBase, lsst.utils.tests.TestCase): 

def setUp(self): 

super().setUp() 

self.transfo1 = photometryTransfo.MagnitudeTransfoSpatiallyInvariant() 

self.transfo2 = photometryTransfo.MagnitudeTransfoSpatiallyInvariant(self.t2InitValue) 

 

def test_transform(self): 

self._test_transform(self.transfo1, self.value) 

self._test_transform(self.transfo2, self.value + self.t2InitValue) 

 

def test_transformError(self): 

expect = self.valueError 

self._test_transformError(self.transfo1, expect) 

expect = self.valueError 

self._test_transformError(self.transfo2, expect) 

 

def test_offsetParams(self): 

"""Offset by +1 means transform by +1.""" 

self._test_offsetParams(self.value + 1) 

 

def test_computeParameterDerivatives(self): 

"""Should always be identically 1.""" 

self._test_computeParameterDerivatives(1.0) 

 

 

class PhotometryTransfoChebyshevTestCase(PhotometryTransfoTestBase, abc.ABC): 

def setUp(self): 

"""Call this first, then construct self.transfo1 from self.order1, 

and self.transfo2 from self.coefficients. 

""" 

super().setUp() 

self.bbox = lsst.afw.geom.Box2D(lsst.afw.geom.Point2D(-5, -6), lsst.afw.geom.Point2D(7, 8)) 

self.order1 = 2 

self.coefficients = np.array([[5, 3], [4, 0]], dtype=float) 

 

# self.transfo1 will have 6 parameters, by construction 

self.delta = np.arange(6, dtype=float) 

# make one of them have opposite sign to check +/- consistency 

self.delta[0] = -self.delta[0] 

 

def test_getNpar(self): 

self.assertEqual(self.transfo1.getNpar(), 6) 

self.assertEqual(self.transfo2.getNpar(), 3) 

 

def _evaluate_chebyshev(self, x, y): 

"""Evaluate the chebyshev defined by self.coefficients at (x,y)""" 

# sx, sy: transform from self.bbox range to [-1, -1] 

cx = (self.bbox.getMinX() + self.bbox.getMaxX())/2.0 

cy = (self.bbox.getMinY() + self.bbox.getMaxY())/2.0 

sx = 2.0 / self.bbox.getWidth() 

sy = 2.0 / self.bbox.getHeight() 

result = 0 

order = len(self.coefficients) 

for j in range(order): 

for i in range(0, order-j): 

Tx = CHEBYSHEV_T[i](sx*(x - cx)) 

Ty = CHEBYSHEV_T[j](sy*(y - cy)) 

result += self.coefficients[j, i]*Tx*Ty 

return result 

 

def _test_offsetParams(self, expect): 

"""Test offsetting; note that offsetParams offsets by `-delta`. 

 

Parameters 

---------- 

expect1 : `numpy.ndarray`, (N,2) 

Expected coefficients from an offset by 0. 

expect2 : `numpy.ndarray`, (N,2) 

Expected coefficients from an offset by self.delta. 

""" 

# first offset by all zeros: nothing should change 

delta = np.zeros(self.transfo1.getNpar(), dtype=float) 

self.transfo1.offsetParams(delta) 

self.assertFloatsAlmostEqual(expect, self.transfo1.getCoefficients()) 

 

# now offset by self.delta 

expect[0, 0] -= self.delta[0] 

expect[0, 1] -= self.delta[1] 

expect[0, 2] -= self.delta[2] 

expect[1, 0] -= self.delta[3] 

expect[1, 1] -= self.delta[4] 

expect[2, 0] -= self.delta[5] 

self.transfo1.offsetParams(self.delta) 

self.assertFloatsAlmostEqual(expect, self.transfo1.getCoefficients()) 

 

def test_clone(self): 

clone1 = self.transfo1.clone() 

self.assertFloatsEqual(self.transfo1.getParameters(), clone1.getParameters()) 

self.assertEqual(self.transfo1.getOrder(), clone1.getOrder()) 

self.assertEqual(self.transfo1.getBBox(), clone1.getBBox()) 

clone2 = self.transfo2.clone() 

self.assertFloatsEqual(self.transfo2.getParameters(), clone2.getParameters()) 

self.assertEqual(self.transfo2.getOrder(), clone2.getOrder()) 

self.assertEqual(self.transfo2.getBBox(), clone2.getBBox()) 

 

@abc.abstractmethod 

def _computeChebyshevDerivative(self, Tx, Ty, value): 

"""Return the derivative of chebyshev component Tx, Ty.""" 

pass 

 

def test_computeParameterDerivatives(self): 

cx = (self.bbox.getMinX() + self.bbox.getMaxX())/2.0 

cy = (self.bbox.getMinY() + self.bbox.getMaxY())/2.0 

sx = 2.0 / self.bbox.getWidth() 

sy = 2.0 / self.bbox.getHeight() 

result = self.transfo1.computeParameterDerivatives(self.point[0], self.point[1], self.value) 

Tx = np.array([CHEBYSHEV_T[i](sx*(self.point[0] - cx)) for i in range(self.order1+1)], dtype=float) 

Ty = np.array([CHEBYSHEV_T[i](sy*(self.point[1] - cy)) for i in range(self.order1+1)], dtype=float) 

expect = [] 

for j in range(len(Ty)): 

for i in range(0, self.order1-j+1): 

expect.append(self._computeChebyshevDerivative(Ty[j], Tx[i], self.value)) 

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

 

 

class FluxTransfoChebyshevTestCase(PhotometryTransfoChebyshevTestCase, lsst.utils.tests.TestCase): 

def setUp(self): 

super().setUp() 

self.transfo1 = photometryTransfo.FluxTransfoChebyshev(self.order1, self.bbox) 

self.transfo2 = photometryTransfo.FluxTransfoChebyshev(self.coefficients, self.bbox) 

 

def test_transform(self): 

result = self.transfo1.transform(self.point[0], self.point[1], self.value) 

self.assertEqual(result, self.value) # transfo1 is the identity 

 

result = self.transfo2.transform(self.point[0], self.point[1], self.value) 

expect = self.value*self._evaluate_chebyshev(self.point[0], self.point[1]) 

self.assertEqual(result, expect) 

 

def test_offsetParams(self): 

# an offset by 0 means we will still have 1 only in the 0th parameter 

expect = np.zeros((self.order1+1, self.order1+1), dtype=float) 

expect[0, 0] = 1 

self._test_offsetParams(expect) 

 

def _computeChebyshevDerivative(self, x, y, value): 

return x * y * value 

 

 

class MagnitudeTransfoChebyshevTestCase(PhotometryTransfoChebyshevTestCase, lsst.utils.tests.TestCase): 

def setUp(self): 

super().setUp() 

self.transfo1 = photometryTransfo.MagnitudeTransfoChebyshev(self.order1, self.bbox) 

self.transfo2 = photometryTransfo.MagnitudeTransfoChebyshev(self.coefficients, self.bbox) 

 

def test_transform(self): 

result = self.transfo1.transform(self.point[0], self.point[1], self.value) 

self.assertEqual(result, self.value) # transfo1 is the identity 

 

result = self.transfo2.transform(self.point[0], self.point[1], self.value) 

expect = self.value + self._evaluate_chebyshev(self.point[0], self.point[1]) 

self.assertEqual(result, expect) 

 

def test_offsetParams(self): 

# an offset by 0 means all parameters still 0 

expect = np.zeros((self.order1+1, self.order1+1), dtype=float) 

self._test_offsetParams(expect) 

 

def _computeChebyshevDerivative(self, x, y, value): 

return x * y 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()