Coverage for tests/test_jointcal_cfht.py: 24%

135 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2022-09-12 01:51 -0700

1# This file is part of jointcal. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <https://www.gnu.org/licenses/>. 

21 

22import unittest 

23import os 

24import tempfile 

25 

26import lsst.geom 

27import lsst.utils 

28import lsst.pex.exceptions 

29import lsst.pex.config 

30 

31import jointcalTestBase 

32 

33 

34# for MemoryTestCase 

35def setup_module(module): 

36 lsst.utils.tests.init() 

37 

38 

39class JointcalTestCFHT(jointcalTestBase.JointcalTestBase, lsst.utils.tests.TestCase): 

40 

41 @classmethod 

42 def setUpClass(cls): 

43 try: 

44 cls.data_dir = lsst.utils.getPackageDir('testdata_jointcal') 

45 except LookupError: 

46 raise unittest.SkipTest("testdata_jointcal not setup") 

47 try: 

48 lsst.utils.getPackageDir('obs_cfht') 

49 except LookupError: 

50 raise unittest.SkipTest("obs_cfht not setup") 

51 

52 def setUp(self): 

53 input_dir = os.path.join(self.data_dir, 'cfht') 

54 all_visits = [849375, 850587] 

55 

56 where = "instrument='MegaPrime' and tract=0 and skymap='discrete'" 

57 inputCollections = ["singleFrame", "skymaps"] 

58 refcats = {"gaia_dr2_20200414": os.path.join(input_dir, "gaia_dr2_20200414.ecsv"), 

59 "ps1_pv3_3pi_20170110": os.path.join(input_dir, "ps1_pv3_3pi_20170110.ecsv"), 

60 "sdss_dr9_fink_v5b": os.path.join(input_dir, "sdss-dr9-fink-v5b.ecsv")} 

61 

62 outputDataId = {'instrument': 'MegaPrime', 'tract': 0, 'skymap': 'discrete'} 

63 self.setUp_base("lsst.obs.cfht.MegaPrime", "MegaPrime", 

64 input_dir=input_dir, 

65 all_visits=all_visits, 

66 where=where, 

67 inputCollections=inputCollections, 

68 refcats=refcats, 

69 refcatPath=input_dir, 

70 outputDataId=outputDataId, 

71 log_level="DEBUG") 

72 

73 # The CFHT tests all produce the same set of output visits+detectors, 

74 # whether astrometry or photometry. 

75 self.outputVisits = {849375: (12, 13, 14, 21, 22, 23), 

76 850587: (12, 13, 14, 21, 22, 23)} 

77 

78 def test_jointcalTask_2_visits_simple(self): 

79 """Test the simple models with two visits and check that some debug 

80 output files also get created. 

81 """ 

82 configOptions = {"astrometryModel": "simple", "photometryModel": "simpleFlux", 

83 "writeInitialModel": True, "writeChi2FilesInitialFinal": True} 

84 

85 # use a temporary directory for debug output, to prevent test collisions 

86 with tempfile.TemporaryDirectory() as tempdir: 

87 configOptions["debugOutputPath"] = tempdir 

88 metrics = {'astrometry_collected_refStars': 867, 

89 'photometry_collected_refStars': 11569, 

90 'astrometry_prepared_refStars': 323, 

91 'photometry_prepared_refStars': 2302, 

92 'astrometry_matched_fittedStars': 2399, 

93 'photometry_matched_fittedStars': 2399, 

94 'astrometry_prepared_fittedStars': 1255, 

95 'photometry_prepared_fittedStars': 2317, 

96 'astrometry_prepared_ccdImages': 12, 

97 'photometry_prepared_ccdImages': 12, 

98 'astrometry_final_chi2': 957.754, 

99 'astrometry_final_ndof': 1872, 

100 'photometry_final_chi2': 11561.1, 

101 'photometry_final_ndof': 2849 

102 } 

103 self._runJointcalTest(configOptions=configOptions, metrics=metrics, 

104 astrometryOutputs=self.outputVisits, photometryOutputs=self.outputVisits) 

105 

106 # Check for the existence of the chi2 contribution files. 

107 expected = ['photometry_initial_chi2-0_r.MP9601', 'astrometry_initial_chi2-0_r.MP9601', 

108 'photometry_final_chi2-0_r.MP9601', 'astrometry_final_chi2-0_r.MP9601'] 

109 for partial in expected: 

110 name = os.path.join(tempdir, partial+'-ref.csv') 

111 self.assertTrue(os.path.exists(name), msg="Did not find file %s"%name) 

112 name = os.path.join(tempdir, partial+'-meas.csv') 

113 self.assertTrue(os.path.exists(name), msg='Did not find file %s'%name) 

114 

115 expected = ["initial_astrometry_model-0_r.MP9601.txt", "initial_photometry_model-0_r.MP9601.txt"] 

116 for name in expected: 

117 fullpath = os.path.join(tempdir, name) 

118 self.assertTrue(os.path.exists(fullpath), msg=f"Did not find file {fullpath}") 

119 

120 def setup_jointcalTask_2_visits_constrainedAstrometry(self): 

121 """Set default values for the constrainedAstrometry tests, and make 

122 the differences between each test and the defaults more obvious. 

123 """ 

124 configOptions = {"astrometryModel": "constrained", "doPhotometry": False} 

125 metrics = {'astrometry_collected_refStars': 867, 

126 'astrometry_prepared_refStars': 323, 

127 'astrometry_matched_fittedStars': 2399, 

128 'astrometry_prepared_fittedStars': 1255, 

129 'astrometry_prepared_ccdImages': 12, 

130 'astrometry_final_chi2': 1192.41, 

131 'astrometry_final_ndof': 2004, 

132 } 

133 return configOptions, metrics 

134 

135 def test_jointcalTask_2_visits_constrainedAstrometry_no_photometry(self): 

136 configOptions, metrics = self.setup_jointcalTask_2_visits_constrainedAstrometry() 

137 configOptions['writeInitialModel'] = True # write the initial models 

138 # use a temporary directory for debug output, to prevent test collisions 

139 with tempfile.TemporaryDirectory() as tempdir: 

140 configOptions['debugOutputPath'] = tempdir 

141 

142 self._runJointcalTest(configOptions=configOptions, metrics=metrics, 

143 astrometryOutputs=self.outputVisits) 

144 

145 filename = os.path.join(tempdir, "initial_astrometry_model-0_r.MP9601.txt") 

146 self.assertTrue(os.path.exists(filename), msg=f"Did not find file {filename}") 

147 

148 def test_jointcalTask_2_visits_constrainedAstrometry_no_rank_update(self): 

149 """Demonstrate that skipping the rank update doesn't substantially affect astrometry. 

150 """ 

151 configOptions, metrics = self.setup_jointcalTask_2_visits_constrainedAstrometry() 

152 metrics['astrometry_final_chi2'] = 1095.70 

153 metrics['astrometry_final_ndof'] = 1740 

154 

155 configOptions['astrometryDoRankUpdate'] = False 

156 

157 self._runJointcalTest(configOptions=configOptions, metrics=metrics, 

158 astrometryOutputs=self.outputVisits) 

159 

160 def test_jointcalTask_2_visits_constrainedAstrometry_4sigma_outliers(self): 

161 """4 sigma outlier rejection means fewer available sources after the 

162 fitter converges, resulting in a smaller ndof and chi2. 

163 """ 

164 configOptions, metrics = self.setup_jointcalTask_2_visits_constrainedAstrometry() 

165 configOptions['outlierRejectSigma'] = 4 

166 metrics['astrometry_final_chi2'] = 732.79 

167 metrics['astrometry_final_ndof'] = 1814 

168 

169 self._runJointcalTest(configOptions=configOptions, metrics=metrics, 

170 astrometryOutputs=self.outputVisits) 

171 

172 def test_jointcalTask_2_visits_constrainedAstrometry_astrometryOutlierRelativeTolerance(self): 

173 """Test that astrometryOutlierRelativeTolerance changes the fit. Setting 

174 1% for the astrometryOutlierRelativeTolerance will result in higher chi2 

175 and ndof. 

176 """ 

177 configOptions, metrics = self.setup_jointcalTask_2_visits_constrainedAstrometry() 

178 configOptions['astrometryOutlierRelativeTolerance'] = 0.01 

179 metrics['astrometry_final_chi2'] = 1542.03 

180 metrics['astrometry_final_ndof'] = 2076 

181 

182 self._runJointcalTest(configOptions=configOptions, metrics=metrics, 

183 astrometryOutputs=self.outputVisits) 

184 

185 def test_jointcalTask_2_visits_constrainedAstrometry_astrometryReferenceUncertainty_smaller(self): 

186 """Test with a smaller fake reference uncertainty: chi2 will be higher.""" 

187 configOptions, metrics = self.setup_jointcalTask_2_visits_constrainedAstrometry() 

188 astrometryRefErrConfig = os.path.join(self.path, 'config/astrometryReferenceErr-config.py') 

189 metrics['astrometry_final_chi2'] = 1213.51 

190 metrics['astrometry_final_ndof'] = 2126 

191 

192 self._runJointcalTest(configFiles=[astrometryRefErrConfig], 

193 configOptions=configOptions, metrics=metrics, 

194 astrometryOutputs=self.outputVisits) 

195 

196 def test_jointcalTask_2_visits_constrainedAstrometry_astrometryReferenceUncertainty_None_fails(self): 

197 """Setting astrometryReferenceUncertainty=None should fail for refcats 

198 that have no position errors. 

199 """ 

200 configOptions, metrics = self.setup_jointcalTask_2_visits_constrainedAstrometry() 

201 badRefErrConfig = os.path.join(self.path, 'config/astrometryReferenceErr-None-config.py') 

202 with self.assertRaisesRegex(lsst.pex.config.FieldValidationError, 

203 "Reference catalog does not contain coordinate errors"): 

204 self._runJointcalTest(configFiles=[badRefErrConfig], configOptions=configOptions, metrics=metrics) 

205 

206 def setup_jointcalTask_2_visits_constrainedPhotometry(self): 

207 """Set default values for the constrainedPhotometry tests, and make 

208 the differences between each test and the defaults more obvious. 

209 """ 

210 configOptions = {"photometryModel": "constrainedFlux", "doAstrometry": False} 

211 

212 metrics = {'photometry_collected_refStars': 11569, 

213 'photometry_prepared_refStars': 2302, 

214 'photometry_matched_fittedStars': 2399, 

215 'photometry_prepared_fittedStars': 2317, 

216 'photometry_prepared_ccdImages': 12, 

217 'photometry_final_chi2': 11264.28, 

218 'photometry_final_ndof': 2821 

219 } 

220 return configOptions, metrics 

221 

222 def test_jointcalTask_2_visits_constrainedPhotometry_no_astrometry(self): 

223 configOptions, metrics = self.setup_jointcalTask_2_visits_constrainedPhotometry() 

224 configOptions['writeInitialModel'] = True # write the initial models 

225 # use a temporary directory for debug output, to prevent test collisions 

226 with tempfile.TemporaryDirectory() as tempdir: 

227 configOptions['debugOutputPath'] = tempdir 

228 

229 self._runJointcalTest(configOptions=configOptions, metrics=metrics, 

230 photometryOutputs=self.outputVisits) 

231 filename = os.path.join(tempdir, "initial_photometry_model-0_r.MP9601.txt") 

232 self.assertTrue(os.path.exists(filename), msg=f"Did not find file {filename}") 

233 

234 def test_jointcalTask_2_visits_constrainedPhotometry_no_rank_update(self): 

235 """Demonstrate that skipping the rank update doesn't substantially affect photometry. 

236 """ 

237 configOptions, metrics = self.setup_jointcalTask_2_visits_constrainedPhotometry() 

238 configOptions['photometryDoRankUpdate'] = False 

239 

240 # The constrainedPhotometry model is not purely linear, so a small 

241 # change in final chi2 is possible. 

242 metrics['photometry_final_chi2'] = 10896.76 

243 metrics['photometry_final_ndof'] = 2787 

244 

245 self._runJointcalTest(configOptions=configOptions, metrics=metrics, 

246 photometryOutputs=self.outputVisits) 

247 

248 def test_jointcalTask_2_visits_constrainedPhotometry_lineSearch(self): 

249 """Activating the line search should only slightly change the chi2. 

250 

251 Activating line search for constrainedPhotometry should result in 

252 nearly the same final fit (the system is somewhat non-linear, so it 

253 may not be exactly the same: check the "Line search scale factor" 

254 lines in the DEBUG log for values that are not ~1 for proof). 

255 """ 

256 configOptions, metrics = self.setup_jointcalTask_2_visits_constrainedPhotometry() 

257 configOptions['allowLineSearch'] = True 

258 

259 metrics['photometry_final_chi2'] = 10000.87 

260 metrics['photometry_final_ndof'] = 2773 

261 

262 self._runJointcalTest(configOptions=configOptions, metrics=metrics, 

263 photometryOutputs=self.outputVisits) 

264 

265 def test_jointcalTask_2_visits_constrainedMagnitude_no_astrometry(self): 

266 configOptions, metrics = self.setup_jointcalTask_2_visits_constrainedPhotometry() 

267 configOptions['photometryModel'] = "constrainedMagnitude" 

268 

269 # The resulting fit should be close to the constrainedFlux model: 

270 # there are few CCDs and 2 visits, so there's not a lot of complexity 

271 # in this case to distinguish the flux vs. magnitude models. 

272 metrics['photometry_final_chi2'] = 10276.57 

273 metrics['photometry_final_ndof'] = 2823 

274 

275 self._runJointcalTest(configOptions=configOptions, metrics=metrics, 

276 photometryOutputs=self.outputVisits) 

277 

278 def test_jointcalTask_2_visits_constrainedFlux_pedestal(self): 

279 """Test that forcing a systematic flux error results in a lower chi2. 

280 """ 

281 configOptions, metrics = self.setup_jointcalTask_2_visits_constrainedPhotometry() 

282 # median fluxErr/flux in ps1 is 0.11, so we have to make this bigger 

283 # than that to actually allow more slop in the fit. 

284 configOptions['photometryErrorPedestal'] = 0.2 

285 

286 # Final chi2 is much lower, because all sources contribute more error. 

287 metrics['photometry_final_chi2'] = 3355.96 

288 # ndof may change; slightly different likelihood contours, and fewer 

289 # reference sources rejected. 

290 metrics['photometry_final_ndof'] = 3262 

291 

292 self._runJointcalTest(configOptions=configOptions, metrics=metrics, 

293 photometryOutputs=self.outputVisits) 

294 

295 def test_jointcalTask_2_visits_constrainedMagnitude_pedestal(self): 

296 """Test that forcing a systematic flux error results in a lower chi2. 

297 """ 

298 configOptions, metrics = self.setup_jointcalTask_2_visits_constrainedPhotometry() 

299 configOptions['photometryModel'] = "constrainedMagnitude" 

300 # median fluxErr/flux in ps1 is 0.11, so we have to make this bigger 

301 # than that to actually allow more slop in the fit. 

302 configOptions['photometryErrorPedestal'] = 0.2 

303 

304 # Final chi2 is much lower, because all sources contribute more error. 

305 metrics['photometry_final_chi2'] = 3165.87 

306 # ndof may change; slightly different likelihood contours, and fewer 

307 # reference sources rejected. 

308 metrics['photometry_final_ndof'] = 3224 

309 

310 self._runJointcalTest(configOptions=configOptions, metrics=metrics, 

311 photometryOutputs=self.outputVisits) 

312 

313 

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

315 pass 

316 

317 

318if __name__ == "__main__": 318 ↛ 319line 318 didn't jump to line 319, because the condition on line 318 was never true

319 lsst.utils.tests.init() 

320 unittest.main()