Exemplo n.º 1
0
def test_Nstates_py_vs_cy():
    burstsph, _ = load_burstsph()
    ns = 1.0
    nm = 1.0
    R0 = 6 * nm
    R_mean = np.array([5.5 * nm, 8 * nm])
    R_sigma = np.array([0.8 * nm, 1 * nm])
    τ_relax = np.array([0.1 * ns, 0.1 * ns])
    k_s01, k_s10 = np.array([1 / (1e6 * ns), 1 / (1e6 * ns)])
    K_s = np.array([[-k_s01, k_s01], [k_s10, -k_s10]])
    δt = 1e-2 * ns
    τ_D = 3.8 * ns

    k_D = 1 / τ_D
    D_fract = np.atleast_1d(1.)
    ts = burstsph.timestamp.values[:5000]
    rg = RandomGenerator(Xoroshiro128(1))
    A_em, R_ph, T_ph, S_ph = depi_ref.sim_DA_from_timestamps2_p2_Nstates(
        ts, δt, k_D, R0, R_mean, R_sigma, τ_relax, K_s, rg=rg)
    rg = RandomGenerator(Xoroshiro128(1))
    R_php, T_php, S_php = depi_cy.sim_DA_from_timestamps2_p2_Nstates_cy(
        ts,
        δt,
        np.atleast_1d(k_D),
        D_fract,
        R0,
        R_mean,
        R_sigma,
        τ_relax,
        K_s,
        rg=rg,
        ndt=0)
    assert np.allclose(R_php, R_ph)
    assert np.allclose(T_php, T_ph)
    assert np.allclose(S_php, S_ph)
Exemplo n.º 2
0
    def test_uniform_double(self):
        rs = RandomGenerator(self.brng(*self.data1['seed']))
        assert_array_equal(uniform_from_dsfmt(self.data1['data']),
                           rs.random_sample(1000))

        rs = RandomGenerator(self.brng(*self.data2['seed']))
        assert_equal(uniform_from_dsfmt(self.data2['data']),
                     rs.random_sample(1000))
Exemplo n.º 3
0
    def test_raw(self):
        rs = RandomGenerator(self.brng(*self.data1['seed']))
        uints = rs.random_raw(1000)
        assert_equal(uints, self.data1['data'])

        rs = RandomGenerator(self.brng(*self.data2['seed']))
        uints = rs.random_raw(1000)
        assert_equal(uints, self.data2['data'])
Exemplo n.º 4
0
 def test_reset_state_uintegers(self):
     rg = RandomGenerator(self.brng(*self.seed))
     rg.random_uintegers(bits=32)
     state = rg.state
     n1 = rg.random_uintegers(bits=32, size=10)
     rg2 = RandomGenerator(self.brng())
     rg2.state = state
     n2 = rg2.random_uintegers(bits=32, size=10)
     assert_((n1 == n2).all())
Exemplo n.º 5
0
 def test_reset_state_uint32(self):
     rg = RandomGenerator(self.brng(*self.seed))
     rg.randint(0, 2**24, 120, dtype=np.uint32)
     state = rg.state
     n1 = rg.randint(0, 2**24, 10, dtype=np.uint32)
     rg2 = RandomGenerator(self.brng())
     rg2.state = state
     n2 = rg2.randint(0, 2**24, 10, dtype=np.uint32)
     assert_array_equal(n1, n2)
Exemplo n.º 6
0
 def test_reset_state_gauss(self):
     rg = RandomGenerator(self.brng(*self.seed))
     rg.standard_normal()
     state = rg.state
     n1 = rg.standard_normal(size=10)
     rg2 = RandomGenerator(self.brng())
     rg2.state = state
     n2 = rg2.standard_normal(size=10)
     assert_array_equal(n1, n2)
Exemplo n.º 7
0
 def test_reset_state_float(self):
     rg = RandomGenerator(self.brng(*self.seed))
     rg.random_sample(dtype='float32')
     state = rg.brng.state
     n1 = rg.random_sample(size=10, dtype='float32')
     rg2 = RandomGenerator(self.brng())
     rg2.brng.state = state
     n2 = rg2.random_sample(size=10, dtype='float32')
     assert_((n1 == n2).all())
Exemplo n.º 8
0
    def test_gauss_inv(self):
        n = 25
        rs = RandomGenerator(self.brng(*self.data1['seed']))
        gauss = rs.standard_normal(n)
        assert_allclose(gauss, gauss_from_uint(self.data1['data'], n, 'dsfmt'))

        rs = RandomGenerator(self.brng(*self.data2['seed']))
        gauss = rs.standard_normal(25)
        assert_allclose(gauss, gauss_from_uint(self.data2['data'], n, 'dsfmt'))
Exemplo n.º 9
0
 def test_seed(self):
     rg = RandomGenerator(self.brng(*self.seed))
     rg2 = RandomGenerator(self.brng(*self.seed))
     rg.random_sample()
     rg2.random_sample()
     if not comp_state(rg.state, rg2.state):
         for key in rg.state:
             print(key)
             print(rg.state[key])
             print(rg2.state[key])
     assert_(comp_state(rg.state, rg2.state))
Exemplo n.º 10
0
 def test_uniform_float(self):
     rg = RandomGenerator(self.brng(12345))
     warmup(rg)
     state = rg.state
     r1 = rg.random_sample(11, dtype=np.float32)
     rg2 = RandomGenerator(self.brng())
     warmup(rg2)
     rg2.state = state
     r2 = rg2.random_sample(11, dtype=np.float32)
     assert_array_equal(r1, r2)
     assert_equal(r1.dtype, np.float32)
     assert_(comp_state(rg.state, rg2.state))
Exemplo n.º 11
0
    def test_array(self):
        s = RandomGenerator(MT19937(range(10)))
        assert_equal(s.randint(1000), 468)
        s = np.random.RandomState(range(10))
        assert_equal(s.randint(1000), 468)

        s = RandomGenerator(MT19937(np.arange(10)))
        assert_equal(s.randint(1000), 468)
        s = RandomGenerator(MT19937([0]))
        assert_equal(s.randint(1000), 973)
        s = RandomGenerator(MT19937([4294967295]))
        assert_equal(s.randint(1000), 265)
Exemplo n.º 12
0
    def test_uniform_float(self):
        rs = RandomGenerator(self.brng(*self.data1['seed']))
        vals = uniform32_from_uint(self.data1['data'], self.bits)
        uniforms = rs.random_sample(len(vals), dtype=np.float32)
        assert_allclose(uniforms, vals)
        assert_equal(uniforms.dtype, np.float32)

        rs = RandomGenerator(self.brng(*self.data2['seed']))
        vals = uniform32_from_uint(self.data2['data'], self.bits)
        uniforms = rs.random_sample(len(vals), dtype=np.float32)
        assert_allclose(uniforms, vals)
        assert_equal(uniforms.dtype, np.float32)
Exemplo n.º 13
0
 def test_normal_zig_floats(self):
     rg = RandomGenerator(self.brng())
     warmup(rg)
     state = rg.state
     r1 = rg.standard_normal(11, dtype=np.float32)
     rg2 = RandomGenerator(self.brng())
     warmup(rg2)
     rg2.state = state
     r2 = rg2.standard_normal(11, dtype=np.float32)
     assert_array_equal(r1, r2)
     assert_equal(r1.dtype, np.float32)
     assert_(comp_state(rg.state, rg2.state))
Exemplo n.º 14
0
def test_dt_tollerance():
    """
    Using small enough dt approximated and correct expression should be similar
    """
    burstsph, _ = load_burstsph()
    ns = 1.0
    nm = 1.0
    δt = 5e-4 * ns
    R0 = 6 * nm
    R_mean = 6.5 * nm
    R_sigma = 1 * nm
    τ_relax = 0.5 * ns
    τ_D = 4 * ns
    k_D = 1 / τ_D
    ts = burstsph.timestamp.values[:10000]
    rg = RandomGenerator(Xoroshiro128(1))
    A_em, R_ph, T_ph = depi_ref.sim_DA_from_timestamps(ts, δt, k_D, R0, R_mean,
                                                       R_sigma, τ_relax, rg)
    rg = RandomGenerator(Xoroshiro128(1))
    A_emp, R_php, T_php = depi_ref.sim_DA_from_timestamps_p(
        ts, δt, k_D, R0, R_mean, R_sigma, τ_relax, rg)
    rg = RandomGenerator(Xoroshiro128(1))
    A_emp2, R_php2, T_php2 = depi_ref.sim_DA_from_timestamps_p2(
        ts, δt, k_D, R0, R_mean, R_sigma, τ_relax, rg)
    assert not all([
        not np.allclose(A_em, A_emp), not np.allclose(R_ph, R_php),
        not np.allclose(T_ph, T_php)
    ])
    assert not all([
        not np.allclose(A_em, A_emp2), not np.allclose(R_ph, R_php2),
        not np.allclose(T_ph, T_php2)
    ])
    # Test R from nanotime vs R_mean
    R_a = fret.dist_from_E(1 - T_ph.mean() / τ_D, R0)
    R_c = fret.dist_from_E(1 - T_php.mean() / τ_D, R0)
    assert abs(R_c - R_a) < 0.12 * nm
    # Test E from nanotime vs ratiometric vs from P(R)
    E_PoR = fret.mean_E_from_gauss_PoR(R_mean, R_sigma, R0)
    E_ratio_a = A_em.sum() / A_em.size
    E_lifetime_a = 1 - T_ph.mean() / τ_D
    E_ratio_c = A_emp.sum() / A_emp.size
    E_lifetime_c = 1 - T_php.mean() / τ_D
    E_ratio_c2 = A_emp2.sum() / A_emp2.size
    E_lifetime_c2 = 1 - T_php2.mean() / τ_D
    assert abs(E_PoR - E_ratio_a) < 0.03
    assert abs(E_PoR - E_lifetime_a) < 0.03
    assert abs(E_ratio_c - E_ratio_a) < 0.03
    assert abs(E_ratio_c2 - E_ratio_a) < 0.03
    assert abs(E_lifetime_c - E_ratio_a) < 0.03
    assert abs(E_lifetime_c2 - E_ratio_a) < 0.03
Exemplo n.º 15
0
def generate_image(size, prng):
    allowed_prngs = [
        "java",
        "python",
        "numpy",
        "Xoroshiro128",
        "MT19937",
        "Philox",
        "SFC64",
        "Xorshift1024",
        "ThreeFry",
    ]
    if prng not in allowed_prngs:
        raise ValueError(f"prng={prng} is not in {allowed_prngs}")
    arr = np.zeros((size, size))
    for i in range(size):
        if prng == "python":
            random.seed(i)
        elif prng == "numpy":
            np.random.seed(i)
        elif prng == "java":
            rnd = javarandom.Random(i)
        elif prng == "Xoroshiro128":
            rnd = RandomGenerator(Xoroshiro128())
        elif prng == "Xorshift1024":
            rnd = RandomGenerator(Xorshift1024())
        elif prng == "ThreeFry":
            rnd = RandomGenerator(ThreeFry())
        elif prng == "MT19937":
            rnd = Generator(MT19937())
        elif prng == "Philox":
            rnd = Generator(Philox())
        elif prng == "SFC64":
            rnd = Generator(SFC64())

        for j in range(size):
            if prng == "python":
                random_number = random.random()
            elif prng == "numpy":
                random_number = np.random.random()
            elif prng == "java":
                random_number = rnd.nextDouble()
            elif prng in ["Xoroshiro128", "Xorshift1024", "ThreeFry"]:
                random_number = rnd.random_sample()
            elif prng in ["MT19937", "Philox", "SFC64"]:
                random_number = rnd.random()
            arr[j, i] = random_number
        print("{}\t{}\t{}".format(i, arr[0, i], arr[1, i]))
    imageio.imwrite(f"1000-random-numbers-{prng}.png", arr)
Exemplo n.º 16
0
def test_approx_vs_correct():
    """
    With α=np.inf, ndt=0 the adaptive functions should give the same
    results as the approximeated function.
    """
    burstsph, _ = load_burstsph()
    ns = 1.0
    nm = 1.0
    δt = 1e-1 * ns
    R0 = 6 * nm
    R_mean = 6.5 * nm
    R_sigma = 0.01 * nm
    τ_relax = 0.08 * ns
    τ_D = 4 * ns
    k_D = 1 / τ_D
    ts = burstsph.timestamp.values[:1000]

    for R_sigma in (0.01, 0.1, 1, 10):
        for R_mean in (4, 5, 6, 7, 8):
            R_sigma *= nm
            R_mean *= nm
            rg = RandomGenerator(Xoroshiro128(1))
            A_em, R_ph, T_ph = depi_ref.sim_DA_from_timestamps(
                ts, δt, k_D, R0, R_mean, R_sigma, τ_relax, rg)
            rg = RandomGenerator(Xoroshiro128(1))
            A_emp, R_php, T_php = depi_ref.sim_DA_from_timestamps_p(ts,
                                                                    δt,
                                                                    k_D,
                                                                    R0,
                                                                    R_mean,
                                                                    R_sigma,
                                                                    τ_relax,
                                                                    rg,
                                                                    α=np.inf,
                                                                    ndt=0)
            rg = RandomGenerator(Xoroshiro128(1))
            A_emp2, R_php2, T_php2 = depi_ref.sim_DA_from_timestamps_p2(
                ts, δt, k_D, R0, R_mean, R_sigma, τ_relax, rg, α=np.inf, ndt=0)
            assert all([
                np.allclose(A_em, A_emp),
                np.allclose(R_ph, R_php),
                np.allclose(T_ph, T_php)
            ])
            assert all([
                np.allclose(A_em, A_emp2),
                np.allclose(R_ph, R_php2),
                np.allclose(T_ph, T_php2)
            ])
Exemplo n.º 17
0
    def test_scalar(self):
        s = RandomGenerator(MT19937(0))
        assert_equal(s.randint(1000), 684)
        s1 = np.random.RandomState(0)
        assert_equal(s1.randint(1000), 684)
        assert_equal(s1.randint(1000), s.randint(1000))

        s = RandomGenerator(MT19937(4294967295))
        assert_equal(s.randint(1000), 419)
        s1 = np.random.RandomState(4294967295)
        assert_equal(s1.randint(1000), 419)
        assert_equal(s1.randint(1000), s.randint(1000))

        self.rg.seed(4294967295)
        self.nprs.seed(4294967295)
        self._is_state_common()
Exemplo n.º 18
0
    def trainInit():
        # I thought about it, and... it probably makes more sense
        # to just run the roll internally as opposed to making the
        # roll and seed externally, and then doing an input (WHEN
        # WE REALLY DON'T NEED ONE)...

        roll = ran.randint(0, 7)
        seed = ran.randint(0, 0b1111111111111)

        # we got a switch statement here
        rng = {
            0: ran.seed,
            1: MT19937,
            2: DSFMT,
            3: PCG64,
            4: Philox,
            5: ThreeFry,
            6: Xoroshiro128,
            7: Xorshift1024,
        }

        # Handling the fact that Python's rng isn't Randomgen's rng
        if roll == 0:
            ran.seed(seed)
            rg = ran
        else:
            rg = RandomGenerator(rng.get(roll)(seed))

        # turns the output into a dictionary
        out = {
            0: [roll, seed],
            1: rg,
        }
        return out
Exemplo n.º 19
0
def _recolor_burstsph(ts, params, cache=False, seed=1):
    if cache:
        bph = depi.recolor_burstsph_cache(ts, **params)
    else:
        rg = RandomGenerator(Xoroshiro128(seed))
        bph = depi.recolor_burstsph(ts, rg=rg, **params)
    return bph
Exemplo n.º 20
0
def test_py_vs_cy():
    ns = 1.0
    nm = 1.0
    δt = 1e-1 * ns
    R0 = 6 * nm
    R_mean = 5.5 * nm
    R_sigma = 0.01 * nm
    τ_relax = 0.08 * ns
    τ_D = 4 * ns
    k_D = 1 / τ_D

    burstsph, _ = load_burstsph()
    ts = burstsph.timestamp.values[:1000]

    rg = RandomGenerator(Xoroshiro128(1))
    A_em, R_ph, T_ph = depi_ref.sim_DA_from_timestamps(ts, δt, k_D, R0, R_mean,
                                                       R_sigma, τ_relax, rg)
    rg = RandomGenerator(Xoroshiro128(1))
    R_phc, T_phc = depi_cy.sim_DA_from_timestamps_cy(ts, δt, k_D, R0, R_mean,
                                                     R_sigma, τ_relax, rg)
    assert all([np.allclose(R_ph, R_phc), np.allclose(T_ph, T_phc)])
    # Test R from nanotime vs R_mean
    R_est = fret.dist_from_E(1 - T_ph.mean() / τ_D, R0)
    assert abs(R_est - R_mean) < 0.12 * nm
    # Test E from nanotime vs ratiometric vs from P(R)
    E_PoR = fret.mean_E_from_gauss_PoR(R_mean, R_sigma, R0)
    E_ratio = A_em.sum() / A_em.size
    E_lifetime = 1 - T_ph.mean() / τ_D
    assert abs(E_PoR - E_ratio) < 0.03
    assert abs(E_PoR - E_lifetime) < 0.03

    rg = RandomGenerator(Xoroshiro128(1))
    A_em, R_ph, T_ph = depi_ref.sim_DA_from_timestamps2_p(
        ts, δt, k_D, R0, R_mean, R_sigma, τ_relax, rg)
    rg = RandomGenerator(Xoroshiro128(1))
    R_phc, T_phc = depi_cy.sim_DA_from_timestamps2_p_cy(
        ts, δt, k_D, R0, R_mean, R_sigma, τ_relax, rg)
    assert all([np.allclose(R_ph, R_phc), np.allclose(T_ph, T_phc)])
    # Test R from nanotime vs R_mean
    R_est = fret.dist_from_E(1 - T_ph.mean() / τ_D, R0)
    assert abs(R_est - R_mean) < 0.12 * nm
    # Test E from nanotime vs ratiometric vs from P(R)
    E_PoR = fret.mean_E_from_gauss_PoR(R_mean, R_sigma, R0)
    E_ratio = A_em.sum() / A_em.size
    E_lifetime = 1 - T_ph.mean() / τ_D
    assert abs(E_PoR - E_ratio) < 0.03
    assert abs(E_PoR - E_lifetime) < 0.03
Exemplo n.º 21
0
 def setup_class(cls):
     cls.brng = PCG32
     cls.advance = 2**48 + 2**21 + 2**16 + 2**5 + 1
     cls.seed = [2**48 + 2**21 + 2**16 + 2**5 + 1, 2**21 + 2**16 + 2**5 + 1]
     cls.rg = RandomGenerator(cls.brng(*cls.seed))
     cls.initial_state = cls.rg.state
     cls.seed_vector_bits = None
     cls._extra_setup()
Exemplo n.º 22
0
 def test_init(self):
     rg = RandomGenerator(self.brng())
     state = rg.state
     rg.standard_normal(1)
     rg.standard_normal(1)
     rg.state = state
     new_state = rg.state
     assert_(comp_state(state, new_state))
Exemplo n.º 23
0
Arquivo: base.py Projeto: ahnitz/epsie
    def random_generator(self):
        """The random number generator.

        This is an instance of :py:class:`randgen.RandomGenerator` that is
        derived from the bit generator. It provides methods to create random
        draws from various distributions.
        """
        return RandomGenerator(self.bit_generator)
Exemplo n.º 24
0
 def setup_class(cls):
     cls.brng = DSFMT
     cls.advance = None
     cls.seed = [12345]
     cls.rg = RandomGenerator(cls.brng(*cls.seed))
     cls.initial_state = cls.rg.state
     cls._extra_setup()
     cls.seed_vector_bits = 32
Exemplo n.º 25
0
 def setup_class(cls):
     cls.brng = Xoshiro512StarStar
     cls.advance = None
     cls.seed = [12345]
     cls.rg = RandomGenerator(cls.brng(*cls.seed))
     cls.initial_state = cls.rg.state
     cls.seed_vector_bits = 64
     cls._extra_setup()
Exemplo n.º 26
0
 def setup_class(cls):
     cls.brng = ThreeFry
     cls.advance = 2**63 + 2**31 + 2**15 + 1
     cls.seed = [12345]
     cls.rg = RandomGenerator(cls.brng(*cls.seed))
     cls.initial_state = cls.rg.state
     cls.seed_vector_bits = 64
     cls._extra_setup()
Exemplo n.º 27
0
 def test_seed_float_array(self):
     rs = RandomGenerator(self.brng(*self.data1['seed']))
     assert_raises(self.seed_error_type, rs.seed, np.array([np.pi]))
     assert_raises(self.seed_error_type, rs.seed, np.array([-np.pi]))
     assert_raises(self.seed_error_type, rs.seed, np.array([np.pi, -np.pi]))
     assert_raises(self.seed_error_type, rs.seed, np.array([0, np.pi]))
     assert_raises(self.seed_error_type, rs.seed, [np.pi])
     assert_raises(self.seed_error_type, rs.seed, [0, np.pi])
Exemplo n.º 28
0
 def setup_class(cls):
     cls.brng = MT19937
     cls.advance = None
     cls.seed = [2**21 + 2**16 + 2**5 + 1]
     cls.rg = RandomGenerator(cls.brng(*cls.seed))
     cls.initial_state = cls.rg.state
     cls.seed_vector_bits = 32
     cls._extra_setup()
     cls.seed_error = ValueError
Exemplo n.º 29
0
 def setup_class(cls):
     cls.np = numpy.random
     cls.brng = MT19937
     cls.seed = [2**21 + 2**16 + 2**5 + 1]
     cls.rg = RandomGenerator(cls.brng(*cls.seed))
     cls.lg = LegacyGenerator(cls.brng(*cls.seed))
     cls.nprs = cls.np.RandomState(*cls.seed)
     cls.initial_state = cls.rg.state
     cls._set_common_state()
Exemplo n.º 30
0
 def setup_class(cls):
     # Overridden in test classes. Place holder to silence IDE noise
     cls.brng = Xoshiro256StarStar
     cls.advance = None
     cls.seed = [12345]
     cls.rg = RandomGenerator(cls.brng(*cls.seed))
     cls.initial_state = cls.rg.state
     cls.seed_vector_bits = 64
     cls._extra_setup()