Exemplo n.º 1
0
def test_Box():
    box = pbm.Box(0, 1, 0, 1, 0, 2)
    assert (box.b == np.array([[0, 1], [0, 1], [0, 2]])).all()
    assert box.volume == 2
    assert box.volume_L == 2000
    box.__repr__()  # smoke test
    box_dict = box.to_dict()
    box2 = pbm.Box(**box_dict)
    assert (box.b == box2.b).all()
    box_json = box.to_json()
    box3 = pbm.Box(**json.loads(box_json))
    assert (box.b == box3.b).all()
Exemplo n.º 2
0
def test_Particles():
    rs = np.random.RandomState(_SEED)
    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)
    D1 = 12e-12
    D2 = D1 / 2
    P = pbm.Particles(num_particles=20, D=D1, box=box, rs=rs)
    P.add(num_particles=15, D=D2)

    Di, counts = list(zip(*P.diffusion_coeff_counts))
    rs2 = np.random.RandomState()
    rs2.set_state(P.init_random_state)
    P2_list = pbm.Particles._generate(num_particles=counts[0],
                                      D=Di[0],
                                      box=P.box,
                                      rs=rs2)
    P2_list += pbm.Particles._generate(num_particles=counts[1],
                                       D=Di[1],
                                       box=P.box,
                                       rs=rs2)
    assert P.to_list() == P2_list

    # Test Particles random states
    assert randomstate_equal(P.rs, rs.get_state())
    assert randomstate_equal(P.init_random_state, np.random.RandomState(_SEED))
    assert not randomstate_equal(P.init_random_state, P.rs)

    # Test JSON serialization
    P_json = P.to_json()
    P3 = pbm.Particles.from_json(P_json)
    assert P.to_list() == P3.to_list()
Exemplo n.º 3
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)
Exemplo n.º 4
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]
Exemplo n.º 5
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
Exemplo n.º 6
0
_SEED = 2345654342

# - GLOBAL SIMULATION PARAMETERS - - - - - - - - - - - - - - - - - - - - - - -

# Diffusion parameters
t_step = 0.5e-6  # (seconds) diffusion simulation time step
t_max = 1  # (seconds) time duration of the diffusion simulation

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

# Simulation box
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 populations
particles_specs = dict(
    # Parameters needed for the diffusion simulation
    num_particles=(1, 3),  # number of particles in each population
    D=(D1, D2),  # (m^2 / s) diffusion coefficiens per population
    box=box,  # simulation box

    # Photo-physics parameters (needed only for timestamps simulation)
    E_values=(0.75, 0.25),  # FRET efficiencies for each population
    em_rates=(200e3, 300e3),  # Peak D+A emission rates (cps) per population

    # Backgroung rates (needed for timestamps simulation)
    bg_rate_d=1500,  # Poisson background rate (cps) Donor channel
    bg_rate_a=800,  # Poisson background rate (cps) Acceptor channel