Coverage for tests/test_matchApFakes.py : 27%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1#
2# This file is part of ap_pipe.
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 shutil
27import tempfile
28import unittest
30import lsst.daf.butler.tests as butlerTests
31import lsst.geom as geom
32import lsst.meas.base.tests as measTests
33from lsst.pipe.base import testUtils
34import lsst.skymap as skyMap
35import lsst.utils.tests
37from lsst.ap.pipe.matchApFakes import MatchApFakesTask, MatchApFakesConfig
38from lsst.ap.pipe.createApFakes import CreateRandomApFakesTask, CreateRandomApFakesConfig
41class TestMatchApFakes(lsst.utils.tests.TestCase):
43 def setUp(self):
44 """Create fake data to use in the tests.
45 """
46 self.bbox = geom.Box2I(geom.Point2I(0, 0),
47 geom.Extent2I(1024, 1153))
48 dataset = measTests.TestDataset(self.bbox)
49 self.exposure = dataset.exposure
51 simpleMapConfig = skyMap.discreteSkyMap.DiscreteSkyMapConfig()
52 simpleMapConfig.raList = [dataset.exposure.getWcs().getSkyOrigin().getRa().asDegrees()]
53 simpleMapConfig.decList = [dataset.exposure.getWcs().getSkyOrigin().getDec().asDegrees()]
54 simpleMapConfig.radiusList = [0.1]
56 self.simpleMap = skyMap.DiscreteSkyMap(simpleMapConfig)
57 self.tractId = 0
58 bCircle = self.simpleMap.generateTract(self.tractId).getInnerSkyPolygon().getBoundingCircle()
59 targetSources = 10000
60 self.sourceDensity = (targetSources
61 / (bCircle.getArea() * (180 / np.pi) ** 2))
62 self.rng = np.random.default_rng(1234)
64 fakesConfig = CreateRandomApFakesConfig()
65 fakesConfig.fraction = 0.0
66 fakesConfig.fakeDensity = self.sourceDensity
67 fakesTask = CreateRandomApFakesTask(config=fakesConfig)
68 self.fakeCat = fakesTask.run(self.tractId, self.simpleMap).fakeCat
70 self.inExp = np.zeros(len(self.fakeCat), dtype=bool)
71 bbox = geom.Box2D(self.exposure.getBBox())
72 for idx, row in self.fakeCat.iterrows():
73 coord = geom.SpherePoint(row[fakesConfig.raColName],
74 row[fakesConfig.decColName],
75 geom.radians)
76 cent = self.exposure.getWcs().skyToPixel(coord)
77 self.inExp[idx] = bbox.contains(cent)
79 tmpCat = self.fakeCat[self.inExp].iloc[:int(self.inExp.sum() / 2)]
80 extraColumnData = self.rng.integers(0, 100, size=len(tmpCat))
81 self.sourceCat = pd.DataFrame(
82 data={"ra": np.degrees(tmpCat[fakesTask.config.raColName]),
83 "decl": np.degrees(tmpCat[fakesTask.config.decColName]),
84 "diaObjectId": np.arange(1, len(tmpCat) + 1, dtype=int),
85 "filterName": "g",
86 "diaSourceId": np.arange(1, len(tmpCat) + 1, dtype=int),
87 "extraColumn": extraColumnData})
88 self.sourceCat.set_index(["diaObjectId", "filterName", "extraColumn"],
89 drop=False,
90 inplace=True)
92 def testRunQuantum(self):
93 """Test the run quantum method with a gen3 butler.
94 """
95 root = tempfile.mkdtemp()
96 dimensions = {"instrument": ["notACam"],
97 "skymap": ["deepCoadd_skyMap"],
98 "tract": [0, 42],
99 "visit": [1234, 4321],
100 "detector": [25, 26]}
101 testRepo = butlerTests.makeTestRepo(root, dimensions)
102 matchTask = MatchApFakesTask()
103 connections = matchTask.config.ConnectionsClass(
104 config=matchTask.config)
106 dataId = {"instrument": "notACam",
107 "skymap": "deepCoadd_skyMap",
108 "tract": 0,
109 "visit": 1234,
110 "detector": 25}
111 butlerTests.addDatasetType(
112 testRepo,
113 connections.fakeCat.name,
114 connections.fakeCat.dimensions,
115 connections.fakeCat.storageClass)
116 butlerTests.addDatasetType(
117 testRepo,
118 connections.diffIm.name,
119 connections.diffIm.dimensions,
120 connections.diffIm.storageClass)
121 butlerTests.addDatasetType(
122 testRepo,
123 connections.associatedDiaSources.name,
124 connections.associatedDiaSources.dimensions,
125 connections.associatedDiaSources.storageClass)
126 butlerTests.addDatasetType(
127 testRepo,
128 connections.matchedDiaSources.name,
129 connections.matchedDiaSources.dimensions,
130 connections.matchedDiaSources.storageClass)
131 butler = butlerTests.makeTestCollection(testRepo)
133 butler.put(self.fakeCat,
134 connections.fakeCat.name,
135 {"tract": dataId["tract"],
136 "skymap": dataId["skymap"]})
137 butler.put(self.exposure,
138 connections.diffIm.name,
139 {"instrument": dataId["instrument"],
140 "visit": dataId["visit"],
141 "detector": dataId["detector"]})
142 butler.put(self.sourceCat,
143 connections.associatedDiaSources.name,
144 {"instrument": dataId["instrument"],
145 "visit": dataId["visit"],
146 "detector": dataId["detector"]})
148 quantum = testUtils.makeQuantum(
149 matchTask, butler, dataId,
150 {key: dataId for key in {"fakeCat", "diffIm", "associatedDiaSources", "matchedDiaSources"}})
151 run = testUtils.runTestQuantum(matchTask, butler, quantum)
152 # Actual input dataset omitted for simplicity
153 run.assert_called_once()
154 shutil.rmtree(root, ignore_errors=True)
156 def testRun(self):
157 """Test the run method.
158 """
159 matchFakesConfig = MatchApFakesConfig()
160 matchFakesConfig.matchDistanceArcseconds = 0.1
161 matchFakes = MatchApFakesTask(config=matchFakesConfig)
162 result = matchFakes.run(self.fakeCat,
163 self.exposure,
164 self.sourceCat)
165 self.assertEqual(self.inExp.sum(), len(result.matchedDiaSources))
166 self.assertEqual(
167 len(self.sourceCat),
168 np.sum(np.isfinite(result.matchedDiaSources["extraColumn"])))
170 def testTrimCat(self):
171 """Test that the correct number of sources are in the ccd area.
172 """
173 matchTask = MatchApFakesTask()
174 result = matchTask._trimFakeCat(self.fakeCat, self.exposure)
175 self.assertEqual(len(result), self.inExp.sum())
178class MemoryTester(lsst.utils.tests.MemoryTestCase):
179 pass
182def setup_module(module):
183 lsst.utils.tests.init()
186if __name__ == "__main__": 186 ↛ 187line 186 didn't jump to line 187, because the condition on line 186 was never true
187 lsst.utils.tests.init()
188 unittest.main()