Example #1
0
def test_strain_mapping_affine_transform():
    latt = diffpy.structure.lattice.Lattice(3, 3, 3, 90, 90, 90)
    atom = diffpy.structure.atom.Atom(atype='Zn', xyz=[0, 0, 0], lattice=latt)
    structure = diffpy.structure.Structure(atoms=[atom], lattice=latt)
    ediff = DiffractionGenerator(300., 0.025)
    affines = [[[1, 0, 0], [0, 1, 0], [0, 0, 1]],
               [[1.04, 0, 0], [0, 1, 0], [0, 0, 1]],
               [[1.08, 0, 0], [0, 1, 0], [0, 0, 1]],
               [[1.12, 0, 0], [0, 1, 0], [0, 0, 1]]]

    data = []
    for affine in affines:
        # same coords as used for latt above
        latt_rot = diffpy.structure.lattice.Lattice(3, 3, 3, 90, 90, 90, baserot=affine)
        structure.placeInLattice(latt_rot)

        diff_dat = ediff.calculate_ed_data(structure, 2.5)
        dpi = sim_as_signal(diff_dat, 64, 0.02, 2.5)
        data.append(dpi.data)
    data = np.array(data)
    dp = ElectronDiffraction2D(data.reshape((2, 2, 64, 64)))

    m = dp.create_model()
    ref = ScalableReferencePattern(dp.inav[0, 0])
    m.append(ref)
    m.multifit()
    disp_grad = ref.construct_displacement_gradient()

    assert disp_grad.data.shape == np.asarray(affines).reshape(2, 2, 3, 3).shape
def generate_diffraction_patterns(structure,edc):
    # generates 4, dumb patterns in a 2x2 array, rotated around c axis
    dp_library = get_template_library(structure, [(90,90,4)], edc)
    for sim in dp_library['A']['simulations']:
        pattern = (sim_as_signal(sim, 2 * half_side_length, 0.025, 1).data)
    dp = pxm.ElectronDiffraction2D([[pattern, pattern], [pattern, pattern]])
    return dp
Example #3
0
def get_template_match_results(structure,
                               pattern_list,
                               edc,
                               rot_list,
                               mask=None,
                               inplane_rotations=[0]):
    dp_library = get_template_library(structure, pattern_list, edc)
    for sim in dp_library['A']['simulations']:
        pattern = (sim_as_signal(sim, 2 * half_side_length, 0.025, 1).data)
    dp = pxm.ElectronDiffraction2D([[pattern, pattern], [pattern, pattern]])
    library = get_template_library(structure, rot_list, edc)
    indexer = IndexationGenerator(dp, library)
    return indexer.correlate(mask=mask, inplane_rotations=inplane_rotations)
Example #4
0
def test_marker_placement_correct_beta():
    dps = []
    dp_cord_list = np.divide(generate_dp_cord_list(), 80)
    max_r = np.max(dp_cord_list) + 0.1
    for coords in dp_cord_list:
        dp_sim = DiffractionSimulation(coordinates=coords,
                                       intensities=np.ones_like(coords[:, 0]))
        dps.append(sim_as_signal(dp_sim, 144, 0.025, max_r).data)  # stores a numpy array of pattern
    dp = pxm.ElectronDiffraction2D(np.array([dps[0:2], dps[2:]]))  # now from a 2x2 array of patterns
    dp.set_diffraction_calibration(2 * max_r / (144))
    local_plotter(dp, dp_cord_list)

    # This is human assessed, if you see this comment, you should check it
    assert True
def create_library_and_diffraction_pattern():
    """ This creates a library, the first 4 entries of which are used to
    create the relevant diffraction patterns, we then test the we get suitable
    results for matching """

    dps = []
    half_shape = (72, 72)
    num_orientations = 11
    simulations = np.empty(num_orientations, dtype="object")
    orientations = np.empty(num_orientations, dtype="object")
    pixel_coords = np.empty(num_orientations, dtype="object")
    intensities = np.empty(num_orientations, dtype="object")

    # Creating the matchresults.
    for alpha in np.arange(11):
        coords = (np.random.rand(5, 2) -
                  0.5) * 2  # zero mean, range from -1 to +1
        simulated_pattern = DiffractionSimulation(
            coordinates=coords,
            intensities=np.ones_like(coords[:, 0]),
            calibration=1 / 72,
        )

        simulations[alpha] = simulated_pattern
        orientations[alpha] = (alpha, alpha, alpha)
        intensities[alpha] = simulated_pattern.intensities
        pixel_coords[alpha] = (
            simulated_pattern.calibrated_coordinates[:, :2] +
            half_shape).astype(int)

        if alpha < 4:
            z = sim_as_signal(simulated_pattern, 2 * 72, 0.075, 1)
            dps.append(z.data)  # stores a numpy array of pattern

    dp = pxm.ElectronDiffraction2D([dps[0:2], dps[2:]
                                    ])  # now from a 2x2 array of patterns
    library = DiffractionLibrary()
    library["Phase"] = {
        "simulations": simulations,
        "orientations": orientations,
        "pixel_coords": pixel_coords,
        "intensities": intensities,
    }

    return dp, library
def update_pattern(_=None):
    reciprocal_angstrom_per_pixel = slider_scale.val
    simulated_gaussian_sigma = slider_sigma.val

    phi = np.deg2rad(slider_phi.val)
    theta = np.deg2rad(slider_theta.val)
    psi = np.deg2rad(slider_psi.val)

    structure_rotation = euler2mat(phi, theta, psi, axes='rzxz')

    structure = structures[current_structure]['structure']
    lattice_rotated = diffpy.structure.lattice.Lattice(
        structure.lattice.a,
        structure.lattice.b,
        structure.lattice.c,
        structure.lattice.alpha,
        structure.lattice.beta,
        structure.lattice.gamma,
        baserot=structure_rotation)

    # Don't change the original
    structure_rotated = diffpy.structure.Structure(structure)
    structure_rotated.placeInLattice(lattice_rotated)

    reciprocal_radius = reciprocal_angstrom_per_pixel * (half_pattern_size - 1)
    sim = gen.calculate_ed_data(structure_rotated,
                                reciprocal_radius,
                                with_direct_beam=False)
    # sim.intensities = np.full(sim.direct_beam_mask.shape, 1)  # For testing, ignore intensities
    # sim._intensities = np.log(1 + sim._intensities)
    s = sim_as_signal(sim, target_pattern_dimension_pixels,
                      simulated_gaussian_sigma, reciprocal_radius)
    img.set_data(s.data)

    max_theta = np.deg2rad(5)
    resolution = np.deg2rad(10)
    rotation_matrices = generate_rotation_list(structure, phi, theta, psi,
                                               max_theta, resolution)
    update_rotation(rotation_matrices_to_euler(rotation_matrices), None)

    fig.canvas.draw_idle()
Example #7
0
def create_library():
    dps = []
    half_side_length = 72
    half_shape = (half_side_length, half_side_length)
    num_orientations = 11
    simulations = np.empty(num_orientations, dtype='object')
    orientations = np.empty(num_orientations, dtype='object')
    pixel_coords = np.empty(num_orientations, dtype='object')
    intensities = np.empty(num_orientations, dtype='object')

    # Creating the matchresults.
    for alpha in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:
        coords = (np.random.rand(5, 2) -
                  0.5) * 2  # zero mean, range from -1 to +1
        dp_sim = DiffractionSimulation(coordinates=coords,
                                       intensities=np.ones_like(coords[:, 0]),
                                       calibration=1 / half_side_length)
        simulations[alpha] = dp_sim
        orientations[alpha] = (alpha, alpha, alpha)
        pixel_coords[alpha] = (dp_sim.calibrated_coordinates[:, :2] +
                               half_shape).astype(int)
        intensities[alpha] = dp_sim.intensities
        if alpha < 4:
            dps.append(
                sim_as_signal(dp_sim, 2 * half_side_length, 0.075,
                              1).data)  # stores a numpy array of pattern

    library = DiffractionLibrary()
    library["Phase"] = {
        'simulations': simulations,
        'orientations': orientations,
        'pixel_coords': pixel_coords,
        'intensities': intensities,
    }
    dp = pxm.ElectronDiffraction2D([dps[0:2], dps[2:]
                                    ])  # now from a 2x2 array of patterns
    return dp, library