numpy-compatible shape definition

can be scalar or sequence.
Try to avoid to use the term 'rotation' in the documentation and also
don't specify the return type because it will be used by
'damask.Orientation'.
This commit is contained in:
Martin Diehl 2022-03-19 10:55:03 +01:00
parent 4972c43b17
commit 52d595ff62
1 changed files with 27 additions and 47 deletions

View File

@ -418,15 +418,15 @@ class Rotation:
def reshape(self: MyType, def reshape(self: MyType,
shape: Union[int, Tuple[int, ...]], shape: Union[int, IntSequence],
order: Literal['C','F','A'] = 'C') -> MyType: order: Literal['C','F','A'] = 'C') -> MyType:
""" """
Reshape array. Reshape array.
Parameters Parameters
---------- ----------
shape : int or tuple of ints shape : int or sequence of ints
The new shape should be compatible with the original shape. New shape, number of elements needs to match the original shape.
If an integer is supplied, then the result will be a 1-D array of that length. If an integer is supplied, then the result will be a 1-D array of that length.
order : {'C', 'F', 'A'}, optional order : {'C', 'F', 'A'}, optional
'C' flattens in row-major (C-style) order. 'C' flattens in row-major (C-style) order.
@ -446,15 +446,15 @@ class Rotation:
def broadcast_to(self: MyType, def broadcast_to(self: MyType,
shape: Union[int, Tuple[int, ...]], shape: Union[int, IntSequence],
mode: Literal['left', 'right'] = 'right') -> MyType: mode: Literal['left', 'right'] = 'right') -> MyType:
""" """
Broadcast array. Broadcast array.
Parameters Parameters
---------- ----------
shape : int or tuple of ints shape : int or sequence of ints
Shape of broadcasted array. Shape of broadcasted array, needs to be compatible with the original shape.
mode : str, optional mode : str, optional
Where to preferentially locate missing dimensions. Where to preferentially locate missing dimensions.
Either 'left' or 'right' (default). Either 'left' or 'right' (default).
@ -465,9 +465,9 @@ class Rotation:
Rotation broadcasted to given shape. Rotation broadcasted to given shape.
""" """
if isinstance(shape,(int,np.integer)): shape = (shape,) shape_ = (shape,) if isinstance(shape,(int,np.integer)) else tuple(shape)
return self.copy(np.broadcast_to(self.quaternion.reshape(util.shapeshifter(self.shape,shape,mode)+(4,)), return self.copy(np.broadcast_to(self.quaternion.reshape(util.shapeshifter(self.shape,shape_,mode)+(4,)),
shape+(4,))) shape_+(4,)))
def average(self: MyType, def average(self: MyType,
@ -979,17 +979,15 @@ class Rotation:
@staticmethod @staticmethod
def from_random(shape: Tuple[int, ...] = None, def from_random(shape: Union[int, IntSequence] = None,
rng_seed: NumpyRngSeed = None) -> 'Rotation': rng_seed: NumpyRngSeed = None) -> 'Rotation':
""" """
Initialize with random rotation. Initialize with samples from a uniform distribution.
Rotations are uniformly distributed.
Parameters Parameters
---------- ----------
shape : tuple of ints, optional shape : int or sequence of ints, optional
Shape of the sample. Defaults to None, which gives a single rotation. Shape of the returned array. Defaults to None, which gives a scalar.
rng_seed : {None, int, array_like[ints], SeedSequence, BitGenerator, Generator}, optional rng_seed : {None, int, array_like[ints], SeedSequence, BitGenerator, Generator}, optional
A seed to initialize the BitGenerator. A seed to initialize the BitGenerator.
Defaults to None, i.e. unpredictable entropy will be pulled from the OS. Defaults to None, i.e. unpredictable entropy will be pulled from the OS.
@ -1011,12 +1009,12 @@ class Rotation:
@staticmethod @staticmethod
def from_ODF(weights: np.ndarray, def from_ODF(weights: np.ndarray,
phi: np.ndarray, phi: np.ndarray,
shape: Tuple[int, ...] = None, shape: Union[int, IntSequence] = None,
degrees: bool = True, degrees: bool = True,
fractions: bool = True, fractions: bool = True,
rng_seed: NumpyRngSeed = None) -> 'Rotation': rng_seed: NumpyRngSeed = None) -> 'Rotation':
""" """
Sample discrete values from a binned orientation distribution function (ODF). Initialize with samples from a binned orientation distribution function (ODF).
Parameters Parameters
---------- ----------
@ -1024,9 +1022,8 @@ class Rotation:
Texture intensity values (probability density or volume fraction) at Euler space grid points. Texture intensity values (probability density or volume fraction) at Euler space grid points.
phi : numpy.ndarray, shape (n,3) phi : numpy.ndarray, shape (n,3)
Grid coordinates in Euler space at which weights are defined. Grid coordinates in Euler space at which weights are defined.
shape : tuple of ints, optional shape : int or sequence of ints, optional
Shape of the array of discrete rotations sampled from the given ODF. Shape of the returned array. Defaults to None, which gives a scalar.
Defaults to None, which gives a single rotation.
degrees : bool, optional degrees : bool, optional
Euler space grid coordinates are in degrees. Defaults to True. Euler space grid coordinates are in degrees. Defaults to True.
fractions : bool, optional fractions : bool, optional
@ -1036,11 +1033,6 @@ class Rotation:
A seed to initialize the BitGenerator. A seed to initialize the BitGenerator.
Defaults to None, i.e. unpredictable entropy will be pulled from the OS. Defaults to None, i.e. unpredictable entropy will be pulled from the OS.
Returns
-------
samples : damask.Rotation
Array of sampled rotations that approximate the input ODF.
Notes Notes
----- -----
Due to the distortion of Euler space in the vicinity of ϕ = 0, probability densities, p, defined on Due to the distortion of Euler space in the vicinity of ϕ = 0, probability densities, p, defined on
@ -1070,32 +1062,26 @@ class Rotation:
@staticmethod @staticmethod
def from_spherical_component(center: 'Rotation', def from_spherical_component(center: 'Rotation',
sigma: float, sigma: float,
shape: Tuple[int, ...] = None, shape: Union[int, IntSequence] = None,
degrees: bool = True, degrees: bool = True,
rng_seed: NumpyRngSeed = None) -> 'Rotation': rng_seed: NumpyRngSeed = None) -> 'Rotation':
""" """
Calculate set of rotations with Gaussian distribution around center. Initialize with samples from a Gaussian distribution around a given center.
Parameters Parameters
---------- ----------
center : Rotation center : Rotation or Orientation
Central Rotation. Central rotation.
sigma : float sigma : float
Standard deviation of (Gaussian) misorientation distribution. Standard deviation of (Gaussian) misorientation distribution.
shape : tuple of ints, optional shape : int or sequence of ints, optional
Shape of the array of sampled rotations. Shape of the returned array. Defaults to None, which gives a scalar.
Defaults to None, which gives a single rotation.
degrees : bool, optional degrees : bool, optional
sigma is given in degrees. Defaults to True. sigma is given in degrees. Defaults to True.
rng_seed : {None, int, array_like[ints], SeedSequence, BitGenerator, Generator}, optional rng_seed : {None, int, array_like[ints], SeedSequence, BitGenerator, Generator}, optional
A seed to initialize the BitGenerator. A seed to initialize the BitGenerator.
Defaults to None, i.e. unpredictable entropy will be pulled from the OS. Defaults to None, i.e. unpredictable entropy will be pulled from the OS.
Returns
-------
samples : damask.Rotation
Array of rotations sampled from the spherical component.
""" """
rng = np.random.default_rng(rng_seed) rng = np.random.default_rng(rng_seed)
sigma = np.radians(sigma) if degrees else sigma sigma = np.radians(sigma) if degrees else sigma
@ -1113,11 +1099,11 @@ class Rotation:
def from_fiber_component(alpha: IntSequence, def from_fiber_component(alpha: IntSequence,
beta: IntSequence, beta: IntSequence,
sigma: float = 0.0, sigma: float = 0.0,
shape: Tuple[int, ...] = None, shape: Union[int, IntSequence] = None,
degrees: bool = True, degrees: bool = True,
rng_seed: NumpyRngSeed = None): rng_seed: NumpyRngSeed = None):
""" """
Calculate set of rotations with Gaussian distribution around direction. Initialize with samples from a Gaussian distribution around a given direction.
Parameters Parameters
---------- ----------
@ -1128,20 +1114,14 @@ class Rotation:
sigma : float, optional sigma : float, optional
Standard deviation of (Gaussian) misorientation distribution. Standard deviation of (Gaussian) misorientation distribution.
Defaults to 0. Defaults to 0.
shape : tuple of ints, optional shape : int or sequence of ints, optional
Shape of the array of sampled rotations. Shape of the returned array. Defaults to None, which gives a scalar.
Defaults to None, which gives a single rotation.
degrees : bool, optional degrees : bool, optional
sigma, alpha, and beta are given in degrees. sigma, alpha, and beta are given in degrees.
rng_seed : {None, int, array_like[ints], SeedSequence, BitGenerator, Generator}, optional rng_seed : {None, int, array_like[ints], SeedSequence, BitGenerator, Generator}, optional
A seed to initialize the BitGenerator. A seed to initialize the BitGenerator.
Defaults to None, i.e. unpredictable entropy will be pulled from the OS. Defaults to None, i.e. unpredictable entropy will be pulled from the OS.
Returns
-------
samples : damask.Rotation
Array of rotations sampled from the fiber component.
""" """
rng = np.random.default_rng(rng_seed) rng = np.random.default_rng(rng_seed)
sigma_,alpha_,beta_ = (np.radians(coordinate) for coordinate in (sigma,alpha,beta)) if degrees else \ sigma_,alpha_,beta_ = (np.radians(coordinate) for coordinate in (sigma,alpha,beta)) if degrees else \