Coverage for tests / test_diff_matched_tract_catalog.py: 23%

77 statements  

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

1# This file is part of pipe_tasks. 

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 

22 

23import unittest 

24import lsst.utils.tests 

25 

26import lsst.afw.geom as afwGeom 

27from lsst.meas.astrom import ConvertCatalogCoordinatesConfig 

28from lsst.pipe.tasks.diff_matched_tract_catalog import ( 

29 DiffMatchedTractCatalogConfig, DiffMatchedTractCatalogTask, MatchedCatalogFluxesConfig, 

30) 

31 

32from astropy.table import Table 

33import numpy as np 

34 

35 

36def _error_format(column): 

37 return f'{column}Err' 

38 

39 

40class DiffMatchedTractCatalogTaskTestCase(lsst.utils.tests.TestCase): 

41 """DiffMatchedTractCatalogTask test case.""" 

42 def setUp(self): 

43 ra_cen = 180. 

44 ra = ra_cen + np.array([-5.1, -2.2, 0., 3.1, -3.2, 2.01, -4.1])/60 

45 dec_cen = 0. 

46 dec = dec_cen + np.array([-4.15, 1.15, 0, 2.15, -7.15, -3.05, 5.7])/60 

47 mag_g = np.array([23., 24., 25., 25.5, 26., 24.7, 23.3]) 

48 mag_r = mag_g + [0.5, -0.2, -0.8, -0.5, -1.5, 0.8, -0.4] 

49 

50 coord_format = ConvertCatalogCoordinatesConfig 

51 zeropoint = coord_format.mag_zeropoint_ref.default 

52 fluxes = tuple(10**(-0.4*(mag - zeropoint)) for mag in (mag_g, mag_r)) 

53 # Percent error in measurement 

54 err_flux = np.array((0.02, 0.015, -0.035, 0.02, -0.04, 0.06, 0.01)) 

55 # Absolute error 

56 eps_coord = np.array((2.3, 0.6, -1.7, 3.6, -2.4, 55.0, -40.8)) 

57 err_coord = np.full_like(eps_coord, 0.02) 

58 eps_coord *= err_coord 

59 flags = np.ones_like(eps_coord, dtype=bool) 

60 

61 bands = ['g', 'r'] 

62 

63 columns_flux = [f'flux_{band}' for band in bands] 

64 columns_flux_err = [_error_format(column) for column in columns_flux] 

65 

66 column_ra_ref = coord_format.column_ref_coord1.default 

67 column_dec_ref = coord_format.column_ref_coord2.default 

68 column_ra_target = coord_format.column_target_coord1.default 

69 column_dec_target = coord_format.column_target_coord2.default 

70 

71 column_ra_target_err, column_dec_target_err = [ 

72 _error_format(col) for col in (column_ra_target, column_dec_target) 

73 ] 

74 

75 n_points = len(ra) 

76 n_unmatched = 2 

77 n_matched = n_points - n_unmatched 

78 # Reorder some indices to make arbitrary differences 

79 idx_ref = np.empty(n_points, dtype=int) 

80 idx_ref[:n_matched] = np.arange(n_matched)[::-1] 

81 idx_ref[n_matched:] = np.arange(n_matched, n_points) 

82 data_ref = { 

83 column_ra_ref: ra[idx_ref], 

84 column_dec_ref: dec[idx_ref], 

85 columns_flux[0]: fluxes[0][idx_ref], 

86 columns_flux[1]: fluxes[1][idx_ref], 

87 } 

88 self.catalog_ref = Table(data=data_ref) 

89 

90 data_target = { 

91 column_ra_target: ra + eps_coord, 

92 column_dec_target: dec + eps_coord, 

93 column_ra_target_err: err_coord, 

94 column_dec_target_err: err_coord, 

95 columns_flux[0]: fluxes[0]*(1 + err_flux), 

96 columns_flux[1]: fluxes[1]*(1 + err_flux), 

97 columns_flux_err[0]: np.sqrt(fluxes[0]), 

98 columns_flux_err[1]: np.sqrt(fluxes[1]), 

99 DiffMatchedTractCatalogConfig.columns_target_select_true.default[0]: flags, 

100 DiffMatchedTractCatalogConfig.columns_target_select_false.default[0]: ~flags, 

101 } 

102 self.catalog_target = Table(data=data_target) 

103 

104 # Make the last two rows unmatched (we set eps_coord very large) 

105 match_row = np.arange(len(ra))[::-1] - n_unmatched 

106 self.catalog_match_ref = Table(data={ 

107 'match_candidate': flags, 

108 'match_row': match_row, 

109 }) 

110 

111 self.catalog_match_target = Table(data={ 

112 'match_candidate': flags, 

113 'match_row': match_row, 

114 }) 

115 

116 columns_flux_configs = { 

117 band: MatchedCatalogFluxesConfig( 

118 column_ref_flux=columns_flux[idx], 

119 columns_target_flux=[columns_flux[idx]], 

120 columns_target_flux_err=[columns_flux_err[idx]], 

121 ) 

122 for idx, band in enumerate(bands) 

123 } 

124 self.config = DiffMatchedTractCatalogConfig( 

125 columns_target_coord_err=[column_ra_target_err, column_dec_target_err], 

126 columns_flux=columns_flux_configs, 

127 ) 

128 

129 self.wcs = afwGeom.makeSkyWcs(crpix=lsst.geom.Point2D(9000, 9000), 

130 crval=lsst.geom.SpherePoint(ra_cen, dec_cen, lsst.geom.degrees), 

131 cdMatrix=afwGeom.makeCdMatrix(scale=0.2*lsst.geom.arcseconds)) 

132 

133 def tearDown(self): 

134 del self.catalog_ref 

135 del self.catalog_target 

136 del self.catalog_match_ref 

137 del self.catalog_match_target 

138 del self.config 

139 del self.wcs 

140 

141 def test_DiffMatchedTractCatalogTask(self): 

142 # These tables will have columns added to them in run 

143 columns_ref, columns_target = (list(x.columns) for x in (self.catalog_ref, self.catalog_target)) 

144 task = DiffMatchedTractCatalogTask(config=self.config) 

145 result = task.run( 

146 catalog_ref=self.catalog_ref, 

147 catalog_target=self.catalog_target, 

148 catalog_match_ref=self.catalog_match_ref, 

149 catalog_match_target=self.catalog_match_target, 

150 wcs=self.wcs, 

151 ) 

152 columns_result = list(result.cat_matched.columns) 

153 columns_expect = list(columns_target) + ["match_candidate", "match_distance", "match_distanceErr"] 

154 prefix = task.config.column_matched_prefix_ref 

155 columns_expect.extend((f"{prefix}{col}" for col in columns_ref)) 

156 columns_expect.append(f"{prefix}match_candidate") 

157 self.assertListEqual(columns_expect, columns_result) 

158 

159 def test_spherical(self): 

160 task = DiffMatchedTractCatalogTask(config=self.config) 

161 task.config.coord_format.coords_spherical = not task.config.coord_format.coords_spherical 

162 task.run( 

163 catalog_ref=self.catalog_ref, 

164 catalog_target=self.catalog_target, 

165 catalog_match_ref=self.catalog_match_ref, 

166 catalog_match_target=self.catalog_match_target, 

167 wcs=self.wcs, 

168 ) 

169 

170 

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

172 pass 

173 

174 

175def setup_module(module): 

176 lsst.utils.tests.init() 

177 

178 

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

180 lsst.utils.tests.init() 

181 unittest.main()