Coverage for tests/test_stamps.py : 23%

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# This file is part of meas_algorithms.
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 numpy as np
24import tempfile
26from lsst.meas.algorithms import stamps
27from lsst.afw import image as afwImage
28from lsst.daf.base import PropertyList
29import lsst.geom as geom
30import lsst.utils.tests
32np.random.seed(90210)
35def make_stamps(n_stamps=3):
36 stampSize = 25
37 # create dummy stamp stamps
38 stampImages = [afwImage.maskedImage.MaskedImageF(stampSize, stampSize)
39 for _ in range(n_stamps)]
40 for stampIm in stampImages:
41 stampImArray = stampIm.image.array
42 stampImArray += np.random.rand(stampSize, stampSize)
43 stampMaskArray = stampIm.mask.array
44 stampMaskArray += 10
45 stampVarArray = stampIm.variance.array
46 stampVarArray += 1000.
47 ras = np.random.rand(n_stamps)*360.
48 decs = np.random.rand(n_stamps)*180 - 90
49 stamp_list = [stamps.Stamp(stamp_im=stampIm,
50 position=geom.SpherePoint(geom.Angle(ra, geom.degrees),
51 geom.Angle(dec, geom.degrees)))
52 for stampIm, ra, dec in zip(stampImages, ras, decs)]
53 metadata = PropertyList()
54 metadata['RA_DEG'] = ras
55 metadata['DEC_DEG'] = decs
57 return stamps.Stamps(stamp_list, metadata=metadata)
60class StampsBaseTestCase(lsst.utils.tests.TestCase):
61 """Test StampsBase.
62 """
63 def testReadFitsWithOptionsNotImplementedErrorRaised(self):
64 """
65 Test that subclasses have their own version
66 of this implemented or an error is raised.
67 """
68 class FakeStampsBase(stamps.StampsBase):
69 def __init__(self):
70 return
72 with self.assertRaises(NotImplementedError):
73 FakeStampsBase.readFitsWithOptions('noFile', {})
75 def testReadFitsWithOptionsMetadataError(self):
76 """Test that error is raised when STAMPCLS returns None
77 """
78 with tempfile.NamedTemporaryFile() as f:
79 ss = make_stamps()
80 emptyMetadata = PropertyList()
81 stamps.writeFits(
82 f.name, [ss[0].stamp_im], emptyMetadata, None, True, True
83 )
84 with self.assertRaises(RuntimeError):
85 stamps.StampsBase.readFits(f.name)
87 def testReadFitsReturnsNewClass(self):
88 """Test that readFits will return subclass
89 """
90 class FakeStampsBase(stamps.StampsBase):
91 def __init__(self):
92 self._metadata = {}
93 return
95 @classmethod
96 def readFitsWithOptions(cls, filename, options):
97 return cls()
99 def _refresh_metadata(self):
100 self._metadata = {}
102 fakeStamps = FakeStampsBase.readFitsWithOptions('noFile', {})
103 self.assertEqual(type(fakeStamps), FakeStampsBase)
106class StampsTestCase(lsst.utils.tests.TestCase):
107 """Test Stamps.
108 """
109 def testAppend(self):
110 """Test ability to append to a Stamps object
111 """
112 ss = make_stamps()
113 s = ss[-1]
114 ss.append(s)
115 self.roundtrip(ss)
116 # check if appending something other than a Stamp raises
117 with self.assertRaises(ValueError):
118 ss.append('hello world')
120 def testExtend(self):
121 ss = make_stamps()
122 ss2 = make_stamps()
123 ss.extend([s for s in ss2])
124 # check if extending with something other than a Stamps
125 # object raises
126 with self.assertRaises(ValueError):
127 ss.extend(['hello', 'world'])
129 def testIO(self):
130 """Test the class' write and readFits methods.
131 """
132 self.roundtrip(make_stamps())
134 def testIOone(self):
135 """Test the class' write and readFits methods for the special case of
136 one stamp.
137 """
138 self.roundtrip(make_stamps(1))
140 def testIOsub(self):
141 """Test the class' write and readFits when passing on a bounding box.
142 """
143 bbox = geom.Box2I(geom.Point2I(3, 9), geom.Extent2I(11, 7))
144 ss = make_stamps()
145 with tempfile.NamedTemporaryFile() as f:
146 ss.writeFits(f.name)
147 options = {'bbox': bbox}
148 subStamps = stamps.Stamps.readFitsWithOptions(f.name, options)
149 for s1, s2 in zip(ss, subStamps):
150 self.assertEqual(bbox.getDimensions(), s2.stamp_im.getDimensions())
151 self.assertMaskedImagesAlmostEqual(s1.stamp_im[bbox], s2.stamp_im)
153 def roundtrip(self, ss):
154 """Round trip a Stamps object to disk and check values
155 """
156 with tempfile.NamedTemporaryFile() as f:
157 ss.writeFits(f.name)
158 options = PropertyList()
159 ss2 = stamps.Stamps.readFitsWithOptions(f.name, options)
160 self.assertEqual(len(ss), len(ss2))
161 for s1, s2 in zip(ss, ss2):
162 self.assertMaskedImagesAlmostEqual(s1.stamp_im, s2.stamp_im)
163 self.assertAlmostEqual(s1.position.getRa().asDegrees(),
164 s2.position.getRa().asDegrees())
165 self.assertAlmostEqual(s1.position.getDec().asDegrees(),
166 s2.position.getDec().asDegrees())
169class MemoryTester(lsst.utils.tests.MemoryTestCase):
170 pass
173def setup_module(module):
174 lsst.utils.tests.init()
177if __name__ == "__main__": 177 ↛ 178line 177 didn't jump to line 178, because the condition on line 177 was never true
178 lsst.utils.tests.init()
179 unittest.main()