Coverage for tests/test_diff_matched_tract_catalog.py: 24%

77 statements  

« prev     ^ index     » next       coverage.py v6.4, created at 2022-06-02 04:29 -0700

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 

32import numpy as np 

33import os 

34import pandas as pd 

35 

36 

37def _error_format(column): 

38 return f'{column}Err' 

39 

40 

41ROOT = os.path.dirname(__file__) 

42filename_diff_matched = os.path.join(ROOT, "data", "test_diff_matched.txt") 

43 

44 

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

46 """DiffMatchedTractCatalogTask test case.""" 

47 def setUp(self): 

48 ra = np.array([-5.1, -2.2, 0., 3.1, -3.2]) 

49 dec = np.array([-4.15, 1.15, 0, 2.15, -7.15]) 

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

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

52 

53 coord_format = ConvertCatalogCoordinatesConfig 

54 zeropoint = coord_format.mag_zeropoint_ref.default 

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

56 # Percent error in measurement 

57 err_flux = np.array((0.02, 0.015, -0.035, 0.02, -0.04)) 

58 # Absolute error 

59 eps_coord = np.array((2.3, 0.6, -1.7, 3.6, -2.4)) 

60 err_coord = np.full_like(eps_coord, 0.02) 

61 eps_coord *= err_coord 

62 extended_ref = [True, False, True, False, True] 

63 extended_target = [False, False, True, True, True] 

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

65 

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

67 

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

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

70 

71 column_ra_ref = coord_format.column_ref_coord1.default 

72 column_dec_ref = coord_format.column_ref_coord2.default 

73 column_ra_target = coord_format.column_target_coord1.default 

74 column_dec_target = coord_format.column_target_coord2.default 

75 

76 column_ra_target_err, column_dec_target_err = [ 

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

78 ] 

79 

80 data_ref = { 

81 column_ra_ref: ra[::-1], 

82 column_dec_ref: dec[::-1], 

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

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

85 DiffMatchedTractCatalogConfig.column_ref_extended.default: extended_ref, 

86 } 

87 self.catalog_ref = pd.DataFrame(data=data_ref) 

88 

89 data_target = { 

90 column_ra_target: ra + eps_coord, 

91 column_dec_target: dec + eps_coord, 

92 column_ra_target_err: err_coord, 

93 column_dec_target_err: err_coord, 

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

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

96 _error_format(columns_flux[0]): np.sqrt(fluxes[0]), 

97 _error_format(columns_flux[1]): np.sqrt(fluxes[1]), 

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

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

100 DiffMatchedTractCatalogConfig.column_target_extended.default: extended_target, 

101 } 

102 self.catalog_target = pd.DataFrame(data=data_target) 

103 

104 self.catalog_match_ref = pd.DataFrame(data={ 

105 'match_candidate': flags, 

106 'match_row': np.arange(len(ra))[::-1], 

107 }) 

108 

109 self.catalog_match_target = pd.DataFrame(data={ 

110 'match_candidate': flags, 

111 'match_row': np.arange(len(ra))[::-1], 

112 }) 

113 

114 self.diff_matched = np.loadtxt(filename_diff_matched) 

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 

125 self.task = DiffMatchedTractCatalogTask(config=DiffMatchedTractCatalogConfig( 

126 columns_target_coord_err=[column_ra_target_err, column_dec_target_err], 

127 columns_flux=columns_flux_configs, 

128 mag_num_bins=1, 

129 )) 

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

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

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

133 

134 def tearDown(self): 

135 del self.catalog_ref 

136 del self.catalog_target 

137 del self.catalog_match_ref 

138 del self.catalog_match_target 

139 del self.diff_matched 

140 del self.task 

141 del self.wcs 

142 

143 def test_DiffMatchedTractCatalogTask(self): 

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

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

146 result = self.task.run( 

147 catalog_ref=self.catalog_ref, 

148 catalog_target=self.catalog_target, 

149 catalog_match_ref=self.catalog_match_ref, 

150 catalog_match_target=self.catalog_match_target, 

151 wcs=self.wcs, 

152 ) 

153 columns_expect = columns_target 

154 prefix = DiffMatchedTractCatalogConfig.column_matched_prefix_ref.default 

155 columns_expect.append(f'{prefix}index') 

156 columns_expect.extend((f'{prefix}{col}' for col in columns_ref)) 

157 self.assertEqual(columns_expect, list(result.cat_matched.columns)) 

158 

159 row = result.diff_matched.iloc[0].values.astype(float) 

160 # Run to re-save reference data. Will be loaded after this test completes. 

161 resave = False 

162 if resave: 

163 np.savetxt(filename_diff_matched, row) 

164 

165 self.assertEqual(len(row), len(self.diff_matched)) 

166 

167 idx_diff = np.where(row != self.diff_matched)[0] 

168 differences = [(row[d], self.diff_matched[d], result.diff_matched.columns[d]) for d in idx_diff] 

169 self.assertEqual(len(idx_diff), 0, f'Differences (meas, ref, name): {differences}') 

170 

171 

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

173 pass 

174 

175 

176def setup_module(module): 

177 lsst.utils.tests.init() 

178 

179 

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

181 lsst.utils.tests.init() 

182 unittest.main()