Coverage for tests/test_association_task.py: 27%

63 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-27 02:40 -0700

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 

28 

29from lsst.ap.association import AssociationTask 

30 

31 

32class TestAssociationTask(unittest.TestCase): 

33 

34 def setUp(self): 

35 """Create sets of diaSources and diaObjects. 

36 """ 

37 rng = np.random.default_rng(1234) 

38 self.nObjects = 5 

39 scatter = 0.1/3600 

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

41 {"ra": 0.04*(idx + 1), "decl": 0.04*(idx + 1), 

42 "diaObjectId": idx + 1} 

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

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

45 self.nSources = 5 

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

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

48 "decl": 0.04*idx + scatter*rng.uniform(-1, 1), 

49 "diaSourceId": idx + 1 + self.nObjects, "diaObjectId": 0} 

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

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

52 {"ra": 0.04*idx, 

53 "decl": 0.04*idx, 

54 "diaSourceId": idx + 1 + self.nObjects, "diaObjectId": 0} 

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

56 

57 def test_run(self): 

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

59 existing diaObjects. 

60 """ 

61 assocTask = AssociationTask() 

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

63 

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

65 self.assertEqual(results.nUnassociatedDiaObjects, 1) 

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

67 len(self.diaObjects) - 1) 

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

69 for test_obj_id, expected_obj_id in zip( 

70 results.matchedDiaSources["diaObjectId"].to_numpy(), 

71 [1, 2, 3, 4]): 

72 self.assertEqual(test_obj_id, expected_obj_id) 

73 for test_obj_id, expected_obj_id in zip( 

74 results.unAssocDiaSources["diaObjectId"].to_numpy(), 

75 [0]): 

76 self.assertEqual(test_obj_id, expected_obj_id) 

77 

78 def test_run_no_existing_objects(self): 

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

80 """ 

81 assocTask = AssociationTask() 

82 results = assocTask.run( 

83 self.diaSources, 

84 pd.DataFrame(columns=["ra", "decl", "diaObjectId"])) 

85 self.assertEqual(results.nUpdatedDiaObjects, 0) 

86 self.assertEqual(results.nUnassociatedDiaObjects, 0) 

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

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

89 

90 def test_associate_sources(self): 

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

92 AssociationTask. 

93 """ 

94 assoc_task = AssociationTask() 

95 assoc_result = assoc_task.associate_sources( 

96 self.diaObjects, self.diaSources) 

97 

98 for test_obj_id, expected_obj_id in zip( 

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

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

101 self.assertEqual(test_obj_id, expected_obj_id) 

102 

103 def test_score_and_match(self): 

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

105 DIAObjectCollection. 

106 """ 

107 

108 assoc_task = AssociationTask() 

109 score_struct = assoc_task.score(self.diaObjects, 

110 self.diaSourceZeroScatter, 

111 1.0 * geom.arcseconds) 

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

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

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

115 # to machine noise. 

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

117 places=16) 

118 

119 # After matching each DIAObject should now contain 2 DIASources 

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

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

122 # DIASource. 

123 match_result = assoc_task.match( 

124 self.diaObjects, self.diaSources, score_struct) 

125 self.assertEqual(match_result.nUpdatedDiaObjects, 4) 

126 self.assertEqual(match_result.nUnassociatedDiaObjects, 1) 

127 

128 def test_remove_nan_dia_sources(self): 

129 """Test removing DiaSources with NaN locations. 

130 """ 

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

132 self.diaSources.loc[3, "decl"] = np.nan 

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

134 self.diaSources.loc[4, "decl"] = np.nan 

135 assoc_task = AssociationTask() 

136 out_dia_sources = assoc_task.check_dia_source_radec(self.diaSources) 

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

138 

139 

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

141 pass 

142 

143 

144def setup_module(module): 

145 lsst.utils.tests.init() 

146 

147 

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

149 lsst.utils.tests.init() 

150 unittest.main()