Coverage for tests/test_brightStarStamps.py : 20%

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 brightStarStamps
27from lsst.afw import image as afwImage
28from lsst.daf.base import PropertyList
29import lsst.utils.tests
32class BrightStarStampsTestCase(lsst.utils.tests.TestCase):
33 """Test BrightStarStamps.
34 """
35 def setUp(self):
36 np.random.seed(12)
37 stampSize = (25, 25)
38 # create dummy star stamps
39 starImages = [afwImage.maskedImage.MaskedImageF(*stampSize)
40 for _ in range(3)]
41 for starIm in starImages:
42 starImArray = starIm.image.array
43 starImArray += np.random.rand(*stampSize)
44 ids = ["111", "aaa", "bbb"]
45 mags = np.random.rand(3)
46 # faint object to test magnitude cuts
47 self.faintObjIdx = 1
48 mags[self.faintObjIdx] = 18.
49 ids[self.faintObjIdx] = "faint"
50 fluxes = np.random.rand(3)
51 self.starStamps = [brightStarStamps.BrightStarStamp(stamp_im=starIm,
52 gaiaGMag=mag,
53 gaiaId=gaiaId,
54 annularFlux=flux)
55 for starIm, mag, gaiaId, flux in zip(starImages, mags, ids, fluxes)]
56 self.innerRadius = 40
57 self.outerRadius = 50
58 self.bss = brightStarStamps.BrightStarStamps(self.starStamps, self.innerRadius, self.outerRadius)
60 def tearDown(self):
61 del self.bss
62 del self.starStamps
63 del self.innerRadius
64 del self.outerRadius
65 del self.faintObjIdx
67 def testIO(self):
68 """Test the class' write and readFits methods.
70 The ``options`` argument to the read method is only used to read
71 sub-BBoxes, which is handled by the Butler. Tests of this are done in
72 afw.
73 """
74 with tempfile.NamedTemporaryFile() as f:
75 self.bss.writeFits(f.name)
76 options = PropertyList()
77 bss2 = brightStarStamps.BrightStarStamps.readFitsWithOptions(f.name, options)
78 self.assertEqual(len(self.bss), len(bss2))
79 for mi1, mi2 in zip(self.bss.getMaskedImages(), bss2.getMaskedImages()):
80 self.assertMaskedImagesAlmostEqual(mi1, mi2)
81 np.testing.assert_almost_equal(self.bss.getMagnitudes(), bss2.getMagnitudes())
82 np.testing.assert_almost_equal(self.bss.getAnnularFluxes(), bss2.getAnnularFluxes())
83 for id1, id2 in zip(self.bss.getGaiaIds(), bss2.getGaiaIds()):
84 self.assertEqual(id1, id2)
86 def testMagnitudeSelection(self):
87 brightOnly = self.bss.selectByMag(magMax=7)
88 self.assertEqual(len(brightOnly), 2)
89 self.assertFalse("faint" in brightOnly.getGaiaIds())
91 faintOnly = self.bss.selectByMag(magMin=7)
92 self.assertEqual(len(faintOnly), 1)
93 self.assertEqual(faintOnly.getGaiaIds()[0], "faint")
94 brightObj = self.bss[self.faintObjIdx]
95 self.assertMaskedImagesAlmostEqual(brightObj.stamp_im, faintOnly.getMaskedImages()[0])
97 def testTypeMismatchHandling(self):
98 fullStar = self.bss[0]
99 # try passing on a dictionary and a maskedImage instead of a
100 # BrightStarStamp
101 falseStar = {"starStamp": fullStar.stamp_im, "gaiaGMag": fullStar.gaiaGMag,
102 "gaiaId": fullStar.gaiaId, "annularFlux": fullStar.annularFlux}
103 starIm = fullStar.stamp_im
104 for wrongType in [falseStar, starIm]:
105 # test at initialization
106 with self.assertRaises(ValueError):
107 _ = brightStarStamps.BrightStarStamps([fullStar, wrongType], innerRadius=self.innerRadius,
108 outerRadius=self.outerRadius)
109 # test at appending time
110 with self.assertRaises(ValueError):
111 self.bss.append(wrongType, innerRadius=self.innerRadius, outerRadius=self.outerRadius)
113 def testAnnulusMismatch(self):
114 """Test an exception is raised if mismatching annulus radii are
115 given (as the annularFlux values would then be meaningless)."""
116 metadata = self.bss.metadata
117 # metadata contains annulus definition; brightStarStamps can be
118 # instanciated without specifying annulus radii
119 _ = brightStarStamps.BrightStarStamps(self.starStamps, metadata=metadata)
120 # or by explicitely passing them on, in which case metadata is optional
121 _ = brightStarStamps.BrightStarStamps(self.starStamps, innerRadius=self.innerRadius,
122 outerRadius=self.outerRadius)
123 # Both can be provided as long as values match
124 _ = brightStarStamps.BrightStarStamps(self.starStamps, innerRadius=self.innerRadius,
125 outerRadius=self.outerRadius,
126 metadata=metadata)
127 # An exception should be raised if they do not
128 with self.assertRaises(AttributeError):
129 _ = brightStarStamps.BrightStarStamps(self.starStamps, innerRadius=self.innerRadius/2,
130 metadata=metadata)
131 with self.assertRaises(AttributeError):
132 _ = brightStarStamps.BrightStarStamps(self.starStamps, outerRadius=self.outerRadius + 1,
133 metadata=metadata)
134 # or if one tries to concatenate BrightStarStamps with different
135 # annulus radii
136 bss2 = brightStarStamps.BrightStarStamps(self.starStamps, innerRadius=self.innerRadius/2,
137 outerRadius=self.outerRadius)
138 with self.assertRaises(AttributeError):
139 self.bss.extend(bss2)
140 # or append an extra stamp with different annulus radii
141 fullStar = self.bss[0]
142 with self.assertRaises(AttributeError):
143 self.bss.append(fullStar, innerRadius=self.innerRadius/2, outerRadius=self.outerRadius + 1)
146class MemoryTester(lsst.utils.tests.MemoryTestCase):
147 pass
150def setup_module(module):
151 lsst.utils.tests.init()
154if __name__ == "__main__": 154 ↛ 155line 154 didn't jump to line 155, because the condition on line 154 was never true
155 lsst.utils.tests.init()
156 unittest.main()