def test_calibrated_coordinates(
         self, diffraction_simulation: DiffractionSimulation, coordinates,
         calibration, offset, expected):
     diffraction_simulation.coordinates = coordinates
     diffraction_simulation.calibration = calibration
     diffraction_simulation.offset = offset
     assert np.allclose(diffraction_simulation.calibrated_coordinates,
                        expected)
Beispiel #2
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(dp_sim.as_signal(144, 0.025, max_r).data)  # stores a numpy array of pattern
    dp = pxm.ElectronDiffraction(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
Beispiel #3
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(
                dp_sim.as_signal(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.ElectronDiffraction([dps[0:2],
                                  dps[2:]])  # now from a 2x2 array of patterns
    return dp, library
Beispiel #4
0
        np.dot(p.intensities, p.intensities))
    return library


dps, dp_sim_list = [], []
half_side_length = 72
library = dict()
half_shape = (half_side_length, half_side_length)
library["Phase"] = {}

# Creating the matchresults.

for alpha in [0, 1, 2, 3]:
    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)
    dp_sim_list.append(dp_sim)  #stores the simulations
    dps.append(dp_sim.as_signal(2 * half_side_length, 0.075,
                                1).data)  #stores a numpy array of pattern

dp = pxm.ElectronDiffraction([dps[0:2],
                              dps[2:]])  #now from a 2x2 array of patterns

for alpha in np.arange(0, 10, 1):
    rotation = (alpha, 0, 0)
    if rotation[0] < 4:
        library = create_library_entry(library, rotation,
                                       dp_sim_list[rotation[0]])
    else:
        local_cords = np.random.rand(5, 2)
def coords_intensity_simulation():
    return DiffractionSimulation(coordinates=np.asarray([[0.3, 1.2, 0]]),
                                 intensities=np.ones(1))
def test_wrong_calibration_setting():
    DiffractionSimulation(coordinates=np.asarray([[0.3, 1.2, 0]]),
                          intensities=np.ones(1),
                          calibration=[1, 2, 5])
 def diffraction_simulation(self):
     return DiffractionSimulation()
Beispiel #8
0
    def calculate_ed_data(self,
                          structure,
                          reciprocal_radius,
                          with_direct_beam=True):
        """Calculates the Electron Diffraction data for a structure.

        Parameters
        ----------
        structure : Structure
            The structure for which to derive the diffraction pattern. Note that
            the structure must be rotated to the appropriate orientation and
            that testing is conducted on unit cells (rather than supercells).
        reciprocal_radius : float
            The maximum radius of the sphere of reciprocal space to sample, in
            reciprocal angstroms.

        Returns
        -------
        pyxem.DiffractionSimulation
            The data associated with this structure and diffraction setup.

        """
        # Specify variables used in calculation
        wavelength = self.wavelength
        max_excitation_error = self.max_excitation_error
        debye_waller_factors = self.debye_waller_factors
        latt = structure.lattice
        scattering_params = self.scattering_params

        # Obtain crystallographic reciprocal lattice points within `max_r` and
        # g-vector magnitudes for intensity calculations.
        recip_latt = latt.reciprocal()
        spot_indicies, cartesian_coordinates, spot_distances = get_points_in_sphere(
            recip_latt, reciprocal_radius)

        # Identify points intersecting the Ewald sphere within maximum
        # excitation error and store the magnitude of their excitation error.
        r_sphere = 1 / wavelength
        r_spot = np.sqrt(np.sum(np.square(cartesian_coordinates[:, :2]), axis=1))
        z_sphere = -np.sqrt(r_sphere**2 - r_spot**2) + r_sphere
        proximity = np.absolute(z_sphere - cartesian_coordinates[:, 2])
        intersection = proximity < max_excitation_error
        # Mask parameters corresponding to excited reflections.
        intersection_coordinates = cartesian_coordinates[intersection]
        intersection_indices = spot_indicies[intersection]
        proximity = proximity[intersection]
        g_hkls = spot_distances[intersection]

        # Calculate diffracted intensities based on a kinematical model.
        intensities = get_kinematical_intensities(structure,
                                                  intersection_indices,
                                                  g_hkls,
                                                  proximity,
                                                  max_excitation_error,
                                                  debye_waller_factors,
                                                  scattering_params)

        # Threshold peaks included in simulation based on minimum intensity.
        peak_mask = intensities > 1e-20
        intensities = intensities[peak_mask]
        intersection_coordinates = intersection_coordinates[peak_mask]
        intersection_indices = intersection_indices[peak_mask]

        return DiffractionSimulation(coordinates=intersection_coordinates,
                                     indices=intersection_indices,
                                     intensities=intensities,
                                     with_direct_beam=with_direct_beam)