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

# 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 

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) 

filename = f"{self.name}_iterate_0_chi2-{self.dataName}" 

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

 

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() 

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.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 MemoryTester(lsst.utils.tests.MemoryTestCase): 

pass 

 

 

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

lsst.utils.tests.init() 

unittest.main()