test_random.py 3.75 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 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 <http://www.gnu.org/licenses/>.
#
# Copyright(C) 2020 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik.

18
19
import numpy as np

Philipp Arras's avatar
Philipp Arras committed
20
import nifty8 as ift
21
22


Martin Reinecke's avatar
Martin Reinecke committed
23
24
25
26
27
def check_state_back_to_orig():
    np.testing.assert_equal(len(ift.random._rng),1)
    np.testing.assert_equal(len(ift.random._sseq),1)


28
def test_rand1():
29
30
31
32
    with ift.random.Context(31):
        a = ift.random.current_rng().integers(0,1000000000)
    with ift.random.Context(31):
        b = ift.random.current_rng().integers(0,1000000000)
Martin Reinecke's avatar
Martin Reinecke committed
33
    check_state_back_to_orig()
34
35
36
37
    np.testing.assert_equal(a,b)


def test_rand2():
Martin Reinecke's avatar
Martin Reinecke committed
38
39
40
41
42
    sseq = ift.random.spawn_sseq(10)
    with ift.random.Context(sseq[2]):
        a = ift.random.current_rng().integers(0,1000000000)
    with ift.random.Context(sseq[2]):
        b = ift.random.current_rng().integers(0,1000000000)
Martin Reinecke's avatar
Martin Reinecke committed
43
    check_state_back_to_orig()
44
45
46
47
    np.testing.assert_equal(a,b)


def test_rand3():
48
49
50
51
52
53
54
55
56
57
    with ift.random.Context(31):
        sseq = ift.random.spawn_sseq(10)
        with ift.random.Context(sseq[2]):
            a = ift.random.current_rng().integers(0,1000000000)
    with ift.random.Context(31):
        sseq = ift.random.spawn_sseq(1)
        sseq = ift.random.spawn_sseq(1)
        sseq = ift.random.spawn_sseq(1)
        with ift.random.Context(sseq[0]):
            b = ift.random.current_rng().integers(0,1000000000)
Martin Reinecke's avatar
Martin Reinecke committed
58
    check_state_back_to_orig()
59
60
61
62
    np.testing.assert_equal(a,b)


def test_rand4():
63
64
65
66
67
68
69
    with ift.random.Context(31):
        a = ift.random.current_rng().integers(0,1000000000)
        with ift.random.Context(31):
            b = ift.random.current_rng().integers(0,1000000000)
    np.testing.assert_equal(a,b)


70
71
72
73
74
75
76
77
78
def test_rand5():
    ift.random.push_sseq_from_seed(31)
    a = ift.random.current_rng().integers(0,1000000000)
    ift.random.push_sseq_from_seed(31)
    b = ift.random.current_rng().integers(0,1000000000)
    c = ift.random.current_rng().integers(0,1000000000)
    ift.random.pop_sseq()
    d = ift.random.current_rng().integers(0,1000000000)
    ift.random.pop_sseq()
Martin Reinecke's avatar
Martin Reinecke committed
79
    check_state_back_to_orig()
80
81
    np.testing.assert_equal(a,b)
    np.testing.assert_equal(c,d)
82

Martin Reinecke's avatar
Martin Reinecke committed
83
84
85
86
87
88
89
90
91
92
93
94
95

def test_rand5b():
    with ift.random.Context(31):
        a = ift.random.current_rng().integers(0,1000000000)
        with ift.random.Context(31):
            b = ift.random.current_rng().integers(0,1000000000)
            c = ift.random.current_rng().integers(0,1000000000)
        d = ift.random.current_rng().integers(0,1000000000)
    check_state_back_to_orig()
    np.testing.assert_equal(a,b)
    np.testing.assert_equal(c,d)


96
97
98
99
100
101
102
def test_rand6():
    ift.random.push_sseq_from_seed(31)
    state = ift.random.getState()
    a = ift.random.current_rng().integers(0,1000000000)
    ift.random.setState(state)
    b = ift.random.current_rng().integers(0,1000000000)
    np.testing.assert_equal(a,b)
Martin Reinecke's avatar
Martin Reinecke committed
103
    ift.random.pop_sseq()
Martin Reinecke's avatar
Martin Reinecke committed
104
    check_state_back_to_orig()
105

Martin Reinecke's avatar
Martin Reinecke committed
106
107
108
109
110
111
112
113
114

def test_rand6b():
    with ift.random.Context(31):
        state = ift.random.getState()
        a = ift.random.current_rng().integers(0,1000000000)
        ift.random.setState(state)
        b = ift.random.current_rng().integers(0,1000000000)
        np.testing.assert_equal(a,b)
    check_state_back_to_orig()