def test_vector_get_indexed_diffraction_vectors_warn(): match_results = VectorMatchingResults(np.array([[1], [2]])) match_results.hkls = [0, 0, 1] vectors = DiffractionVectors(np.array([[1], [2]])) vectors.hkls = [0, 0, 0] with pytest.warns(Warning): match_results.get_indexed_diffraction_vectors(vectors) np.testing.assert_allclose(vectors.hkls, [0, 0, 0])
def test_vector_get_indexed_diffraction_vectors(overwrite, result_hkl, current_hkl, expected_hkl): match_results = VectorMatchingResults(np.array([[1], [2]])) match_results.hkls = result_hkl vectors = DiffractionVectors(np.array([[1], [2]])) vectors.hkls = current_hkl match_results.get_indexed_diffraction_vectors(vectors, overwrite) np.testing.assert_allclose(vectors.hkls, expected_hkl)
def test_vector_get_crystallographic_map(dp_vector_match_result, sp_vector_match_result): # Assertion free test, as the tests in test_indexation_utils do the heavy # lifting match_results = np.hstack([dp_vector_match_result, sp_vector_match_result]) match_results = VectorMatchingResults(match_results) match_results.axes_manager.set_signal_dimension(1) cryst_map = match_results.get_crystallographic_map() assert cryst_map.method == "vector_matching"
def index_vectors(self, mag_tol, angle_tol, index_error_tol, n_peaks_to_index, n_best, *args, **kwargs): """Assigns hkl indices to diffraction vectors. Parameters ---------- mag_tol : float The maximum absolute error in diffraction vector magnitude, in units of reciprocal Angstroms, allowed for indexation. angle_tol : float The maximum absolute error in inter-vector angle, in units of degrees, allowed for indexation. index_error_tol : float Max allowed error in peak indexation for classifying it as indexed, calculated as :math:`|hkl_calculated - round(hkl_calculated)|`. n_peaks_to_index : int The maximum number of peak to index. n_best : int The maximum number of good solutions to be retained. *args : arguments Arguments passed to the map() function. **kwargs : arguments Keyword arguments passed to the map() function. Returns ------- indexation_results : VectorMatchingResults Navigation axes of the diffraction vectors signal containing vector indexation results for each probe position. """ vectors = self.vectors library = self.library matched = vectors.cartesian.map(match_vectors, library=library, mag_tol=mag_tol, angle_tol=np.deg2rad(angle_tol), index_error_tol=index_error_tol, n_peaks_to_index=n_peaks_to_index, n_best=n_best, inplace=False, *args, **kwargs) indexation = matched.isig[0] rhkls = matched.isig[1].data indexation_results = VectorMatchingResults(indexation) indexation_results.vectors = vectors indexation_results.hkls = rhkls indexation_results = transfer_navigation_axes(indexation_results, vectors.cartesian) vectors.hkls = rhkls return indexation_results
def dp_vector_match_result(): res = np.empty(4, dtype="object") res = res.reshape(2,2) res[0,0] = OrientationResult( 0, euler2mat(*np.deg2rad([90, 0, 0]), "rzxz"), 0.6, np.array([0.1, 0.10, 0.2]), 0.3, 1.0, 0, 0, ) res[0,1] = OrientationResult( 0, euler2mat(*np.deg2rad([0, 10, 20]), "rzxz"), 0.5, np.array([0.1, 0.05, 0.2]), 0.4, 1.0, 0, 0, ) res[1,0] = OrientationResult( 1, euler2mat(*np.deg2rad([0, 45, 45]), "rzxz"), 0.8, np.array([0.1, 0.30, 0.2]), 0.1, 1.0, 0, 0, ) res[1,1] = OrientationResult( 1, euler2mat(*np.deg2rad([0, 0, 90]), "rzxz"), 0.7, np.array([0.1, 0.05, 0.1]), 0.2, 1.0, 0, 0, ) return VectorMatchingResults(res)
def sp_vector_match_result(): # We require (total_error of row_1 > correlation row_2) res = np.empty(2, dtype="object") res[0] = OrientationResult( 0, euler2mat(*np.deg2rad([0, 0, 90]), "rzxz"), 0.5, np.array([0.1, 0.05, 0.2]), 0.1, 1.0, 0, 0, ) res[1] = OrientationResult( 0, euler2mat(*np.deg2rad([0, 0, 90]), "rzxz"), 0.6, np.array([0.1, 0.10, 0.2]), 0.2, 1.0, 0, 0, ) return VectorMatchingResults(res)
def refine_n_best_orientations( self, orientations, accelarating_voltage, camera_length, n_best=0, rank=0, index_error_tol=0.2, vary_angles=True, vary_center=False, vary_scale=False, method="leastsq", ): """Refines the best orientation and assigns hkl indices to diffraction vectors. Parameters ---------- orientations : VectorMatchingResults List of orientations to refine, must be an instance of `VectorMatchingResults`. accelerating_voltage : float The acceleration voltage with which the data was acquired. camera_length : float The camera length in meters. n_best : int Refine the best `n` orientations starting from `rank`. With `n_best=0` (default), all orientations are refined. rank : int The rank of the solution to start from. index_error_tol : float Max allowed error in peak indexation for classifying it as indexed, calculated as :math:`|hkl_calculated - round(hkl_calculated)|`. method : str Minimization algorithm to use, choose from: 'leastsq', 'nelder', 'powell', 'cobyla', 'least-squares'. See `lmfit` documentation (https://lmfit.github.io/lmfit-py/fitting.html) for more information. vary_angles : bool, Free the euler angles (rotation matrix) during the refinement. vary_center : bool Free the center of the diffraction pattern (beam center) during the refinement. vary_scale : bool Free the scale (i.e. pixel size) of the diffraction vectors during refinement. Returns ------- indexation_results : VectorMatchingResults Navigation axes of the diffraction vectors signal containing vector indexation results for each probe position. """ vectors = self.vectors library = self.library matched = orientations.map( _refine_best_orientations, vectors=vectors, library=library, accelarating_voltage=accelarating_voltage, camera_length=camera_length, n_best=n_best, rank=rank, method="leastsq", verbose=False, vary_angles=vary_angles, vary_center=vary_center, vary_scale=vary_scale, inplace=False, parallel=False, ) indexation = matched.isig[0] rhkls = matched.isig[1].data indexation_results = VectorMatchingResults(indexation) indexation_results.vectors = vectors indexation_results.hkls = rhkls indexation_results = transfer_navigation_axes(indexation_results, vectors.cartesian) return indexation_results
def index_vectors(self, mag_tol, angle_tol, index_error_tol, n_peaks_to_index, n_best, keys=[], *args, **kwargs): """Assigns hkl indices to diffraction vectors. Parameters ---------- mag_tol : float The maximum absolute error in diffraction vector magnitude, in units of reciprocal Angstroms, allowed for indexation. angle_tol : float The maximum absolute error in inter-vector angle, in units of degrees, allowed for indexation. index_error_tol : float Max allowed error in peak indexation for classifying it as indexed, calculated as |hkl_calculated - round(hkl_calculated)|. n_peaks_to_index : int The maximum number of peak to index. n_best : int The maximum number of good solutions to be retained. keys : list If more than one phase present in library it is recommended that these are submitted. This allows a mapping from the number to the phase. For example, keys = ['si','ga'] will have an output with 0 for 'si' and 1 for 'ga'. *args : arguments Arguments passed to the map() function. **kwargs : arguments Keyword arguments passed to the map() function. Returns ------- indexation_results : VectorMatchingResults Navigation axes of the diffraction vectors signal containing vector indexation results for each probe position. """ vectors = self.vectors library = self.library matched = vectors.cartesian.map( match_vectors, library=library, mag_tol=mag_tol, angle_tol=np.deg2rad(angle_tol), index_error_tol=index_error_tol, n_peaks_to_index=n_peaks_to_index, n_best=n_best, keys=keys, inplace=False, parallel=False, # TODO: For testing *args, **kwargs) indexation = np.array(matched.isig[0].data.tolist(), dtype='object') rhkls = matched.isig[1].data indexation_results = VectorMatchingResults(indexation) indexation_results.vectors = vectors indexation_results.hkls = rhkls indexation_results = transfer_navigation_axes(indexation_results, vectors.cartesian) vectors.hkls = rhkls return indexation_results