Skip to content
New issue

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

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

Already on GitHub? # to your account

Small fix to GeometricalKikuchiPatternSimulation #720

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

tgwoodcock
Copy link
Contributor

Problem: calling GeometricalKikuchiPatternSimulation.plot() with the kwarg coordinates="gnomonic" and an EBSDDetector with navigation_dimension > 1 fails with a ValueError.

Description of the change

Details: plot() calls as_collections(), which calls _zone_axes_as_collection() and _zone_axis_labels_as_list(). The ValueError comes from the definition of scatter_size in the "else" branch of _zone_axes_as_collection() AND because of the application of the y_offset to the "coords" in the "else" branch of _zone_axis_labels_as_list(). For an EBSDDetector with navigation_dimension > 1, the call np.diff(self.detector.x_range)[0] results in a 2D output, whereas a float is needed to calculate the offsets,

Solution: add the "index" to each of these lines so that the specific x_range and y_range for the pattern specified by "index" is chosen. Leave the "[0]" there so that we select a float rather than a np.ndarray. The case that a 1d detector is supplied together with a >1d rotation is also handled.

Progress of the PR

Minimal example of the bug fix or new feature

>>> import numpy as np
>>> from diffsims.crystallography import ReciprocalLatticeVector
>>> from diffpy.structure import Lattice, Structure
>>> from orix.crystal_map import Phase
>>> from orix.quaternion import Rotation
>>> import kikuchipy as kp
>>> from kikuchipy.detectors import EBSDDetector
>>> # get Euler angles as rotations
>>> rot_2d = Rotation.from_euler(np.array([[[0.6, 0.1, 0.2],
... [0.7, 0.2, 0.4]],
... [[0.8, 0.5, 0.5],
... [0.9, 0.8, 0.6]]])
... )
>>> lattice = Lattice(5.431, 5.431, 5.431, 90.0, 90.0, 90.0)
>>> phase = Phase(name="Si", 
... space_group=227, 
... structure=Structure(atoms=None, lattice=lattice)
... )
>>> poles = np.array([[2,2,0], 
... [4,0,0], 
... [2,2,4], 
... [4,0,4], 
... [2,0,6]]
... )
>>> ref = ReciprocalLatticeVector(phase=phase, hkl=poles)
>>> ref = ref.symmetrise()
>>> simulator = kp.simulations.KikuchiPatternSimulator(ref)
>>> # make several different EBSDDetector instances
>>> pc = np.array([[[0.52, 0.22, 0.67], 
... [0.55, 0.20, 0.65]], 
... [[0.45, 0.24, 0.63], 
... [0.40, 0.23, 0.61]]]
... )
>>> shape = (1024, 1244)
>>> detector_2d = EBSDDetector(shape, pc=pc, convention='bruker')
>>> detector_1d = EBSDDetector(shape, pc=pc[0], convention='bruker')
>>> detector_0d = EBSDDetector(shape, pc=pc[0, 1], convention='bruker')
>>> # Project simulations onto the detectors
>>> sim_2d = simulator.on_detector(detector_2d, rot_2d)
>>> sim_1d = simulator.on_detector(detector_1d, rot_2d[0,])
>>> sim_0d = simulator.on_detector(detector_0d, rot_2d[0, 1])
>>> sim_0d_rot_2d = simulator.on_detector(detector_0d, rot_2d)
>>> # make collections
>>> # works on both the main and PR branches
>>> collections_0d = sim_0d.as_collections(
... (0,), 
... "gnomonic", 
... zone_axes=True, 
... zone_axes_labels=True
... )
>>> # works on both branches: the detector is still 0d
>>> collections_0d_rot_2d = sim_0d_rot_2d.as_collections(
... (0, 1), 
... "gnomonic", 
... zone_axes=True, 
... zone_axes_labels=True
... )
>>> # works on both branches but on the main branch, the zone axes are from index 0 not index 1
>>> collections_1d = sim_1d.as_collections(
... (1,),
... "gnomonic",
... zone_axes=True,
... zone_axes_labels=True
... )
>>> # fails on the current main branch
>>> try:
... collections_2d = sim_2d.as_collections((0, 1), "gnomonic", zone_axes=True, zone_axes_labels=True )
... print("as_collections() with an EBSDDetector (2, 2) passed")
>>> except ValueError:
... collections_2d = None
... print("as_collections() with an EBSDDetector (2, 2) failed with a ValueError")
>>> # check the vertices of the patches marking the zone axes are the same
>>> q1 = (collections_0d[1].get_paths()[0].vertices == collections_0d_rot_2d[1].get_paths()[0].vertices).all()
>>> q2 = (collections_0d[1].get_paths()[0].vertices == collections_1d[1].get_paths()[0].vertices).all()
>>> if collections_2d:
... q3 = (collections_0d[1].get_paths()[0].vertices == collections_2d[1].get_paths()[0].vertices).all()
>>> else:
... q3 = False
>>> print(f"0d == 0d with 2d rotation: {q1}")
>>> print(f"0d == 1d[(1,)]: {q2}")
>>> print(f"0d == 2d[(0, 1)]: {q3}")

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.

…dinates

Problem: calling GeometricalKikuchiPatternSimulation.plot() with the kwarg
coordinates="gnomonic" and an EBSDDetector with navigation_dimension > 1 fails
with a ValueError.

Details: plot() calls as_collections(), which calls _zone_axes_as_collection()
and _zone_axis_labels_as_list(). The ValueError comes from the definition of
scatter_size in the "else" branch of _zone_axes_as_collection()
AND because of the application of the y_offset to the "coords" in the
"else" branch of _zone_axis_labels_as_list(). For an EBSDDetector with
navigation_dimension > 1, the call np.diff(self.detector.x_range)[0] results
in a 2D output, whereas a float is needed to calculate the offsets,

Solution: add the "index" to each of these lines so that the specific x_range
and y_range for the pattern specified by "index" is chosen. Leave the "[0]" there
so that we select a float rather than a np.ndarray. The case that a 1d detector is
supplied together with a >1d rotation is also handled.

Signed-off-by: tgwoodcock <tgwoodcock@users.noreply.github.com>
Copy link

codecov bot commented Jan 21, 2025

Codecov Report

Attention: Patch coverage is 50.00000% with 4 lines in your changes missing coverage. Please review.

Please upload report for BASE (develop@aae3a10). Learn more about missing BASE report.

Files with missing lines Patch % Lines
...kuchipy/simulations/_kikuchi_pattern_simulation.py 50.00% 2 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             develop     #720   +/-   ##
==========================================
  Coverage           ?   98.09%           
==========================================
  Files              ?       94           
  Lines              ?     7343           
  Branches           ?      992           
==========================================
  Hits               ?     7203           
  Misses             ?       52           
  Partials           ?       88           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant