def test_correlate_library_from_dict(): new_template_library = DiffractionLibrary() new_template_library["GaSb"] = { "orientations": np.array([[0.0, 0.0, 0.0], [1.0, 1.0, 1.0]]), "pixel_coords": np.array([np.asarray([[1, 1]]), np.asarray([[2, 2], [1, 1]])]), "intensities": np.array([np.array([ 1, ]), np.array([1, 1])]), } shape = (3, 3) fsize = (5, 5) new_template_dict = get_library_FT_dict(new_template_library, shape, fsize) image = np.zeros((3, 3)) image[1, 1] = 1 match_results = correlate_library_from_dict( image, new_template_dict, n_largest=1, method="full_frame_correlation", mask=1) np.testing.assert_approx_equal(match_results[0][1][1], 0.0) np.testing.assert_approx_equal(match_results[0][2], 1.0)
def test_plot_phase_map(self, dp_cryst_map): """ Generates a CrystallographicMap with two phases and a corresponding DiffractionLibraryself and tests the CrystallographicMap member function plot_phase_map. """ crystal_map = dp_cryst_map # Create DiffractionLibrary with two entries lib = DiffractionLibrary() lib["Al"] = "" lib["Si"] = "" assert crystal_map.plot_phase_map(lib) == None
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 test_get_library_FT_dict(): new_template_library = DiffractionLibrary() new_template_library["GaSb"] = { "orientations": np.array([[0.0, 0.0, 0.0],]), "pixel_coords": np.array([np.asarray([[1, 1],])]), "intensities": np.array([np.array([1,])]), } shape = (3, 3) fsize = (5, 5) normalization_constant = 0.9278426705718053 new_template_dict = get_library_FT_dict(new_template_library, shape, fsize) for phase_index, library_entry in enumerate(new_template_dict.values()): orientations = library_entry["orientations"] patterns = library_entry["patterns"] pattern_norms = library_entry["pattern_norms"] np.testing.assert_approx_equal(orientations[0][0], 0.0) np.testing.assert_approx_equal(np.real(patterns[0][2, 1]), 1) np.testing.assert_approx_equal(pattern_norms[0], normalization_constant)
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
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)): simulation = diffractor.calculate_ed_data( structure, reciprocal_radius, rotation=orientation, with_direct_beam=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