Coverage for tests/test_association_task.py: 25%
58 statements
« prev ^ index » next coverage.py v7.2.1, created at 2023-03-12 10:45 +0000
« prev ^ index » next coverage.py v7.2.1, created at 2023-03-12 10:45 +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/>.
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), "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)])
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 for test_obj_id, expected_obj_id in zip(
67 results.diaSources["diaObjectId"].to_numpy(),
68 [0, 1, 2, 3, 4]):
69 self.assertEqual(test_obj_id, expected_obj_id)
71 def test_run_no_existing_objects(self):
72 """Test the run method with a completely empty database.
73 """
74 assocTask = AssociationTask()
75 results = assocTask.run(
76 self.diaSources,
77 pd.DataFrame(columns=["ra", "decl", "diaObjectId"]))
78 self.assertEqual(results.nUpdatedDiaObjects, 0)
79 self.assertEqual(results.nUnassociatedDiaObjects, 0)
80 self.assertTrue(np.all(results.diaSources["diaObjectId"] == 0))
82 def test_associate_sources(self):
83 """Test the performance of the associate_sources method in
84 AssociationTask.
85 """
86 assoc_task = AssociationTask()
87 assoc_result = assoc_task.associate_sources(
88 self.diaObjects, self.diaSources)
90 for test_obj_id, expected_obj_id in zip(
91 assoc_result.diaSources["diaObjectId"].to_numpy(),
92 [0, 1, 2, 3, 4]):
93 self.assertEqual(test_obj_id, expected_obj_id)
95 def test_score_and_match(self):
96 """Test association between a set of sources and an existing
97 DIAObjectCollection.
98 """
100 assoc_task = AssociationTask()
101 score_struct = assoc_task.score(self.diaObjects,
102 self.diaSourceZeroScatter,
103 1.0 * geom.arcseconds)
104 self.assertFalse(np.isfinite(score_struct.scores[0]))
105 for src_idx in range(1, len(self.diaSources)):
106 # Our scores should be extremely close to 0 but not exactly so due
107 # to machine noise.
108 self.assertAlmostEqual(score_struct.scores[src_idx], 0.0,
109 places=16)
111 # After matching each DIAObject should now contain 2 DIASources
112 # except the last DIAObject in this collection which should be
113 # newly created during the matching step and contain only one
114 # DIASource.
115 match_result = assoc_task.match(
116 self.diaObjects, self.diaSources, score_struct)
117 self.assertEqual(match_result.nUpdatedDiaObjects, 4)
118 self.assertEqual(match_result.nUnassociatedDiaObjects, 1)
120 def test_remove_nan_dia_sources(self):
121 """Test removing DiaSources with NaN locations.
122 """
123 self.diaSources.loc[2, "ra"] = np.nan
124 self.diaSources.loc[3, "decl"] = np.nan
125 self.diaSources.loc[4, "ra"] = np.nan
126 self.diaSources.loc[4, "decl"] = np.nan
127 assoc_task = AssociationTask()
128 out_dia_sources = assoc_task.check_dia_source_radec(self.diaSources)
129 self.assertEqual(len(out_dia_sources), len(self.diaSources) - 3)
132class MemoryTester(lsst.utils.tests.MemoryTestCase):
133 pass
136def setup_module(module):
137 lsst.utils.tests.init()
140if __name__ == "__main__": 140 ↛ 141line 140 didn't jump to line 141, because the condition on line 140 was never true
141 lsst.utils.tests.init()
142 unittest.main()