Coverage for tests/test_random1.py: 32%

82 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-03-09 03:27 -0800

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/>. 

21 

22""" 

23Tests for the lsst.afw.math.Random Python wrapper 

24 

25Run with: 

26 python test_random1.py 

27or 

28 pytest test_random1.py 

29 

30""" 

31 

32import sys 

33import time 

34import unittest 

35 

36import lsst.pex.exceptions 

37import lsst.utils.tests 

38import lsst.geom 

39import lsst.afw.image as afwImage 

40import lsst.afw.math as afwMath 

41 

42 

43def checkRngEquivalence(rng1, rng2): 

44 for i in range(1000): 

45 assert rng1.uniform() == rng2.uniform() 

46 

47 

48def getSeed(): 

49 return int(time.time() * 1000000.0) % 1000000 

50 

51 

52class RandomTestCase(unittest.TestCase): 

53 """A test case for lsst.afw.math.Random""" 

54 

55 def setUp(self): 

56 pass 

57 

58 def tearDown(self): 

59 pass 

60 

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() 

71 

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) 

80 

81 

82class RandomImageTestCase(unittest.TestCase): 

83 """A test case for lsst.afw.math.Random applied to Images""" 

84 

85 def setUp(self): 

86 self.rand = afwMath.Random() 

87 self.image = afwImage.ImageF(lsst.geom.Extent2I(1000, 1000)) 

88 

89 def tearDown(self): 

90 del self.image 

91 

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) 

101 

102 def testStateRaisesType(self): 

103 with self.assertRaises(TypeError): 

104 self.rand.setState(self.rand) 

105 

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") 

110 

111 def testStateRaisesLength(self): 

112 with self.assertRaises(lsst.pex.exceptions.LengthError): 

113 self.rand.setState(b"too small") 

114 

115 def testRandomUniformImage(self): 

116 afwMath.randomUniformImage(self.image, self.rand) 

117 

118 def testRandomGaussianImage(self): 

119 afwMath.randomGaussianImage(self.image, self.rand) 

120 

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) 

131 

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) 

142 

143 

144class TestMemory(lsst.utils.tests.MemoryTestCase): 

145 pass 

146 

147 

148def setup_module(module): 

149 lsst.utils.tests.init() 

150 

151 

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()