Coverage for tests/test_jointcal_cfht.py: 25%

Shortcuts 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

136 statements  

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 do_plot = False 

54 

55 # center of the cfht validation_data catalog 

56 center = lsst.geom.SpherePoint(214.884832, 52.6622199, lsst.geom.degrees) 

57 radius = 3*lsst.geom.degrees 

58 

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

60 all_visits = [849375, 850587] 

61 

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

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

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

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

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

67 

68 self.setUp_base(center, radius, 

69 input_dir=input_dir, 

70 all_visits=all_visits, 

71 do_plot=do_plot, 

72 where=where, 

73 inputCollections=inputCollections, 

74 refcats=refcats, 

75 refcatPath=input_dir, 

76 log_level="DEBUG") 

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._runGen3Jointcal("lsst.obs.cfht.MegaPrime", "MegaPrime", 

104 configOptions=configOptions, metrics=metrics) 

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 

126 metrics = {'astrometry_collected_refStars': 867, 

127 'astrometry_prepared_refStars': 323, 

128 'astrometry_matched_fittedStars': 2399, 

129 'astrometry_prepared_fittedStars': 1255, 

130 'astrometry_prepared_ccdImages': 12, 

131 'astrometry_final_chi2': 1192.41, 

132 'astrometry_final_ndof': 2004, 

133 } 

134 

135 return configOptions, metrics 

136 

137 def test_jointcalTask_2_visits_constrainedAstrometry_no_photometry(self): 

138 configOptions, metrics = self.setup_jointcalTask_2_visits_constrainedAstrometry() 

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

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

141 with tempfile.TemporaryDirectory() as tempdir: 

142 configOptions['debugOutputPath'] = tempdir 

143 

144 self._runGen3Jointcal("lsst.obs.cfht.MegaPrime", "MegaPrime", 

145 configOptions=configOptions, metrics=metrics) 

146 

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

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

149 

150 def test_jointcalTask_2_visits_constrainedAstrometry_no_rank_update(self): 

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

152 """ 

153 configOptions, metrics = self.setup_jointcalTask_2_visits_constrainedAstrometry() 

154 metrics['astrometry_final_chi2'] = 1095.70 

155 metrics['astrometry_final_ndof'] = 1740 

156 

157 configOptions['astrometryDoRankUpdate'] = False 

158 

159 self._runGen3Jointcal("lsst.obs.cfht.MegaPrime", "MegaPrime", 

160 configOptions=configOptions, metrics=metrics) 

161 

162 def test_jointcalTask_2_visits_constrainedAstrometry_4sigma_outliers(self): 

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

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

165 """ 

166 configOptions, metrics = self.setup_jointcalTask_2_visits_constrainedAstrometry() 

167 configOptions['outlierRejectSigma'] = 4 

168 metrics['astrometry_final_chi2'] = 732.79 

169 metrics['astrometry_final_ndof'] = 1814 

170 

171 self._runGen3Jointcal("lsst.obs.cfht.MegaPrime", "MegaPrime", 

172 configOptions=configOptions, metrics=metrics) 

173 

174 def test_jointcalTask_2_visits_constrainedAstrometry_astrometryOutlierRelativeTolerance(self): 

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

176 1% for the astrometryOutlierRelativeTolerance will result in higher chi2 

177 and ndof. 

178 """ 

179 configOptions, metrics = self.setup_jointcalTask_2_visits_constrainedAstrometry() 

180 configOptions['astrometryOutlierRelativeTolerance'] = 0.01 

181 metrics['astrometry_final_chi2'] = 1542.03 

182 metrics['astrometry_final_ndof'] = 2076 

183 

184 self._runGen3Jointcal("lsst.obs.cfht.MegaPrime", "MegaPrime", 

185 configOptions=configOptions, metrics=metrics) 

186 

187 def test_jointcalTask_2_visits_constrainedAstrometry_astrometryReferenceUncertainty_smaller(self): 

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

189 configOptions, metrics = self.setup_jointcalTask_2_visits_constrainedAstrometry() 

190 astrometryRefErrConfig = os.path.join(lsst.utils.getPackageDir('jointcal'), 

191 'tests/config/astrometryReferenceErr-config.py') 

192 metrics['astrometry_final_chi2'] = 1213.51 

193 metrics['astrometry_final_ndof'] = 2126 

194 

195 self._runGen3Jointcal("lsst.obs.cfht.MegaPrime", "MegaPrime", configFiles=[astrometryRefErrConfig], 

196 configOptions=configOptions, metrics=metrics) 

197 

198 def test_jointcalTask_2_visits_constrainedAstrometry_astrometryReferenceUncertainty_None_fails(self): 

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

200 that have no position errors. 

201 """ 

202 configOptions, metrics = self.setup_jointcalTask_2_visits_constrainedAstrometry() 

203 badRefErrConfig = os.path.join(lsst.utils.getPackageDir('jointcal'), 

204 'tests/config/astrometryReferenceErr-None-config.py') 

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

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

207 self._runGen3Jointcal("lsst.obs.cfht.MegaPrime", "MegaPrime", configFiles=[badRefErrConfig], 

208 configOptions=configOptions, metrics=metrics) 

209 

210 def setup_jointcalTask_2_visits_constrainedPhotometry(self): 

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

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

213 """ 

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

215 

216 metrics = {'photometry_collected_refStars': 11569, 

217 'photometry_prepared_refStars': 2302, 

218 'photometry_matched_fittedStars': 2399, 

219 'photometry_prepared_fittedStars': 2317, 

220 'photometry_prepared_ccdImages': 12, 

221 'photometry_final_chi2': 11264.28, 

222 'photometry_final_ndof': 2821 

223 } 

224 return configOptions, metrics 

225 

226 def test_jointcalTask_2_visits_constrainedPhotometry_no_astrometry(self): 

227 configOptions, metrics = self.setup_jointcalTask_2_visits_constrainedPhotometry() 

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

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

230 with tempfile.TemporaryDirectory() as tempdir: 

231 configOptions['debugOutputPath'] = tempdir 

232 

233 self._runGen3Jointcal("lsst.obs.cfht.MegaPrime", "MegaPrime", 

234 configOptions=configOptions, metrics=metrics) 

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

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

237 

238 def test_jointcalTask_2_visits_constrainedPhotometry_no_rank_update(self): 

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

240 """ 

241 configOptions, metrics = self.setup_jointcalTask_2_visits_constrainedPhotometry() 

242 configOptions['photometryDoRankUpdate'] = False 

243 

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

245 # change in final chi2 is possible. 

246 metrics['photometry_final_chi2'] = 10896.76 

247 metrics['photometry_final_ndof'] = 2787 

248 

249 self._runGen3Jointcal("lsst.obs.cfht.MegaPrime", "MegaPrime", 

250 configOptions=configOptions, metrics=metrics) 

251 

252 def test_jointcalTask_2_visits_constrainedPhotometry_lineSearch(self): 

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

254 

255 Activating line search for constrainedPhotometry should result in 

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

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

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

259 """ 

260 configOptions, metrics = self.setup_jointcalTask_2_visits_constrainedPhotometry() 

261 configOptions['allowLineSearch'] = True 

262 

263 metrics['photometry_final_chi2'] = 10000.87 

264 metrics['photometry_final_ndof'] = 2773 

265 

266 self._runGen3Jointcal("lsst.obs.cfht.MegaPrime", "MegaPrime", 

267 configOptions=configOptions, metrics=metrics) 

268 

269 def test_jointcalTask_2_visits_constrainedMagnitude_no_astrometry(self): 

270 configOptions, metrics = self.setup_jointcalTask_2_visits_constrainedPhotometry() 

271 configOptions['photometryModel'] = "constrainedMagnitude" 

272 

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

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

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

276 metrics['photometry_final_chi2'] = 10276.57 

277 metrics['photometry_final_ndof'] = 2823 

278 

279 self._runGen3Jointcal("lsst.obs.cfht.MegaPrime", "MegaPrime", 

280 configOptions=configOptions, metrics=metrics) 

281 

282 def test_jointcalTask_2_visits_constrainedFlux_pedestal(self): 

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

284 """ 

285 configOptions, metrics = self.setup_jointcalTask_2_visits_constrainedPhotometry() 

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

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

288 configOptions['photometryErrorPedestal'] = 0.2 

289 

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

291 metrics['photometry_final_chi2'] = 3355.96 

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

293 # reference sources rejected. 

294 metrics['photometry_final_ndof'] = 3262 

295 

296 self._runGen3Jointcal("lsst.obs.cfht.MegaPrime", "MegaPrime", 

297 configOptions=configOptions, metrics=metrics) 

298 

299 def test_jointcalTask_2_visits_constrainedMagnitude_pedestal(self): 

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

301 """ 

302 configOptions, metrics = self.setup_jointcalTask_2_visits_constrainedPhotometry() 

303 configOptions['photometryModel'] = "constrainedMagnitude" 

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

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

306 configOptions['photometryErrorPedestal'] = 0.2 

307 

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

309 metrics['photometry_final_chi2'] = 3165.87 

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

311 # reference sources rejected. 

312 metrics['photometry_final_ndof'] = 3224 

313 

314 self._runGen3Jointcal("lsst.obs.cfht.MegaPrime", "MegaPrime", 

315 configOptions=configOptions, metrics=metrics) 

316 

317 

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

319 pass 

320 

321 

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

323 lsst.utils.tests.init() 

324 unittest.main()