changed quote layout to single quote Added NotImplemented returnvalue to __eq__ functions

This commit is contained in:
Daniel Otto de Mentock 2022-02-02 11:11:59 +01:00
parent cb1143a472
commit 71bc92fed0
2 changed files with 51 additions and 51 deletions

View File

@ -125,7 +125,7 @@ class Orientation(Rotation,Crystal):
Rotation.__repr__(self)]) Rotation.__repr__(self)])
def __copy__(self, def __copy__(self,
rotation: Union[FloatSequence, Rotation] = None) -> "Orientation": rotation: Union[FloatSequence, Rotation] = None) -> 'Orientation':
"""Create deep copy.""" """Create deep copy."""
dup = copy.deepcopy(self) dup = copy.deepcopy(self)
if rotation is not None: if rotation is not None:
@ -148,7 +148,7 @@ class Orientation(Rotation,Crystal):
""" """
if not isinstance(other, Orientation): if not isinstance(other, Orientation):
raise TypeError raise NotImplemented
matching_type = self.family == other.family and \ matching_type = self.family == other.family and \
self.lattice == other.lattice and \ self.lattice == other.lattice and \
self.parameters == other.parameters self.parameters == other.parameters
@ -235,7 +235,7 @@ class Orientation(Rotation,Crystal):
def __mul__(self, def __mul__(self,
other: Union[Rotation, "Orientation"]) -> "Orientation": other: Union[Rotation, 'Orientation']) -> 'Orientation':
""" """
Compose this orientation with other. Compose this orientation with other.
@ -290,77 +290,77 @@ class Orientation(Rotation,Crystal):
@classmethod @classmethod
@util.extended_docstring(Rotation.from_random, _parameter_doc) @util.extended_docstring(Rotation.from_random, _parameter_doc)
def from_random(cls, **kwargs) -> "Orientation": def from_random(cls, **kwargs) -> 'Orientation':
kwargs_rot,kwargs_ori = Orientation._split_kwargs(kwargs,Rotation.from_random) kwargs_rot,kwargs_ori = Orientation._split_kwargs(kwargs,Rotation.from_random)
return cls(rotation=Rotation.from_random(**kwargs_rot),**kwargs_ori) return cls(rotation=Rotation.from_random(**kwargs_rot),**kwargs_ori)
@classmethod @classmethod
@util.extended_docstring(Rotation.from_quaternion,_parameter_doc) @util.extended_docstring(Rotation.from_quaternion,_parameter_doc)
def from_quaternion(cls, **kwargs) -> "Orientation": def from_quaternion(cls, **kwargs) -> 'Orientation':
kwargs_rot,kwargs_ori = Orientation._split_kwargs(kwargs,Rotation.from_quaternion) kwargs_rot,kwargs_ori = Orientation._split_kwargs(kwargs,Rotation.from_quaternion)
return cls(rotation=Rotation.from_quaternion(**kwargs_rot),**kwargs_ori) return cls(rotation=Rotation.from_quaternion(**kwargs_rot),**kwargs_ori)
@classmethod @classmethod
@util.extended_docstring(Rotation.from_Euler_angles,_parameter_doc) @util.extended_docstring(Rotation.from_Euler_angles,_parameter_doc)
def from_Euler_angles(cls, **kwargs) -> "Orientation": def from_Euler_angles(cls, **kwargs) -> 'Orientation':
kwargs_rot,kwargs_ori = Orientation._split_kwargs(kwargs,Rotation.from_Euler_angles) kwargs_rot,kwargs_ori = Orientation._split_kwargs(kwargs,Rotation.from_Euler_angles)
return cls(rotation=Rotation.from_Euler_angles(**kwargs_rot),**kwargs_ori) return cls(rotation=Rotation.from_Euler_angles(**kwargs_rot),**kwargs_ori)
@classmethod @classmethod
@util.extended_docstring(Rotation.from_axis_angle,_parameter_doc) @util.extended_docstring(Rotation.from_axis_angle,_parameter_doc)
def from_axis_angle(cls, **kwargs) -> "Orientation": def from_axis_angle(cls, **kwargs) -> 'Orientation':
kwargs_rot,kwargs_ori = Orientation._split_kwargs(kwargs,Rotation.from_axis_angle) kwargs_rot,kwargs_ori = Orientation._split_kwargs(kwargs,Rotation.from_axis_angle)
return cls(rotation=Rotation.from_axis_angle(**kwargs_rot),**kwargs_ori) return cls(rotation=Rotation.from_axis_angle(**kwargs_rot),**kwargs_ori)
@classmethod @classmethod
@util.extended_docstring(Rotation.from_basis,_parameter_doc) @util.extended_docstring(Rotation.from_basis,_parameter_doc)
def from_basis(cls, **kwargs) -> "Orientation": def from_basis(cls, **kwargs) -> 'Orientation':
kwargs_rot,kwargs_ori = Orientation._split_kwargs(kwargs,Rotation.from_basis) kwargs_rot,kwargs_ori = Orientation._split_kwargs(kwargs,Rotation.from_basis)
return cls(rotation=Rotation.from_basis(**kwargs_rot),**kwargs_ori) return cls(rotation=Rotation.from_basis(**kwargs_rot),**kwargs_ori)
@classmethod @classmethod
@util.extended_docstring(Rotation.from_matrix,_parameter_doc) @util.extended_docstring(Rotation.from_matrix,_parameter_doc)
def from_matrix(cls, **kwargs) -> "Orientation": def from_matrix(cls, **kwargs) -> 'Orientation':
kwargs_rot,kwargs_ori = Orientation._split_kwargs(kwargs,Rotation.from_matrix) kwargs_rot,kwargs_ori = Orientation._split_kwargs(kwargs,Rotation.from_matrix)
return cls(rotation=Rotation.from_matrix(**kwargs_rot),**kwargs_ori) return cls(rotation=Rotation.from_matrix(**kwargs_rot),**kwargs_ori)
@classmethod @classmethod
@util.extended_docstring(Rotation.from_Rodrigues_vector,_parameter_doc) @util.extended_docstring(Rotation.from_Rodrigues_vector,_parameter_doc)
def from_Rodrigues_vector(cls, **kwargs) -> "Orientation": def from_Rodrigues_vector(cls, **kwargs) -> 'Orientation':
kwargs_rot,kwargs_ori = Orientation._split_kwargs(kwargs,Rotation.from_Rodrigues_vector) kwargs_rot,kwargs_ori = Orientation._split_kwargs(kwargs,Rotation.from_Rodrigues_vector)
return cls(rotation=Rotation.from_Rodrigues_vector(**kwargs_rot),**kwargs_ori) return cls(rotation=Rotation.from_Rodrigues_vector(**kwargs_rot),**kwargs_ori)
@classmethod @classmethod
@util.extended_docstring(Rotation.from_homochoric,_parameter_doc) @util.extended_docstring(Rotation.from_homochoric,_parameter_doc)
def from_homochoric(cls, **kwargs) -> "Orientation": def from_homochoric(cls, **kwargs) -> 'Orientation':
kwargs_rot,kwargs_ori = Orientation._split_kwargs(kwargs,Rotation.from_homochoric) kwargs_rot,kwargs_ori = Orientation._split_kwargs(kwargs,Rotation.from_homochoric)
return cls(rotation=Rotation.from_homochoric(**kwargs_rot),**kwargs_ori) return cls(rotation=Rotation.from_homochoric(**kwargs_rot),**kwargs_ori)
@classmethod @classmethod
@util.extended_docstring(Rotation.from_cubochoric,_parameter_doc) @util.extended_docstring(Rotation.from_cubochoric,_parameter_doc)
def from_cubochoric(cls, **kwargs) -> "Orientation": def from_cubochoric(cls, **kwargs) -> 'Orientation':
kwargs_rot,kwargs_ori = Orientation._split_kwargs(kwargs,Rotation.from_cubochoric) kwargs_rot,kwargs_ori = Orientation._split_kwargs(kwargs,Rotation.from_cubochoric)
return cls(rotation=Rotation.from_cubochoric(**kwargs_rot),**kwargs_ori) return cls(rotation=Rotation.from_cubochoric(**kwargs_rot),**kwargs_ori)
@classmethod @classmethod
@util.extended_docstring(Rotation.from_spherical_component,_parameter_doc) @util.extended_docstring(Rotation.from_spherical_component,_parameter_doc)
def from_spherical_component(cls, **kwargs) -> "Orientation": def from_spherical_component(cls, **kwargs) -> 'Orientation':
kwargs_rot,kwargs_ori = Orientation._split_kwargs(kwargs,Rotation.from_spherical_component) kwargs_rot,kwargs_ori = Orientation._split_kwargs(kwargs,Rotation.from_spherical_component)
return cls(rotation=Rotation.from_spherical_component(**kwargs_rot),**kwargs_ori) return cls(rotation=Rotation.from_spherical_component(**kwargs_rot),**kwargs_ori)
@classmethod @classmethod
@util.extended_docstring(Rotation.from_fiber_component,_parameter_doc) @util.extended_docstring(Rotation.from_fiber_component,_parameter_doc)
def from_fiber_component(cls, **kwargs) -> "Orientation": def from_fiber_component(cls, **kwargs) -> 'Orientation':
kwargs_rot,kwargs_ori = Orientation._split_kwargs(kwargs,Rotation.from_fiber_component) kwargs_rot,kwargs_ori = Orientation._split_kwargs(kwargs,Rotation.from_fiber_component)
return cls(rotation=Rotation.from_fiber_component(**kwargs_rot),**kwargs_ori) return cls(rotation=Rotation.from_fiber_component(**kwargs_rot),**kwargs_ori)
@ -370,7 +370,7 @@ class Orientation(Rotation,Crystal):
def from_directions(cls, def from_directions(cls,
uvw: FloatSequence, uvw: FloatSequence,
hkl: FloatSequence, hkl: FloatSequence,
**kwargs) -> "Orientation": **kwargs) -> 'Orientation':
""" """
Initialize orientation object from two crystallographic directions. Initialize orientation object from two crystallographic directions.
@ -390,7 +390,7 @@ class Orientation(Rotation,Crystal):
@property @property
def equivalent(self) -> "Orientation": def equivalent(self) -> 'Orientation':
""" """
Orientations that are symmetrically equivalent. Orientations that are symmetrically equivalent.
@ -404,7 +404,7 @@ class Orientation(Rotation,Crystal):
@property @property
def reduced(self) -> "Orientation": def reduced(self) -> 'Orientation':
"""Select symmetrically equivalent orientation that falls into fundamental zone according to symmetry.""" """Select symmetrically equivalent orientation that falls into fundamental zone according to symmetry."""
eq = self.equivalent eq = self.equivalent
ok = eq.in_FZ ok = eq.in_FZ
@ -940,7 +940,7 @@ class Orientation(Rotation,Crystal):
def related(self, def related(self,
model: str) -> "Orientation": model: str) -> 'Orientation':
""" """
Orientations derived from the given relationship. Orientations derived from the given relationship.

View File

@ -65,7 +65,7 @@ class Rotation:
__slots__ = ['quaternion'] __slots__ = ['quaternion']
def __init__(self, def __init__(self,
rotation: Union[FloatSequence, "Rotation"] = np.array([1.0,0.0,0.0,0.0])): rotation: Union[FloatSequence, 'Rotation'] = np.array([1.0,0.0,0.0,0.0])):
""" """
New rotation. New rotation.
@ -83,7 +83,7 @@ class Rotation:
elif np.array(rotation).shape[-1] == 4: elif np.array(rotation).shape[-1] == 4:
self.quaternion = np.array(rotation) self.quaternion = np.array(rotation)
else: else:
raise TypeError('"rotation" is neither a Rotation nor a quaternion') raise TypeError('Rotation is neither a Rotation nor a quaternion')
def __repr__(self) -> str: def __repr__(self) -> str:
@ -93,7 +93,7 @@ class Rotation:
def __copy__(self, def __copy__(self,
rotation: Union[FloatSequence, "Rotation"] = None) -> "Rotation": rotation: Union[FloatSequence, 'Rotation'] = None) -> 'Rotation':
"""Create deep copy.""" """Create deep copy."""
dup = copy.deepcopy(self) dup = copy.deepcopy(self)
if rotation is not None: if rotation is not None:
@ -122,7 +122,7 @@ class Rotation:
""" """
if not isinstance(other, Rotation): if not isinstance(other, Rotation):
raise TypeError return NotImplemented
return np.logical_or(np.all(self.quaternion == other.quaternion,axis=-1), return np.logical_or(np.all(self.quaternion == other.quaternion,axis=-1),
np.all(self.quaternion == -1.0*other.quaternion,axis=-1)) np.all(self.quaternion == -1.0*other.quaternion,axis=-1))
@ -143,7 +143,7 @@ class Rotation:
def isclose(self, def isclose(self,
other: "Rotation", other: 'Rotation',
rtol: float = 1e-5, rtol: float = 1e-5,
atol: float = 1e-8, atol: float = 1e-8,
equal_nan: bool = True) -> bool: equal_nan: bool = True) -> bool:
@ -174,7 +174,7 @@ class Rotation:
def allclose(self, def allclose(self,
other: "Rotation", other: 'Rotation',
rtol: float = 1e-5, rtol: float = 1e-5,
atol: float = 1e-8, atol: float = 1e-8,
equal_nan: bool = True) -> Union[np.bool_, bool]: equal_nan: bool = True) -> Union[np.bool_, bool]:
@ -220,14 +220,14 @@ class Rotation:
return 0 if self.shape == () else self.shape[0] return 0 if self.shape == () else self.shape[0]
def __invert__(self) -> "Rotation": def __invert__(self) -> 'Rotation':
"""Inverse rotation (backward rotation).""" """Inverse rotation (backward rotation)."""
dup = self.copy() dup = self.copy()
dup.quaternion[...,1:] *= -1 dup.quaternion[...,1:] *= -1
return dup return dup
def __pow__(self, exp: int) -> "Rotation": def __pow__(self, exp: int) -> 'Rotation':
""" """
Perform the rotation 'exp' times. Perform the rotation 'exp' times.
@ -241,7 +241,7 @@ class Rotation:
p = self.quaternion[...,1:]/np.linalg.norm(self.quaternion[...,1:],axis=-1,keepdims=True) p = self.quaternion[...,1:]/np.linalg.norm(self.quaternion[...,1:],axis=-1,keepdims=True)
return self.copy(rotation=Rotation(np.block([np.cos(exp*phi),np.sin(exp*phi)*p]))._standardize()) return self.copy(rotation=Rotation(np.block([np.cos(exp*phi),np.sin(exp*phi)*p]))._standardize())
def __ipow__(self, exp: int) -> "Rotation": def __ipow__(self, exp: int) -> 'Rotation':
""" """
Perform the rotation 'exp' times (in-place). Perform the rotation 'exp' times (in-place).
@ -254,7 +254,7 @@ class Rotation:
return self**exp return self**exp
def __mul__(self, other: "Rotation") -> "Rotation": def __mul__(self, other: 'Rotation') -> 'Rotation':
""" """
Compose with other. Compose with other.
@ -280,7 +280,7 @@ class Rotation:
else: else:
raise TypeError('Use "R@b", i.e. matmul, to apply rotation "R" to object "b"') raise TypeError('Use "R@b", i.e. matmul, to apply rotation "R" to object "b"')
def __imul__(self, other: "Rotation") -> "Rotation": def __imul__(self, other: 'Rotation') -> 'Rotation':
""" """
Compose with other (in-place). Compose with other (in-place).
@ -293,7 +293,7 @@ class Rotation:
return self*other return self*other
def __truediv__(self, other: "Rotation") -> "Rotation": def __truediv__(self, other: 'Rotation') -> 'Rotation':
""" """
Compose with inverse of other. Compose with inverse of other.
@ -313,7 +313,7 @@ class Rotation:
else: else:
raise TypeError('Use "R@b", i.e. matmul, to apply rotation "R" to object "b"') raise TypeError('Use "R@b", i.e. matmul, to apply rotation "R" to object "b"')
def __itruediv__(self, other: "Rotation") -> "Rotation": def __itruediv__(self, other: 'Rotation') -> 'Rotation':
""" """
Compose with inverse of other (in-place). Compose with inverse of other (in-place).
@ -369,14 +369,14 @@ class Rotation:
apply = __matmul__ apply = __matmul__
def _standardize(self) -> "Rotation": def _standardize(self) -> 'Rotation':
"""Standardize quaternion (ensure positive real hemisphere).""" """Standardize quaternion (ensure positive real hemisphere)."""
self.quaternion[self.quaternion[...,0] < 0.0] *= -1 self.quaternion[self.quaternion[...,0] < 0.0] *= -1
return self return self
def append(self, def append(self,
other: Union["Rotation", List["Rotation"]]) -> "Rotation": other: Union['Rotation', List['Rotation']]) -> 'Rotation':
""" """
Extend array along first dimension with other array(s). Extend array along first dimension with other array(s).
@ -390,7 +390,7 @@ class Rotation:
def flatten(self, def flatten(self,
order: Literal['C','F','A'] = 'C') -> "Rotation": order: Literal['C','F','A'] = 'C') -> 'Rotation':
""" """
Flatten array. Flatten array.
@ -405,7 +405,7 @@ class Rotation:
def reshape(self, def reshape(self,
shape: Union[int, Tuple[int, ...]], shape: Union[int, Tuple[int, ...]],
order: Literal['C','F','A'] = 'C') -> "Rotation": order: Literal['C','F','A'] = 'C') -> 'Rotation':
""" """
Reshape array. Reshape array.
@ -421,7 +421,7 @@ class Rotation:
def broadcast_to(self, def broadcast_to(self,
shape: Union[int, Tuple[int, ...]], shape: Union[int, Tuple[int, ...]],
mode: Literal["left", "right"] = 'right') -> "Rotation": mode: Literal['left', 'right'] = 'right') -> 'Rotation':
""" """
Broadcast array. Broadcast array.
@ -445,7 +445,7 @@ class Rotation:
def average(self, def average(self,
weights: np.ndarray = None) -> "Rotation": weights: np.ndarray = None) -> 'Rotation':
""" """
Average along last array dimension. Average along last array dimension.
@ -485,7 +485,7 @@ class Rotation:
def misorientation(self, def misorientation(self,
other: "Rotation") -> "Rotation": other: 'Rotation') -> 'Rotation':
""" """
Calculate misorientation to other Rotation. Calculate misorientation to other Rotation.
@ -690,7 +690,7 @@ class Rotation:
@staticmethod @staticmethod
def from_quaternion(q: Union[Sequence[FloatSequence], np.ndarray], def from_quaternion(q: Union[Sequence[FloatSequence], np.ndarray],
accept_homomorph: bool = False, accept_homomorph: bool = False,
P: Literal[1, -1] = -1) -> "Rotation": P: Literal[1, -1] = -1) -> 'Rotation':
""" """
Initialize from quaternion. Initialize from quaternion.
@ -724,7 +724,7 @@ class Rotation:
@staticmethod @staticmethod
def from_Euler_angles(phi: np.ndarray, def from_Euler_angles(phi: np.ndarray,
degrees: bool = False) -> "Rotation": degrees: bool = False) -> 'Rotation':
""" """
Initialize from Bunge Euler angles. Initialize from Bunge Euler angles.
@ -755,7 +755,7 @@ class Rotation:
def from_axis_angle(axis_angle: np.ndarray, def from_axis_angle(axis_angle: np.ndarray,
degrees:bool = False, degrees:bool = False,
normalize: bool = False, normalize: bool = False,
P: Literal[1, -1] = -1) -> "Rotation": P: Literal[1, -1] = -1) -> 'Rotation':
""" """
Initialize from Axis angle pair. Initialize from Axis angle pair.
@ -792,7 +792,7 @@ class Rotation:
@staticmethod @staticmethod
def from_basis(basis: np.ndarray, def from_basis(basis: np.ndarray,
orthonormal: bool = True, orthonormal: bool = True,
reciprocal: bool = False) -> "Rotation": reciprocal: bool = False) -> 'Rotation':
""" """
Initialize from lattice basis vectors. Initialize from lattice basis vectors.
@ -826,7 +826,7 @@ class Rotation:
return Rotation(Rotation._om2qu(om)) return Rotation(Rotation._om2qu(om))
@staticmethod @staticmethod
def from_matrix(R: np.ndarray) -> "Rotation": def from_matrix(R: np.ndarray) -> 'Rotation':
""" """
Initialize from rotation matrix. Initialize from rotation matrix.
@ -840,7 +840,7 @@ class Rotation:
@staticmethod @staticmethod
def from_parallel(a: np.ndarray, def from_parallel(a: np.ndarray,
b: np.ndarray ) -> "Rotation": b: np.ndarray ) -> 'Rotation':
""" """
Initialize from pairs of two orthogonal lattice basis vectors. Initialize from pairs of two orthogonal lattice basis vectors.
@ -870,7 +870,7 @@ class Rotation:
@staticmethod @staticmethod
def from_Rodrigues_vector(rho: np.ndarray, def from_Rodrigues_vector(rho: np.ndarray,
normalize: bool = False, normalize: bool = False,
P: Literal[1, -1] = -1) -> "Rotation": P: Literal[1, -1] = -1) -> 'Rotation':
""" """
Initialize from RodriguesFrank vector (angle separated from axis). Initialize from RodriguesFrank vector (angle separated from axis).
@ -901,7 +901,7 @@ class Rotation:
@staticmethod @staticmethod
def from_homochoric(h: np.ndarray, def from_homochoric(h: np.ndarray,
P: Literal[1, -1] = -1) -> "Rotation": P: Literal[1, -1] = -1) -> 'Rotation':
""" """
Initialize from homochoric vector. Initialize from homochoric vector.
@ -928,7 +928,7 @@ class Rotation:
@staticmethod @staticmethod
def from_cubochoric(x: np.ndarray, def from_cubochoric(x: np.ndarray,
P: Literal[1, -1] = -1) -> "Rotation": P: Literal[1, -1] = -1) -> 'Rotation':
""" """
Initialize from cubochoric vector. Initialize from cubochoric vector.
@ -956,7 +956,7 @@ class Rotation:
@staticmethod @staticmethod
def from_random(shape: Tuple[int, ...] = None, def from_random(shape: Tuple[int, ...] = None,
rng_seed: Union[int, IntSequence] = None) -> "Rotation": rng_seed: Union[int, IntSequence] = None) -> 'Rotation':
""" """
Initialize with random rotation. Initialize with random rotation.
@ -990,7 +990,7 @@ class Rotation:
N: int = 500, N: int = 500,
degrees: bool = True, degrees: bool = True,
fractions: bool = True, fractions: bool = True,
rng_seed: Union[None, int, IntSequence] = None) -> "Rotation": rng_seed: Union[None, int, IntSequence] = None) -> 'Rotation':
""" """
Sample discrete values from a binned orientation distribution function (ODF). Sample discrete values from a binned orientation distribution function (ODF).
@ -1043,11 +1043,11 @@ class Rotation:
@staticmethod @staticmethod
def from_spherical_component(center: "Rotation", def from_spherical_component(center: 'Rotation',
sigma: float, sigma: float,
N: int = 500, N: int = 500,
degrees: bool = True, degrees: bool = True,
rng_seed: Union[None, int, IntSequence] = None) -> "Rotation": rng_seed: Union[None, int, IntSequence] = None) -> 'Rotation':
""" """
Calculate set of rotations with Gaussian distribution around center. Calculate set of rotations with Gaussian distribution around center.