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

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

# 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 numpy as np 

 

import unittest 

import lsst.utils.tests 

import lsst.jointcal.testUtils 

 

import lsst.afw.cameraGeom 

import lsst.afw.geom 

import lsst.afw.table 

import lsst.afw.image 

import lsst.afw.image.utils 

import lsst.daf.persistence 

import lsst.jointcal.ccdImage 

import lsst.jointcal.photometryModels 

import lsst.jointcal.star 

 

 

def getNParametersPolynomial(order): 

"""Number of parameters in a photometry polynomial model is (d+1)(d+2)/2.""" 

return (order + 1)*(order + 2)/2 

 

 

class PhotometryModelTestBase: 

"""Have the sublass also derive from ``lsst.utils.tests.TestCase`` to cause 

unittest to use the test_* methods in this class. 

""" 

def setUp(self): 

# Ensure that the filter list is reset for each test so that we avoid 

# confusion or contamination each time we create a cfht camera below. 

lsst.afw.image.utils.resetFilters() 

 

struct = lsst.jointcal.testUtils.createTwoFakeCcdImages(100, 100) 

self.ccdImageList = struct.ccdImageList 

self.camera = struct.camera 

self.catalogs = struct.catalogs 

self.fluxFieldName = struct.fluxFieldName 

 

self.stars = [] 

for catalog, ccdImage in zip(self.catalogs, self.ccdImageList): 

pixToFocal = ccdImage.getDetector().getTransform(lsst.afw.cameraGeom.PIXELS, 

lsst.afw.cameraGeom.FOCAL_PLANE) 

self.stars.append(lsst.jointcal.testUtils.getMeasuredStarsFromCatalog(catalog, pixToFocal)) 

 

self.fittedStar = lsst.jointcal.star.FittedStar(self.stars[0][0]) 

# Make a refStar at this fittedStar position, but with different 

# flux and fluxErr, so that it does interesting things when subtracted. 

self.refStar = lsst.jointcal.star.RefStar(self.fittedStar.x, 

self.fittedStar.y, 

self.fittedStar.flux + 50, 

self.fittedStar.fluxErr * 0.01, 

[], []) 

 

self.firstIndex = 0 # for assignIndices 

 

# Set to True in the subclass constructor to do the PhotoCalib calculations in magnitudes. 

self.useMagnitude = False 

 

def _toPhotoCalib(self, ccdImage, catalog, stars): 

"""Test converting this object to a PhotoCalib.""" 

photoCalib = self.model.toPhotoCalib(ccdImage) 

if self.useMagnitude: 

result = photoCalib.instFluxToMagnitude(catalog, self.fluxFieldName) 

else: 

result = photoCalib.instFluxToMaggies(catalog, self.fluxFieldName) 

 

expects = np.empty(len(stars)) 

for i, star in enumerate(stars): 

expects[i] = self.model.transform(ccdImage, star) 

self.assertFloatsAlmostEqual(result[:, 0], expects, rtol=2e-13) 

# NOTE: don't compare transformed errors, as they will be different: 

# photoCalib incorporates the model error, while jointcal computes the 

# full covariance matrix, from which the model error should be derived. 

 

def test_toPhotoCalib(self): 

self._toPhotoCalib(self.ccdImageList[0], self.catalogs[0], self.stars[0]) 

self._toPhotoCalib(self.ccdImageList[1], self.catalogs[1], self.stars[1]) 

 

def test_freezeErrorTransform(self): 

"""After calling freezeErrorTransform(), the error transform is unchanged 

by offsetParams(). 

""" 

ccdImage = self.ccdImageList[0] 

star0 = self.stars[0][0] 

 

self.model.offsetParams(self.delta) 

t1 = self.model.transform(ccdImage, star0) 

t1Err = self.model.transformError(ccdImage, star0) 

self.model.freezeErrorTransform() 

self.model.offsetParams(self.delta) 

t2 = self.model.transform(ccdImage, star0) 

t2Err = self.model.transformError(ccdImage, star0) 

 

self.assertFloatsNotEqual(t1, t2) 

self.assertFloatsEqual(t1Err, t2Err) 

 

 

class FluxTestBase: 

"""Have the sublass also derive from ``lsst.utils.tests.TestCase`` to cause 

unittest to use the test_* methods in this class. 

""" 

def test_offsetFittedStar(self): 

value = self.fittedStar.flux 

 

self.model.offsetFittedStar(self.fittedStar, 0) 

self.assertEqual(self.fittedStar.flux, value) 

 

self.model.offsetFittedStar(self.fittedStar, 1) 

self.assertEqual(self.fittedStar.flux, value-1) 

 

def test_computeRefResidual(self): 

result = self.model.computeRefResidual(self.fittedStar, self.refStar) 

self.assertEqual(result, self.fittedStar.flux - self.refStar.flux) 

 

 

class MagnitudeTestBase: 

"""Have the sublass also derive from ``lsst.utils.tests.TestCase`` to cause 

unittest to use the test_* methods in this class. 

""" 

def test_offsetFittedStar(self): 

value = self.fittedStar.mag 

 

self.model.offsetFittedStar(self.fittedStar, 0) 

self.assertEqual(self.fittedStar.mag, value) 

 

self.model.offsetFittedStar(self.fittedStar, 1) 

self.assertEqual(self.fittedStar.mag, value-1) 

 

def test_computeRefResidual(self): 

result = self.model.computeRefResidual(self.fittedStar, self.refStar) 

self.assertEqual(result, self.fittedStar.mag - self.refStar.mag) 

 

 

class SimplePhotometryModelTestBase(PhotometryModelTestBase): 

"""Have the sublass also derive from ``lsst.utils.tests.TestCase`` to cause 

unittest to use the test_* methods in this class. 

""" 

def test_getNpar(self): 

result = self.model.getNpar(self.ccdImageList[0]) 

self.assertEqual(result, 1) 

result = self.model.getNpar(self.ccdImageList[1]) 

self.assertEqual(result, 1) 

 

def testGetTotalParameters(self): 

result = self.model.getTotalParameters() 

self.assertEqual(result, 2) 

 

 

class SimpleFluxModelTestCase(SimplePhotometryModelTestBase, FluxTestBase, lsst.utils.tests.TestCase): 

def setUp(self): 

super().setUp() 

self.model = lsst.jointcal.photometryModels.SimpleFluxModel(self.ccdImageList) 

self.model.assignIndices("", self.firstIndex) # have to call this once to let offsetParams work. 

self.delta = np.arange(len(self.ccdImageList), dtype=float)*-0.2 + 1 

 

 

class SimpleMagnitudeModelTestCase(SimplePhotometryModelTestBase, 

MagnitudeTestBase, 

lsst.utils.tests.TestCase): 

def setUp(self): 

super().setUp() 

self.model = lsst.jointcal.photometryModels.SimpleMagnitudeModel(self.ccdImageList) 

self.model.assignIndices("", self.firstIndex) # have to call this once to let offsetParams work. 

self.delta = np.arange(len(self.ccdImageList), dtype=float)*-0.2 + 1 

self.useMagnitude = True 

 

 

class ConstrainedPhotometryModelTestCase(PhotometryModelTestBase): 

def setUp(self): 

super().setUp() 

self.visitOrder = 3 

self.focalPlaneBBox = self.camera.getFpBBox() 

# Amount to shift the parameters to get more than just a constant field 

# for the second ccdImage. 

# Reverse the range so that the low order terms are the largest. 

self.delta = (np.arange(20, dtype=float)*-0.2 + 1)[::-1] 

# but keep the first ccdImage constant, to help distinguish test failures. 

self.delta[:10] = 0.0 

self.delta[0] = -5.0 

 

def _initModel2(self, Model): 

""" 

Initialize self.model2 with 2 fake sensor catalogs. Call after setUp(). 

 

Parameters 

---------- 

Model : `PhotometryModel`-type 

The PhotometryModel-derived class to construct. 

""" 

# We need at least two sensors to distinguish "Model" from "ModelVisit" 

# in `test_assignIndices()`. 

# createTwoFakeCcdImages() always uses the same two visitIds, 

# so there will be 2 visits total here. 

struct1 = lsst.jointcal.testUtils.createTwoFakeCcdImages(100, 100, seed=100, fakeCcdId=12, 

photoCalibMean1=1e-2, 

photoCalibMean2=1.2e-2) 

self.ccdImageList2 = struct1.ccdImageList 

struct2 = lsst.jointcal.testUtils.createTwoFakeCcdImages(100, 100, seed=101, fakeCcdId=13, 

photoCalibMean1=2.0e-2, 

photoCalibMean2=2.2e-2) 

self.ccdImageList2.extend(struct2.ccdImageList) 

camera = struct1.camera # the camera is the same in both structs 

focalPlaneBBox = camera.getFpBBox() 

self.model2 = Model(self.ccdImageList2, focalPlaneBBox, self.visitOrder) 

 

def test_getNpar(self): 

""" 

Order 3 => (3+1)*(3+2))/2 = 10 parameters, 

and the chip map is fixed (only one ccd), so does not contribute. 

""" 

expect = getNParametersPolynomial(self.visitOrder) 

result = self.model.getNpar(self.ccdImageList[0]) 

self.assertEqual(result, expect) 

result = self.model.getNpar(self.ccdImageList[1]) 

self.assertEqual(result, expect) 

 

def testGetTotalParameters(self): 

"""Two visits, one (fixed) ccd.""" 

expect = getNParametersPolynomial(self.visitOrder) * 2 

result = self.model.getTotalParameters() 

self.assertEqual(result, expect) 

 

def test_assignIndices(self): 

"""Test that the correct number of indices were assigned. 

Does not check that the internal mappings are assigned the correct 

indices. 

""" 

# one polynomial per visit, plus one fitted scale for the second chip. 

expect = 2 * getNParametersPolynomial(self.visitOrder) + 1 

index = self.model2.assignIndices("Model", self.firstIndex) 

self.assertEqual(index, expect) 

 

# one polynomial per visit 

expect = 2 * getNParametersPolynomial(self.visitOrder) 

index = self.model2.assignIndices("ModelVisit", self.firstIndex) 

self.assertEqual(index, expect) 

 

# one fitted chip 

expect = 1 

index = self.model2.assignIndices("ModelChip", self.firstIndex) 

self.assertEqual(index, expect) 

 

def _testConstructor(self, expectVisit, expectChips): 

"""Post-construction, the ChipTransforms should be the PhotoCalib mean of 

the first visit's ccds, and the VisitTransforms should be the identity. 

""" 

# Identify to the model that we're fitting both components. 

self.model2.assignIndices("Model", self.firstIndex) 

 

# check the visitMappings 

for ccdImage in self.ccdImageList2: 

result = self.model2.getMapping(ccdImage).getVisitMapping().getTransform().getParameters() 

self.assertFloatsEqual(result, expectVisit, msg=ccdImage.getName()) 

 

# check the chipMappings 

for ccdImage, expect in zip(self.ccdImageList2, expectChips): 

result = self.model2.getMapping(ccdImage).getChipMapping().getTransform().getParameters() 

# almost equal because log() may have been involved in the math 

self.assertFloatsAlmostEqual(result, expect, msg=ccdImage.getName()) 

 

def test_photoCalibMean(self): 

"""The mean of the photoCalib should match the mean over a calibrated image.""" 

image = lsst.afw.image.MaskedImageF(self.ccdImageList[0].getDetector().getBBox()) 

image[:] = 1 

photoCalib = self.model.toPhotoCalib(self.ccdImageList[0]) 

expect = photoCalib.calibrateImage(image).image.array.mean() 

self.assertFloatsAlmostEqual(expect, photoCalib.getCalibrationMean(), rtol=2e-5) 

 

 

class ConstrainedFluxModelTestCase(ConstrainedPhotometryModelTestCase, 

FluxTestBase, 

lsst.utils.tests.TestCase): 

def setUp(self): 

super().setUp() 

self.model = lsst.jointcal.ConstrainedFluxModel(self.ccdImageList, 

self.focalPlaneBBox, 

self.visitOrder) 

# have to call this once to let offsetParams work. 

self.model.assignIndices("Model", self.firstIndex) 

self.model.offsetParams(self.delta) 

 

self._initModel2(lsst.jointcal.ConstrainedFluxModel) 

 

def testConstructor(self): 

expectVisit = np.zeros(int(getNParametersPolynomial(self.visitOrder))) 

expectVisit[0] = 1 

# chipMappings are fixed per-chip, and thus are 

# shared between the first pair and second pair of fake ccdImages 

expectChips = [self.ccdImageList2[0].getPhotoCalib().getCalibrationMean(), 

self.ccdImageList2[0].getPhotoCalib().getCalibrationMean(), 

self.ccdImageList2[2].getPhotoCalib().getCalibrationMean(), 

self.ccdImageList2[2].getPhotoCalib().getCalibrationMean()] 

self._testConstructor(expectVisit, expectChips) 

 

 

class ConstrainedMagnitudeModelTestCase(ConstrainedPhotometryModelTestCase, 

MagnitudeTestBase, 

lsst.utils.tests.TestCase): 

def setUp(self): 

super().setUp() 

self.model = lsst.jointcal.ConstrainedMagnitudeModel(self.ccdImageList, 

self.focalPlaneBBox, 

self.visitOrder) 

# have to call this once to let offsetParams work. 

self.model.assignIndices("Model", self.firstIndex) 

self.model.offsetParams(self.delta) 

 

self._initModel2(lsst.jointcal.ConstrainedMagnitudeModel) 

 

self.useMagnitude = True 

 

def testConstructor(self): 

expectVisit = np.zeros(int(getNParametersPolynomial(self.visitOrder))) 

 

def fluxToMag(flux): 

return -2.5*np.log10(flux) 

 

# chipMappings are fixed per-chip, and thus are 

# shared between the first pair and second pair of fake ccdImages 

expectChips = [fluxToMag(self.ccdImageList2[0].getPhotoCalib().getCalibrationMean()), 

fluxToMag(self.ccdImageList2[0].getPhotoCalib().getCalibrationMean()), 

fluxToMag(self.ccdImageList2[2].getPhotoCalib().getCalibrationMean()), 

fluxToMag(self.ccdImageList2[2].getPhotoCalib().getCalibrationMean())] 

self._testConstructor(expectVisit, expectChips) 

 

def test_checkPositiveOnBBox(self): 

self.assertTrue(self.model.checkPositiveOnBBox(self.ccdImageList[0])) 

self.assertTrue(self.model.checkPositiveOnBBox(self.ccdImageList[1])) 

 

# make a model that is negative all over 

struct = lsst.jointcal.testUtils.createTwoFakeCcdImages(100, 100, seed=100, fakeCcdId=12, 

photoCalibMean1=1000, 

photoCalibMean2=1200) 

model = lsst.jointcal.ConstrainedMagnitudeModel(struct.ccdImageList, 

struct.camera.getFpBBox(), 

self.visitOrder) 

self.assertFalse(model.checkPositiveOnBBox(struct.ccdImageList[0])) 

 

def test_validate(self): 

self.assertTrue(self.model.validate(self.ccdImageList)) 

# Make the model go negative 

self.model.offsetParams(-3*self.delta) 

self.assertFalse(self.model.validate(self.ccdImageList)) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()