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_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 _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_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_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_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 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)
def _get_randomgens(num_threads): """Cached generation of random number generators, enables ``random_seed_fn`` functionality and greater efficiency. """ global _RANDOM_GENS num_gens = len(_RANDOM_GENS) if num_gens < num_threads: from randomgen import Xoroshiro128 # add more generators if not enough for _ in range(num_threads - num_gens): _RANDOM_GENS.append(Xoroshiro128()) return _RANDOM_GENS[:num_threads]
def test_background(): N = 100000 burstsph, meta = load_burstsph() burstsph = burstsph.iloc[:N] ns = nm = 1. params = dict( name='gaussian', # physical parameters R_mean=6.5 * nm, R_sigma=0.01 * nm, R0=6 * nm, τ_relax=200 * ns, τ_D=[3.8 * ns, 1 * ns], D_fract=[0.5, 0.5], τ_A=[4 * ns, 1 * ns], A_fract=[0.3, 0.7], bg_rate_d=1000, bg_rate_a=1500, ts_unit=meta['timestamp_unit'], tcspc_unit=30 * ns, # simulation parameters δt=1e-2 * ns, ndt=10, α=0.1, gamma=0.6, lk=0.4, dir_ex_t=0.3, ) rg = RandomGenerator(Xoroshiro128(1)) burstsph_sim = depi.recolor_burstsph(burstsph.timestamp, rg=rg, **params) def func(burst): bg_counts = burst.bg_ph.sum() bg_counts_d = burst.bg_ph.loc[burst.stream == 'DexDem'].sum() bg_counts_a = burst.bg_ph.loc[burst.stream == 'DexAem'].sum() burst_width = (burst.timestamp.iloc[-1] - burst.timestamp.iloc[0]) * 1e-9 bg_rate = bg_counts / burst_width bg_rate_d = bg_counts_d / burst_width bg_rate_a = bg_counts_a / burst_width return pd.Series([bg_rate, bg_rate_d, bg_rate_a], index=['bg', 'bg_d', 'bg_a']) bg = burstsph_sim.groupby('burst').apply(func) assert np.allclose(bg.bg.mean(), 2500, atol=0, rtol=3e-2) assert np.allclose(bg.bg_d.mean(), 1000, atol=0, rtol=6e-2) assert np.allclose(bg.bg_a.mean(), 1500, atol=0, rtol=6e-2)
def test_corrections(): N = 50000 burstsph, _ = load_burstsph() burstsph = burstsph.iloc[:N] ns = nm = 1. params = dict( name='gaussian', # physical parameters R_mean=[6.37 * nm, 4 * ns], R_sigma=[0.01 * nm] * 2, R0=6. * nm, τ_relax=[200 * ns] * 2, τ_D=[3.8 * ns, 1 * ns], D_fract=[0.5, 0.5], τ_A=4 * ns, k_s=[1, 1], # simulation parameters δt=1e-2 * ns, ndt=10, α=0.1, gamma=2, lk=0.3, dir_ex_t=0.4, ) # 2-states Gaussian rg = RandomGenerator(Xoroshiro128(1)) burstsph_sim = depi.recolor_burstsph(burstsph.timestamp, rg=rg, **params) E_corr = fret.E_from_dist(np.array(params['R_mean']), R0=params['R0']) state0 = burstsph_sim.state == 0 state1 = burstsph_sim.state == 1 FA = burstsph_sim.stream == 'DexAem' Eraw0 = (FA & state0).sum() / state0.sum() Eraw1 = (FA & state1).sum() / state1.sum() Eraw = np.array([Eraw0, Eraw1]) Eraw2 = fret.uncorrect_E_gamma_leak_dir(E_corr, params['gamma'], params['lk'], dir_ex_t=params['dir_ex_t']) assert np.allclose(Eraw, Eraw2, atol=5e-3, rtol=0) # 1-state Gaussian params.update(R_mean=params['R_mean'][0], R_sigma=params['R_sigma'][0], τ_relax=params['τ_relax'][0]) burstsph_sim = depi.recolor_burstsph(burstsph.timestamp, rg=rg, **params) E_corr = fret.E_from_dist(np.array(params['R_mean']), R0=params['R0']) FA = (burstsph_sim.stream == 'DexAem').sum() Eraw = FA / burstsph_sim.shape[0] Eraw2 = fret.uncorrect_E_gamma_leak_dir(E_corr, params['gamma'], params['lk'], dir_ex_t=params['dir_ex_t']) assert np.allclose(Eraw, Eraw2, atol=5e-3, rtol=0) # 1-state Radial Gaussian params.update(name='radial_gaussian', mu=4 * nm, sigma=0.01 * nm, offset=1, du=0.01, u_max=6., dr=0.001) del params['R_mean'], params['R_sigma'] d = dd.distribution(params) E_corr = d.mean_E(params['R0']) rg = RandomGenerator(Xoroshiro128(1)) burstsph_sim = depi.recolor_burstsph(burstsph.timestamp, rg=rg, **params) FA = (burstsph_sim.stream == 'DexAem').sum() Eraw = FA / burstsph_sim.shape[0] Eraw2 = fret.uncorrect_E_gamma_leak_dir(E_corr, params['gamma'], params['lk'], dir_ex_t=params['dir_ex_t']) assert np.allclose(Eraw, Eraw2, atol=5e-3, rtol=0)
from cffi import FFI from randomgen import Xoroshiro128 ffi = FFI() if os.path.exists('./distributions.dll'): lib = ffi.dlopen('./distributions.dll') elif os.path.exists('./libdistributions.so'): lib = ffi.dlopen('./libdistributions.so') else: raise RuntimeError('Required DLL/so file was not found.') ffi.cdef(""" double random_gauss_zig(void *brng_state); """) x = Xoroshiro128() xffi = x.cffi brng = xffi.brng random_gauss_zig = lib.random_gauss_zig def normals(n, brng): out = np.empty(n) for i in range(n): out[i] = random_gauss_zig(brng) return out normalsj = nb.jit(normals, nopython=True)
def recolor_burstsph_cache(timestamp, seed=1, **params): """Cached version of :func:`recolor_burstsph`.""" from randomgen import RandomGenerator, Xoroshiro128 rg = RandomGenerator(Xoroshiro128(seed)) burstsph = recolor_burstsph(timestamp, rg=rg, **params) return burstsph