def peaks_from_best_vector_match(single_match_result, library): """ Takes a VectorMatchingResults object and return the associated peaks, to be used in combination with map(). Parameters ---------- single_match_result : ndarray An entry in a VectorMatchingResults library : DiffractionLibrary Diffraction library containing the phases and rotations Returns ------- peaks : ndarray Coordinates of peaks in the matching results object in calibrated units. """ best_fit = single_match_result[np.argmax(single_match_result[:, 2])] best_index = best_fit[0] rotation_matrix = best_fit[1] # Don't change the original structure = library.structures[best_index] sim = simulate_rotated_structure(library.diffraction_generator, structure, rotation_matrix, library.reciprocal_radius, with_direct_beam=False) # Cut z return sim.coordinates[:, :2]
def peaks_from_best_vector_match(single_match_result, library, rank=0): """Takes a VectorMatchingResults object and return the associated peaks, to be used in combination with map(). Parameters ---------- single_match_result : ndarray An entry in a VectorMatchingResults library : DiffractionLibrary Diffraction library containing the phases and rotations rank : int Get peaks from nth best orientation (default: 0, best vector match) Returns ------- peaks : ndarray Coordinates of peaks in the matching results object in calibrated units. """ best_fit = get_nth_best_solution(single_match_result, rank=rank) phase_index = best_fit.phase_index rotation_matrix = best_fit.rotation_matrix # Don't change the original structure = library.structures[phase_index] sim = simulate_rotated_structure(library.diffraction_generator, structure, rotation_matrix, library.reciprocal_radius, with_direct_beam=False) # Cut z return sim.coordinates[:, :2]
def peaks_from_best_template(single_match_result, library): """ Takes a TemplateMatchingResults object and return the associated peaks, to be used in combination with map(). Parameters ---------- single_match_result : ndarray An entry in a TemplateMatchingResults. library : DiffractionLibrary Diffraction library containing the phases and rotations. Returns ------- peaks : array Coordinates of peaks in the matching results object in calibrated units. """ best_fit = single_match_result[np.argmax(single_match_result[:, 2])] phase_names = list(library.keys()) best_index = int(best_fit[0]) phase = phase_names[best_index] try: simulation = library.get_library_entry(phase=phase, angle=tuple(best_fit[1]))['Sim'] except ValueError: structure = library.structures[best_index] rotation_matrix = euler2mat(*np.deg2rad(best_fit[1]), 'rzxz') simulation = simulate_rotated_structure(library.diffraction_generator, structure, rotation_matrix, library.reciprocal_radius, library.with_direct_beam) peaks = simulation.coordinates[:, :2] # cut z return peaks
def get_diffraction_library(self, structure_library, calibration, reciprocal_radius, half_shape, with_direct_beam=True): """Calculates a dictionary of diffraction data for a library of crystal structures and orientations. Each structure in the structure library is rotated to each associated orientation and the diffraction pattern is calculated each time. Angles must be in the Euler representation (Z,X,Z) and in degrees Parameters ---------- structure_library : difffsims:StructureLibrary Object Dictionary of structures and associated orientations for which electron diffraction is to be simulated. calibration : float The calibration of experimental data to be correlated with the library, in reciprocal Angstroms per pixel. reciprocal_radius : float The maximum g-vector magnitude to be included in the simulations. half_shape : tuple The half shape of the target patterns, for 144x144 use (72,72) etc with_direct_beam : bool Include the direct beam in the library. Returns ------- diffraction_library : :class:`DiffractionLibrary` Mapping of crystal structure and orientation to diffraction data objects. """ # Define DiffractionLibrary object to contain results diffraction_library = DiffractionLibrary() # The electron diffraction calculator to do simulations diffractor = self.electron_diffraction_calculator # Iterate through phases in library. for phase_name in structure_library.struct_lib.keys(): phase_diffraction_library = dict() structure = structure_library.struct_lib[phase_name][0] orientations = structure_library.struct_lib[phase_name][1] num_orientations = len(orientations) simulations = np.empty(num_orientations, dtype='object') pixel_coords = np.empty(num_orientations, dtype='object') intensities = np.empty(num_orientations, dtype='object') # Iterate through orientations of each phase. for i, orientation in enumerate(tqdm(orientations, leave=False)): matrix = euler2mat(*np.deg2rad(orientation), 'rzxz') simulation = simulate_rotated_structure( diffractor, structure, matrix, reciprocal_radius, with_direct_beam) # Calibrate simulation simulation.calibration = calibration pixel_coordinates = np.rint( simulation.calibrated_coordinates[:, :2] + half_shape).astype(int) # Construct diffraction simulation library simulations[i] = simulation pixel_coords[i] = pixel_coordinates intensities[i] = simulation.intensities diffraction_library[phase_name] = { 'simulations': simulations, 'orientations': orientations, 'pixel_coords': pixel_coords, 'intensities': intensities, } # Pass attributes to diffraction library from structure library. diffraction_library.identifiers = structure_library.identifiers diffraction_library.structures = structure_library.structures diffraction_library.diffraction_generator = diffractor diffraction_library.reciprocal_radius = reciprocal_radius diffraction_library.with_direct_beam = with_direct_beam return diffraction_library