Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detector orientation with euler angles: simulations from master patterns and GeometricalKikuchiPatternSimulation #691

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from

Conversation

tgwoodcock
Copy link

Description of the change

Hi @hakonanes and the kikuchipy team,
firstly, many thanks for kikuchipy - it's a great library which we use a lot in our work!

The focus of this PR is to include the detector Euler angles as a means of describing the full orientation of the detector and to use these in simulating EBSD patterns from master patterns and in producing lines to overlay on those patterns using KikuchiPatternSimulator.on_detector().

To achieve this, the following main changes have been made:

  1. Added a property "euler" to EBSDDetector, which is used to describe the full orientation of the detector via the Euler angles (in degrees).
    • As EBSDDetector.euler[0] is the same as EBSDDetector.azimuthal and EBSDDetector.euler[1] is the same as EBSDDetector.tilt + 90, getters and setters for EBSDDetector.tilt, EBSDDetector.azimuthal and EBSDDetector.euler have been added, which ensure consistency with the other properties when setting one of them.
    • Tests have been added to check the consistency when setting these properties.
    • Added the euler to EBSDDetector.__repr__().
    • Added properties "u_s" and "u_s_inv" to EBSDDetector, which are orientation matrices used to transform from CSs to CSd and the reverse. The matrix "u_s_inv" is used in the new functions for getting the direction cosines of the detector (see 3.).
  2. Modified KikuchiPatternSimulator.on_detector() so that the euler angles of the detector are now used in calculating the orientation matrix "u_s_bruker". Previously, only the EBSDDetector.tilt seemed to be considered.
  3. Modified functions for getting direction cosines of the detector pixels (these are in kikuchipy/signals/util/_master_pattern.py). The new approach uses the gnomonic coordinates of the EBSDDetector and orientation matrices, which consider all three detector euler angles (previously only tilt and azimuthal were considered).
    • Removed the now unneeded function, _get_cosine_sine_of_alpha_and_azimuthal(),
    • updated all tests calling the functions for direction cosines and
    • updated the signatures of the direction cosine functions in the objective functions and solvers for indexing.
  4. Added a new test class TestFitPatternDetectorOrientation to tests/test_signals/test_ebsd_master_pattern.py. This checks the fit between an EBSD pattern simulated from a master pattern and the associated GeometricalKikuchiPatternSimulation. This seemed to be worth testing because with the current version of the functions to get the direction cosines from the detector, there was a rotation of the EBSD simulation from the master pattern compared to the GeometricalKikuchiPatternSimulation if both tilt and azimuthal were non-zero:
    current_method_tilt_40_azimuthal_40
    Whereas with the new method for getting the direction cosines in this PR, the EBSD pattern and GeometricalKikuchiPatternSimulation fit perfectly for any combination of detector euler angles:
    new_method_tilt_40_azimuthal_40

The entire kikuchipy test suite passes. I was not able to build the documentation on Windows but I think everything should be fine with the tutorial notebooks.

Progress of the #PR

Minimal example of the bug fix or new feature

>>> import kikuchipy as kp
>>> det = kp.detectors.EBSDDetector()
>>> det
<EBSDDetector(shape=(1, 1), pc=(0.5, 0.5, 0.5), sample_tilt=70.0, tilt=0.0, azimuthal=0.0, euler=(0.0, 90.0, 0.0), binning=1.0, px_size=1.0 um)>
>>> det.euler = [3.4, 96.6, 0.7]
>>> det
<EBSDDetector(shape=(1, 1), pc=(0.5, 0.5, 0.5), sample_tilt=70.0, tilt=6.6, azimuthal=3.4, euler=(3.4, 96.6, 0.7), binning=1.0, px_size=1.0 um)>

For reviewers

  • The PR title is short, concise, and will make sense 1 year later.
  • New functions are imported in corresponding __init__.py.
  • New features, API changes, and deprecations are mentioned in the unreleased
    section in CHANGELOG.rst.
  • New contributors are added to kikuchipy/__init__.py and .zenodo.json.

@hakonanes
Copy link
Member

Hi @tgwoodcock,

Thank you for this PR and for the kind words! Glad you use kikuchipy and find it useful :)

I don't have time to look at your changes in detail now. From the top comment it seems like they fix a bug and add some nice new features. Although I believe we have to iterate on the additions to the public API (naming of the Euler angles and matrices). I hope to get back to you within a week at the latest so we can get this in.

@hakonanes hakonanes added bug Something isn't working enhancement New feature or request labels Oct 13, 2024
@hakonanes hakonanes added this to the v0.11.0 milestone Oct 13, 2024
@hakonanes hakonanes removed this from the v0.11.0 milestone Nov 9, 2024
@hakonanes hakonanes added this to the v0.12.0 milestone Nov 12, 2024
@hakonanes
Copy link
Member

Hi @tgwoodcock, is it OK if I push changes that makes the test suite pass?

And have you done any benchmarking of refinement and projection from master pattern to detector after making your changes?

src/kikuchipy/detectors/ebsd_detector.py Outdated Show resolved Hide resolved
src/kikuchipy/detectors/ebsd_detector.py Outdated Show resolved Hide resolved
src/kikuchipy/detectors/ebsd_detector.py Outdated Show resolved Hide resolved
src/kikuchipy/detectors/ebsd_detector.py Outdated Show resolved Hide resolved
src/kikuchipy/detectors/ebsd_detector.py Outdated Show resolved Hide resolved
src/kikuchipy/signals/util/_master_pattern.py Outdated Show resolved Hide resolved
src/kikuchipy/signals/util/_master_pattern.py Outdated Show resolved Hide resolved
tests/test_signals/test_ebsd_master_pattern.py Outdated Show resolved Hide resolved
tests/test_signals/test_ebsd_master_pattern.py Outdated Show resolved Hide resolved
tests/test_signals/test_ebsd_master_pattern.py Outdated Show resolved Hide resolved
@tgwoodcock
Copy link
Author

Hi @tgwoodcock, is it OK if I push changes that makes the test suite pass?

I hope I have fixed the problems now - the test suite passes on my Windows machine with Python3.12. Have updated my remote branch.

And have you done any benchmarking of refinement and projection from master pattern to detector after making your changes?

Not with refinement but I did benchmark getting the direction cosines. The performance was slower with the new version. I'll look into whether this can be improved.

@hakonanes
Copy link
Member

I assume you want to make the changes I suggested?

@hakonanes
Copy link
Member

If you merge in recent changes in develop, tests should pass.

Added an attribute self.twist, which holds the rotation
angle of the detector about the normal to the detector
screen (Zd). This defaults to 0 and can be passed during
instantiation.

Defined a dependent property, self.euler which holds the
three detector Euler angles in degrees. These describe the
orientation of the detector.

self.euler[0] == self.azimuthal
self.euler[1] == 90 + self.tilt
self.euler[2] == self.twist

The methods EBSDDetector.save() and EBSDDetector.load() were
updated so that the EBSDDetector.twist attribute is additionally
saved.

The test method TestSaveLoadDetector.test_save_load_detector()
in tests/test_detectors/test_ebsd_detector.py has been updated
to include an extra assert statement to check that the saved and
read twist angles are the same.

All tests in tests/test_detectors/test_ebsd_detector.py
pass with the current code.

Signed-off-by: tgwoodcock <[email protected]>
This property returns an orix.quaternion.Rotation
representing the orientaiton matrix which transforms
vectors in the sample coordinate
system CSs, to the detector coordinate system, CSd.

Signed-off-by: tgwoodcock <[email protected]>
The __repr__ method now prints the detector twist angle
as well as the detector tilt and detector azimuthal.

The associated test has been updated.

All tests in test_ebsd_detector.py are passing.

Signed-off-by: tgwoodcock <[email protected]>
Modified the calculation of the orientation matrix
u_s_bruker in KikuchiPatternSimulator.on_detector().
Previously u_s_bruker was determined from the 'total_tilt',
which only considered EBSDDetector.sample_tilt and
EBSDDetector.tilt but NOT EBSDDetector.azimuthal.
Now EBSDDetector has the detector Euler angles, which
describe the full orientation of the detector and these can
be used to calculate u_s_bruker. In this way, the full
orientation of the detector can be considered when making
simulations to overlay on patterns. This will give a much
better fit for cases where the detector azimuthal angle
was non-zero (i.e. EBSDDetector.euler[0] != 0) or where the
third detector Euler angle was non-zero.

Signed-off-by: tgwoodcock <[email protected]>
….util._master_pattern.py

The functions _get_direction_cosines_for_fixed_pc() and
_get_direction_cosines_for_varying_pc() have been modified. The
previous functions used an algorithm adapted from EMSoft to
calculate the direction cosines of the detector pixels. This
has now been changed and the functions use an orientation matrix
approach, adapted from that in the tutorial paper by Britton et al.
This enables the detector tilt, azimuthal and twist,
i.e. all 3 detector Euler angles, to be correctly
incorporated in the pattern simulation.

The function _get_direction_cosines_from_detector() has been
updated to reflect the new signatures of the other two functions.

Signed-off-by: tgwoodcock <[email protected]>
…sociated test

The funtion _get_cosine_sine_of_alpha_and_azimuthal() has been
removed from src/kikuchipy/signals/util/_master_pattern.py as it
is no longer needed.

The function import and associated test in
tests/test_signals/test_ebsd_master_pattern.py has also been
removed.

Signed-off-by: tgwoodcock <[email protected]>
…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 <[email protected]>
These changes are necessary due to the new signature of the functions
for getting direction cosines in src/kikuchipy/signals/util/_master_pattern.py.

Added a new module src/kikuchipy/_utils/_gnonomic_bounds.py with a function
get_gnomonic_bounds(). This is used by the _solvers and _objective_functions
to calculate the gnomonic bounds of a detector without needing an EBSDDetector
Python object. Added a test class for the new function for getting a single set
of gnomonic bounds.

In src/kikuchipy/indexing/_refinement/_refinement.py, modified the function
_RefinementSetup.set_fixed_parameters() to pass om_detector_to_sample
through to the _solvers and _objective_functions, rather than the
detector tilt angles, which are no longer needed. The functions
_refine_orientation_chunk_scipy() and _refine_orientation_chunk_nlopt()
have been similarly updated.

In src/kikuchipy/indexing/_refinement/_solvers.py,
_refine_orientation_solver_scipy() and _refine_orientation_solver_nlopt()
were modified to handle the new way of calculating direction cosines.

In src/kikuchipy/indexing/_refinement/_objective_functions.py,
_refine_pc_objective_function() and _refine_orientation_pc_objective_function()
were modified to handle the new way of calculating direction cosines.

Imports to these modules have been added/modified accordingly.

Signed-off-by: tgwoodcock <[email protected]>
The above function in the suite TestEBSDRefineOrientationPC
has been modified by adding a trust region as an argument
in the call to s.refine_orientation_projection_centre() with
the method "differential_evolution". This was necessary because
the new version of _refine_orientation_pc_objective_function()
in indexing\_refinement\_objective_functions.py
requires the calculation of gnomonic_bounds. If pc is
(0, 0, 0), this cause divide by zero errors
in the function get_gnomonic_bounds() in
_utils\_gnomonic_bounds.py. Adding the trust
region (same parameters as in the earlier call to
s.refine_orientation_projection_centre() within the same
test function) solves this problem.

The test test_refine_orientation_pc_pseudo_symmetry_scipy() now
passes, as do ALL the tests in tests/test_indexing/test_ebsd_refinement.py.

Signed-off-by: tgwoodcock <[email protected]>
Added the new module kikuchipy/_utils\_detector_coordinates.py.
This contains functions for converting coordinates on the
detector screen (EBSD pattern) from pixels to gnomonic
units.

Also added associated tests.

Signed-off-by: tgwoodcock <[email protected]>
Added a property coordinate_conversion_factors,
and a method convert_detector_coordinates() to the EBSDDetector.
These call the functions in kikuchipy/_utils/_detector_coordinates.py
The EBSDDetector can now convert between pixel and gnomonic coordinates
in both directions.

Added appropriate tests for the new functions.

Signed-off-by: tgwoodcock <[email protected]>
This class in tests/test_signals/test_ebsd_master_pattern.py
helps to test the fit between an EBSD pattern generated
from a master pattern and an associated
GeometricalKikuchiPatternSimulation for different
detector orientations.

Signed-off-by: tgwoodcock <[email protected]>
The new EBSDDetector property sample_to_detector returns a Rotation.
The test checks the type is correct and that values are correct according
to some expected values for different parameters.

Signed-off-by: tgwoodcock <[email protected]>
@tgwoodcock
Copy link
Author

All tests are passing on my local windows machine. Requested changes have been completed.

Signed-off-by: tgwoodcock <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants