Coverage for tests/test_association_task.py: 23%
63 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-27 03:54 -0700
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-27 03:54 -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/>.
22import numpy as np
23import pandas as pd
24import unittest
26import lsst.geom as geom
27import lsst.utils.tests
29from lsst.ap.association import AssociationTask
32class TestAssociationTask(unittest.TestCase):
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), "dec": 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 "dec": 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 "dec": 0.04*idx,
54 "diaSourceId": idx + 1 + self.nObjects, "diaObjectId": 0}
55 for idx in range(self.nSources)])
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)
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)
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", "dec", "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))
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)
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)
103 def test_score_and_match(self):
104 """Test association between a set of sources and an existing
105 DIAObjectCollection.
106 """
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)
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)
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, "dec"] = np.nan
133 self.diaSources.loc[4, "ra"] = np.nan
134 self.diaSources.loc[4, "dec"] = 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)
140class MemoryTester(lsst.utils.tests.MemoryTestCase):
141 pass
144def setup_module(module):
145 lsst.utils.tests.init()
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()