Ejemplo n.º 1
0
def test_map_interpolation(tolerance=0.1):
    import logging
    logger = logging.getLogger("condor")
    logger.setLevel("DEBUG")
    src = condor.Source(wavelength=10.0E-9,
                        pulse_energy=1E-3,
                        focus_diameter=1E-6)
    det = condor.Detector(distance=1.0, pixel_size=300E-6, nx=256, ny=256)
    par = condor.ParticleMap(diameter=600E-9,
                             material_type="cell",
                             geometry="custom",
                             map3d_filename=here + "/examples/map3d.h5",
                             map3d_dataset="data",
                             dx=5E-9)
    s = "particle_map"
    E = condor.Experiment(src, {s: par}, det)
    res0 = E.propagate()
    I0 = res0["entry_1"]["data_1"]["data"]
    # Now without interpolation
    print "NOW WITHOUT INTERPOLATION"
    condor.particle.particle_map.ENABLE_MAP_INTERPOLATION = False
    src = condor.Source(wavelength=10.0E-9,
                        pulse_energy=1E-3,
                        focus_diameter=1E-6)
    det = condor.Detector(distance=1.0, pixel_size=300E-6, nx=256, ny=256)
    par = condor.ParticleMap(diameter=600E-9,
                             material_type="cell",
                             geometry="custom",
                             map3d_filename=here + "/examples/map3d.h5",
                             map3d_dataset="data",
                             dx=5E-9)
    s = "particle_map"
    E = condor.Experiment(src, {s: par}, det)
    res1 = E.propagate()
    I1 = res1["entry_1"]["data_1"]["data"]
    import matplotlib.pyplot as pypl
    pypl.imsave("./Imap_interp.png", abs(I0), vmin=0, vmax=I0.max())
    pypl.imsave("./Imap_no_interp.png", abs(I1), vmin=0, vmax=I0.max())
    err = abs(I0 - I1).sum() / ((I0 + I1).sum() / 2.)
    if err < tolerance:
        print "\t=> Test successful (err = %e)" % err
        return False
    else:
        print "\t=> Test failed (err = %e)" % err
        return True
Ejemplo n.º 2
0
            numpy.fft.ifftn(res["entry_1"]["data_1"]["data_fourier"]))
        vmin = numpy.log10(res["entry_1"]["data_1"]["data"].max() / 10000.)
        if plotting:
            pypl.imsave(out_dir + "/%s_%2.2fdeg.png" % (s, angle_d),
                        numpy.log10(res["entry_1"]["data_1"]["data"]),
                        vmin=vmin)
            pypl.imsave(out_dir + "/%s_rs_%2.2fdeg.png" % (s, angle_d),
                        abs(real_space))

    if True:
        # Map (spheroid)
        #print("Simulating map (spheroid)")
        par = condor.ParticleMap(diameter=spheroid_diameter,
                                 material_type="water",
                                 flattening=spheroid_flattening,
                                 geometry="spheroid",
                                 rotation_values=rotation_values,
                                 rotation_formalism=rotation_formalism,
                                 rotation_mode=rotation_mode)
        s = "particle_map_spheroid"
        E = condor.Experiment(src, {s: par}, det)
        res = E.propagate()
        real_space = numpy.fft.fftshift(
            numpy.fft.ifftn(res["entry_1"]["data_1"]["data_fourier"]))
        vmin = numpy.log10(res["entry_1"]["data_1"]["data"].max() / 10000.)
        if plotting:
            pypl.imsave(out_dir + "/%s_%2.2f.png" % (s, angle_d),
                        numpy.log10(res["entry_1"]["data_1"]["data"]),
                        vmin=vmin)
            pypl.imsave(out_dir + "/%s_rs_%2.2f.png" % (s, angle_d),
                        abs(real_space))
Ejemplo n.º 3
0
#logger.setLevel("DEBUG")
#logger.setLevel("WARNING")
logger.setLevel("INFO")

N = 1
rotation_formalism="random"
rotation_values = None

# Source
src = condor.Source(wavelength=0.147E-9, pulse_energy=1E-3, focus_diameter=1E-6)
# Detector
det = condor.Detector(distance=0.9, pixel_size=400E-6, nx=250, ny=250)
# Map
#print("Simulating map")
par = condor.ParticleMap(diameter=None, material_type="poliovirus", geometry="custom",
                         emd_id="1144",
                         rotation_formalism=rotation_formalism, rotation_values=rotation_values)
s = "particle_map"
E = condor.Experiment(src, {s : par}, det)

W = condor.utils.cxiwriter.CXIWriter("./condor.cxi")
for i in range(N):
    t = time.time()
    res = E.propagate()
    #print(time.time()-t)
    if plotting:
        real_space = numpy.fft.fftshift(numpy.fft.ifftn(res["entry_1"]["data_1"]["data_fourier"]))
        pypl.imsave(this_dir + "/simple_test_%s_%i.png" % (s,i), numpy.log10(res["entry_1"]["data_1"]["data"]))
        pypl.imsave(this_dir + "/simple_test_%s_%i_rs.png" % (s,i), abs(real_space))
    W.write(res)
W.close()
Ejemplo n.º 4
0
pixelsize = 110e-6
distance = 2.4
dx = 3.76e-10

# Source
src = condor.Source(wavelength=wavelength,
                    pulse_energy=pulse_energy,
                    focus_diameter=focus_diameter)
# Detector
det = condor.Detector(distance=distance, pixel_size=pixelsize, nx=414, ny=414)
# Map
print "Simulating map"
par = condor.ParticleMap(diameter=40E-9,
                         material_type="poliovirus",
                         geometry="custom",
                         map3d_filename="../../map3d.h5",
                         map3d_dataset="data",
                         dx=dx,
                         rotation_formalism=rotation_formalism,
                         rotation_values=rotation_values)
s = "particle_map"
E = condor.Experiment(src, {s: par}, det)

W = condor.utils.cxiwriter.CXIWriter("./condor.cxi")
for i in range(N):
    res = E.propagate()
    real_space = numpy.fft.fftshift(
        numpy.fft.ifftn(res["entry_1"]["data_1"]["data_fourier"]))
    pypl.imsave(this_dir + "/simple_test_%s_%i.png" % (s, i),
                numpy.log10(res["entry_1"]["data_1"]["data"]))
    pypl.imsave(this_dir + "/simple_test_%s_%i_rs.png" % (s, i),
                abs(real_space))
Ejemplo n.º 5
0
D = condor.Detector(pixel_size=75E-6, nx=1024, ny=1024, distance=0.15)

# Build 3D map for particle model
dx = 0.25*D.get_resolution_element_x(S.photon.get_wavelength())
N = int(numpy.round((1.2*diameter_core+2*thickness_shell)/dx))
assert (dx*N) > (1.1*diameter_core+2*thickness_shell)
map3d = numpy.zeros(shape=(2,N,N,N))
X,Y,Z = numpy.mgrid[:N,:N,:N]
R = numpy.sqrt((X-(N-1)/2.)**2+(Y-(N-1)/2.)**2+(Z-(N-1)/2.)**2)*dx
(map3d[0])[R <= diameter_core/2.] = 1.
if thickness_shell > dx:
    (map3d[1])[(R > diameter_core/2.)*(R <= (diameter_core+thickness_shell*2)/2.)] = 1.

# Initialise particle class instance
P = condor.ParticleMap(geometry='custom',
                       material_type=['custom','custom'], 
                       dx=dx, map3d=map3d,
                       atomic_composition=[atomic_composition_core, atomic_composition_shell],
                       massdensity=[massdensity_core, massdensity_shell])
    
# Initialise experiment class instance
E = condor.Experiment(source=S, particles={"particle_map": P}, detector=D)

# Calculate diffraction pattern
res = E.propagate()

# Images for plotting
img_intensities = res["entry_1"]["data_1"]["data"]
img_fourier = res["entry_1"]["data_1"]["data_fourier"]
real_space = numpy.fft.fftshift(numpy.fft.fft2(numpy.fft.fftshift(img_fourier)))
Ejemplo n.º 6
0
this_dir = os.path.dirname(os.path.realpath(__file__))

do_plot = False

if do_plot:
    import matplotlib
    matplotlib.use('TkAgg')
    from matplotlib import pyplot
    from matplotlib.colors import LogNorm

src = condor.Source(wavelength=0.1E-10, pulse_energy=1E-3, focus_diameter=1E-6)
ds = 16
det = condor.Detector(distance=2., pixel_size=110E-6*ds, nx=1024/ds+1, ny=1024/ds+1, solid_angle_correction=False)

psphere = {"particle_sphere": condor.ParticleSphere(diameter=1E-9, material_type="water")}
pmap = {"particle_map": condor.ParticleMap(diameter=1E-9, material_type="water", geometry="icosahedron")}
patoms = {"particle_atoms": condor.ParticleAtoms(pdb_filename="%s/../../DNA.pdb" % this_dir)}
          
particles = [psphere, pmap, patoms]

if do_plot:
    fig, (axs1, axs2) = pyplot.subplots(2, len(particles), figsize=(3*len(particles), 3*2))

for i,par in enumerate(particles): 

    E = condor.Experiment(src, par, det)
    res = E.propagate()
    data = res["entry_1"]["data_1"]["data"]
    if do_plot:
        axs1[i].set_title("2D: " + par.keys()[0])
        lims = (data.min(), data.max())
Ejemplo n.º 7
0
def test_compare_atoms_with_map(tolerance=0.1):
    """
    Compare the output of two diffraction patterns, one simulated with descrete atoms (spsim) and the other one from a 3D refractive index map on a regular grid.
    """
    src = condor.Source(wavelength=0.1E-9,
                        pulse_energy=1E-3,
                        focus_diameter=1E-6)
    det = condor.Detector(distance=0.5,
                          pixel_size=750E-6,
                          nx=100,
                          ny=100,
                          cx=45,
                          cy=59)
    angle_d = 72.
    angle = angle_d / 360. * 2 * numpy.pi
    rotation_axis = numpy.array([0.43, 0.643, 0.])
    rotation_axis = rotation_axis / condor.utils.linalg.length(rotation_axis)
    quaternion = condor.utils.rotation.quat(angle, rotation_axis[0],
                                            rotation_axis[1], rotation_axis[2])
    rotation_values = numpy.array([quaternion])
    rotation_formalism = "quaternion"
    rotation_mode = "extrinsic"
    short_diameter = 25E-9 * 12 / 100.
    long_diameter = 2 * short_diameter
    N_long = 20
    N_short = int(round(short_diameter / long_diameter * N_long))
    dx = long_diameter / (N_long - 1)
    massdensity = condor.utils.material.get_atomic_mass(
        "H") * scipy.constants.value("atomic mass constant") / dx**3
    # Map
    map3d = numpy.zeros(shape=(N_long, N_long, N_long))
    map3d[:N_short, :, :N_short] = 1.
    map3d[N_short:N_short + N_short, :N_short, :N_short] = 1.
    par = condor.ParticleMap(diameter=long_diameter,
                             material_type="custom",
                             massdensity=massdensity,
                             atomic_composition={"H": 1.},
                             geometry="custom",
                             map3d=map3d,
                             dx=dx,
                             rotation_values=rotation_values,
                             rotation_formalism=rotation_formalism,
                             rotation_mode=rotation_mode)
    s = "particle_map_custom"
    E = condor.Experiment(src, {s: par}, det)
    res = E.propagate()
    F_map = res["entry_1"]["data_1"]["data_fourier"]
    # Atoms
    Z1, Y1, X1 = numpy.meshgrid(numpy.linspace(0, short_diameter, N_short),
                                numpy.linspace(0, long_diameter, N_long),
                                numpy.linspace(0, short_diameter, N_short),
                                indexing="ij")
    Z2, Y2, X2 = numpy.meshgrid(numpy.linspace(0, short_diameter, N_short) +
                                long_diameter / 2.,
                                numpy.linspace(0, short_diameter, N_short),
                                numpy.linspace(0, short_diameter, N_short),
                                indexing="ij")
    Z = numpy.concatenate((Z1.ravel(), Z2.ravel()))
    Y = numpy.concatenate((Y1.ravel(), Y2.ravel()))
    X = numpy.concatenate((X1.ravel(), X2.ravel()))
    atomic_positions = numpy.array(
        [[x, y, z] for x, y, z in zip(X.ravel(), Y.ravel(), Z.ravel())])
    atomic_numbers = numpy.ones(atomic_positions.size // 3, dtype=numpy.int16)
    par = condor.ParticleAtoms(atomic_positions=atomic_positions,
                               atomic_numbers=atomic_numbers,
                               rotation_values=rotation_values,
                               rotation_formalism=rotation_formalism,
                               rotation_mode=rotation_mode)
    s = "particle_atoms"
    E = condor.Experiment(src, {s: par}, det)
    res = E.propagate()
    F_atoms = res["entry_1"]["data_1"]["data_fourier"]
    # Compare
    I_atoms = abs(F_atoms)**2
    I_map = abs(F_map)**2
    diff = I_atoms - I_map
    err = abs(diff).sum() / ((I_atoms.sum() + I_map.sum()) / 2.)
    if SAVE_OUTPUT:
        import matplotlib.pyplot as pypl
        pypl.imsave("./Iatoms_mol.png", abs(I_atoms))
        pypl.imsave("./Iatoms_map.png", abs(I_map))
    assert err < tolerance
Ejemplo n.º 8
0
def test_compare_spheroid_with_map(tolerance=0.15):
    """
    Compare the output of two diffraction patterns, one simulated with the direct formula and the other one from a 3D refractive index map on a regular grid
    """
    src = condor.Source(wavelength=0.1E-9,
                        pulse_energy=1E-3,
                        focus_diameter=1E-6)
    det = condor.Detector(distance=0.5,
                          pixel_size=750E-6,
                          nx=100,
                          ny=100,
                          cx=45,
                          cy=59)
    angle_d = 72.
    angle = angle_d / 360. * 2 * numpy.pi
    rotation_axis = numpy.array([0.43, 0.643, 0.])
    rotation_axis = rotation_axis / condor.utils.linalg.length(rotation_axis)
    quaternion = condor.utils.rotation.quat(angle, rotation_axis[0],
                                            rotation_axis[1], rotation_axis[2])
    rotation_values = numpy.array([quaternion])
    rotation_formalism = "quaternion"
    rotation_mode = "extrinsic"
    short_diameter = 25E-9 * 12 / 100.
    long_diameter = 2 * short_diameter
    spheroid_diameter = condor.utils.spheroid_diffraction.to_spheroid_diameter(
        short_diameter / 2., long_diameter / 2.)
    spheroid_flattening = condor.utils.spheroid_diffraction.to_spheroid_flattening(
        short_diameter / 2., long_diameter / 2.)
    # Ideal spheroid
    par = condor.ParticleSpheroid(diameter=spheroid_diameter,
                                  material_type="water",
                                  flattening=spheroid_flattening,
                                  rotation_values=rotation_values,
                                  rotation_formalism=rotation_formalism,
                                  rotation_mode=rotation_mode)
    s = "particle_spheroid"
    E = condor.Experiment(src, {s: par}, det)
    res = E.propagate()
    F_ideal = res["entry_1"]["data_1"]["data_fourier"]
    # Map (spheroid)
    par = condor.ParticleMap(diameter=spheroid_diameter,
                             material_type="water",
                             flattening=spheroid_flattening,
                             geometry="spheroid",
                             rotation_values=rotation_values,
                             rotation_formalism=rotation_formalism,
                             rotation_mode=rotation_mode)
    s = "particle_map_spheroid"
    E = condor.Experiment(src, {s: par}, det)
    res = E.propagate()
    F_map = res["entry_1"]["data_1"]["data_fourier"]
    # Compare
    I_ideal = abs(F_ideal)**2
    I_map = abs(F_map)**2
    if SAVE_OUTPUT:
        import matplotlib.pyplot as pypl
        pypl.imsave("./Ispheroid_sph.png", abs(I_ideal))
        pypl.imsave("./Ispheroid_map.png", abs(I_map))
    diff = I_ideal - I_map
    err = abs(diff).sum() / ((I_ideal.sum() + I_map.sum()) / 2.)
    assert err < tolerance
Ejemplo n.º 9
0
# Save map
with h5py.File("emd_1144.h5", "w") as f:
    f["/dn_map"] = dn_map3d
    f["/dx"] = dx
    f["/wavelength"] = wavelength

# Source
src = condor.Source(wavelength=wavelength,
                    pulse_energy=1E-3,
                    focus_diameter=0.1E-6)
# Detector
det = condor.Detector(distance=0.4, pixel_size=125E-6, nx=512, ny=512)
# Particle
par = condor.ParticleMap(diameter=1E-9,
                         geometry="custom",
                         map3d=dn_map3d,
                         dx=dx,
                         rotation_formalism="quaternion",
                         rotation_values=[0.01, 0.69, 0.69, -0.22])

E = condor.Experiment(src, {"particle_map_1144": par}, det)
res = E.propagate()

W = condor.utils.cxiwriter.CXIWriter("./condor.cxi")
W.write(res)
W.close()

if plotting:
    real_space = numpy.fft.fftshift(
        numpy.fft.ifftn(res["entry_1"]["data_1"]["data_fourier"]))
    pypl.imsave(this_dir + "/intensities.png",
                numpy.log10(res["entry_1"]["data_1"]["data"]))
Ejemplo n.º 10
0
import numpy as np
import scipy.interpolate
import condor

# Number of frames
N = 100

# Dimensions in diffraction space
nx, ny, nz = (100, 100, 100)

S = condor.Source(wavelength=1E-9, focus_diameter=1E-6, pulse_energy=1E-3)
P = condor.ParticleMap(geometry="icosahedron",
                       diameter=100E-9,
                       material_type="cell",
                       rotation_formalism="random")
D = condor.Detector(pixel_size=1000E-6, distance=0.5, nx=nx, ny=ny)

E = condor.Experiment(source=S, particles={"particle_map": P}, detector=D)

points = []
values = []
for i in range(N):
    res = E.propagate()
    img = res["entry_1"]["data_1"]["data_fourier"]
    qmap = E.get_qmap_from_cache()
    c = 2 * np.pi * D.pixel_size / (S.photon.get_wavelength() * D.distance)
    points.append(qmap.reshape((qmap.shape[0] * qmap.shape[1], 3)) / c)
    values.append(img.flatten())
points = np.array(points)
points = points.reshape((points.shape[0] * points.shape[1], 3))
values = np.array(values).flatten()