Coverage for tests/test_trailedSourceFilter.py: 27%
68 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-02 04:31 -0700
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-02 04:31 -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 rng = np.random.default_rng(1234)
42 scatter = 0.1 / 3600
43 self.nSources = 5
44 self.diaSources = pd.DataFrame(data=[
45 {"ra": 0.04*idx + scatter*rng.uniform(-1, 1),
46 "dec": 0.04*idx + scatter*rng.uniform(-1, 1),
47 "diaSourceId": idx, "diaObjectId": 0, "trailLength": 5.5*idx,
48 "flags": 0}
49 for idx in range(self.nSources)])
50 self.exposure_time = 30.0
52 # For use only with testing the edge flag
53 self.edgeDiaSources = pd.DataFrame(data=[
54 {"ra": 0.04*idx + scatter*rng.uniform(-1, 1),
55 "dec": 0.04*idx + scatter*rng.uniform(-1, 1),
56 "diaSourceId": idx, "diaObjectId": 0, "trailLength": 0,
57 "flags": 0}
58 for idx in range(self.nSources)])
60 flagMap = os.path.join(utils.getPackageDir("ap_association"), "data/association-flag-map.yaml")
61 unpacker = UnpackApdbFlags(flagMap, "DiaSource")
62 bitMask = unpacker.makeFlagBitMask(["ext_trailedSources_Naive_flag_edge"])
63 # Flag two sources as "trailed on the edge".
64 self.edgeDiaSources.loc[[1, 4], "flags"] |= bitMask
66 def test_run(self):
67 """Run trailedSourceFilterTask with the default max distance.
69 With the default settings and an exposure of 30 seconds, the max trail
70 length is 12.5 arcseconds. Two out of five of the diaSources will be
71 filtered out of the final results and put into results.trailedSources.
72 """
73 trailedSourceFilterTask = TrailedSourceFilterTask()
75 results = trailedSourceFilterTask.run(self.diaSources, self.exposure_time)
77 self.assertEqual(len(results.diaSources), 3)
78 np.testing.assert_array_equal(results.diaSources['diaSourceId'].values, [0, 1, 2])
79 np.testing.assert_array_equal(results.longTrailedDiaSources['diaSourceId'].values, [3, 4])
81 def test_run_short_max_trail(self):
82 """Run trailedSourceFilterTask with aggressive trail length cutoff
84 With a max_trail_length config of 0.01 arcseconds/second and an
85 exposure of 30 seconds,the max trail length is 0.3 arcseconds. Only the
86 source with a trail of 0 stays in the catalog and the rest are filtered
87 out and put into results.trailedSources.
88 """
89 config = TrailedSourceFilterTask.ConfigClass()
90 config.max_trail_length = 0.01
91 trailedSourceFilterTask = TrailedSourceFilterTask(config=config)
92 results = trailedSourceFilterTask.run(self.diaSources, self.exposure_time)
94 self.assertEqual(len(results.diaSources), 1)
95 np.testing.assert_array_equal(results.diaSources['diaSourceId'].values, [0])
96 np.testing.assert_array_equal(results.longTrailedDiaSources['diaSourceId'].values, [1, 2, 3, 4])
98 def test_run_no_trails(self):
99 """Run trailedSourceFilterTask with a long trail length so that
100 every source in the catalog is in the final diaSource catalog.
102 With a max_trail_length config of 10 arcseconds/second and an
103 exposure of 30 seconds,the max trail length is 300 arcseconds. All
104 sources in the initial catalog should be in the final diaSource
105 catalog.
106 """
107 config = TrailedSourceFilterTask.ConfigClass()
108 config.max_trail_length = 10.00
109 trailedSourceFilterTask = TrailedSourceFilterTask(config=config)
110 results = trailedSourceFilterTask.run(self.diaSources, self.exposure_time)
112 self.assertEqual(len(results.diaSources), 5)
113 self.assertEqual(len(results.longTrailedDiaSources), 0)
114 np.testing.assert_array_equal(results.diaSources["diaSourceId"].values, [0, 1, 2, 3, 4])
115 np.testing.assert_array_equal(results.longTrailedDiaSources["diaSourceId"].values, [])
117 def test_run_edge(self):
118 """Run trailedSourceFilterTask on a source on the edge.
119 """
120 trailedSourceFilterTask = TrailedSourceFilterTask()
122 results = trailedSourceFilterTask.run(self.edgeDiaSources, self.exposure_time)
124 self.assertEqual(len(results.diaSources), 3)
125 np.testing.assert_array_equal(results.diaSources['diaSourceId'].values, [0, 2, 3])
126 np.testing.assert_array_equal(results.longTrailedDiaSources['diaSourceId'].values, [1, 4])
128 def test_check_dia_source_trail(self):
129 """Test that the DiaSource trail checker is correctly identifying
130 long trails
132 Test that the trail source mask filter returns the expected mask array.
133 """
134 trailedSourceFilterTask = TrailedSourceFilterTask()
135 flag_map = os.path.join(utils.getPackageDir("ap_association"), "data/association-flag-map.yaml")
136 unpacker = UnpackApdbFlags(flag_map, "DiaSource")
137 flags = unpacker.unpack(self.diaSources["flags"], "flags")
138 trailed_source_mask = trailedSourceFilterTask._check_dia_source_trail(self.diaSources,
139 self.exposure_time, flags)
141 np.testing.assert_array_equal(trailed_source_mask, [False, False, False, True, True])
143 flags = unpacker.unpack(self.edgeDiaSources["flags"], "flags")
144 trailed_source_mask = trailedSourceFilterTask._check_dia_source_trail(self.edgeDiaSources,
145 self.exposure_time, flags)
146 np.testing.assert_array_equal(trailed_source_mask, [False, True, False, False, True])
148 # Mixing the flags from edgeDiaSources and diaSources means the mask
149 # will be set using both criteria.
150 trailed_source_mask = trailedSourceFilterTask._check_dia_source_trail(self.diaSources,
151 self.exposure_time, flags)
152 np.testing.assert_array_equal(trailed_source_mask, [False, True, False, True, True])
155class MemoryTester(lsst.utils.tests.MemoryTestCase):
156 pass
159def setup_module(module):
160 lsst.utils.tests.init()
163if __name__ == "__main__": 163 ↛ 164line 163 didn't jump to line 164, because the condition on line 163 was never true
164 lsst.utils.tests.init()
165 unittest.main()