Coverage for tests/test_trailedSourceFilter.py: 27%
68 statements
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-07 03:43 -0700
« prev ^ index » next coverage.py v7.5.1, created at 2024-05-07 03:43 -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 unittest
23import os
24import numpy as np
25import pandas as pd
27import lsst.utils.tests
28import lsst.utils as utils
29from lsst.ap.association import TrailedSourceFilterTask
30from lsst.ap.association.transformDiaSourceCatalog import UnpackApdbFlags
33class TestTrailedSourceFilterTask(unittest.TestCase):
35 def setUp(self):
36 """Create sets of diaSources.
38 The trail lengths of the dia sources are 0, 5.5, 11, 16.5, 21.5
39 arcseconds.
40 """
41 # Create an instance of random generator with fixed seed.
42 rng = np.random.default_rng(1234)
44 scatter = 0.1 / 3600
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, "diaObjectId": 0, "trailLength": 5.5*idx,
50 "flags": 0}
51 for idx in range(self.nSources)])
52 self.exposure_time = 30.0
54 # For use only with testing the edge flag
55 self.edgeDiaSources = pd.DataFrame(data=[
56 {"ra": 0.04*idx + scatter*rng.uniform(-1, 1),
57 "dec": 0.04*idx + scatter*rng.uniform(-1, 1),
58 "diaSourceId": idx, "diaObjectId": 0, "trailLength": 0,
59 "flags": 0}
60 for idx in range(self.nSources)])
62 flagMap = os.path.join(utils.getPackageDir("ap_association"), "data/association-flag-map.yaml")
63 unpacker = UnpackApdbFlags(flagMap, "DiaSource")
64 bitMask = unpacker.makeFlagBitMask(["ext_trailedSources_Naive_flag_edge"])
65 # Flag two sources as "trailed on the edge".
66 self.edgeDiaSources.loc[[1, 4], "flags"] |= bitMask
68 def test_run(self):
69 """Run trailedSourceFilterTask with the default max distance.
71 With the default settings and an exposure of 30 seconds, the max trail
72 length is 12.5 arcseconds. Two out of five of the diaSources will be
73 filtered out of the final results and put into results.trailedSources.
74 """
75 trailedSourceFilterTask = TrailedSourceFilterTask()
77 results = trailedSourceFilterTask.run(self.diaSources, self.exposure_time)
79 self.assertEqual(len(results.diaSources), 3)
80 np.testing.assert_array_equal(results.diaSources['diaSourceId'].values, [0, 1, 2])
81 np.testing.assert_array_equal(results.longTrailedDiaSources['diaSourceId'].values, [3, 4])
83 def test_run_short_max_trail(self):
84 """Run trailedSourceFilterTask with aggressive trail length cutoff
86 With a max_trail_length config of 0.01 arcseconds/second and an
87 exposure of 30 seconds,the max trail length is 0.3 arcseconds. Only the
88 source with a trail of 0 stays in the catalog and the rest are filtered
89 out and put into results.trailedSources.
90 """
91 config = TrailedSourceFilterTask.ConfigClass()
92 config.max_trail_length = 0.01
93 trailedSourceFilterTask = TrailedSourceFilterTask(config=config)
94 results = trailedSourceFilterTask.run(self.diaSources, self.exposure_time)
96 self.assertEqual(len(results.diaSources), 1)
97 np.testing.assert_array_equal(results.diaSources['diaSourceId'].values, [0])
98 np.testing.assert_array_equal(results.longTrailedDiaSources['diaSourceId'].values, [1, 2, 3, 4])
100 def test_run_no_trails(self):
101 """Run trailedSourceFilterTask with a long trail length so that
102 every source in the catalog is in the final diaSource catalog.
104 With a max_trail_length config of 10 arcseconds/second and an
105 exposure of 30 seconds,the max trail length is 300 arcseconds. All
106 sources in the initial catalog should be in the final diaSource
107 catalog.
108 """
109 config = TrailedSourceFilterTask.ConfigClass()
110 config.max_trail_length = 10.00
111 trailedSourceFilterTask = TrailedSourceFilterTask(config=config)
112 results = trailedSourceFilterTask.run(self.diaSources, self.exposure_time)
114 self.assertEqual(len(results.diaSources), 5)
115 self.assertEqual(len(results.longTrailedDiaSources), 0)
116 np.testing.assert_array_equal(results.diaSources["diaSourceId"].values, [0, 1, 2, 3, 4])
117 np.testing.assert_array_equal(results.longTrailedDiaSources["diaSourceId"].values, [])
119 def test_run_edge(self):
120 """Run trailedSourceFilterTask on a source on the edge.
121 """
122 trailedSourceFilterTask = TrailedSourceFilterTask()
124 results = trailedSourceFilterTask.run(self.edgeDiaSources, self.exposure_time)
126 self.assertEqual(len(results.diaSources), 3)
127 np.testing.assert_array_equal(results.diaSources['diaSourceId'].values, [0, 2, 3])
128 np.testing.assert_array_equal(results.longTrailedDiaSources['diaSourceId'].values, [1, 4])
130 def test_check_dia_source_trail(self):
131 """Test that the DiaSource trail checker is correctly identifying
132 long trails
134 Test that the trail source mask filter returns the expected mask array.
135 """
136 trailedSourceFilterTask = TrailedSourceFilterTask()
137 flag_map = os.path.join(utils.getPackageDir("ap_association"), "data/association-flag-map.yaml")
138 unpacker = UnpackApdbFlags(flag_map, "DiaSource")
139 flags = unpacker.unpack(self.diaSources["flags"], "flags")
140 trailed_source_mask = trailedSourceFilterTask._check_dia_source_trail(self.diaSources,
141 self.exposure_time, flags)
143 np.testing.assert_array_equal(trailed_source_mask, [False, False, False, True, True])
145 flags = unpacker.unpack(self.edgeDiaSources["flags"], "flags")
146 trailed_source_mask = trailedSourceFilterTask._check_dia_source_trail(self.edgeDiaSources,
147 self.exposure_time, flags)
148 np.testing.assert_array_equal(trailed_source_mask, [False, True, False, False, True])
150 # Mixing the flags from edgeDiaSources and diaSources means the mask
151 # will be set using both criteria.
152 trailed_source_mask = trailedSourceFilterTask._check_dia_source_trail(self.diaSources,
153 self.exposure_time, flags)
154 np.testing.assert_array_equal(trailed_source_mask, [False, True, False, True, True])
157class MemoryTester(lsst.utils.tests.MemoryTestCase):
158 pass
161def setup_module(module):
162 lsst.utils.tests.init()
165if __name__ == "__main__": 165 ↛ 166line 165 didn't jump to line 166, because the condition on line 165 was never true
166 lsst.utils.tests.init()
167 unittest.main()