added typehinting

This commit is contained in:
Philip Eisenlohr 2023-10-24 14:39:17 -04:00
parent 199b84d096
commit 926d86935f
3 changed files with 30 additions and 26 deletions

View File

@ -6,7 +6,8 @@ from ._typehints import FloatSequence, CrystalFamily, BravaisLattice, CrystalKin
from . import util from . import util
from . import Rotation from . import Rotation
lattice_symmetries: Dict[BravaisLattice, CrystalFamily] = {
lattice_symmetries: Dict[Optional[BravaisLattice], CrystalFamily] = {
'aP': 'triclinic', 'aP': 'triclinic',
'mP': 'monoclinic', 'mP': 'monoclinic',
@ -27,7 +28,7 @@ lattice_symmetries: Dict[BravaisLattice, CrystalFamily] = {
'cF': 'cubic', 'cF': 'cubic',
} }
orientation_relationships: Dict[str, Dict[BravaisLattice,np.ndarray]] = { orientation_relationships: Dict[str, Dict[str,List[np.ndarray]]] = {
'KS': { # https://doi.org/10.1016/j.jallcom.2012.02.004 'KS': { # https://doi.org/10.1016/j.jallcom.2012.02.004
'cF-->cI' : [ 'cF-->cI' : [
np.repeat(np.array([ np.repeat(np.array([
@ -546,7 +547,7 @@ class Crystal():
def __eq__(self, def __eq__(self,
other: object) -> bool: other):
""" """
Return self==other. Return self==other.
@ -558,20 +559,20 @@ class Crystal():
Crystal to check for equality. Crystal to check for equality.
""" """
return NotImplemented if not isinstance(other, Crystal) else \ return (NotImplemented if not isinstance(other, Crystal) else
self.lattice == other.lattice and \ self.lattice == other.lattice and
self.parameters == other.parameters and \ self.parameters == other.parameters and
self.family == other.family self.family == other.family)
@property @property
def parameters(self): def parameters(self) -> Tuple:
"""Return lattice parameters a, b, c, alpha, beta, gamma.""" """Return lattice parameters a, b, c, alpha, beta, gamma."""
if hasattr(self,'a'): return (self.a,self.b,self.c,self.alpha,self.beta,self.gamma) return (self.a,self.b,self.c,self.alpha,self.beta,self.gamma) if hasattr(self,'a') else ()
@property @property
def immutable(self): def immutable(self) -> Dict[str, float]:
"""Return immutable lattice parameters.""" """Return immutable lattice parameters."""
_immutable = { _immutable: Dict[CrystalFamily, Dict[str,float]] = {
'cubic': { 'cubic': {
'b': 1.0, 'b': 1.0,
'c': 1.0, 'c': 1.0,
@ -606,9 +607,9 @@ class Crystal():
@property @property
def orientation_relationships(self): def orientation_relationships(self) -> List[str]:
"""Return labels of orientation relationships.""" """Return labels of orientation relationships."""
return [k for k,v in orientation_relationships.items() if np.any([m.startswith(self.lattice) for m in v])] return [k for k,v in orientation_relationships.items() if np.any([m.startswith(str(self.lattice)) for m in v])]
@property @property
@ -801,9 +802,9 @@ class Crystal():
@property @property
def lattice_points(self): def lattice_points(self) -> np.ndarray:
"""Return lattice points.""" """Return lattice points."""
_lattice_points = { _lattice_points: Dict[str, List] = {
'P': [ 'P': [
], ],
'S': [ 'S': [
@ -824,8 +825,8 @@ class Crystal():
if self.lattice is None: raise KeyError('no lattice type specified') if self.lattice is None: raise KeyError('no lattice type specified')
return np.array([[0,0,0]] return np.array([[0,0,0]]
+ _lattice_points.get(self.lattice if self.lattice == 'hP' else \ + _lattice_points.get(self.lattice if self.lattice == 'hP' else
self.lattice[-1],None),dtype=float) self.lattice[-1],[]),dtype=float)
def to_lattice(self, *, def to_lattice(self, *,
direction: Optional[FloatSequence] = None, direction: Optional[FloatSequence] = None,
@ -912,7 +913,7 @@ class Crystal():
Directions and planes of deformation mode families. Directions and planes of deformation mode families.
""" """
_kinematics: Dict[BravaisLattice, Dict[CrystalKinematics, List[np.ndarray]]] = { _kinematics: Dict[Optional[BravaisLattice], Dict[CrystalKinematics, List[np.ndarray]]] = {
'cF': { 'cF': {
'slip': [np.array([ 'slip': [np.array([
[ 0,+1,-1, +1,+1,+1], [ 0,+1,-1, +1,+1,+1],
@ -1192,14 +1193,17 @@ class Crystal():
https://doi.org/10.1016/j.actamat.2004.11.021 https://doi.org/10.1016/j.actamat.2004.11.021
""" """
m_l: BravaisLattice
o_l: BravaisLattice
if model not in self.orientation_relationships: if model not in self.orientation_relationships:
raise KeyError(f'unknown orientation relationship "{model}"') raise KeyError(f'unknown orientation relationship "{model}"')
sep = '-->' sep = '-->'
search = self.lattice+sep+('' if target is None else target.lattice) search = self.lattice+sep+('' if target is None else target.lattice) # type: ignore
m_l,o_l = [transform.split(sep) for transform in orientation_relationships[model].keys() # type: ignore
m_l,o_l = [transform.split(sep) for transform in orientation_relationships[model].keys()
if transform.startswith(search)][0] if transform.startswith(search)][0]
m_p,o_p = orientation_relationships[model][m_l+sep+o_l] m_p,o_p = orientation_relationships[model][m_l+sep+o_l]
other = Crystal(lattice=o_l) if target is None else target other = Crystal(lattice=o_l) if target is None else target
m_p = np.stack((self.to_frame(uvw=m_p[:,0] if len(m_p[0,0])==3 else util.Bravais_to_Miller(uvtw=m_p[:,0])), m_p = np.stack((self.to_frame(uvw=m_p[:,0] if len(m_p[0,0])==3 else util.Bravais_to_Miller(uvtw=m_p[:,0])),

View File

@ -122,7 +122,7 @@ class Orientation(Rotation,Crystal):
def __eq__(self, def __eq__(self,
other: object) -> bool: other: Union[object,MyType]) -> bool:
""" """
Return self==other. Return self==other.
@ -910,7 +910,7 @@ class Orientation(Rotation,Crystal):
lattice,o = self.relation_operations(model,target) lattice,o = self.relation_operations(model,target)
target = Crystal(lattice=lattice) if target is None else target target = Crystal(lattice=lattice) if target is None else target
return Orientation(rotation=o*Rotation(self.quaternion)[np.newaxis,...], return Orientation(rotation=o*Rotation(self.quaternion)[np.newaxis,...], # type: ignore
lattice=lattice, lattice=lattice,
b = self.b if target.ratio['b'] is None else self.a*target.ratio['b'], b = self.b if target.ratio['b'] is None else self.a*target.ratio['b'],
c = self.c if target.ratio['c'] is None else self.a*target.ratio['c'], c = self.c if target.ratio['c'] is None else self.a*target.ratio['c'],

View File

@ -10,8 +10,8 @@ FloatSequence = Union[np.ndarray,Sequence[float]]
IntSequence = Union[np.ndarray,Sequence[int]] IntSequence = Union[np.ndarray,Sequence[int]]
StrSequence = Union[np.ndarray,Sequence[str]] StrSequence = Union[np.ndarray,Sequence[str]]
FileHandle = Union[TextIO, str, Path] FileHandle = Union[TextIO, str, Path]
CrystalFamily = Union[None,Literal['triclinic', 'monoclinic', 'orthorhombic', 'tetragonal', 'hexagonal', 'cubic']] CrystalFamily = Literal['triclinic', 'monoclinic', 'orthorhombic', 'tetragonal', 'hexagonal', 'cubic']
BravaisLattice = Union[None,Literal['aP', 'mP', 'mS', 'oP', 'oS', 'oI', 'oF', 'tP', 'tI', 'hP', 'cP', 'cI', 'cF']] BravaisLattice = Literal['aP', 'mP', 'mS', 'oP', 'oS', 'oI', 'oF', 'tP', 'tI', 'hP', 'cP', 'cI', 'cF']
CrystalKinematics = Literal['slip', 'twin'] CrystalKinematics = Literal['slip', 'twin']
NumpyRngSeed = Union[int, IntSequence, np.random.SeedSequence, np.random.Generator] NumpyRngSeed = Union[int, IntSequence, np.random.SeedSequence, np.random.Generator]
# BitGenerator does not exists in older numpy versions # BitGenerator does not exists in older numpy versions