From d15c199b8b7b793c4e0485304ef1738f1d08cab5 Mon Sep 17 00:00:00 2001 From: tgwoodcock Date: Tue, 10 Dec 2024 13:37:50 +0100 Subject: [PATCH] Updated tests calling _get_direction_cosines_for_fixed_pc() and _get_direction_cosines_for_varying_pc() In tests/test_rotation/test_rotation.py: In TestRotationVectorTools.test_rotate_vector(), added the instantiation of an EBSDDetector so that the gnomonic_bounds can be passed into the new signature of _get_direction_cosines_for_fixed_pc(). Modified the arguments in the function call accordingly. In tests/test_signals/test_ebsd_master_pattern.py: In TestProjectFromLambert.test_get_direction_cosines(), updated the arguments in the function call to _get_direction_cosines_for_fixed_pc(). Added a further call to that function but WITHOUT .py_func and added a further assert statement to check that the results with and without .py_func are the same (as recommended in the contributing guidelines). In TestProjectFromLambert.test_get_direction_cosines_for_multiple_pcs(), updated the arguments in the function call to _get_direction_cosines_for_varying_pc(). Added a further call to that function but WITHOUT .py_func and added a further assert statement to check that the results with and without .py_func are the same (as recommended in the contributing guidelines). In TestProjectFromLambert.test_detector_azimuthal() As the function_get_direction_cosines_for_fixed_pc() has been modified, the small rotation in the EBSD pattern simulated from the master pattern for tilt!=0 and azimuthal!=0 is no longer present. The mean values of the data in the simulated patterns sim2 and sim3 in the above test are therefore very slightly different to the old values. The mean values have been updated. The above tests are now all passing. Signed-off-by: tgwoodcock --- .../test_signals/test_ebsd_master_pattern.py | 60 +++++++++++++------ tests/test_utils/test_rotation.py | 16 ++--- 2 files changed, 49 insertions(+), 27 deletions(-) diff --git a/tests/test_signals/test_ebsd_master_pattern.py b/tests/test_signals/test_ebsd_master_pattern.py index 85e60371..2c7a04f3 100644 --- a/tests/test_signals/test_ebsd_master_pattern.py +++ b/tests/test_signals/test_ebsd_master_pattern.py @@ -189,23 +189,31 @@ class TestProjectFromLambert: ) def test_get_direction_cosines(self): + """Make sure the Numba function is covered.""" det = self.detector dc = _get_direction_cosines_from_detector(det) assert dc.shape == (det.size, 3) assert np.max(dc) <= 1 dc2 = _get_direction_cosines_for_fixed_pc.py_func( - pcx=det.pcx, - pcy=det.pcy, - pcz=det.pcz, + gnomonic_bounds=det.gnomonic_bounds.squeeze().astype(np.float64), + pcz=det.pc.squeeze().astype(np.float64)[2], + nrows=det.nrows, + ncols=det.ncols, + om_detector_to_sample=(~det.sample_to_detector).to_matrix().squeeze(), + signal_mask=np.ones(det.size, dtype=bool), + ) + + dc3 = _get_direction_cosines_for_fixed_pc( + gnomonic_bounds=det.gnomonic_bounds.squeeze().astype(np.float64), + pcz=det.pc.squeeze().astype(np.float64)[2], nrows=det.nrows, ncols=det.ncols, - tilt=det.tilt, - azimuthal=det.azimuthal, - sample_tilt=det.sample_tilt, + om_detector_to_sample=(~det.sample_to_detector).to_matrix().squeeze(), signal_mask=np.ones(det.size, dtype=bool), ) assert np.allclose(dc, dc2) + assert np.allclose(dc2, dc3) def test_get_patterns(self, emsoft_ebsd_file): emsoft_key = kp.load(emsoft_ebsd_file) @@ -383,8 +391,8 @@ def test_detector_azimuthal(self): sim3 = mp.get_patterns(detector=det3, **kwargs) assert not np.allclose(sim1.data, sim2.data) - assert np.allclose(sim2.data.mean(), 43.56, atol=1e-2) - assert np.allclose(sim3.data.mean(), 43.39, atol=1e-2) + assert np.allclose(sim2.data.mean(), 43.51, atol=1e-2) + assert np.allclose(sim3.data.mean(), 43.30, atol=1e-2) def test_project_patterns_from_master_pattern(self): """Cover the Numba functions.""" @@ -495,23 +503,37 @@ def test_get_direction_cosines_for_multiple_pcs(self): """Make sure the Numba function is covered.""" det = self.detector dc0 = _get_direction_cosines_from_detector(det) + nav_shape = (2, 3) det.pc = np.full(nav_shape + (3,), det.pc) nrows, ncols = det.shape - dc = _get_direction_cosines_for_varying_pc.py_func( - pcx=det.pcx.ravel(), - pcy=det.pcy.ravel(), - pcz=det.pcz.ravel(), - nrows=nrows, - ncols=ncols, - tilt=det.tilt, - azimuthal=det.azimuthal, - sample_tilt=det.sample_tilt, + + gnomonic_bounds = det.gnomonic_bounds.reshape( + (np.prod(det.navigation_shape), 4) + ).astype(np.float64) + pcz = det.pc_flattened.T.astype(np.float64)[2] + + dc1 = _get_direction_cosines_for_varying_pc.py_func( + gnomonic_bounds=gnomonic_bounds, + pcz=pcz, + nrows=det.nrows, + ncols=det.ncols, + om_detector_to_sample=(~det.sample_to_detector).to_matrix().squeeze(), + signal_mask=np.ones(det.size, dtype=bool), + ) + + dc2 = _get_direction_cosines_for_varying_pc( + gnomonic_bounds=gnomonic_bounds, + pcz=pcz, + nrows=det.nrows, + ncols=det.ncols, + om_detector_to_sample=(~det.sample_to_detector).to_matrix().squeeze(), signal_mask=np.ones(det.size, dtype=bool), ) - assert np.allclose(dc0, dc[0]) - assert dc.shape == (np.prod(nav_shape), nrows * ncols, 3) + assert np.allclose(dc0, dc1[0]) + assert np.allclose(dc1[0], dc2[0]) + assert dc1.shape == (np.prod(nav_shape), nrows * ncols, 3) class TestMasterPatternPlotting: diff --git a/tests/test_utils/test_rotation.py b/tests/test_utils/test_rotation.py index fb08e29b..00ad7966 100644 --- a/tests/test_utils/test_rotation.py +++ b/tests/test_utils/test_rotation.py @@ -35,15 +35,15 @@ def test_rotate_vector(self): """ rot = np.array([0.7071, 0.7071, 0, 0]) sig_shape = (20, 30) + det = kp.detectors.EBSDDetector( + shape=sig_shape, pc=(0.5, 0.5, 0.5), sample_tilt=70, tilt=10, azimuthal=0 + ) dc = _get_direction_cosines_for_fixed_pc.py_func( - pcx=0.5, - pcy=0.5, - pcz=0.5, - nrows=sig_shape[0], - ncols=sig_shape[1], - tilt=10, - azimuthal=0, - sample_tilt=70, + gnomonic_bounds=det.gnomonic_bounds.squeeze().astype(np.float64), + pcz=det.pc.squeeze().astype(np.float64)[2], + nrows=det.nrows, + ncols=det.ncols, + om_detector_to_sample=(~det.sample_to_detector).to_matrix().squeeze(), signal_mask=np.ones(sig_shape[0] * sig_shape[1], dtype=bool), )