Hide keyboard shortcuts

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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

# This file is part of afw. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (https://www.lsst.org). 

# See the COPYRIGHT file at the top-level directory of this distribution 

# for details of code ownership. 

# 

# 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 GNU General Public License 

# along with this program. If not, see <https://www.gnu.org/licenses/>. 

 

""" 

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

 

Run with: 

python test_random1.py 

or 

pytest test_random1.py 

 

""" 

 

import sys 

import time 

import unittest 

 

import lsst.pex.exceptions 

import lsst.utils.tests 

import lsst.geom 

import lsst.afw.image as afwImage 

import lsst.afw.math as afwMath 

 

 

def checkRngEquivalence(rng1, rng2): 

for i in range(1000): 

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

 

 

def getSeed(): 

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

 

 

class RandomTestCase(unittest.TestCase): 

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

 

def setUp(self): 

pass 

 

def tearDown(self): 

pass 

 

def testCreate(self): 

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

 

def testCopy(self): 

""" 

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) 

 

 

class RandomImageTestCase(unittest.TestCase): 

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

 

def setUp(self): 

self.rand = afwMath.Random() 

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

 

def tearDown(self): 

del self.image 

 

def testState(self): 

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) 

 

def testStateRaisesType(self): 

with self.assertRaises(TypeError): 

self.rand.setState(self.rand) 

 

@unittest.skipIf(sys.version_info.major < 3, "setState can not distinguish unicode from bytes") 

def testStateRaisesUnicodeType(self): 

with self.assertRaises(TypeError): 

self.rand.setState(u"\u03bc not bytes") 

 

def testStateRaisesLength(self): 

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

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

 

def testRandomUniformImage(self): 

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

 

def testRandomGaussianImage(self): 

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

 

def testRandomChisqImage(self): 

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) 

 

def testRandomPoissonImage(self): 

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) 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

152 ↛ 153line 152 didn't jump to line 153, because the condition on line 152 was never trueif __name__ == "__main__": 

lsst.utils.tests.init() 

unittest.main()