Coverage for tests/test_loadReferenceCatalog.py: 22%
136 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-03-08 02:12 -0800
« prev ^ index » next coverage.py v6.5.0, created at 2023-03-08 02:12 -0800
1import unittest
2import copy
4import astropy.units as u
6import lsst.utils.tests
7import lsst.afw.geom
8import lsst.pipe.base as pipeBase
9from lsst.meas.algorithms import (convertReferenceCatalog,
10 getRefFluxField,
11 ReferenceObjectLoader)
12from lsst.pipe.tasks.loadReferenceCatalog import LoadReferenceCatalogConfig, LoadReferenceCatalogTask
13from lsst.pipe.tasks.colorterms import Colorterm, ColortermDict, ColortermLibrary
16synthTerms = ColortermLibrary(data={
17 "synth*": ColortermDict(data={
18 "filter1": Colorterm(primary="ref1", secondary="ref2", c0=0.0, c1=0.01),
19 "filter2": Colorterm(primary="ref2", secondary="ref3", c0=0.0, c1=-0.01),
20 })
21})
24_synthFlux = 100.0
25_synthCenter = lsst.geom.SpherePoint(30, -30, lsst.geom.degrees)
28def setup_module(module):
29 lsst.utils.tests.init()
32class TrivialLoader(ReferenceObjectLoader):
33 """Minimal subclass of LoadReferenceObjectsTask"""
34 def make_synthetic_refcat(self, center, flux):
35 """Make a synthetic reference catalog."""
36 filters = ["ref1", "ref2", "ref3"]
37 schema = convertReferenceCatalog._makeSchema(filters)
38 schema.addField('pm_ra', 'D')
39 schema.addField('pm_dec', 'D')
41 catalog = lsst.afw.table.SimpleCatalog(schema)
42 record = catalog.addNew()
43 record.setCoord(center)
44 record[filters[0] + '_flux'] = flux
45 record[filters[0] + '_fluxErr'] = flux*0.1
46 record[filters[1] + '_flux'] = flux*10
47 record[filters[1] + '_fluxErr'] = flux*10*0.1
48 record[filters[2] + '_flux'] = flux*100
49 record[filters[2] + '_fluxErr'] = flux*100*0.1
50 record['pm_ra'] = 0.0
51 record['pm_dec'] = 0.0
53 return catalog
55 def loadSkyCircle(self, ctrCoord, radius, filterName, **kwargs):
56 refCat = self.make_synthetic_refcat(_synthCenter, _synthFlux)
57 fluxField = getRefFluxField(schema=refCat.schema, filterName=filterName)
58 return pipeBase.Struct(
59 refCat=self.make_synthetic_refcat(_synthCenter, _synthFlux),
60 fluxField=fluxField
61 )
63 def loadPixelBox(self, bbox, wcs, referenceFilter, **kwargs):
64 return self.loadSkyCircle(None, None, referenceFilter)
67class LoadReferenceCatalogTestCase(lsst.utils.tests.TestCase):
68 @classmethod
69 def setUpClass(cls):
70 cls.config = LoadReferenceCatalogConfig()
71 cls.config.refObjLoader.filterMap = {"filter1": "ref1",
72 "filter2": "ref2"}
73 cls.config.colorterms = synthTerms
74 cls.config.referenceSelector.doSignalToNoise = True
75 cls.config.referenceSelector.signalToNoise.fluxField = 'ref1_flux'
76 cls.config.referenceSelector.signalToNoise.errField = 'ref1_fluxErr'
77 cls.config.referenceSelector.signalToNoise.minimum = 20.0
79 cls.config.doApplyColorTerms = False
80 cls.config.doReferenceSelection = False
82 cls.synthMag1 = (_synthFlux*u.nanojansky).to(u.ABmag).value
83 cls.synthMag2 = ((_synthFlux*10)*u.nanojansky).to(u.ABmag).value
84 cls.synthMag3 = ((_synthFlux*100)*u.nanojansky).to(u.ABmag).value
86 cls.synthMag1Corr = cls.synthMag1 + 0.01*(cls.synthMag1 - cls.synthMag2)
87 cls.synthMag2Corr = cls.synthMag2 - 0.01*(cls.synthMag2 - cls.synthMag3)
89 cls.trivialLoader = TrivialLoader(dataIds=[],
90 refCats=[],
91 name="synthCam",
92 config=cls.config.refObjLoader)
94 def testGetReferenceCatalogCircle(self):
95 """Get a reference catalog skycircle."""
96 config = copy.copy(self.config)
97 config.freeze()
99 loaderTask = LoadReferenceCatalogTask(config=config, dataIds=[], refCats=[], name="synthCam")
100 # Monkey-patch our testing trivial loader to bypass the butler
101 loaderTask.refObjLoader = self.trivialLoader
103 cat = loaderTask.getSkyCircleCatalog(_synthCenter,
104 1.0*lsst.geom.degrees,
105 ['filter1', 'filter2'])
107 self.assertAlmostEqual(cat['ra'], _synthCenter.getRa().asDegrees())
108 self.assertAlmostEqual(cat['dec'], _synthCenter.getDec().asDegrees())
109 self.assertFloatsAlmostEqual(cat['refMag'][0, 0], self.synthMag1, rtol=1e-7)
110 self.assertFloatsAlmostEqual(cat['refMag'][0, 1], self.synthMag2, rtol=1e-7)
112 def testGetReferenceCatalogBox(self):
113 """Get a reference catalog box."""
114 config = copy.copy(self.config)
115 config.freeze()
117 loaderTask = LoadReferenceCatalogTask(config=config, dataIds=[], refCats=[], name="synthCam")
118 # Monkey-patch our testing trivial loader to bypass the butler
119 loaderTask.refObjLoader = self.trivialLoader
121 bbox = lsst.geom.Box2I(corner=lsst.geom.Point2I(0, 0),
122 dimensions=lsst.geom.Extent2I(100, 100))
123 crpix = lsst.geom.Point2D(50, 50)
124 crval = _synthCenter
125 cdMatrix = lsst.afw.geom.makeCdMatrix(scale=1.0*lsst.geom.arcseconds)
126 wcs = lsst.afw.geom.makeSkyWcs(crpix, crval, cdMatrix)
128 cat = loaderTask.getPixelBoxCatalog(bbox,
129 wcs,
130 ['filter1', 'filter2'])
132 self.assertAlmostEqual(cat['ra'], _synthCenter.getRa().asDegrees())
133 self.assertAlmostEqual(cat['dec'], _synthCenter.getDec().asDegrees())
134 self.assertFloatsAlmostEqual(cat['refMag'][0, 0], self.synthMag1, rtol=1e-7)
135 self.assertFloatsAlmostEqual(cat['refMag'][0, 1], self.synthMag2, rtol=1e-7)
137 def testGetReferenceCatalogCircleColorterms(self):
138 """Get a reference catalog circle, with color terms applied."""
139 config = copy.copy(self.config)
140 config.doApplyColorTerms = True
141 config.freeze()
143 loaderTask = LoadReferenceCatalogTask(config=config, dataIds=[], refCats=[], name="synthCam")
144 # Monkey-patch our testing trivial loader to bypass the butler
145 loaderTask.refObjLoader = self.trivialLoader
147 cat = loaderTask.getSkyCircleCatalog(_synthCenter,
148 1.0*lsst.geom.degrees,
149 ['filter1', 'filter2'])
151 self.assertAlmostEqual(cat['ra'], _synthCenter.getRa().asDegrees())
152 self.assertAlmostEqual(cat['dec'], _synthCenter.getDec().asDegrees())
153 self.assertFloatsAlmostEqual(cat['refMag'][0, 0], self.synthMag1Corr, rtol=1e-7)
154 self.assertFloatsAlmostEqual(cat['refMag'][0, 1], self.synthMag2Corr, rtol=1e-7)
156 def testGetReferenceCatalogCircleSelection(self):
157 """Get a reference catalog circle, apply selection."""
158 config = copy.copy(self.config)
159 config.doReferenceSelection = True
160 config.freeze()
162 loaderTask = LoadReferenceCatalogTask(config=config, dataIds=[], refCats=[], name="synthCam")
163 # Monkey-patch our testing trivial loader to bypass the butler
164 loaderTask.refObjLoader = self.trivialLoader
166 cat = loaderTask.getSkyCircleCatalog(_synthCenter,
167 1.0*lsst.geom.degrees,
168 ['filter1', 'filter2'])
170 # The selection removed all the objects.
171 self.assertEqual(len(cat), 0)
173 def testGetReferenceCatalogCircleSingleFilter(self):
174 """Get a reference catalog circle, single filter."""
175 config = copy.copy(self.config)
176 config.freeze()
178 loaderTask = LoadReferenceCatalogTask(config=config, dataIds=[], refCats=[], name="synthCam")
179 # Monkey-patch our testing trivial loader to bypass the butler
180 loaderTask.refObjLoader = self.trivialLoader
182 cat = loaderTask.getSkyCircleCatalog(_synthCenter,
183 1.0*lsst.geom.degrees,
184 ['filter1'])
186 self.assertAlmostEqual(cat['ra'], _synthCenter.getRa().asDegrees())
187 self.assertAlmostEqual(cat['dec'], _synthCenter.getDec().asDegrees())
188 self.assertFloatsAlmostEqual(cat['refMag'][0, 0], self.synthMag1, rtol=1e-7)
190 def testGetReferenceCatalogAnyFilter(self):
191 """Get a reference catalog circle, using anyFilterMapsToThis."""
192 config = LoadReferenceCatalogConfig()
193 config.refObjLoader.anyFilterMapsToThis = 'ref1'
195 config.doApplyColorTerms = False
196 config.doReferenceSelection = False
197 config.freeze()
199 loaderTask = LoadReferenceCatalogTask(config=config, dataIds=[], refCats=[], name="synthCam")
200 # Monkey-patch our testing trivial loader to bypass the butler
201 loaderTask.refObjLoader = self.trivialLoader
203 cat = loaderTask.getSkyCircleCatalog(_synthCenter,
204 1.0*lsst.geom.degrees,
205 ['filter1', 'filter2'])
207 self.assertFloatsAlmostEqual(cat['refMag'][0, 0], self.synthMag1, rtol=1e-7)
208 self.assertFloatsAlmostEqual(cat['refMag'][0, 1], self.synthMag1, rtol=1e-7)
210 def testGetReferenceCatalogRequirePm(self):
211 """Get a reference catalog circle, requiring proper motion."""
212 config = copy.copy(self.config)
213 config.refObjLoader.requireProperMotion = True
214 config.freeze()
216 loaderTask = LoadReferenceCatalogTask(config=config, dataIds=[], refCats=[], name="synthCam")
217 # Monkey-patch our testing trivial loader to bypass the butler
218 trivialLoader2 = TrivialLoader(dataIds=[], refCats=[], name="synthCam", config=config.refObjLoader)
219 loaderTask.refObjLoader = trivialLoader2
221 cat = loaderTask.getSkyCircleCatalog(_synthCenter,
222 1.0*lsst.geom.degrees,
223 ['filter1'])
225 self.assertAlmostEqual(cat['ra'], _synthCenter.getRa().asDegrees())
226 self.assertAlmostEqual(cat['dec'], _synthCenter.getDec().asDegrees())
227 self.assertFloatsAlmostEqual(cat['refMag'][0, 0], self.synthMag1, rtol=1e-7)
230class MemoryTester(lsst.utils.tests.MemoryTestCase):
231 pass
234if __name__ == "__main__": 234 ↛ 235line 234 didn't jump to line 235, because the condition on line 234 was never true
235 lsst.utils.tests.init()
236 unittest.main()