Coverage for tests/test_match_probabilistic_task.py: 25%

59 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-02-26 09:44 +0000

1# This file is part of meas_astrom. 

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, MatchProbabilisticConfig, MatchProbabilisticTask 

28 

29import astropy.table 

30import numpy as np 

31import pytest 

32 

33 

34class MatchProbabilisticTaskTestCase(lsst.utils.tests.TestCase): 

35 """MatchProbabilisticTask test case.""" 

36 def setUp(self): 

37 # Add an extra target with a small spatial offset at the end 

38 ra = np.array([-0.1, -0.2, 0., 0.1, 0.2, 0.2+1e-10]) 

39 dec = np.array([-0.15, 0.15, 0, 0.15, -0.15, -0.15-1e-10]) 

40 mag_g = np.array([23., 24., 25., 25.5, 26., 27.]) 

41 mag_r = mag_g + [0.5, -0.2, -0.8, -0.5, -1.5, 0.1] 

42 coord_format = ConvertCatalogCoordinatesConfig 

43 zeropoint = coord_format.mag_zeropoint_ref.default 

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

45 eps_coord = np.full_like(ra, lsst.geom.Angle(0.2, lsst.geom.arcseconds).asDegrees()) 

46 eps_flux = np.full_like(eps_coord, 10) 

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

48 name_index = 'index' 

49 

50 columns_flux = ['flux_g', 'flux_r'] 

51 columns_ref_meas = [ 

52 coord_format.column_ref_coord1.default, 

53 coord_format.column_ref_coord2.default, 

54 ] + columns_flux 

55 

56 n_target = len(ra) 

57 # Exclude the extra target from the ref cat 

58 # This makes it a spurious detection and gives this ref object two 

59 # candidates to match to 

60 n_exclude = 1 

61 self.n_exclude = 1 

62 # This removes the last n_exclude elements and then reversing 

63 slice_ref = slice(-n_exclude - 1, None, -1) 

64 data_ref = { 

65 name_index: np.arange(n_target - n_exclude), 

66 columns_ref_meas[0]: ra[slice_ref], 

67 columns_ref_meas[1]: dec[slice_ref], 

68 columns_flux[0]: fluxes[0][slice_ref], 

69 columns_flux[1]: fluxes[1][slice_ref], 

70 } 

71 self.catalog_ref = astropy.table.Table(data=data_ref) 

72 value_unmatched = np.iinfo(data_ref[name_index].dtype).min 

73 self.indices_expected = np.concatenate( 

74 (np.arange(n_target - n_exclude - 1, -1, -1), np.full(self.n_exclude, value_unmatched)) 

75 ) 

76 

77 columns_target_meas = [ 

78 coord_format.column_target_coord1.default, 

79 coord_format.column_target_coord2.default, 

80 ] + columns_flux 

81 columns_target_err = [f'{column}Err' for column in columns_target_meas] 

82 

83 data_target = { 

84 name_index: np.arange(len(ra)), 

85 columns_target_meas[0]: ra + eps_coord, 

86 columns_target_meas[1]: dec + eps_coord, 

87 f'{columns_target_meas[0]}Err': eps_coord, 

88 f'{columns_target_meas[1]}Err': eps_coord, 

89 columns_flux[0]: fluxes[0] + eps_flux, 

90 columns_flux[1]: fluxes[1] - eps_flux, 

91 f'{columns_flux[0]}Err': eps_flux, 

92 f'{columns_flux[1]}Err': eps_flux, 

93 MatchProbabilisticConfig.columns_target_select_true.default[0]: flags, 

94 MatchProbabilisticConfig.columns_target_select_false.default[0]: ~flags, 

95 } 

96 self.catalog_target = astropy.table.Table(data=data_target) 

97 

98 self.task = MatchProbabilisticTask(config=MatchProbabilisticConfig( 

99 columns_ref_flux=columns_flux, 

100 columns_ref_meas=columns_ref_meas, 

101 columns_ref_copy=[name_index], 

102 columns_target_meas=columns_target_meas, 

103 columns_target_err=columns_target_err, 

104 columns_target_copy=[name_index], 

105 )) 

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

107 crval=lsst.geom.SpherePoint(180., 0., lsst.geom.degrees), 

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

109 

110 def tearDown(self): 

111 del self.catalog_ref 

112 del self.catalog_target 

113 del self.indices_expected 

114 del self.n_exclude 

115 del self.task 

116 del self.wcs 

117 

118 def test_MatchProbabilisticTask(self): 

119 for (columns, catalog) in ( 

120 (self.task.columns_in_ref, self.catalog_ref), 

121 (self.task.columns_in_target, self.catalog_target), 

122 ): 

123 self.assertTrue(all((column in catalog.columns for column in columns))) 

124 result = self.task.run( 

125 catalog_ref=self.catalog_ref, 

126 catalog_target=self.catalog_target, 

127 wcs=self.wcs, 

128 logging_n_rows=2, 

129 ) 

130 indices_target = result.cat_output_target["match_row"] 

131 np.testing.assert_array_equal(indices_target, self.indices_expected) 

132 # TODO: Remove pandas support in DM-46523 

133 with pytest.warns(FutureWarning): 

134 result_pd = self.task.run( 

135 catalog_ref=self.catalog_ref.to_pandas(), 

136 catalog_target=self.catalog_target.to_pandas(), 

137 wcs=self.wcs, 

138 logging_n_rows=2, 

139 ) 

140 np.testing.assert_array_equal(indices_target, result_pd.cat_output_target["match_row"]) 

141 

142 

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

144 pass 

145 

146 

147def setup_module(module): 

148 lsst.utils.tests.init() 

149 

150 

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

152 lsst.utils.tests.init() 

153 unittest.main()