Coverage for tests / test_loadref_hsc.py: 19%

119 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-05-05 08:41 +0000

1# See COPYRIGHT file at the top of the source tree. 

2# 

3# This file is part of fgcmcal. 

4# 

5# Developed for the LSST Data Management System. 

6# This product includes software developed by the LSST Project 

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

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

9# for details of code ownership. 

10# 

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

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

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

14# (at your option) any later version. 

15# 

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

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

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

19# GNU General Public License for more details. 

20# 

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

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

23"""Test the fgcmcal fgcmLoadReferenceCatalog code with testdata_jointcal/hsc. 

24 

25""" 

26 

27import unittest 

28import os 

29import numpy as np 

30import hpgeom as hpg 

31import esutil 

32import tempfile 

33 

34import lsst.utils 

35import lsst.daf.butler.cli.cliLog 

36import lsst.pipe.tasks 

37from lsst.meas.algorithms import ReferenceObjectLoader, LoadReferenceObjectsConfig 

38 

39import lsst.fgcmcal as fgcmcal 

40 

41import fgcmcalTestBase 

42 

43ROOT = os.path.abspath(os.path.dirname(__file__)) 

44 

45 

46class FgcmLoadReferenceTestHSC(fgcmcalTestBase.FgcmcalTestBase, lsst.utils.tests.TestCase): 

47 @classmethod 

48 def setUpClass(cls): 

49 try: 

50 cls.dataDir = lsst.utils.getPackageDir('testdata_jointcal') 

51 except LookupError: 

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

53 try: 

54 lsst.utils.getPackageDir('obs_subaru') 

55 except LookupError: 

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

57 

58 lsst.daf.butler.cli.cliLog.CliLog.initLog(longlog=False) 

59 

60 cls.testDir = tempfile.mkdtemp(dir=ROOT, prefix="TestFgcm-") 

61 

62 cls._importRepository('lsst.obs.subaru.HyperSuprimeCam', 

63 os.path.join(cls.dataDir, 'hsc/repo'), 

64 os.path.join(cls.dataDir, 'hsc', 'exports.yaml')) 

65 

66 def test_fgcmLoadReference(self): 

67 """ 

68 Test loading of the fgcm reference catalogs. 

69 """ 

70 

71 filterList = ['HSC-R', 'HSC-I'] 

72 

73 config = fgcmcal.FgcmLoadReferenceCatalogConfig() 

74 config.applyColorTerms = True 

75 config.filterMap = {'HSC-R': 'r', 'HSC-I': 'i'} 

76 config.colorterms.data = {} 

77 config.colorterms.data['ps1*'] = lsst.pipe.tasks.colorterms.ColortermDict() 

78 config.colorterms.data['ps1*'].data = {} 

79 config.colorterms.data['ps1*'].data['HSC-R'] = lsst.pipe.tasks.colorterms.Colorterm() 

80 config.colorterms.data['ps1*'].data['HSC-R'].primary = 'r' 

81 config.colorterms.data['ps1*'].data['HSC-R'].secondary = 'i' 

82 config.colorterms.data['ps1*'].data['HSC-R'].c0 = -0.000144 

83 config.colorterms.data['ps1*'].data['HSC-R'].c1 = 0.001369 

84 config.colorterms.data['ps1*'].data['HSC-R'].c2 = -0.008380 

85 config.colorterms.data['ps1*'].data['HSC-I'] = lsst.pipe.tasks.colorterms.Colorterm() 

86 config.colorterms.data['ps1*'].data['HSC-I'].primary = 'i' 

87 config.colorterms.data['ps1*'].data['HSC-I'].secondary = 'z' 

88 config.colorterms.data['ps1*'].data['HSC-I'].c0 = 0.000643 

89 config.colorterms.data['ps1*'].data['HSC-I'].c1 = -0.130078 

90 config.colorterms.data['ps1*'].data['HSC-I'].c2 = -0.006855 

91 

92 refCatName = 'ps1_pv3_3pi_20170110' 

93 

94 butler = lsst.daf.butler.Butler(self.repo, instrument='HSC', collections=['HSC/testdata', 

95 'refcats/gen2']) 

96 refs = set(butler.registry.queryDatasets(refCatName)) 

97 dataIds = [butler.registry.expandDataId(ref.dataId) for ref in refs] 

98 refCats = [butler.getDeferred(ref) for ref in refs] 

99 

100 refConfig = LoadReferenceObjectsConfig() 

101 refConfig.filterMap = config.filterMap 

102 

103 refObjLoader = ReferenceObjectLoader(dataIds=dataIds, 

104 refCats=refCats, 

105 name=refCatName, 

106 config=refConfig) 

107 

108 loadCat = fgcmcal.FgcmLoadReferenceCatalogTask(refObjLoader=refObjLoader, 

109 refCatName=refCatName, 

110 config=config) 

111 

112 ra = 337.656174 

113 dec = 0.823595 

114 rad = 0.1 

115 

116 refCat = loadCat.getFgcmReferenceStarsSkyCircle(ra, dec, rad, filterList) 

117 

118 # Check the number of mags and ranges 

119 self.assertEqual(len(filterList), refCat['refMag'].shape[1]) 

120 self.assertEqual(len(filterList), refCat['refMagErr'].shape[1]) 

121 self.assertLess(np.max(refCat['refMag'][:, 0]), 99.1) 

122 self.assertLess(np.max(refCat['refMag'][:, 1]), 99.1) 

123 self.assertLess(np.max(refCat['refMagErr'][:, 0]), 99.1) 

124 self.assertLess(np.max(refCat['refMagErr'][:, 1]), 99.1) 

125 test, = np.where((refCat['refMag'][:, 0] < 30.0) 

126 & (refCat['refMag'][:, 1] < 30.0)) 

127 self.assertGreater(test.size, 0) 

128 

129 # Check the separations from the center 

130 self.assertLess(np.max(esutil.coords.sphdist(ra, dec, refCat['ra'], refCat['dec'])), rad) 

131 

132 # And load a healpixel 

133 nside = 256 

134 pixel = 387520 

135 

136 refCat = loadCat.getFgcmReferenceStarsHealpix(nside, pixel, filterList) 

137 

138 ipring = hpg.angle_to_pixel(nside, refCat['ra'], refCat['dec'], nest=False) 

139 self.assertEqual(pixel, np.max(ipring)) 

140 self.assertEqual(pixel, np.min(ipring)) 

141 

142 def test_fgcmLoadReferenceOtherFilters(self): 

143 """ 

144 Test loading of the fgcm reference catalogs using unmatched filter names. 

145 """ 

146 

147 filterList = ['HSC-R2', 'HSC-I2'] 

148 

149 config = fgcmcal.FgcmLoadReferenceCatalogConfig() 

150 config.applyColorTerms = True 

151 config.filterMap = {'HSC-R2': 'r', 'HSC-I2': 'i'} 

152 config.colorterms.data = {} 

153 config.colorterms.data['ps1*'] = lsst.pipe.tasks.colorterms.ColortermDict() 

154 config.colorterms.data['ps1*'].data = {} 

155 config.colorterms.data['ps1*'].data['HSC-R2'] = lsst.pipe.tasks.colorterms.Colorterm() 

156 config.colorterms.data['ps1*'].data['HSC-R2'].primary = 'r' 

157 config.colorterms.data['ps1*'].data['HSC-R2'].secondary = 'i' 

158 config.colorterms.data['ps1*'].data['HSC-R2'].c0 = -0.000032 

159 config.colorterms.data['ps1*'].data['HSC-R2'].c1 = -0.002866 

160 config.colorterms.data['ps1*'].data['HSC-R2'].c2 = -0.012638 

161 config.colorterms.data['ps1*'].data['HSC-I2'] = lsst.pipe.tasks.colorterms.Colorterm() 

162 config.colorterms.data['ps1*'].data['HSC-I2'].primary = 'i' 

163 config.colorterms.data['ps1*'].data['HSC-I2'].secondary = 'z' 

164 config.colorterms.data['ps1*'].data['HSC-I2'].c0 = 0.001625 

165 config.colorterms.data['ps1*'].data['HSC-I2'].c1 = -0.200406 

166 config.colorterms.data['ps1*'].data['HSC-I2'].c2 = -0.013666 

167 

168 refCatName = 'ps1_pv3_3pi_20170110' 

169 

170 butler = lsst.daf.butler.Butler(self.repo, instrument='HSC', collections=['HSC/testdata', 

171 'refcats/gen2']) 

172 refs = set(butler.registry.queryDatasets(refCatName)) 

173 dataIds = [butler.registry.expandDataId(ref.dataId) for ref in refs] 

174 refCats = [butler.getDeferred(ref) for ref in refs] 

175 

176 refConfig = LoadReferenceObjectsConfig() 

177 refConfig.filterMap = config.filterMap 

178 

179 refObjLoader = ReferenceObjectLoader(dataIds=dataIds, 

180 refCats=refCats, 

181 name=refCatName, 

182 config=refConfig) 

183 

184 loadCat = fgcmcal.FgcmLoadReferenceCatalogTask(refObjLoader=refObjLoader, 

185 refCatName=refCatName, 

186 config=config) 

187 

188 ra = 337.656174 

189 dec = 0.823595 

190 rad = 0.1 

191 

192 refCat = loadCat.getFgcmReferenceStarsSkyCircle(ra, dec, rad, filterList) 

193 

194 self.assertEqual(len(filterList), refCat['refMag'].shape[1]) 

195 self.assertEqual(len(filterList), refCat['refMagErr'].shape[1]) 

196 test, = np.where((refCat['refMag'][:, 0] < 30.0) 

197 & (refCat['refMag'][:, 1] < 30.0)) 

198 self.assertGreater(test.size, 0) 

199 

200 

201class TestMemory(lsst.utils.tests.MemoryTestCase): 

202 pass 

203 

204 

205def setup_module(module): 

206 lsst.utils.tests.init() 

207 

208 

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

210 lsst.utils.tests.init() 

211 unittest.main()