Skip to content

Commit

Permalink
Refactor: Remove changes from RLV
Browse files Browse the repository at this point in the history
  • Loading branch information
CSSFrancis committed May 8, 2024
1 parent efde31d commit da45a32
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 54 deletions.
44 changes: 43 additions & 1 deletion diffsims/crystallography/_diffracting_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from diffsims.crystallography import ReciprocalLatticeVector
import numpy as np
from orix.vector.miller import _transform_space
from orix.quaternion import Rotation


class DiffractingVector(ReciprocalLatticeVector):
Expand Down Expand Up @@ -90,7 +91,18 @@ def __init__(self, phase, xyz=None, hkl=None, hkil=None, intensity=None):
self._intensity = np.array(intensity)

def __getitem__(self, key):
dv_new = super().__getitem__(key)
new_data = self.data[key]
dv_new = self.__class__(self.phase, xyz=new_data)

if np.isnan(self.structure_factor).all():
dv_new._structure_factor = np.full(dv_new.shape, np.nan, dtype="complex128")

else:
dv_new._structure_factor = self.structure_factor[key]
if np.isnan(self.theta).all():
dv_new._theta = np.full(dv_new.shape, np.nan)
else:
dv_new._theta = self.theta[key]
if np.isnan(self.intensity).all():
dv_new._intensity = np.full(dv_new.shape, np.nan)
else:
Expand All @@ -105,6 +117,36 @@ def __getitem__(self, key):

return dv_new

@property
def basis_rotation(self):
"""
Returns the lattice basis rotation.
"""
return Rotation.from_matrix(self.phase.structure.lattice.baserot)

def rotate_with_basis(self, rotation):
"""Rotate both vectors and the basis with a given `Rotation`.
This differs from simply multiplying with a `Rotation`,
as that would NOT update the basis.
Parameters
----------
rot : orix.quaternion.Rotation
A rotation to apply to vectors and the basis.
"""

if rotation.size != 1:
raise ValueError("Rotation must be a single rotation")
# rotate basis
new_phase = self.phase.deepcopy()
br = new_phase.structure.lattice.baserot
# In case the base rotation is set already
new_br = br @ rotation.to_matrix().squeeze()
new_phase.structure.lattice.setLatPar(baserot=new_br)
# rotate vectors
vecs = ~rotation * self.to_miller()
return ReciprocalLatticeVector(new_phase, xyz=vecs.data)

@property
def intensity(self):
return self._intensity
Expand Down
39 changes: 3 additions & 36 deletions diffsims/crystallography/reciprocal_lattice_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import numba as nb
import numpy as np
from orix.vector import Miller, Vector3d
from orix.quaternion import Rotation
from orix.vector.miller import (
_check_hkil,
_get_highest_hkl,
Expand Down Expand Up @@ -78,8 +77,6 @@ class ReciprocalLatticeVector(Vector3d):
Indices of reciprocal lattice vector(s), often preferred over
``hkl`` in trigonal and hexagonal lattices. Default is ``None``.
This, ``xyz``, or ``hkl`` is required.
rotation : orix.quaternion.Rotation, optional
Rotation to apply to the vectors. Default is ``None``.
Examples
--------
Expand Down Expand Up @@ -126,8 +123,8 @@ def __init__(self, phase, xyz=None, hkl=None, hkil=None):
self._structure_factor = np.full(self.shape, np.nan, dtype="complex128")

def __getitem__(self, key):
new_data = self.data[key]
rlv_new = self.__class__(self.phase, xyz=new_data)
miller_new = self.to_miller().__getitem__(key)
rlv_new = self.from_miller(miller_new)

if np.isnan(self.structure_factor).all():
rlv_new._structure_factor = np.full(
Expand All @@ -152,36 +149,6 @@ def __repr__(self):
phase_name = self.phase.name
return f"{name} {shape}, {phase_name} ({symmetry})\n" f"{data}"

@property
def basis_rotation(self):
"""
Returns the lattice basis rotation.
"""
return Rotation.from_matrix(self.phase.structure.lattice.baserot)

def rotate_with_basis(self, rotation):
"""Rotate both vectors and the basis with a given `Rotation`.
This differs from simply multiplying with a `Rotation`,
as that would NOT update the basis.
Parameters
----------
rot : orix.quaternion.Rotation
A rotation to apply to vectors and the basis.
"""

if rotation.size != 1:
raise ValueError("Rotation must be a single rotation")
# rotate basis
new_phase = self.phase.deepcopy()
br = new_phase.structure.lattice.baserot
# In case the base rotation is set already
new_br = br @ rotation.to_matrix().squeeze()
new_phase.structure.lattice.setLatPar(baserot=new_br)
# rotate vectors
vecs = ~rotation * self.to_miller()
return ReciprocalLatticeVector(new_phase, xyz=vecs.data)

@property
def hkl(self):
"""Miller indices.
Expand Down Expand Up @@ -1171,7 +1138,7 @@ def from_min_dspacing(cls, phase, min_dspacing=0.7, include_zero_vector=False):
new = cls(phase, hkl=hkl).unique()
if include_zero_vector:
new_data = np.vstack((new.hkl, np.zeros(3, dtype=int)))
new = ReciprocalLatticeVector(phase, hkl=new_data)
new = cls(phase, hkl=new_data)
return new

@classmethod
Expand Down
5 changes: 2 additions & 3 deletions diffsims/generators/simulation_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from orix.quaternion import Rotation
from orix.crystal_map import Phase

from diffsims.crystallography import ReciprocalLatticeVector
from diffsims.crystallography._diffracting_vector import DiffractingVector
from diffsims.utils.shape_factor_models import (
linear,
Expand Down Expand Up @@ -191,7 +190,7 @@ def calculate_diffraction2d(
# Rotate using all the rotations in the list
vectors = []
for p, rotate in zip(phase, rotation):
recip = ReciprocalLatticeVector.from_min_dspacing(
recip = DiffractingVector.from_min_dspacing(
p,
min_dspacing=1 / reciprocal_radius,
include_zero_vector=with_direct_beam,
Expand Down Expand Up @@ -335,7 +334,7 @@ def calculate_diffraction1d(

def get_intersecting_reflections(
self,
recip: ReciprocalLatticeVector,
recip: DiffractingVector,
rot: np.ndarray,
wavelength: float,
max_excitation_error: float,
Expand Down
14 changes: 14 additions & 0 deletions diffsims/tests/crystallography/test_diffracting_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,17 @@ def test_flat_polar(self, ferrite_phase):
r, t = dv.to_flat_polar()
assert np.allclose(r, [np.sqrt(2), np.sqrt(2), 0.70710678, 0.70710678])
assert np.allclose(t, [np.pi / 4, np.pi / 4, -np.pi / 4, -np.pi / 4])

def test_get_lattice_basis_rotation(self, ferrite_phase):
"""Rotation matrix to align the lattice basis with the Cartesian
basis is correct.
"""
rlv = DiffractingVector(ferrite_phase, hkl=[[1, 1, 1], [2, 0, 0]])
rot = rlv.basis_rotation
assert np.allclose(rot.to_matrix(), np.eye(3))

def test_rotation_with_basis_raises(self, ferrite_phase):
rlv = DiffractingVector(ferrite_phase, hkl=[[1, 1, 1], [2, 0, 0]])
rot = Rotation.from_euler([[90, 90, 0], [90, 90, 1]], degrees=True)
with pytest.raises(ValueError):
rlv.rotate_with_basis(rotation=rot)
14 changes: 0 additions & 14 deletions diffsims/tests/crystallography/test_reciprocal_lattice_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,20 +155,6 @@ def test_get_hkil(self, silicon_carbide_phase):
assert np.allclose(rlv.i, [0, 0, 0, 0, 0, 0])
assert np.allclose(rlv.l, [3, 2, 1, -1, -2, -3])

def test_get_lattice_basis_rotation(self, ferrite_phase):
"""Rotation matrix to align the lattice basis with the Cartesian
basis is correct.
"""
rlv = ReciprocalLatticeVector(ferrite_phase, hkl=[[1, 1, 1], [2, 0, 0]])
rot = rlv.basis_rotation
assert np.allclose(rot.to_matrix(), np.eye(3))

def test_rotation_with_basis_raises(self, ferrite_phase):
rlv = ReciprocalLatticeVector(ferrite_phase, hkl=[[1, 1, 1], [2, 0, 0]])
rot = Rotation.from_euler([[90, 90, 0], [90, 90, 1]], degrees=True)
with pytest.raises(ValueError):
rlv.rotate_with_basis(rotation=rot)

def test_gspacing_dspacing_scattering_parameter(self, ferrite_phase):
"""Length and scattering parameter properties give desired
values.
Expand Down

0 comments on commit da45a32

Please sign in to comment.