Coverage for tests/test_match_tract_catalog.py: 96%

67 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-07-23 09:30 +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 

26from lsst.meas.astrom import ConvertCatalogCoordinatesConfig 

27from lsst.pipe.tasks.match_tract_catalog import MatchTractCatalogConfig, MatchTractCatalogTask 

28from lsst.pipe.tasks.match_tract_catalog_probabilistic import MatchTractCatalogProbabilisticTask 

29from lsst.skymap.discreteSkyMap import DiscreteSkyMap 

30 

31from astropy.table import Table 

32import numpy as np 

33 

34 

35def _error_format(column): 

36 return f'{column}Err' 

37 

38 

39class MatchTractCatalogTaskTestCase(lsst.utils.tests.TestCase): 

40 """Test matching with some arbitrary mock data. 

41 

42 This test is largely copied from diff_matched_tract_catalog, which 

43 implemented outputting a matched catalog separately from this task. 

44 """ 

45 def setUp(self): 

46 # test wrapping from 0 to 360 

47 ra_cen = 0. 

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

49 dec_cen = -30. 

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

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

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

53 

54 coord_format = ConvertCatalogCoordinatesConfig 

55 zeropoint = coord_format.mag_zeropoint_ref.default 

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

57 # Percent error in measurement 

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

59 # Absolute error 

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

61 err_coord = np.full_like(eps_coord, 0.02) 

62 eps_coord *= err_coord 

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

64 

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

66 

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

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

69 

70 column_ra_ref = coord_format.column_ref_coord1.default 

71 column_dec_ref = coord_format.column_ref_coord2.default 

72 column_ra_target = coord_format.column_target_coord1.default 

73 column_dec_target = coord_format.column_target_coord2.default 

74 

75 column_ra_target_err, column_dec_target_err = [ 

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

77 ] 

78 

79 data_ref = { 

80 column_ra_ref: ra[::-1], 

81 column_dec_ref: dec[::-1], 

82 columns_flux[0]: fluxes[0][::-1], 

83 columns_flux[1]: fluxes[1][::-1], 

84 } 

85 self.catalog_ref = Table(data=data_ref) 

86 

87 data_target = { 

88 column_ra_target: ra + eps_coord, 

89 column_dec_target: dec + eps_coord, 

90 column_ra_target_err: err_coord, 

91 column_dec_target_err: err_coord, 

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

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

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

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

96 "detect_isPrimary": flags, 

97 "merge_peak_sky": ~flags, 

98 } 

99 self.catalog_target = Table(data=data_target) 

100 

101 config = MatchTractCatalogConfig( 

102 coord_unit="deg", 

103 output_matched_catalog=True, 

104 refcat_sharding_type="none", 

105 target_sharding_type="none", 

106 ) 

107 mtc = config.match_tract_catalog 

108 mtc.retarget(MatchTractCatalogProbabilisticTask) 

109 mtc.columns_ref_flux = [columns_flux[0], columns_flux[0]] 

110 mtc.columns_ref_meas = [column_ra_ref, column_dec_ref, columns_flux[0], columns_flux[0]] 

111 mtc.columns_target_meas = [ 

112 column_ra_target, column_dec_target, columns_flux[0], columns_flux[0], 

113 ] 

114 mtc.columns_target_err = [ 

115 column_ra_target_err, column_dec_target_err, columns_flux_err[0], columns_flux_err[0], 

116 ] 

117 config.validate() 

118 self.config = config 

119 self.skymap = DiscreteSkyMap( 

120 DiscreteSkyMap.ConfigClass(raList=[ra_cen], decList=[dec_cen], radiusList=[1.]) 

121 ) 

122 self.wcs = self.skymap[0].wcs 

123 

124 def test_MatchTractCatalogTask(self): 

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

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

127 task = MatchTractCatalogTask(None, config=self.config) 

128 task._add_tract_column_to_catalogs(self.catalog_ref, self.catalog_target, self.skymap) 

129 result = task.run( 

130 catalog_ref=self.catalog_ref, 

131 catalog_target=self.catalog_target, 

132 wcs=self.wcs, 

133 ) 

134 columns_result = list(result.cat_output_matched.columns) 

135 columns_expect = list(columns_target) + [ 

136 "tract", "patch", "match_candidate", "match_distance", "match_distanceErr", 

137 ] 

138 prefix = task.diff_matched_catalog.config.column_matched_prefix_ref 

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

140 columns_expect.extend(( 

141 f"{prefix}{col}" for col in ("tract", "patch", "flux_total", "match_candidate") 

142 )) 

143 self.assertListEqual(columns_expect, columns_result) 

144 

145 

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

147 pass 

148 

149 

150def setup_module(module): 

151 lsst.utils.tests.init() 

152 

153 

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

155 lsst.utils.tests.init() 

156 unittest.main()