示例#1
0
def test_NumericPSF_hash_roundtrip():
    psf = pbm.NumericPSF()
    with tables.open_file('psfntest.h5', mode='w') as h5:
        psf.to_hdf5(h5)
    with tables.open_file('psfntest.h5', mode='r') as h5:
        a = h5.get_node('/xz_realistic_z50_150_160_580nm_n1335_HR2')
        assert pbm.NumericPSF(psf_pytables=a).hash() == psf.hash()
示例#2
0
def test_diffusion_sim_random_state():
    # Initialize the random state
    rs = np.random.RandomState(_SEED)

    # Diffusion coefficient
    Du = 12.0  # um^2 / s
    D1 = Du * (1e-6)**2  # m^2 / s
    D2 = D1 / 2

    # Simulation box definition
    box = pbm.Box(x1=-4.e-6, x2=4.e-6, y1=-4.e-6, y2=4.e-6, z1=-6e-6, z2=6e-6)

    # PSF definition
    psf = pbm.NumericPSF()

    # Particles definition
    P = pbm.Particles(num_particles=20, D=D1, box=box, rs=rs)
    P.add(num_particles=15, D=D2)

    # Simulation time step (seconds)
    t_step = 0.5e-6

    # Time duration of the simulation (seconds)
    t_max = 0.01

    # Particle simulation definition
    S = pbm.ParticlesSimulation(t_step=t_step,
                                t_max=t_max,
                                particles=P,
                                box=box,
                                psf=psf)

    rs_prediffusion = rs.get_state()
    S.simulate_diffusion(total_emission=False,
                         save_pos=True,
                         verbose=True,
                         rs=rs,
                         chunksize=2**13,
                         chunkslice='times')
    rs_postdiffusion = rs.get_state()

    # Test diffusion random states
    saved_rs = S.traj_group._v_attrs['init_random_state']
    assert randomstate_equal(saved_rs, rs_prediffusion)
    saved_rs = S.traj_group._v_attrs['last_random_state']
    assert randomstate_equal(saved_rs, rs_postdiffusion)
示例#3
0
def create_diffusion_sim():
    rs = np.random.RandomState(_SEED)
    Du = 12.0  # um^2 / s
    D = Du * (1e-6)**2  # m^2 / s
    box = pbm.Box(x1=-4.e-6, x2=4.e-6, y1=-4.e-6, y2=4.e-6, z1=-6e-6, z2=6e-6)
    psf = pbm.NumericPSF()
    P = pbm.Particles(num_particles=100, D=D, box=box, rs=rs)
    t_step = 0.5e-6
    t_max = 0.1
    S = pbm.ParticlesSimulation(t_step=t_step,
                                t_max=t_max,
                                particles=P,
                                box=box,
                                psf=psf)
    S.simulate_diffusion(save_pos=True,
                         total_emission=False,
                         radial=True,
                         rs=rs)
    S.store.close()
    return S.hash()[:6]
示例#4
0
def create_diffusion_sim(psf=pbm.NumericPSF()):
    rs = np.random.RandomState(_SEED)
    specs = {
        k: v
        for k, v in particles_specs.items()
        if k in ['num_particles', 'D', 'box']
    }
    P = pbm.Particles.from_specs(**specs, rs=rs)

    S = pbm.ParticlesSimulation(t_step=t_step,
                                t_max=t_max,
                                particles=P,
                                box=box,
                                psf=psf)
    S.simulate_diffusion(save_pos=True,
                         total_emission=False,
                         radial=False,
                         rs=rs)
    S.store.close()
    return S.hash()[:6]
示例#5
0
def test_TimestampSimulation():
    for psf in (pbm.GaussianPSF(), pbm.NumericPSF()):
        hash_ = create_diffusion_sim(psf)
        S = pbm.ParticlesSimulation.from_datafile(hash_, mode='a')

        params = dict(
            em_rates=(400e3,
                      ),  # Peak emission rates (cps) for each population (D+A)
            E_values=(0.75, ),  # FRET efficiency for each population
            num_particles=(1, ),  # Number of particles in each population
            bg_rate_d=1400,  # Poisson background rate (cps) Donor channel
            bg_rate_a=800,  # Poisson background rate (cps) Acceptor channel
        )

        mix_sim = pbm.TimestampSimulation(S, **params)
        mix_sim.summarize()

        rs = np.random.RandomState(_SEED)
        mix_sim.run(rs=rs, overwrite=True)
        mix_sim.save_photon_hdf5()
示例#6
0
def test_diffusion_sim_core():
    # Initialize the random state
    rs = np.random.RandomState(_SEED)
    Du = 12.0  # um^2 / s
    D = Du * (1e-6)**2  # m^2 / s
    box = pbm.Box(x1=-4.e-6, x2=4.e-6, y1=-4.e-6, y2=4.e-6, z1=-6e-6, z2=6e-6)
    psf = pbm.NumericPSF()
    P = pbm.Particles(num_particles=100, D=D, box=box, rs=rs)
    t_step = 0.5e-6
    t_max = 0.001
    time_size = t_max / t_step
    assert t_max < 1e4
    S = pbm.ParticlesSimulation(t_step=t_step,
                                t_max=t_max,
                                particles=P,
                                box=box,
                                psf=psf)

    start_pos = [p.r0 for p in S.particles]
    start_pos = np.vstack(start_pos).reshape(S.num_particles, 3, 1)

    for wrap_func in [pbm.diffusion.wrap_mirror, pbm.diffusion.wrap_periodic]:
        for total_emission in [True, False]:
            sim = S._sim_trajectories(time_size,
                                      start_pos,
                                      rs=rs,
                                      total_emission=total_emission,
                                      save_pos=True,
                                      wrap_func=wrap_func)

    POS, em = sim
    POS = np.concatenate(POS, axis=0)
    #x, y, z = POS[:, :, 0], POS[:, :, 1], POS[:, :, 2]
    #r_squared = x**2 + y**2 + z**2

    DR = np.diff(POS, axis=2)
    dx, dy, dz = DR[:, :, 0], DR[:, :, 1], DR[:, :, 2]
    dr_squared = dx**2 + dy**2 + dz**2

    D_fitted = dr_squared.mean() / (6 * t_max)  # Fitted diffusion coefficient
    assert np.abs(D - D_fitted) < 0.01
示例#7
0
def test_diffusion_sim_random_state():
    for psf in (pbm.NumericPSF(), pbm.GaussianPSF()):
        # Initialize the random state
        rs = np.random.RandomState(_SEED)

        # Particles definition
        P = pbm.Particles.from_specs(num_particles=(5, 7),
                                     D=(D1, D2),
                                     box=box,
                                     rs=rs)

        # Time duration of the simulation (seconds)
        t_max = 0.01

        # Particle simulation definition
        S = pbm.ParticlesSimulation(t_step=t_step,
                                    t_max=t_max,
                                    particles=P,
                                    box=box,
                                    psf=psf)

        rs_prediffusion = rs.get_state()
        S.simulate_diffusion(total_emission=False,
                             save_pos=True,
                             verbose=True,
                             rs=rs,
                             chunksize=2**13,
                             chunkslice='times')
        rs_postdiffusion = rs.get_state()

        # Test diffusion random states
        saved_rs = S.traj_group._v_attrs['init_random_state']
        assert randomstate_equal(saved_rs, rs_prediffusion)
        saved_rs = S.traj_group._v_attrs['last_random_state']
        assert randomstate_equal(saved_rs, rs_postdiffusion)
        S.store.close()
示例#8
0
def test_diffusion_sim_core_npsf():
    _test_diffusion_sim_core(pbm.NumericPSF())
示例#9
0
# Simulation box definition
box = pbm.Box(x1=-4.e-6, x2=4.e-6, y1=-4.e-6, y2=4.e-6, z1=-6e-6, z2=6e-6)

# Particles definition
n1 = 10
n2 = 0
#nn = n1+n2
P = pbm.Particles.from_specs(num_particles=(n1,)
        ,D=(D1,)
        ,box=box
        ,rs=rs)

# PSF definition
#psf = pbm.NumericPSF()
#psf = pbm.GaussianPSF(sx=0.3e-6,sy=0.3e-6,sz=0.5e-6)
psf = pbm.NumericPSF()

# Particle simulation definition
S = pbm.ParticlesSimulation(t_step=t_step, t_max=t_max, 
                            particles=P, box=box, psf=psf)

###Run simulation:
#Note that the save_pos=True seems to be very important in order to use the
#particle simulation visualizer (PSV)
print('Current random state:', pbm.hashfunc(rs.get_state()))
S.simulate_diffusion(total_emission=False, save_pos=True, verbose=True,
                     rs=rs, chunksize=2**19, chunkslice='times')
print('Current random state:', pbm.hashfunc(rs.get_state()))
print(S.compact_name())

#%% Simulate FRET: