Coverage for tests/test_trailedSourceFilter.py: 28%

65 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-01-31 12:13 +0000

1# This file is part of ap_association. 

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 

22import unittest 

23import os 

24import numpy as np 

25import pandas as pd 

26 

27import lsst.utils.tests 

28import lsst.utils as utils 

29from lsst.ap.association import TrailedSourceFilterTask 

30from lsst.ap.association.transformDiaSourceCatalog import UnpackApdbFlags 

31 

32 

33class TestTrailedSourceFilterTask(unittest.TestCase): 

34 

35 def setUp(self): 

36 """Create sets of diaSources. 

37 

38 The trail lengths of the dia sources are 0, 5.5, 11, 16.5, 21.5 

39 arcseconds. 

40 """ 

41 rng = np.random.default_rng(1234) 

42 scatter = 0.1 / 3600 

43 self.nSources = 5 

44 self.diaSources = pd.DataFrame(data=[ 

45 {"ra": 0.04*idx + scatter*rng.uniform(-1, 1), 

46 "dec": 0.04*idx + scatter*rng.uniform(-1, 1), 

47 "diaSourceId": idx, "diaObjectId": 0, "trailLength": 5.5*idx, 

48 "flags": 0} 

49 for idx in range(self.nSources)]) 

50 self.exposure_time = 30.0 

51 

52 # For use only with testing the edge flag 

53 self.edgeDiaSources = pd.DataFrame(data=[ 

54 {"ra": 0.04*idx + scatter*rng.uniform(-1, 1), 

55 "dec": 0.04*idx + scatter*rng.uniform(-1, 1), 

56 "diaSourceId": idx, "diaObjectId": 0, "trailLength": 0, 

57 "flags": 0} 

58 for idx in range(self.nSources)]) 

59 

60 self.edgeDiaSources.loc[[1, 4], 'flags'] = np.power(2, 27) 

61 

62 def test_run(self): 

63 """Run trailedSourceFilterTask with the default max distance. 

64 

65 With the default settings and an exposure of 30 seconds, the max trail 

66 length is 12.5 arcseconds. Two out of five of the diaSources will be 

67 filtered out of the final results and put into results.trailedSources. 

68 """ 

69 trailedSourceFilterTask = TrailedSourceFilterTask() 

70 

71 results = trailedSourceFilterTask.run(self.diaSources, self.exposure_time) 

72 

73 self.assertEqual(len(results.diaSources), 3) 

74 np.testing.assert_array_equal(results.diaSources['diaSourceId'].values, [0, 1, 2]) 

75 np.testing.assert_array_equal(results.longTrailedDiaSources['diaSourceId'].values, [3, 4]) 

76 

77 def test_run_short_max_trail(self): 

78 """Run trailedSourceFilterTask with aggressive trail length cutoff 

79 

80 With a max_trail_length config of 0.01 arcseconds/second and an 

81 exposure of 30 seconds,the max trail length is 0.3 arcseconds. Only the 

82 source with a trail of 0 stays in the catalog and the rest are filtered 

83 out and put into results.trailedSources. 

84 """ 

85 config = TrailedSourceFilterTask.ConfigClass() 

86 config.max_trail_length = 0.01 

87 trailedSourceFilterTask = TrailedSourceFilterTask(config=config) 

88 results = trailedSourceFilterTask.run(self.diaSources, self.exposure_time) 

89 

90 self.assertEqual(len(results.diaSources), 1) 

91 np.testing.assert_array_equal(results.diaSources['diaSourceId'].values, [0]) 

92 np.testing.assert_array_equal(results.longTrailedDiaSources['diaSourceId'].values, [1, 2, 3, 4]) 

93 

94 def test_run_no_trails(self): 

95 """Run trailedSourceFilterTask with a long trail length so that 

96 every source in the catalog is in the final diaSource catalog. 

97 

98 With a max_trail_length config of 10 arcseconds/second and an 

99 exposure of 30 seconds,the max trail length is 300 arcseconds. All 

100 sources in the initial catalog should be in the final diaSource 

101 catalog. 

102 """ 

103 config = TrailedSourceFilterTask.ConfigClass() 

104 config.max_trail_length = 10.00 

105 trailedSourceFilterTask = TrailedSourceFilterTask(config=config) 

106 results = trailedSourceFilterTask.run(self.diaSources, self.exposure_time) 

107 

108 self.assertEqual(len(results.diaSources), 5) 

109 self.assertEqual(len(results.longTrailedDiaSources), 0) 

110 np.testing.assert_array_equal(results.diaSources["diaSourceId"].values, [0, 1, 2, 3, 4]) 

111 np.testing.assert_array_equal(results.longTrailedDiaSources["diaSourceId"].values, []) 

112 

113 def test_run_edge(self): 

114 """Run trailedSourceFilterTask with the default max distance. 

115 filtered out of the final results and put into results.trailedSources. 

116 """ 

117 trailedSourceFilterTask = TrailedSourceFilterTask() 

118 

119 results = trailedSourceFilterTask.run(self.edgeDiaSources, self.exposure_time) 

120 

121 self.assertEqual(len(results.diaSources), 3) 

122 np.testing.assert_array_equal(results.diaSources['diaSourceId'].values, [0, 2, 3]) 

123 np.testing.assert_array_equal(results.longTrailedDiaSources['diaSourceId'].values, [1, 4]) 

124 

125 def test_check_dia_source_trail(self): 

126 """Test that the DiaSource trail checker is correctly identifying 

127 long trails 

128 

129 Test that the trail source mask filter returns the expected mask array. 

130 """ 

131 trailedSourceFilterTask = TrailedSourceFilterTask() 

132 flag_map = os.path.join(utils.getPackageDir("ap_association"), "data/association-flag-map.yaml") 

133 unpacker = UnpackApdbFlags(flag_map, "DiaSource") 

134 flags = unpacker.unpack(self.diaSources["flags"], "flags") 

135 trailed_source_mask = trailedSourceFilterTask._check_dia_source_trail(self.diaSources, 

136 self.exposure_time, flags) 

137 

138 np.testing.assert_array_equal(trailed_source_mask, [False, False, False, True, True]) 

139 

140 flags = unpacker.unpack(self.edgeDiaSources["flags"], "flags") 

141 trailed_source_mask = trailedSourceFilterTask._check_dia_source_trail(self.edgeDiaSources, 

142 self.exposure_time, flags) 

143 np.testing.assert_array_equal(trailed_source_mask, [False, True, False, False, True]) 

144 

145 # Mixing the flags from edgeDiaSources and diaSources means the mask 

146 # will be set using both criteria. 

147 trailed_source_mask = trailedSourceFilterTask._check_dia_source_trail(self.diaSources, 

148 self.exposure_time, flags) 

149 np.testing.assert_array_equal(trailed_source_mask, [False, True, False, True, True]) 

150 

151 

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

153 pass 

154 

155 

156def setup_module(module): 

157 lsst.utils.tests.init() 

158 

159 

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

161 lsst.utils.tests.init() 

162 unittest.main()