Coverage for tests/test_random1.py: 34%
82 statements
« prev ^ index » next coverage.py v7.4.4, created at 2024-03-23 03:25 -0700
« prev ^ index » next coverage.py v7.4.4, created at 2024-03-23 03:25 -0700
1# This file is part of afw.
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/>.
22"""
23Tests for the lsst.afw.math.Random Python wrapper
25Run with:
26 python test_random1.py
27or
28 pytest test_random1.py
30"""
32import sys
33import time
34import unittest
36import lsst.pex.exceptions
37import lsst.utils.tests
38import lsst.geom
39import lsst.afw.image as afwImage
40import lsst.afw.math as afwMath
43def checkRngEquivalence(rng1, rng2):
44 for i in range(1000):
45 assert rng1.uniform() == rng2.uniform()
48def getSeed():
49 return int(time.time() * 1000000.0) % 1000000
52class RandomTestCase(unittest.TestCase):
53 """A test case for lsst.afw.math.Random"""
55 def setUp(self):
56 pass
58 def tearDown(self):
59 pass
61 def testCreate(self):
62 rngs = []
63 for name in afwMath.Random.getAlgorithmNames():
64 rngs.append(afwMath.Random(name))
65 for r in rngs:
66 assert afwMath.Random(r.getAlgorithmName()
67 ).uniform() == r.uniform()
68 r2 = afwMath.Random(r.getAlgorithm())
69 r2.uniform()
70 assert r2.uniform() == r.uniform()
72 def testCopy(self):
73 """
74 Test that the generator returned by deepCopy() produces an
75 identical random number sequence to its prototype
76 """
77 rng1 = afwMath.Random(afwMath.Random.MT19937, getSeed())
78 rng2 = rng1.deepCopy()
79 checkRngEquivalence(rng1, rng2)
82class RandomImageTestCase(unittest.TestCase):
83 """A test case for lsst.afw.math.Random applied to Images"""
85 def setUp(self):
86 self.rand = afwMath.Random()
87 self.image = afwImage.ImageF(lsst.geom.Extent2I(1000, 1000))
89 def tearDown(self):
90 del self.image
92 def testState(self):
93 for i in range(100):
94 self.rand.uniformInt(10000)
95 state = self.rand.getState()
96 self.assertIsInstance(state, bytes)
97 v1 = [self.rand.uniformInt(10000) for i in range(100)]
98 self.rand.setState(state)
99 v2 = [self.rand.uniformInt(10000) for i in range(100)]
100 self.assertEqual(v1, v2)
102 def testStateRaisesType(self):
103 with self.assertRaises(TypeError):
104 self.rand.setState(self.rand)
106 @unittest.skipIf(sys.version_info.major < 3, "setState can not distinguish unicode from bytes")
107 def testStateRaisesUnicodeType(self):
108 with self.assertRaises(TypeError):
109 self.rand.setState(u"\u03bc not bytes")
111 def testStateRaisesLength(self):
112 with self.assertRaises(lsst.pex.exceptions.LengthError):
113 self.rand.setState(b"too small")
115 def testRandomUniformImage(self):
116 afwMath.randomUniformImage(self.image, self.rand)
118 def testRandomGaussianImage(self):
119 afwMath.randomGaussianImage(self.image, self.rand)
121 def testRandomChisqImage(self):
122 nu = 10
123 afwMath.randomChisqImage(self.image, self.rand, nu)
124 stats = afwMath.makeStatistics(
125 self.image, afwMath.MEAN | afwMath.VARIANCE)
126 if False:
127 print("nu = %g. mean = %g, variance = %g" %
128 (nu, stats.getValue(afwMath.MEAN), stats.getValue(afwMath.VARIANCE)))
129 self.assertAlmostEqual(stats.getValue(afwMath.MEAN), nu, 1)
130 self.assertAlmostEqual(stats.getValue(afwMath.VARIANCE), 2*nu, 1)
132 def testRandomPoissonImage(self):
133 mu = 10
134 afwMath.randomPoissonImage(self.image, self.rand, mu)
135 stats = afwMath.makeStatistics(
136 self.image, afwMath.MEAN | afwMath.VARIANCE)
137 if False:
138 print("mu = %g. mean = %g, variance = %g" %
139 (mu, stats.getValue(afwMath.MEAN), stats.getValue(afwMath.VARIANCE)))
140 self.assertAlmostEqual(stats.getValue(afwMath.MEAN), mu, 1)
141 self.assertAlmostEqual(stats.getValue(afwMath.VARIANCE), mu, 1)
144class TestMemory(lsst.utils.tests.MemoryTestCase):
145 pass
148def setup_module(module):
149 lsst.utils.tests.init()
152if __name__ == "__main__": 152 ↛ 153line 152 didn't jump to line 153, because the condition on line 152 was never true
153 lsst.utils.tests.init()
154 unittest.main()