Coverage for tests/test_matchFakes.py: 31%
56 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-29 10:48 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-29 10:48 +0000
1#
2# This file is part of pipe_tasks.
3#
4# Developed for the LSST Data Management System.
5# This product includes software developed by the LSST Project
6# (http://www.lsst.org).
7# See the COPYRIGHT file at the top-level directory of this distribution
8# for details of code ownership.
9#
10# This program is free software: you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation, either version 3 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program. If not, see <http://www.gnu.org/licenses/>.
22#
24import numpy as np
25import pandas as pd
26import unittest
27import uuid
29import lsst.sphgeom as sphgeom
30import lsst.geom as geom
31import lsst.meas.base.tests as measTests
32import lsst.skymap as skyMap
33import lsst.utils.tests
35from lsst.pipe.tasks.matchFakes import MatchFakesTask, MatchFakesConfig
38class TestMatchFakes(lsst.utils.tests.TestCase):
40 def setUp(self):
41 """Create fake data to use in the tests.
42 """
43 self.bbox = geom.Box2I(geom.Point2I(0, 0),
44 geom.Extent2I(1024, 1153))
45 dataset = measTests.TestDataset(self.bbox)
46 self.exposure = dataset.exposure
48 simpleMapConfig = skyMap.discreteSkyMap.DiscreteSkyMapConfig()
49 simpleMapConfig.raList = [dataset.exposure.getWcs().getSkyOrigin().getRa().asDegrees()]
50 simpleMapConfig.decList = [dataset.exposure.getWcs().getSkyOrigin().getDec().asDegrees()]
51 simpleMapConfig.radiusList = [0.1]
53 self.simpleMap = skyMap.DiscreteSkyMap(simpleMapConfig)
54 self.tractId = 0
55 bCircle = self.simpleMap.generateTract(self.tractId).getInnerSkyPolygon().getBoundingCircle()
56 bCenter = sphgeom.LonLat(bCircle.getCenter())
57 bRadius = bCircle.getOpeningAngle().asRadians()
58 targetSources = 10000
59 self.sourceDensity = (targetSources
60 / (bCircle.getArea() * (180 / np.pi) ** 2))
61 self.rng = np.random.default_rng(1234)
63 self.fakeCat = pd.DataFrame({
64 "fakeId": [uuid.uuid4().int & (1 << 64) - 1 for n in range(targetSources)],
65 # Quick-and-dirty values for testing
66 "ra": bCenter.getLon().asRadians() + bRadius * (2.0 * self.rng.random(targetSources) - 1.0),
67 "dec": bCenter.getLat().asRadians() + bRadius * (2.0 * self.rng.random(targetSources) - 1.0),
68 "isVisitSource": np.concatenate([np.ones(targetSources//2, dtype="bool"),
69 np.zeros(targetSources - targetSources//2, dtype="bool")]),
70 "isTemplateSource": np.concatenate([np.zeros(targetSources//2, dtype="bool"),
71 np.ones(targetSources - targetSources//2, dtype="bool")]),
72 **{band: self.rng.uniform(20, 30, size=targetSources)
73 for band in {"u", "g", "r", "i", "z", "y"}},
74 "DiskHalfLightRadius": np.ones(targetSources, dtype="float"),
75 "BulgeHalfLightRadius": np.ones(targetSources, dtype="float"),
76 "disk_n": np.ones(targetSources, dtype="float"),
77 "bulge_n": np.ones(targetSources, dtype="float"),
78 "a_d": np.ones(targetSources, dtype="float"),
79 "a_b": np.ones(targetSources, dtype="float"),
80 "b_d": np.ones(targetSources, dtype="float"),
81 "b_b": np.ones(targetSources, dtype="float"),
82 "pa_disk": np.ones(targetSources, dtype="float"),
83 "pa_bulge": np.ones(targetSources, dtype="float"),
84 "sourceType": targetSources * ["star"],
85 })
87 self.inExp = np.zeros(len(self.fakeCat), dtype=bool)
88 bbox = geom.Box2D(self.exposure.getBBox())
89 for idx, row in self.fakeCat.iterrows():
90 coord = geom.SpherePoint(row["ra"],
91 row["dec"],
92 geom.radians)
93 cent = self.exposure.getWcs().skyToPixel(coord)
94 self.inExp[idx] = bbox.contains(cent)
96 tmpCat = self.fakeCat[self.inExp].iloc[:int(self.inExp.sum() / 2)]
97 extraColumnData = self.rng.integers(0, 100, size=len(tmpCat))
98 self.sourceCat = pd.DataFrame(
99 data={"ra": np.degrees(tmpCat["ra"]),
100 "dec": np.degrees(tmpCat["dec"]),
101 "diaObjectId": np.arange(1, len(tmpCat) + 1, dtype=int),
102 "band": "g",
103 "diaSourceId": np.arange(1, len(tmpCat) + 1, dtype=int),
104 "extraColumn": extraColumnData})
105 self.sourceCat.set_index(["diaObjectId", "band", "extraColumn"],
106 drop=False,
107 inplace=True)
109 def testProcessFakes(self):
110 """Test the run method.
111 """
112 matchFakesConfig = MatchFakesConfig()
113 matchFakesConfig.matchDistanceArcseconds = 0.1
114 matchFakes = MatchFakesTask(config=matchFakesConfig)
115 result = matchFakes._processFakes(self.fakeCat,
116 self.exposure,
117 self.sourceCat)
118 self.assertEqual(self.inExp.sum(), len(result.matchedDiaSources))
119 self.assertEqual(
120 len(self.sourceCat),
121 np.sum(np.isfinite(result.matchedDiaSources["extraColumn"])))
123 def testTrimCat(self):
124 """Test that the correct number of sources are in the ccd area.
125 """
126 matchTask = MatchFakesTask()
127 result = matchTask._trimFakeCat(self.fakeCat, self.exposure)
128 self.assertEqual(len(result), self.inExp.sum())
131class MemoryTester(lsst.utils.tests.MemoryTestCase):
132 pass
135def setup_module(module):
136 lsst.utils.tests.init()
139if __name__ == "__main__": 139 ↛ 140line 139 didn't jump to line 140, because the condition on line 139 was never true
140 lsst.utils.tests.init()
141 unittest.main()