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

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

from unittest import mock 

 

import numpy as np 

 

import lsst.log 

import lsst.utils 

 

import lsst.afw.table 

import lsst.daf.persistence 

from lsst.daf.base import DateTime 

import lsst.geom 

from lsst.meas.algorithms import getRefFluxField, LoadIndexedReferenceObjectsTask, DatasetConfig 

import lsst.pipe.base 

import lsst.jointcal 

from lsst.jointcal import MinimizeResult 

import lsst.jointcal.chi2 

import lsst.jointcal.testUtils 

 

 

# for MemoryTestCase 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

def make_fake_refcat(center, flux, filterName): 

"""Make a fake reference catalog.""" 

schema = LoadIndexedReferenceObjectsTask.makeMinimalSchema([filterName]) 

catalog = lsst.afw.table.SimpleCatalog(schema) 

record = catalog.addNew() 

record.setCoord(center) 

record[filterName + '_flux'] = flux 

record[filterName + '_fluxErr'] = flux*0.1 

return catalog 

 

 

class JointcalTestBase: 

def setUp(self): 

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

self.ccdImageList = struct.ccdImageList 

# so that countStars() returns nonzero results 

for ccdImage in self.ccdImageList: 

ccdImage.resetCatalogForFit() 

 

self.goodChi2 = lsst.jointcal.chi2.Chi2Statistic() 

# chi2/ndof == 2.0 should be non-bad 

self.goodChi2.chi2 = 200.0 

self.goodChi2.ndof = 100 

 

self.badChi2 = lsst.jointcal.chi2.Chi2Statistic() 

self.badChi2.chi2 = 600.0 

self.badChi2.ndof = 100 

 

self.nanChi2 = lsst.jointcal.chi2.Chi2Statistic() 

self.nanChi2.chi2 = np.nan 

self.nanChi2.ndof = 100 

 

self.maxSteps = 20 

self.name = "testing" 

self.dataName = "fake" 

self.whatToFit = "" # unneeded, since we're mocking the fitter 

 

# Mock a Butler so the refObjLoaders have something to call `get()` on. 

self.butler = unittest.mock.Mock(spec=lsst.daf.persistence.Butler) 

self.butler.get.return_value.indexer = DatasetConfig().indexer 

 

# Mock the association manager and give it access to the ccd list above. 

self.associations = mock.Mock(spec=lsst.jointcal.Associations) 

self.associations.getCcdImageList.return_value = self.ccdImageList 

 

# a default config to be modified by individual tests 

self.config = lsst.jointcal.jointcal.JointcalConfig() 

 

 

class TestJointcalIterateFit(JointcalTestBase, lsst.utils.tests.TestCase): 

def setUp(self): 

super().setUp() 

# Mock the fitter and model, so we can force particular 

# return values/exceptions. Default to "good" return values. 

self.fitter = mock.Mock(spec=lsst.jointcal.PhotometryFit) 

self.fitter.computeChi2.return_value = self.goodChi2 

self.fitter.minimize.return_value = MinimizeResult.Converged 

self.model = mock.Mock(spec=lsst.jointcal.SimpleFluxModel) 

 

self.jointcal = lsst.jointcal.JointcalTask(config=self.config, butler=self.butler) 

 

def test_iterateFit_success(self): 

chi2 = self.jointcal._iterate_fit(self.associations, self.fitter, 

self.maxSteps, self.name, self.whatToFit) 

self.assertEqual(chi2, self.goodChi2) 

# Once for the for loop, the second time for the rank update. 

self.assertEqual(self.fitter.minimize.call_count, 2) 

 

def test_iterateFit_writeChi2Outer(self): 

chi2 = self.jointcal._iterate_fit(self.associations, self.fitter, 

self.maxSteps, self.name, self.whatToFit, 

dataName=self.dataName) 

self.assertEqual(chi2, self.goodChi2) 

# Once for the for loop, the second time for the rank update. 

self.assertEqual(self.fitter.minimize.call_count, 2) 

# Default config should not call saveChi2Contributions 

self.fitter.saveChi2Contributions.assert_not_called() 

 

def test_iterateFit_failed(self): 

self.fitter.minimize.return_value = MinimizeResult.Failed 

 

with self.assertRaises(RuntimeError): 

self.jointcal._iterate_fit(self.associations, self.fitter, 

self.maxSteps, self.name, self.whatToFit) 

self.assertEqual(self.fitter.minimize.call_count, 1) 

 

def test_iterateFit_badFinalChi2(self): 

log = mock.Mock(spec=lsst.log.Log) 

self.jointcal.log = log 

self.fitter.computeChi2.return_value = self.badChi2 

 

chi2 = self.jointcal._iterate_fit(self.associations, self.fitter, 

self.maxSteps, self.name, self.whatToFit) 

self.assertEqual(chi2, self.badChi2) 

log.info.assert_called_with("%s %s", "Fit completed", self.badChi2) 

log.error.assert_called_with("Potentially bad fit: High chi-squared/ndof.") 

 

def test_iterateFit_exceedMaxSteps(self): 

log = mock.Mock(spec=lsst.log.Log) 

self.jointcal.log = log 

self.fitter.minimize.return_value = MinimizeResult.Chi2Increased 

maxSteps = 3 

 

chi2 = self.jointcal._iterate_fit(self.associations, self.fitter, 

maxSteps, self.name, self.whatToFit) 

self.assertEqual(chi2, self.goodChi2) 

self.assertEqual(self.fitter.minimize.call_count, maxSteps) 

log.error.assert_called_with("testing failed to converge after %s steps" % maxSteps) 

 

def test_invalid_model(self): 

self.model.validate.return_value = False 

with(self.assertRaises(ValueError)): 

self.jointcal._logChi2AndValidate(self.associations, self.fitter, self.model) 

 

def test_nonfinite_chi2(self): 

self.fitter.computeChi2.return_value = self.nanChi2 

with(self.assertRaises(FloatingPointError)): 

self.jointcal._logChi2AndValidate(self.associations, self.fitter, self.model) 

 

def test_writeChi2(self): 

filename = "somefile" 

self.jointcal._logChi2AndValidate(self.associations, self.fitter, self.model, 

writeChi2Name=filename) 

self.fitter.saveChi2Contributions.assert_called_with(filename+"{type}") 

 

 

class TestJointcalLoadRefCat(JointcalTestBase, lsst.utils.tests.TestCase): 

 

def _make_fake_refcat(self): 

"""Make a fake reference catalog and the bits necessary to use it.""" 

center = lsst.geom.SpherePoint(30, -30, lsst.geom.degrees) 

flux = 10 

radius = 1 * lsst.geom.degrees 

filterName = 'fake' 

 

fakeRefCat = make_fake_refcat(center, flux, filterName) 

fluxField = getRefFluxField(fakeRefCat.schema, filterName) 

returnStruct = lsst.pipe.base.Struct(refCat=fakeRefCat, fluxField=fluxField) 

refObjLoader = mock.Mock(spec=LoadIndexedReferenceObjectsTask) 

refObjLoader.loadSkyCircle.return_value = returnStruct 

 

return refObjLoader, center, radius, filterName, fakeRefCat 

 

def test_load_reference_catalog(self): 

refObjLoader, center, radius, filterName, fakeRefCat = self._make_fake_refcat() 

 

config = lsst.jointcal.jointcal.JointcalConfig() 

config.astrometryReferenceErr = 0.1 # our test refcats don't have coord errors 

jointcal = lsst.jointcal.JointcalTask(config=config, butler=self.butler) 

 

refCat, fluxField = jointcal._load_reference_catalog(refObjLoader, 

jointcal.astrometryReferenceSelector, 

center, 

radius, 

filterName) 

# operator== isn't implemented for Catalogs, so we have to check like 

# this, in case the records are copied during load. 

self.assertEqual(len(refCat), len(fakeRefCat)) 

for r1, r2 in zip(refCat, fakeRefCat): 

self.assertEqual(r1, r2) 

 

def test_load_reference_catalog_subselect(self): 

"""Test that we can select out the one source in the fake refcat 

with a ridiculous S/N cut. 

""" 

refObjLoader, center, radius, filterName, fakeRefCat = self._make_fake_refcat() 

 

config = lsst.jointcal.jointcal.JointcalConfig() 

config.astrometryReferenceErr = 0.1 # our test refcats don't have coord errors 

config.astrometryReferenceSelector.doSignalToNoise = True 

config.astrometryReferenceSelector.signalToNoise.minimum = 1e10 

config.astrometryReferenceSelector.signalToNoise.fluxField = "fake_flux" 

config.astrometryReferenceSelector.signalToNoise.errField = "fake_fluxErr" 

jointcal = lsst.jointcal.JointcalTask(config=config, butler=self.butler) 

 

refCat, fluxField = jointcal._load_reference_catalog(refObjLoader, 

jointcal.astrometryReferenceSelector, 

center, 

radius, 

filterName) 

self.assertEqual(len(refCat), 0) 

 

 

class TestJointcalFitModel(JointcalTestBase, lsst.utils.tests.TestCase): 

def test_fit_photometry_writeChi2(self): 

"""Test that we are calling saveChi2 with appropriate file prefixes.""" 

self.config.photometryModel = "constrainedFlux" 

self.config.writeChi2FilesOuterLoop = True 

jointcal = lsst.jointcal.JointcalTask(config=self.config, butler=self.butler) 

jointcal.focalPlaneBBox = lsst.geom.Box2D() 

 

# Mock the fitter, so we can pretend it found a good fit 

with mock.patch("lsst.jointcal.PhotometryFit", autospect=True) as fitPatch: 

fitPatch.return_value.computeChi2.return_value = self.goodChi2 

fitPatch.return_value.minimize.return_value = MinimizeResult.Converged 

 

expected = ["photometry_init-ModelVisit_chi2", "photometry_init-Model_chi2", 

"photometry_init-Fluxes_chi2", "photometry_init-ModelFluxes_chi2"] 

expected = [mock.call(x+"-fake{type}") for x in expected] 

jointcal._fit_photometry(self.associations, dataName=self.dataName) 

fitPatch.return_value.saveChi2Contributions.assert_has_calls(expected) 

 

def test_fit_astrometry_writeChi2(self): 

"""Test that we are calling saveChi2 with appropriate file prefixes.""" 

self.config.astrometryModel = "constrained" 

self.config.writeChi2FilesOuterLoop = True 

jointcal = lsst.jointcal.JointcalTask(config=self.config, butler=self.butler) 

jointcal.focalPlaneBBox = lsst.geom.Box2D() 

 

# Mock the fitter, so we can pretend it found a good fit 

fitPatch = mock.patch("lsst.jointcal.AstrometryFit") 

# Mock the projection handler so we don't segfault due to not-fully initialized ccdImages 

projectorPatch = mock.patch("lsst.jointcal.OneTPPerVisitHandler") 

with fitPatch as fit, projectorPatch as projector: 

fit.return_value.computeChi2.return_value = self.goodChi2 

fit.return_value.minimize.return_value = MinimizeResult.Converged 

# return a real ProjectionHandler to keep ConstrainedAstrometryModel() happy 

projector.return_value = lsst.jointcal.IdentityProjectionHandler() 

 

expected = ["astrometry_init-DistortionsVisit_chi2", "astrometry_init-Distortions_chi2", 

"astrometry_init-Positions_chi2", "astrometry_init-DistortionsPositions_chi2"] 

expected = [mock.call(x+"-fake{type}") for x in expected] 

jointcal._fit_astrometry(self.associations, dataName=self.dataName) 

fit.return_value.saveChi2Contributions.assert_has_calls(expected) 

 

 

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

"""Tests of Associations.computeBoundingCircle()""" 

def _checkPointsInCircle(self, points, center, radius): 

"""Check that all points are within the (center, radius) circle. 

 

The test is whether the max(points - center) separation is equal to 

(or slightly less than) radius. 

""" 

maxSeparation = 0*lsst.geom.degrees 

for point in points: 

maxSeparation = max(maxSeparation, center.separation(point)) 

self.assertAnglesAlmostEqual(maxSeparation, radius, maxDiff=3*lsst.geom.arcseconds) 

self.assertLess(maxSeparation, radius) 

 

def _testPoints(self, ccdImage1, ccdImage2, skyWcs1, skyWcs2, bbox): 

"""Fill an Associations object and test that it computes the correct 

bounding circle for the input data. 

 

Parameters 

---------- 

ccdImage1, ccdImage2 : `lsst.jointcal.CcdImage` 

The CcdImages to add to the Associations object. 

skyWcs1, skyWcs2 : `lsst.afw.geom.SkyWcs` 

The WCS of each of the above images. 

bbox : `lsst.geom.Box2D` 

The ccd bounding box of both images. 

""" 

lsst.log.setLevel('jointcal', lsst.log.DEBUG) 

associations = lsst.jointcal.Associations() 

associations.addCcdImage(ccdImage1) 

associations.addCcdImage(ccdImage2) 

associations.computeCommonTangentPoint() 

 

circle = associations.computeBoundingCircle() 

center = lsst.geom.SpherePoint(circle.getCenter()) 

radius = lsst.geom.Angle(circle.getOpeningAngle().asRadians(), lsst.geom.radians) 

points = [lsst.geom.SpherePoint(skyWcs1.pixelToSky(lsst.geom.Point2D(x))) 

for x in bbox.getCorners()] 

points.extend([lsst.geom.SpherePoint(skyWcs2.pixelToSky(lsst.geom.Point2D(x))) 

for x in bbox.getCorners()]) 

self._checkPointsInCircle(points, center, radius) 

 

def testPoints(self): 

"""Test for points in an "easy" area, far from RA=0 or the poles.""" 

struct = lsst.jointcal.testUtils.createTwoFakeCcdImages() 

self._testPoints(struct.ccdImageList[0], struct.ccdImageList[1], 

struct.skyWcs[0], struct.skyWcs[1], struct.bbox) 

 

def testPointsRA0(self): 

"""Test for CcdImages crossing RA=0; this demonstrates a fix for 

the bug described in DM-19802. 

""" 

# Use the same pixel origins as the cfht_minimal data, but put the sky origin at RA=0 

crpix = lsst.geom.Point2D(931.517869, 2438.572109) 

cd = np.array([[5.19513851e-05, -2.81124812e-07], 

[-3.25186974e-07, -5.19112119e-05]]) 

crval1 = lsst.geom.SpherePoint(0.01, -0.01, lsst.geom.degrees) 

crval2 = lsst.geom.SpherePoint(-0.01, 0.01, lsst.geom.degrees) 

wcs1 = lsst.afw.geom.makeSkyWcs(crpix, crval1, cd) 

wcs2 = lsst.afw.geom.makeSkyWcs(crpix, crval2, cd) 

 

# Put the visit boresights at the WCS origin, for consistency 

visitInfo1 = lsst.afw.image.VisitInfo(exposureId=30577512, 

date=DateTime(65321.1), 

boresightRaDec=wcs1.getSkyOrigin()) 

visitInfo2 = lsst.afw.image.VisitInfo(exposureId=30621144, 

date=DateTime(65322.1), 

boresightRaDec=wcs1.getSkyOrigin()) 

 

struct = lsst.jointcal.testUtils.createTwoFakeCcdImages(fakeWcses=[wcs1, wcs2], 

fakeVisitInfos=[visitInfo1, visitInfo2]) 

self._testPoints(struct.ccdImageList[0], struct.ccdImageList[1], 

struct.skyWcs[0], struct.skyWcs[1], struct.bbox) 

 

 

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

pass 

 

 

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

lsst.utils.tests.init() 

unittest.main()