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)
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))
def test_call_within_randomstate(self): # Check that custom RandomState does not call into global state m = RandomGenerator(MT19937()) # mt19937.RandomState() res = np.array([0, 8, 7, 2, 1, 9, 4, 7, 0, 3]) for i in range(3): mt19937.seed(i) m.seed(4321) # If m.state is not honored, the result will change assert_array_equal(m.choice(10, size=10, p=np.ones(10)/10.), res)
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)
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)
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())
def test_state_tuple(self): rs = RandomGenerator(self.brng(*self.data1['seed'])) brng = rs.brng state = brng.state desired = rs.randint(2 ** 16) tup = (state['brng'], state['state']['key'], state['state']['pos']) brng.state = tup actual = rs.randint(2 ** 16) assert_equal(actual, desired) tup = tup + (0, 0.0) brng.state = tup actual = rs.randint(2 ** 16) assert_equal(actual, desired)
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
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) ])
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
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
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
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())
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)
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
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()
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()
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])
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()
def test_seed_float_array(self): # GH #82 rs = RandomGenerator(self.brng(*self.data1['seed'])) assert_raises(TypeError, rs.brng.seed, np.array([np.pi])) assert_raises(TypeError, rs.brng.seed, np.array([-np.pi])) assert_raises(TypeError, rs.brng.seed, np.array([np.pi, -np.pi])) assert_raises(TypeError, rs.brng.seed, np.array([0, np.pi])) assert_raises(TypeError, rs.brng.seed, [np.pi]) assert_raises(TypeError, rs.brng.seed, [0, np.pi])
def setup_class(cls): cls.brng = ThreeFry32 cls.advance = 2**63 + 2**31 + 2**15 + 1 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 = 64 cls._extra_setup() cls.seed_error = ValueError
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()
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()
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
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))
def test_cdf_vs_dt_python(): """ Test CDF vs small-dt correction in python code """ burstsph, _ = load_burstsph() ns = 1.0 nm = 1.0 δt = 1e-2 * ns R0 = 6 * nm R_mean = 6.5 * nm R_sigma = 1 * nm τ_relax = 0.2 * ns τ_D = 4 * ns k_D = 1 / τ_D ts = burstsph.timestamp.values[:2000] 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, ndt=0, alpha=np.inf) rg = RandomGenerator(Xoroshiro128(1)) A_emp, R_php, T_php = depi_ref.sim_DA_from_timestamps2_p2(ts, δt, k_D, R0, R_mean, R_sigma, τ_relax, rg, ndt=0, alpha=np.inf) assert not all([ not np.allclose(A_em, A_emp), not np.allclose(R_ph, R_php), not np.allclose(T_ph, T_php) ])
def test_cdf_vs_dt_cy(): """ Test CDF vs small-dt correction in cython code """ burstsph, _ = load_burstsph() ns = 1.0 nm = 1.0 δt = 1e-2 * ns R0 = 6 * nm R_mean = 6.5 * nm R_sigma = 1 * nm τ_relax = 0.2 * ns τ_D = 4. * ns k_D = 1. / τ_D D_fract = np.atleast_1d(1.) ts = burstsph.timestamp.values[:100000] rg = RandomGenerator(Xoroshiro128(1)) R_ph, T_ph = depi_cy.sim_DA_from_timestamps2_p_cy(ts, δt, k_D, R0, R_mean, R_sigma, τ_relax, rg, ndt=0, alpha=np.inf) rg = RandomGenerator(Xoroshiro128(1)) R_php, T_php = depi_cy.sim_DA_from_timestamps2_p2_cy(ts, δt, np.atleast_1d(k_D), D_fract, R0, R_mean, R_sigma, τ_relax, rg=rg, ndt=0, alpha=np.inf) assert not all( [not np.allclose(R_ph, R_php), not np.allclose(T_ph, T_php)])
def test_calc_intrisic_nanotime(): rg = RandomGenerator(Xoroshiro128(1)) n = int(1e6) τ = 2 fract = None nanot = depi._calc_intrisic_nanotime(n, τ, fract, rg) assert np.allclose(nanot.mean(), τ, rtol=5e-3) n = int(1e6) τ = np.array([1, 7]) fract = np.array([0.3, 0.7]) nanot = depi._calc_intrisic_nanotime(n, τ, fract, rg) assert np.allclose(sum(fract * τ), nanot.mean(), rtol=1e-2)
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'])
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))