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

24 

25from astropy import units as u 

26import numpy as np 

27 

28from lsst.daf.butler import Butler 

29import lsst.geom 

30import lsst.pex.config 

31import lsst.utils 

32import lsst.pex.exceptions 

33 

34import jointcalTestBase 

35 

36 

37# for MemoryTestCase 

38def setup_module(module): 

39 lsst.utils.tests.init() 

40 

41 

42class JointcalTestHSC(jointcalTestBase.JointcalTestBase, lsst.utils.tests.TestCase): 

43 

44 @classmethod 

45 def setUpClass(cls): 

46 try: 

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

48 except LookupError: 

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

50 try: 

51 lsst.utils.getPackageDir('obs_subaru') 

52 except LookupError: 

53 raise unittest.SkipTest("obs_subaru not setup") 

54 

55 def setUp(self): 

56 # See Readme for an explanation of these empirical values. 

57 self.dist_rms_absolute = 21e-3*u.arcsecond 

58 self.dist_rms_relative = 7e-3*u.arcsecond 

59 

60 do_plot = False 

61 

62 # center of the hsc validation_data catalog 

63 center = lsst.geom.SpherePoint(337.710899, +0.807006, lsst.geom.degrees) 

64 radius = 0.5*lsst.geom.degrees 

65 

66 input_dir = os.path.join(self.data_dir, 'hsc') 

67 all_visits = [34648, 34690, 34714, 34674, 34670, 36140, 35892, 36192, 36260, 36236] 

68 

69 where = "instrument='HSC' and tract=9697 and skymap='hsc_rings_v1'" 

70 

71 self.setUp_base(center, radius, 

72 input_dir=input_dir, 

73 all_visits=all_visits, 

74 do_plot=do_plot, 

75 where=where) 

76 

77 test_config = os.path.join(lsst.utils.getPackageDir('jointcal'), 'tests/config/hsc-config.py') 

78 self.configfiles.append(test_config) 

79 

80 def test_jointcalTask_2_visits_simple(self): 

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

82 self.config.astrometryModel = "simple" 

83 self.config.photometryModel = "simpleFlux" 

84 

85 # See Readme for an explanation of these empirical values. 

86 pa1 = 0.016 

87 metrics = {'astrometry_collected_refStars': 568, 

88 'photometry_collected_refStars': 6485, 

89 'astrometry_prepared_refStars': 137, 

90 'photometry_prepared_refStars': 1609, 

91 'astrometry_matched_fittedStars': 2070, 

92 'photometry_matched_fittedStars': 2070, 

93 'astrometry_prepared_fittedStars': 989, 

94 'photometry_prepared_fittedStars': 1731, 

95 'astrometry_prepared_ccdImages': 6, 

96 'photometry_prepared_ccdImages': 6, 

97 'astrometry_final_chi2': 835.473, 

98 'astrometry_final_ndof': 1918, 

99 'photometry_final_chi2': 4997.62, 

100 'photometry_final_ndof': 2188 

101 } 

102 self._testJointcalTask(2, self.dist_rms_relative, self.dist_rms_absolute, pa1, metrics=metrics) 

103 

104 def test_jointcalTask_2_visits_simple_gen3(self): 

105 """Test gen3 butler jointcal.""" 

106 configOptions = {"astrometryModel": "simple", "photometryModel": "simpleFlux"} 

107 where = f" and visit in ({self.all_visits[0]},{self.all_visits[1]})" 

108 

109 metrics = {'astrometry_collected_refStars': 568, 

110 'photometry_collected_refStars': 6485, 

111 'astrometry_prepared_refStars': 137, 

112 'photometry_prepared_refStars': 1609, 

113 'astrometry_matched_fittedStars': 2070, 

114 'photometry_matched_fittedStars': 2070, 

115 'astrometry_prepared_fittedStars': 989, 

116 'photometry_prepared_fittedStars': 1731, 

117 'astrometry_prepared_ccdImages': 6, 

118 'photometry_prepared_ccdImages': 6, 

119 'astrometry_final_chi2': 835.473, 

120 'astrometry_final_ndof': 1918, 

121 'photometry_final_chi2': 4997.62, 

122 'photometry_final_ndof': 2188 

123 } 

124 self._runGen3Jointcal("lsst.obs.subaru.HyperSuprimeCam", "HSC", whereSuffix=where, 

125 configOptions=configOptions, metrics=metrics) 

126 # TODO DM-28863: this does not currently test anything other than the code 

127 # running without raising and that it writes non-empty output. 

128 butler = Butler(self.repo, collections=['HSC/testdata/jointcal']) 

129 

130 def check_output(visit, detectors): 

131 """Check that there is something for each detector, and only 

132 entries for the correct detectors.""" 

133 dataId = {'visit': visit, 'instrument': 'HSC', 'tract': 9697} 

134 

135 catalog = butler.get('jointcalPhotoCalibCatalog', dataId) 

136 for record in catalog: 

137 self.assertIsInstance(record.getPhotoCalib(), lsst.afw.image.PhotoCalib, 

138 msg=f"visit {visit}: {record}") 

139 np.testing.assert_array_equal(catalog['id'], detectors) 

140 

141 catalog = butler.get('jointcalSkyWcsCatalog', dataId) 

142 for record in catalog: 

143 self.assertIsInstance(record.getWcs(), lsst.afw.geom.SkyWcs, 

144 msg=f"visit {visit}: {record}") 

145 np.testing.assert_array_equal(catalog['id'], detectors) 

146 

147 check_output(34648, [51, 59, 67]) 

148 check_output(34690, [48, 56, 64]) 

149 

150 def test_jointcalTask_10_visits_simple_astrometry_no_photometry(self): 

151 """Test all 10 visits with different filters. 

152 Testing photometry doesn't make sense for this currently. 

153 """ 

154 

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

156 self.config.astrometryModel = "simple" 

157 self.config.doPhotometry = False 

158 self.jointcalStatistics.do_photometry = False 

159 

160 # See Readme for an explanation of these empirical values. 

161 dist_rms_absolute = 23e-3*u.arcsecond 

162 dist_rms_relative = 13e-3*u.arcsecond 

163 pa1 = None 

164 metrics = {'astrometry_collected_refStars': 1316, 

165 'astrometry_prepared_refStars': 318, 

166 'astrometry_matched_fittedStars': 5860, 

167 'astrometry_prepared_fittedStars': 3568, 

168 'astrometry_prepared_ccdImages': 30, 

169 'astrometry_final_chi2': 10225.31, 

170 'astrometry_final_ndof': 18576, 

171 } 

172 self._testJointcalTask(10, dist_rms_relative, dist_rms_absolute, pa1, metrics=metrics) 

173 

174 def setup_jointcalTask_2_visits_simplePhotometry(self): 

175 """Set default values for the simplePhotometry tests, and make it so 

176 the differences between each test and the defaults are more obvious. 

177 """ 

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

179 self.config.photometryModel = "simpleFlux" 

180 self.config.doAstrometry = False 

181 self.jointcalStatistics.do_astrometry = False 

182 

183 # See Readme for an explanation of these empirical values. 

184 pa1 = 0.016 

185 metrics = {'photometry_collected_refStars': 6485, 

186 'photometry_prepared_refStars': 1609, 

187 'photometry_matched_fittedStars': 2070, 

188 'photometry_prepared_fittedStars': 1731, 

189 'photometry_prepared_ccdImages': 6, 

190 'photometry_final_chi2': 4997.62, 

191 'photometry_final_ndof': 2188 

192 } 

193 return pa1, metrics 

194 

195 def test_jointcalTask_2_visits_simpleFlux(self): 

196 pa1, metrics = self.setup_jointcalTask_2_visits_simplePhotometry() 

197 self._testJointcalTask(2, None, None, pa1, metrics=metrics) 

198 

199 def test_jointcalTask_2_visits_simpleMagnitude(self): 

200 pa1, metrics = self.setup_jointcalTask_2_visits_simplePhotometry() 

201 self.config.photometryModel = "simpleMagnitude" 

202 metrics['photometry_final_chi2'] = 5236.91 

203 metrics['photometry_final_ndof'] = 2200 

204 

205 self._testJointcalTask(2, None, None, pa1, metrics=metrics) 

206 

207 def test_jointcalTask_2_visits_simpleMagnitude_colorterms(self): 

208 """Test that colorterms are applied and change the fit.""" 

209 pa1, metrics = self.setup_jointcalTask_2_visits_simplePhotometry() 

210 self.config.photometryModel = "simpleMagnitude" 

211 test_config = os.path.join(lsst.utils.getPackageDir('jointcal'), 

212 'tests/config/hsc-colorterms-config.py') 

213 self.configfiles.append(test_config) 

214 

215 # slightly fewer refstars because they each need the specific filters required by the colorterms 

216 metrics['photometry_collected_refStars'] = 6478 

217 # Final chi2 should be different, but I don't have an a-priori reason 

218 # to expect it to be larger or smaller. 

219 metrics['photometry_final_chi2'] = 5181.25 

220 metrics['photometry_final_ndof'] = 2197 

221 

222 self._testJointcalTask(2, None, None, pa1, metrics=metrics) 

223 

224 def test_jointcalTask_2_visits_simpleMagnitude_colorterms_no_library(self): 

225 """Fail Config validation if the color term library isn't defined.""" 

226 pa1, metrics = self.setup_jointcalTask_2_visits_simplePhotometry() 

227 test_config = os.path.join(lsst.utils.getPackageDir('jointcal'), 

228 'tests/config/hsc-colorterms_no_library-config.py') 

229 self.configfiles.append(test_config) 

230 

231 with self.assertRaises(lsst.pex.config.FieldValidationError): 

232 self._testJointcalTask(2, None, None, pa1) 

233 

234 def test_JointcalTask_2_visits_simple_astrometry_no_photometry(self): 

235 """Test turning off fitting photometry.""" 

236 metrics = {'astrometry_collected_refStars': 568, 

237 'astrometry_prepared_refStars': 137, 

238 'astrometry_matched_fittedStars': 2070, 

239 'astrometry_prepared_fittedStars': 989, 

240 'astrometry_prepared_ccdImages': 6, 

241 'astrometry_final_chi2': 835.473, 

242 'astrometry_final_ndof': 1918, 

243 } 

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

245 self.config.astrometryModel = "simple" 

246 self.config.doPhotometry = False 

247 self.jointcalStatistics.do_photometry = False 

248 

249 data_refs = self._testJointcalTask(2, self.dist_rms_relative, self.dist_rms_absolute, 

250 None, metrics=metrics) 

251 

252 for data_ref in data_refs: 

253 with self.assertRaises(lsst.daf.persistence.butlerExceptions.NoResults): 

254 data_ref.get('jointcal_photoCalib') 

255 

256 def test_jointcalTask_2_visits_simple_astrometry_no_photometry_match_cut_10(self): 

257 """A larger matching radius will result in more associated fittedStars, 

258 and a somewhat worse final fit because stars that should not have been 

259 associated, were. 

260 """ 

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

262 self.config.astrometryModel = "simple" 

263 self.config.matchCut = 10.0 # TODO: once DM-6885 is fixed, we need to put `*lsst.geom.arcseconds` 

264 self.config.doPhotometry = False 

265 self.jointcalStatistics.do_photometry = False 

266 

267 # Slightly larger absolute astrometry RMS because of the larger matching radius 

268 dist_rms_absolute = 23e-3*u.arcsecond 

269 metrics = {'astrometry_collected_refStars': 568, 

270 'astrometry_prepared_refStars': 211, 

271 'astrometry_matched_fittedStars': 2070, 

272 'astrometry_prepared_fittedStars': 1042, 

273 'astrometry_prepared_ccdImages': 6, 

274 'astrometry_final_chi2': 819.608, 

275 'astrometry_final_ndof': 1872, 

276 } 

277 pa1 = None 

278 self._testJointcalTask(2, self.dist_rms_relative, dist_rms_absolute, pa1, metrics=metrics) 

279 

280 def test_jointcalTask_3_visits_simple_astrometry_no_photometry(self): 

281 """3 visit, default config to compare with min_measurements_3 test.""" 

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

283 self.config.astrometryModel = "simple" 

284 self.config.minMeasurements = 2 

285 self.config.doPhotometry = False 

286 self.jointcalStatistics.do_photometry = False 

287 

288 # More visits and slightly worse relative and absolute rms. 

289 dist_rms_relative = 8.3e-3*u.arcsecond 

290 dist_rms_absolute = 24e-3*u.arcsecond 

291 metrics = {'astrometry_collected_refStars': 1038, 

292 'astrometry_prepared_refStars': 209, 

293 'astrometry_matched_fittedStars': 3199, 

294 'astrometry_prepared_fittedStars': 1282, 

295 'astrometry_prepared_ccdImages': 9, 

296 'astrometry_final_chi2': 1221.63, 

297 'astrometry_final_ndof': 2892, 

298 } 

299 pa1 = None 

300 self._testJointcalTask(3, dist_rms_relative, dist_rms_absolute, pa1, metrics=metrics) 

301 

302 def test_jointcalTask_3_visits_simple_astrometry_no_photometry_min_measurements_3(self): 

303 """Raising min_measurements to 3 will reduce the number of selected 

304 fitted stars (and thus the chisq and Ndof), but should not change the 

305 other values.""" 

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

307 self.config.minMeasurements = 3 

308 self.config.astrometryModel = "simple" 

309 self.config.doPhotometry = False 

310 self.jointcalStatistics.do_photometry = False 

311 

312 # More visits and slightly worse relative and absolute rms. 

313 dist_rms_relative = 11e-3*u.arcsecond 

314 dist_rms_absolute = 24e-3*u.arcsecond 

315 metrics = {'astrometry_collected_refStars': 1038, 

316 'astrometry_prepared_refStars': 209, 

317 'astrometry_matched_fittedStars': 3199, 

318 'astrometry_prepared_fittedStars': 432, 

319 'astrometry_prepared_ccdImages': 9, 

320 'astrometry_final_chi2': 567.433, 

321 'astrometry_final_ndof': 1286, 

322 } 

323 pa1 = None 

324 self._testJointcalTask(3, dist_rms_relative, dist_rms_absolute, pa1, metrics=metrics) 

325 

326 

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

328 pass 

329 

330 

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

332 lsst.utils.tests.init() 

333 unittest.main()