Coverage for tests/test_association_task.py: 24%

63 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2024-05-09 11:35 +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 numpy as np 

23import pandas as pd 

24import unittest 

25 

26import lsst.geom as geom 

27import lsst.utils.tests 

28from lsst.ap.association import AssociationTask 

29 

30 

31class TestAssociationTask(unittest.TestCase): 

32 

33 def setUp(self): 

34 """Create sets of diaSources and diaObjects. 

35 """ 

36 rng = np.random.default_rng(1234) 

37 self.nObjects = 5 

38 scatter = 0.1/3600 

39 self.diaObjects = pd.DataFrame(data=[ 

40 {"ra": 0.04*(idx + 1), "dec": 0.04*(idx + 1), 

41 "diaObjectId": idx + 1} 

42 for idx in range(self.nObjects)]) 

43 self.diaObjects.set_index("diaObjectId", drop=False, inplace=True) 

44 self.nSources = 5 

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

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

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

48 "diaSourceId": idx + 1 + self.nObjects, "diaObjectId": 0, "trailLength": 5.5*idx, 

49 "flags": 0} 

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

51 self.diaSourceZeroScatter = pd.DataFrame(data=[ 

52 {"ra": 0.04*idx, 

53 "dec": 0.04*idx, 

54 "diaSourceId": idx + 1 + self.nObjects, "diaObjectId": 0, "trailLength": 5.5*idx, 

55 "flags": 0} 

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

57 

58 def test_run(self): 

59 """Test the full task by associating a set of diaSources to 

60 existing diaObjects. 

61 """ 

62 config = AssociationTask.ConfigClass() 

63 assocTask = AssociationTask(config=config) 

64 results = assocTask.run(self.diaSources, self.diaObjects) 

65 

66 self.assertEqual(results.nUpdatedDiaObjects, len(self.diaObjects) - 1) 

67 self.assertEqual(results.nUnassociatedDiaObjects, 1) 

68 self.assertEqual(len(results.matchedDiaSources), 

69 len(self.diaObjects) - 1) 

70 self.assertEqual(len(results.unAssocDiaSources), 1) 

71 np.testing.assert_array_equal(results.matchedDiaSources["diaObjectId"].values, [1, 2, 3, 4]) 

72 np.testing.assert_array_equal(results.unAssocDiaSources["diaObjectId"].values, [0]) 

73 

74 def test_run_no_existing_objects(self): 

75 """Test the run method with a completely empty database. 

76 """ 

77 assocTask = AssociationTask() 

78 results = assocTask.run( 

79 self.diaSources, 

80 pd.DataFrame(columns=["ra", "dec", "diaObjectId", "trailLength"])) 

81 self.assertEqual(results.nUpdatedDiaObjects, 0) 

82 self.assertEqual(results.nUnassociatedDiaObjects, 0) 

83 self.assertEqual(len(results.matchedDiaSources), 0) 

84 self.assertTrue(np.all(results.unAssocDiaSources["diaObjectId"] == 0)) 

85 

86 def test_associate_sources(self): 

87 """Test the performance of the associate_sources method in 

88 AssociationTask. 

89 """ 

90 assoc_task = AssociationTask() 

91 assoc_result = assoc_task.associate_sources( 

92 self.diaObjects, self.diaSources) 

93 

94 for test_obj_id, expected_obj_id in zip( 

95 assoc_result.diaSources["diaObjectId"].to_numpy(), 

96 [0, 1, 2, 3, 4]): 

97 self.assertEqual(test_obj_id, expected_obj_id) 

98 np.testing.assert_array_equal(assoc_result.diaSources["diaObjectId"].values, [0, 1, 2, 3, 4]) 

99 

100 def test_score_and_match(self): 

101 """Test association between a set of sources and an existing 

102 DIAObjectCollection. 

103 """ 

104 

105 assoc_task = AssociationTask() 

106 score_struct = assoc_task.score(self.diaObjects, 

107 self.diaSourceZeroScatter, 

108 1.0 * geom.arcseconds) 

109 self.assertFalse(np.isfinite(score_struct.scores[0])) 

110 for src_idx in range(1, len(self.diaSources)): 

111 # Our scores should be extremely close to 0 but not exactly so due 

112 # to machine noise. 

113 self.assertAlmostEqual(score_struct.scores[src_idx], 0.0, 

114 places=16) 

115 

116 # After matching each DIAObject should now contain 2 DIASources 

117 # except the last DIAObject in this collection which should be 

118 # newly created during the matching step and contain only one 

119 # DIASource. 

120 match_result = assoc_task.match( 

121 self.diaObjects, self.diaSources, score_struct) 

122 self.assertEqual(match_result.nUpdatedDiaObjects, 4) 

123 self.assertEqual(match_result.nUnassociatedDiaObjects, 1) 

124 

125 def test_remove_nan_dia_sources(self): 

126 """Test removing DiaSources with NaN locations. 

127 """ 

128 self.diaSources.loc[2, "ra"] = np.nan 

129 self.diaSources.loc[3, "dec"] = np.nan 

130 self.diaSources.loc[4, "ra"] = np.nan 

131 self.diaSources.loc[4, "dec"] = np.nan 

132 assoc_task = AssociationTask() 

133 out_dia_sources = assoc_task.check_dia_source_radec(self.diaSources) 

134 self.assertEqual(len(out_dia_sources), len(self.diaSources) - 3) 

135 

136 

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

138 pass 

139 

140 

141def setup_module(module): 

142 lsst.utils.tests.init() 

143 

144 

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

146 lsst.utils.tests.init() 

147 unittest.main()