Coverage for tests/test_apCorrMap.py: 25%
90 statements
« prev ^ index » next coverage.py v6.4.2, created at 2022-07-19 04:54 -0700
« prev ^ index » next coverage.py v6.4.2, created at 2022-07-19 04:54 -0700
1#
2# LSST Data Management System
3# Copyright 2008-2014 LSST Corporation.
4#
5# This product includes software developed by the
6# LSST Project (http://www.lsst.org/).
7#
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the LSST License Statement and
19# the GNU General Public License along with this program. If not,
20# see <http://www.lsstcorp.org/LegalNotices/>.
21#
23"""
24Tests for image.ApCorrMap
26Run with:
27 python test_apCorrMap.py
28or
29 pytest test_apCorrMap.py
30"""
31import collections
32import os
33import unittest
35import numpy as np
37import lsst.utils.tests
38import lsst.pex.exceptions
39import lsst.geom
40import lsst.afw.math
41import lsst.afw.image
44class ApCorrMapTestCase(lsst.utils.tests.TestCase):
46 def setUp(self):
47 np.random.seed(100)
48 self.bbox = lsst.geom.Box2I(
49 lsst.geom.Point2I(-5, -5), lsst.geom.Point2I(5, 5))
50 self.map = lsst.afw.image.ApCorrMap()
51 for name in ("a", "b", "c"):
52 self.map.set(name, lsst.afw.math.ChebyshevBoundedField(
53 self.bbox, np.random.randn(3, 3)))
55 def tearDown(self):
56 del self.map
57 del self.bbox
59 def compare(self, a, b):
60 """Compare two ApCorrMaps for equality, without assuming that their BoundedFields have the
61 same addresses (i.e. so we can compare after serialization).
62 """
63 self.assertEqual(len(a), len(b))
64 for name, value in list(a.items()):
65 value2 = b.get(name)
66 self.assertIsNotNone(value2)
67 self.assertEqual(value.getBBox(), value2.getBBox())
68 self.assertFloatsAlmostEqual(
69 value.getCoefficients(), value2.getCoefficients(), rtol=0.0)
71 def testAccessors(self):
72 """Test the accessors and other custom Swig code we've added to make ApCorrMap behave like a Python
73 mapping.
74 """
75 self.assertEqual(len(self.map), 3)
76 self.assertEqual(collections.OrderedDict(self.map),
77 {name: value for (name, value) in list(self.map.items())})
78 self.assertEqual(list(collections.OrderedDict(self.map).keys()),
79 list(self.map.keys()))
80 self.assertEqual(list(collections.OrderedDict(self.map).values()),
81 list(self.map.values()))
82 self.assertEqual(list(collections.OrderedDict(self.map).items()),
83 list(self.map.items()))
84 self.assertEqual(list(collections.OrderedDict(self.map).keys()),
85 list(self.map))
86 self.assertIn("b", self.map)
87 self.assertNotIn("d", self.map)
88 self.map["d"] = lsst.afw.math.ChebyshevBoundedField(
89 self.bbox, np.random.randn(2, 2))
90 self.assertIn("d", self.map)
91 self.assertIsNone(self.map.get("e"))
92 with self.assertRaises(lsst.pex.exceptions.NotFoundError):
93 self.map["e"]
94 self.assertEqual(self.map.get("d"), self.map["d"])
96 def testPersistence(self):
97 """Test that we can round-trip an ApCorrMap through FITS persistence.
98 """
99 with lsst.utils.tests.getTempFilePath(".fits") as filename:
100 self.map.writeFits(filename)
101 map2 = lsst.afw.image.ApCorrMap.readFits(filename)
102 self.compare(self.map, map2)
104 def testExposurePersistence(self):
105 """Test that the ApCorrMap is saved with an Exposure.
106 """
107 with lsst.utils.tests.getTempFilePath(".fits") as filename:
108 exposure1 = lsst.afw.image.ExposureF(self.bbox)
109 exposure1.getInfo().setApCorrMap(self.map)
110 exposure1.writeFits(filename)
111 exposure2 = lsst.afw.image.ExposureF(filename)
112 map2 = exposure2.getInfo().getApCorrMap()
113 self.compare(self.map, map2)
115 def testExposureRecordPersistence(self):
116 """Test that the ApCorrMap is saved with an ExposureRecord.
117 """
118 with lsst.utils.tests.getTempFilePath(".fits") as filename:
119 cat1 = lsst.afw.table.ExposureCatalog(
120 lsst.afw.table.ExposureTable.makeMinimalSchema())
121 record1 = cat1.addNew()
122 record1.setApCorrMap(self.map)
123 cat1.writeFits(filename)
124 cat2 = lsst.afw.table.ExposureCatalog.readFits(filename)
125 record2 = cat2[0]
126 map2 = record2.getApCorrMap()
127 self.compare(self.map, map2)
129 def testExposureCatalogBackwardsCompatibility(self):
130 """Test that we can read an ExposureCatalog written with an old version of the code.
131 """
132 testPath = os.path.abspath(os.path.dirname(__file__))
133 filename = os.path.join(testPath, "data", "version-0-ExposureCatalog.fits")
134 cat = lsst.afw.table.ExposureCatalog.readFits(filename)
135 record = cat[0]
136 self.assertIsNone(record.getApCorrMap())
138 def testScale(self):
139 """Test that we can scale an ApCorrMap.
140 """
141 scale = 12.345
142 new = lsst.afw.image.ApCorrMap()
143 for name, value in self.map.items():
144 new.set(name, value*scale)
145 new /= scale
146 self.compare(self.map, new)
147 # And back the other way
148 new = lsst.afw.image.ApCorrMap()
149 for name, value in self.map.items():
150 new.set(name, value/scale)
151 new *= scale
152 self.compare(self.map, new)
155class TestMemory(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()