Coverage for tests/test_random1.py : 33%
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
|
# # LSST Data Management System # Copyright 2008, 2009, 2010 LSST Corporation. # # This product includes software developed by the # LSST Project (http://www.lsst.org/). # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the LSST License Statement and # the GNU General Public License along with this program. If not, # see <http://www.lsstcorp.org/LegalNotices/>. #
Tests for the lsst.afw.math.Random Python wrapper
Run with: python Random_1.py or python >>> import unittest; T=load("Random_1"); unittest.TextTestRunner(verbosity=1).run(T.suite()) """
for i in range(1000): assert rng1.uniform() == rng2.uniform()
return int(time.time() * 1000000.0) % 1000000
"""A test case for lsst.afw.math.Random"""
pass
pass
rngs = [] for name in afwMath.Random.getAlgorithmNames(): rngs.append(afwMath.Random(name)) for r in rngs: assert afwMath.Random(r.getAlgorithmName() ).uniform() == r.uniform() r2 = afwMath.Random(r.getAlgorithm()) r2.uniform() assert r2.uniform() == r.uniform()
""" Test that the generator returned by deepCopy() produces an identical random number sequence to its prototype """ rng1 = afwMath.Random(afwMath.Random.MT19937, getSeed()) rng2 = rng1.deepCopy() checkRngEquivalence(rng1, rng2)
""" Tests that policy files and environment variables can override user specified algorithms and seed values. """ pol = pexPolicy.Policy() seed = getSeed() pol.set("rngSeed", str(seed)) pol.set("rngAlgorithm", afwMath.Random.getAlgorithmNames() [int(afwMath.Random.Algorithm.RANLXD2)]) r1 = afwMath.Random(afwMath.Random.RANLXD2, seed) r2 = afwMath.Random(pol) checkRngEquivalence(r1, r2)
"""A test case for lsst.afw.math.Random applied to Images"""
self.rand = afwMath.Random() self.image = afwImage.ImageF(lsst.geom.Extent2I(1000, 1000))
del self.image
for i in range(100): self.rand.uniformInt(10000) state = self.rand.getState() self.assertIsInstance(state, bytes) v1 = [self.rand.uniformInt(10000) for i in range(100)] self.rand.setState(state) v2 = [self.rand.uniformInt(10000) for i in range(100)] self.assertEqual(v1, v2)
with self.assertRaises(TypeError): self.rand.setState(self.rand)
def testStateRaisesUnicodeType(self): with self.assertRaises(TypeError): self.rand.setState(u"\u03bc not bytes")
with self.assertRaises(lsst.pex.exceptions.LengthError): self.rand.setState(b"too small")
afwMath.randomUniformImage(self.image, self.rand)
afwMath.randomGaussianImage(self.image, self.rand)
nu = 10 afwMath.randomChisqImage(self.image, self.rand, nu) stats = afwMath.makeStatistics( self.image, afwMath.MEAN | afwMath.VARIANCE) if False: print("nu = %g. mean = %g, variance = %g" % (nu, stats.getValue(afwMath.MEAN), stats.getValue(afwMath.VARIANCE))) self.assertAlmostEqual(stats.getValue(afwMath.MEAN), nu, 1) self.assertAlmostEqual(stats.getValue(afwMath.VARIANCE), 2*nu, 1)
mu = 10 afwMath.randomPoissonImage(self.image, self.rand, mu) stats = afwMath.makeStatistics( self.image, afwMath.MEAN | afwMath.VARIANCE) if False: print("mu = %g. mean = %g, variance = %g" % (mu, stats.getValue(afwMath.MEAN), stats.getValue(afwMath.VARIANCE))) self.assertAlmostEqual(stats.getValue(afwMath.MEAN), mu, 1) self.assertAlmostEqual(stats.getValue(afwMath.VARIANCE), mu, 1)
lsst.utils.tests.init()
lsst.utils.tests.init() unittest.main() |