From 509835bf0bed72a2e3842e368caeb80b996d2e95 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sun, 31 Oct 2021 22:37:54 +0100 Subject: [PATCH 01/10] type hints for tensor adjustments to Rotation/Orientation needed to enable type checking with mypy --- python/damask/_orientation.py | 2 +- python/damask/_rotation.py | 9 +++++++-- python/damask/tensor.py | 12 ++++++------ 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/python/damask/_orientation.py b/python/damask/_orientation.py index 3d4d259ff..bb14fd38b 100644 --- a/python/damask/_orientation.py +++ b/python/damask/_orientation.py @@ -125,7 +125,7 @@ class Orientation(Rotation,Crystal): """Create deep copy.""" dup = copy.deepcopy(self) if rotation is not None: - dup.quaternion = Orientation(rotation,family='cubic').quaternion + dup.quaternion = Rotation(rotation).quaternion return dup copy = __copy__ diff --git a/python/damask/_rotation.py b/python/damask/_rotation.py index 74a3f7419..ac921d70a 100644 --- a/python/damask/_rotation.py +++ b/python/damask/_rotation.py @@ -1,3 +1,5 @@ +import copy + import numpy as np from . import tensor @@ -85,9 +87,12 @@ class Rotation: + str(self.quaternion) - def __copy__(self,**kwargs): + def __copy__(self,rotation=None): """Create deep copy.""" - return self.__class__(rotation=kwargs['rotation'] if 'rotation' in kwargs else self.quaternion) + dup = copy.deepcopy(self) + if rotation is not None: + dup.quaternion = Rotation(rotation).quaternion + return dup copy = __copy__ diff --git a/python/damask/tensor.py b/python/damask/tensor.py index cf5d94020..a735b355e 100644 --- a/python/damask/tensor.py +++ b/python/damask/tensor.py @@ -8,7 +8,7 @@ All routines operate on numpy.ndarrays of shape (...,3,3). import numpy as _np -def deviatoric(T): +def deviatoric(T: _np.ndarray) -> _np.ndarray: """ Calculate deviatoric part of a tensor. @@ -26,7 +26,7 @@ def deviatoric(T): return T - spherical(T,tensor=True) -def eigenvalues(T_sym): +def eigenvalues(T_sym: _np.ndarray) -> _np.ndarray: """ Eigenvalues, i.e. principal components, of a symmetric tensor. @@ -45,7 +45,7 @@ def eigenvalues(T_sym): return _np.linalg.eigvalsh(symmetric(T_sym)) -def eigenvectors(T_sym,RHS=False): +def eigenvectors(T_sym: _np.ndarray, RHS: bool = False) -> _np.ndarray: """ Eigenvectors of a symmetric tensor. @@ -70,7 +70,7 @@ def eigenvectors(T_sym,RHS=False): return v -def spherical(T,tensor=True): +def spherical(T: _np.ndarray, tensor: bool = True) -> _np.ndarray: """ Calculate spherical part of a tensor. @@ -92,7 +92,7 @@ def spherical(T,tensor=True): return _np.einsum('...jk,...',_np.eye(3),sph) if tensor else sph -def symmetric(T): +def symmetric(T: _np.ndarray) -> _np.ndarray: """ Symmetrize tensor. @@ -110,7 +110,7 @@ def symmetric(T): return (T+transpose(T))*0.5 -def transpose(T): +def transpose(T: _np.ndarray) -> _np.ndarray: """ Transpose tensor. From 2d25dfcdf2811d1671e66d57bfc94fb5da2f8f9b Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sun, 31 Oct 2021 22:43:06 +0100 Subject: [PATCH 02/10] automated type checking --- .gitlab-ci.yml | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 8680b2873..1fd5a6555 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -81,7 +81,7 @@ checkout: - release ################################################################################################### -processing: +pytest: stage: python script: - cd $DAMASKROOT/python @@ -91,6 +91,16 @@ processing: - master - release +mypy: + stage: python + script: + - cd $DAMASKROOT/python + - mypy damask/mechanics.py + except: + - master + - release + + ################################################################################################### compile_grid_Intel: From 0bc267c76b6101e9886101b522343eff6445df41 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sun, 31 Oct 2021 22:50:41 +0100 Subject: [PATCH 03/10] automated type checking for mechanics --- .gitlab-ci.yml | 3 +-- python/damask/mechanics.py | 42 ++++++++++++++++++++------------------ python/mypy.ini | 16 +++++++++++++++ 3 files changed, 39 insertions(+), 22 deletions(-) create mode 100644 python/mypy.ini diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 1fd5a6555..83e9f8934 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -95,13 +95,12 @@ mypy: stage: python script: - cd $DAMASKROOT/python - - mypy damask/mechanics.py + - mypy damask/tensor.py damask/mechanics.py except: - master - release - ################################################################################################### compile_grid_Intel: stage: compile diff --git a/python/damask/mechanics.py b/python/damask/mechanics.py index 0e160523b..1a03f390b 100644 --- a/python/damask/mechanics.py +++ b/python/damask/mechanics.py @@ -5,13 +5,15 @@ All routines operate on numpy.ndarrays of shape (...,3,3). """ -from . import tensor as _tensor -from . import _rotation +from typing import Sequence import numpy as _np +from . import tensor as _tensor +from . import _rotation -def deformation_Cauchy_Green_left(F): + +def deformation_Cauchy_Green_left(F: _np.ndarray) -> _np.ndarray: """ Calculate left Cauchy-Green deformation tensor (Finger deformation tensor). @@ -29,7 +31,7 @@ def deformation_Cauchy_Green_left(F): return _np.matmul(F,_tensor.transpose(F)) -def deformation_Cauchy_Green_right(F): +def deformation_Cauchy_Green_right(F: _np.ndarray) -> _np.ndarray: """ Calculate right Cauchy-Green deformation tensor. @@ -47,7 +49,7 @@ def deformation_Cauchy_Green_right(F): return _np.matmul(_tensor.transpose(F),F) -def equivalent_strain_Mises(epsilon): +def equivalent_strain_Mises(epsilon: _np.ndarray) -> _np.ndarray: """ Calculate the Mises equivalent of a strain tensor. @@ -65,7 +67,7 @@ def equivalent_strain_Mises(epsilon): return _equivalent_Mises(epsilon,2.0/3.0) -def equivalent_stress_Mises(sigma): +def equivalent_stress_Mises(sigma: _np.ndarray) -> _np.ndarray: """ Calculate the Mises equivalent of a stress tensor. @@ -83,7 +85,7 @@ def equivalent_stress_Mises(sigma): return _equivalent_Mises(sigma,3.0/2.0) -def maximum_shear(T_sym): +def maximum_shear(T_sym: _np.ndarray) -> _np.ndarray: """ Calculate the maximum shear component of a symmetric tensor. @@ -102,7 +104,7 @@ def maximum_shear(T_sym): return (w[...,0] - w[...,2])*0.5 -def rotation(T): +def rotation(T: _np.ndarray) -> _rotation.Rotation: """ Calculate the rotational part of a tensor. @@ -120,7 +122,7 @@ def rotation(T): return _rotation.Rotation.from_matrix(_polar_decomposition(T,'R')[0]) -def strain(F,t,m): +def strain(F: _np.ndarray, t: str, m: float) -> _np.ndarray: """ Calculate strain tensor (Seth–Hill family). @@ -160,7 +162,7 @@ def strain(F,t,m): return eps -def stress_Cauchy(P,F): +def stress_Cauchy(P: _np.ndarray, F: _np.ndarray) -> _np.ndarray: """ Calculate the Cauchy stress (true stress). @@ -182,7 +184,7 @@ def stress_Cauchy(P,F): return _tensor.symmetric(_np.einsum('...,...ij,...kj',1.0/_np.linalg.det(F),P,F)) -def stress_second_Piola_Kirchhoff(P,F): +def stress_second_Piola_Kirchhoff(P: _np.ndarray, F: _np.ndarray) -> _np.ndarray: """ Calculate the second Piola-Kirchhoff stress. @@ -205,7 +207,7 @@ def stress_second_Piola_Kirchhoff(P,F): return _tensor.symmetric(_np.einsum('...ij,...jk',_np.linalg.inv(F),P)) -def stretch_left(T): +def stretch_left(T: _np.ndarray) -> _np.ndarray: """ Calculate left stretch of a tensor. @@ -223,7 +225,7 @@ def stretch_left(T): return _polar_decomposition(T,'V')[0] -def stretch_right(T): +def stretch_right(T: _np.ndarray) -> _np.ndarray: """ Calculate right stretch of a tensor. @@ -241,7 +243,7 @@ def stretch_right(T): return _polar_decomposition(T,'U')[0] -def _polar_decomposition(T,requested): +def _polar_decomposition(T: _np.ndarray, requested: Sequence[str]) -> tuple: """ Perform singular value decomposition. @@ -257,21 +259,21 @@ def _polar_decomposition(T,requested): u, _, vh = _np.linalg.svd(T) R = _np.einsum('...ij,...jk',u,vh) - output = () + output = [] if 'R' in requested: - output+=(R,) + output+=[R] if 'V' in requested: - output+=(_np.einsum('...ij,...kj',T,R),) + output+=[_np.einsum('...ij,...kj',T,R)] if 'U' in requested: - output+=(_np.einsum('...ji,...jk',R,T),) + output+=[_np.einsum('...ji,...jk',R,T)] if len(output) == 0: raise ValueError('output needs to be out of V, R, U') - return output + return tuple(output) -def _equivalent_Mises(T_sym,s): +def _equivalent_Mises(T_sym: _np.ndarray, s: float) -> _np.ndarray: """ Base equation for Mises equivalent of a stress or strain tensor. diff --git a/python/mypy.ini b/python/mypy.ini new file mode 100644 index 000000000..e6900781c --- /dev/null +++ b/python/mypy.ini @@ -0,0 +1,16 @@ +[mypy-scipy.*] +ignore_missing_imports = True +[mypy-h5py.*] +ignore_missing_imports = True +[mypy-vtk.*] +ignore_missing_imports = True +[mypy-PIL.*] +ignore_missing_imports = True +[mypy-matplotlib.*] +ignore_missing_imports = True +[mypy-yaml.*] +ignore_missing_imports = True +[mypy-pandas.*] +ignore_missing_imports = True +[mypy-wx.*] +ignore_missing_imports = True From 34e04fa45e98c522280c5832758d907c07d8445e Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Mon, 1 Nov 2021 10:00:13 +0100 Subject: [PATCH 04/10] tests should work after updating test server typehints for numpy and pyyaml are now available --- python/mypy.ini | 2 -- 1 file changed, 2 deletions(-) diff --git a/python/mypy.ini b/python/mypy.ini index e6900781c..01001daa6 100644 --- a/python/mypy.ini +++ b/python/mypy.ini @@ -8,8 +8,6 @@ ignore_missing_imports = True ignore_missing_imports = True [mypy-matplotlib.*] ignore_missing_imports = True -[mypy-yaml.*] -ignore_missing_imports = True [mypy-pandas.*] ignore_missing_imports = True [mypy-wx.*] From 32aff9d96619c68b85652efdb69157b2bd099851 Mon Sep 17 00:00:00 2001 From: Philip Eisenlohr Date: Tue, 2 Nov 2021 13:01:32 -0400 Subject: [PATCH 05/10] added typehints to seeds.py --- .gitlab-ci.yml | 2 +- python/damask/seeds.py | 16 ++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 83e9f8934..e064cb0af 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -95,7 +95,7 @@ mypy: stage: python script: - cd $DAMASKROOT/python - - mypy damask/tensor.py damask/mechanics.py + - mypy damask/tensor.py damask/mechanics.py damask/seeds.py except: - master - release diff --git a/python/damask/seeds.py b/python/damask/seeds.py index 26aa3084b..2203c9495 100644 --- a/python/damask/seeds.py +++ b/python/damask/seeds.py @@ -1,13 +1,16 @@ """Functionality for generation of seed points for Voronoi or Laguerre tessellation.""" from scipy import spatial as _spatial +from typing import Sequence + import numpy as _np from . import util as _util from . import grid_filters as _grid_filters +from . import _grid -def from_random(size,N_seeds,cells=None,rng_seed=None): +def from_random(size: _np.ndarray, N_seeds: int, cells: _np.ndarray = None, rng_seed=None) -> _np.ndarray: """ Place seeds randomly in space. @@ -41,7 +44,8 @@ def from_random(size,N_seeds,cells=None,rng_seed=None): return coords -def from_Poisson_disc(size,N_seeds,N_candidates,distance,periodic=True,rng_seed=None): +def from_Poisson_disc(size: _np.ndarray, N_seeds: int, N_candidates: int, distance: float, + periodic: bool = True, rng_seed=None) -> _np.ndarray: """ Place seeds according to a Poisson disc distribution. @@ -75,18 +79,17 @@ def from_Poisson_disc(size,N_seeds,N_candidates,distance,periodic=True,rng_seed= i = 0 progress = _util._ProgressBar(N_seeds+1,'',50) while s < N_seeds: + i += 1 candidates = rng.random((N_candidates,3))*_np.broadcast_to(size,(N_candidates,3)) tree = _spatial.cKDTree(coords[:s],boxsize=size) if periodic else \ _spatial.cKDTree(coords[:s]) distances = tree.query(candidates)[0] best = distances.argmax() if distances[best] > distance: # require minimum separation + i = 0 coords[s] = candidates[best] # maximum separation to existing point cloud s += 1 progress.update(s) - i = 0 - else: - i += 1 if i == 100: raise ValueError('Seeding not possible') @@ -94,7 +97,8 @@ def from_Poisson_disc(size,N_seeds,N_candidates,distance,periodic=True,rng_seed= return coords -def from_grid(grid,selection=None,invert=False,average=False,periodic=True): +def from_grid(grid: _grid.Grid, selection: Sequence[int] = None, + invert: bool = False, average: bool = False, periodic: bool = True) -> tuple[_np.ndarray, _np.ndarray]: """ Create seeds from grid description. From 735952bd32013b182b9c599c77e8d2b37ebd1404 Mon Sep 17 00:00:00 2001 From: Philip Eisenlohr Date: Tue, 2 Nov 2021 13:40:09 -0400 Subject: [PATCH 06/10] fixed typo --- python/damask/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/damask/__init__.py b/python/damask/__init__.py index 001e46276..231fa8b30 100644 --- a/python/damask/__init__.py +++ b/python/damask/__init__.py @@ -14,8 +14,8 @@ from . import tensor # noqa from . import mechanics # noqa from . import solver # noqa from . import grid_filters # noqa -#Modules that contain only one class (of the same name), are prefixed by a '_'. -#For example, '_colormap' containsa class called 'Colormap' which is imported as 'damask.Colormap'. +# Modules that contain only one class (of the same name), are prefixed by a '_'. +# For example, '_colormap' contains a class called 'Colormap' which is imported as 'damask.Colormap'. from ._rotation import Rotation # noqa from ._crystal import Crystal # noqa from ._orientation import Orientation # noqa From 8636c5dad4be71fee5805c6394634ee7b146bbeb Mon Sep 17 00:00:00 2001 From: Philip Eisenlohr Date: Tue, 2 Nov 2021 13:41:05 -0400 Subject: [PATCH 07/10] removed grid typehint <-- no clue how to break circular import --- python/damask/seeds.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/python/damask/seeds.py b/python/damask/seeds.py index 2203c9495..85b28850b 100644 --- a/python/damask/seeds.py +++ b/python/damask/seeds.py @@ -7,7 +7,6 @@ import numpy as _np from . import util as _util from . import grid_filters as _grid_filters -from . import _grid def from_random(size: _np.ndarray, N_seeds: int, cells: _np.ndarray = None, rng_seed=None) -> _np.ndarray: @@ -97,7 +96,7 @@ def from_Poisson_disc(size: _np.ndarray, N_seeds: int, N_candidates: int, distan return coords -def from_grid(grid: _grid.Grid, selection: Sequence[int] = None, +def from_grid(grid, selection: Sequence[int] = None, invert: bool = False, average: bool = False, periodic: bool = True) -> tuple[_np.ndarray, _np.ndarray]: """ Create seeds from grid description. @@ -105,15 +104,15 @@ def from_grid(grid: _grid.Grid, selection: Sequence[int] = None, Parameters ---------- grid : damask.Grid - Grid, from which the material IDs are used as seeds. + Grid from which the material IDs are used as seeds. selection : iterable of integers, optional Material IDs to consider. invert : boolean, false - Do not consider the material IDs given in selection. Defaults to False. + Consider all material IDs except those in selection. Defaults to False. average : boolean, optional Seed corresponds to center of gravity of material ID cloud. periodic : boolean, optional - Center of gravity with periodic boundaries. + Center of gravity accounts for periodic boundaries. Returns ------- From ccfe276ae1283a2806b0b1dd9ad08119d7371b7e Mon Sep 17 00:00:00 2001 From: Philip Eisenlohr Date: Tue, 2 Nov 2021 14:28:54 -0400 Subject: [PATCH 08/10] fixed type hinting for seeds.py and grid_filters.py --- python/damask/grid_filters.py | 56 ++++++++++++++++++++--------------- python/damask/seeds.py | 4 +-- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/python/damask/grid_filters.py b/python/damask/grid_filters.py index 816c727cd..cb6388f41 100644 --- a/python/damask/grid_filters.py +++ b/python/damask/grid_filters.py @@ -12,9 +12,11 @@ the following operations are required for tensorial data: """ from scipy import spatial as _spatial +from typing import Sequence, Tuple, Union + import numpy as _np -def _ks(size,cells,first_order=False): +def _ks(size: _np.ndarray, cells: Union[_np.ndarray,Sequence[int]], first_order: bool = False) -> _np.ndarray: """ Get wave numbers operator. @@ -41,7 +43,7 @@ def _ks(size,cells,first_order=False): return _np.stack(_np.meshgrid(k_sk,k_sj,k_si,indexing = 'ij'), axis=-1) -def curl(size,f): +def curl(size: _np.ndarray, f: _np.ndarray) -> _np.ndarray: u""" Calculate curl of a vector or tensor field in Fourier space. @@ -72,7 +74,7 @@ def curl(size,f): return _np.fft.irfftn(curl_,axes=(0,1,2),s=f.shape[:3]) -def divergence(size,f): +def divergence(size: _np.ndarray, f: _np.ndarray) -> _np.ndarray: u""" Calculate divergence of a vector or tensor field in Fourier space. @@ -99,7 +101,7 @@ def divergence(size,f): return _np.fft.irfftn(div_,axes=(0,1,2),s=f.shape[:3]) -def gradient(size,f): +def gradient(size: _np.ndarray, f: _np.ndarray) -> _np.ndarray: u""" Calculate gradient of a scalar or vector fieldin Fourier space. @@ -126,7 +128,9 @@ def gradient(size,f): return _np.fft.irfftn(grad_,axes=(0,1,2),s=f.shape[:3]) -def coordinates0_point(cells,size,origin=_np.zeros(3)): +def coordinates0_point(cells: Union[ _np.ndarray,Sequence[int]], + size: _np.ndarray, + origin: _np.ndarray = _np.zeros(3)) -> _np.ndarray: """ Cell center positions (undeformed). @@ -145,8 +149,8 @@ def coordinates0_point(cells,size,origin=_np.zeros(3)): Undeformed cell center coordinates. """ - start = origin + size/cells*.5 - end = origin + size - size/cells*.5 + start = origin + size/_np.array(cells)*.5 + end = origin + size - size/_np.array(cells)*.5 return _np.stack(_np.meshgrid(_np.linspace(start[0],end[0],cells[0]), _np.linspace(start[1],end[1],cells[1]), @@ -154,7 +158,7 @@ def coordinates0_point(cells,size,origin=_np.zeros(3)): axis = -1) -def displacement_fluct_point(size,F): +def displacement_fluct_point(size: _np.ndarray, F: _np.ndarray) -> _np.ndarray: """ Cell center displacement field from fluctuation part of the deformation gradient field. @@ -186,7 +190,7 @@ def displacement_fluct_point(size,F): return _np.fft.irfftn(displacement,axes=(0,1,2),s=F.shape[:3]) -def displacement_avg_point(size,F): +def displacement_avg_point(size: _np.ndarray, F: _np.ndarray) -> _np.ndarray: """ Cell center displacement field from average part of the deformation gradient field. @@ -207,7 +211,7 @@ def displacement_avg_point(size,F): return _np.einsum('ml,ijkl->ijkm',F_avg - _np.eye(3),coordinates0_point(F.shape[:3],size)) -def displacement_point(size,F): +def displacement_point(size: _np.ndarray, F: _np.ndarray) -> _np.ndarray: """ Cell center displacement field from deformation gradient field. @@ -227,7 +231,7 @@ def displacement_point(size,F): return displacement_avg_point(size,F) + displacement_fluct_point(size,F) -def coordinates_point(size,F,origin=_np.zeros(3)): +def coordinates_point(size: _np.ndarray, F: _np.ndarray, origin: _np.ndarray = _np.zeros(3)) -> _np.ndarray: """ Cell center positions. @@ -249,7 +253,8 @@ def coordinates_point(size,F,origin=_np.zeros(3)): return coordinates0_point(F.shape[:3],size,origin) + displacement_point(size,F) -def cellsSizeOrigin_coordinates0_point(coordinates0,ordered=True): +def cellsSizeOrigin_coordinates0_point(coordinates0: _np.ndarray, + ordered: bool = True) -> Tuple[_np.ndarray,_np.ndarray,_np.ndarray]: """ Return grid 'DNA', i.e. cells, size, and origin from 1D array of point positions. @@ -292,13 +297,15 @@ def cellsSizeOrigin_coordinates0_point(coordinates0,ordered=True): raise ValueError('Regular cell spacing violated.') if ordered and not _np.allclose(coordinates0.reshape(tuple(cells)+(3,),order='F'), - coordinates0_point(cells,size,origin),atol=atol): + coordinates0_point(list(cells),size,origin),atol=atol): raise ValueError('Input data is not ordered (x fast, z slow).') return (cells,size,origin) -def coordinates0_node(cells,size,origin=_np.zeros(3)): +def coordinates0_node(cells: Union[_np.ndarray,Sequence[int]], + size: _np.ndarray, + origin: _np.ndarray = _np.zeros(3)) -> _np.ndarray: """ Nodal positions (undeformed). @@ -323,7 +330,7 @@ def coordinates0_node(cells,size,origin=_np.zeros(3)): axis = -1) -def displacement_fluct_node(size,F): +def displacement_fluct_node(size: _np.ndarray, F: _np.ndarray) -> _np.ndarray: """ Nodal displacement field from fluctuation part of the deformation gradient field. @@ -343,7 +350,7 @@ def displacement_fluct_node(size,F): return point_to_node(displacement_fluct_point(size,F)) -def displacement_avg_node(size,F): +def displacement_avg_node(size: _np.ndarray, F: _np.ndarray) -> _np.ndarray: """ Nodal displacement field from average part of the deformation gradient field. @@ -364,7 +371,7 @@ def displacement_avg_node(size,F): return _np.einsum('ml,ijkl->ijkm',F_avg - _np.eye(3),coordinates0_node(F.shape[:3],size)) -def displacement_node(size,F): +def displacement_node(size: _np.ndarray, F: _np.ndarray) -> _np.ndarray: """ Nodal displacement field from deformation gradient field. @@ -384,7 +391,7 @@ def displacement_node(size,F): return displacement_avg_node(size,F) + displacement_fluct_node(size,F) -def coordinates_node(size,F,origin=_np.zeros(3)): +def coordinates_node(size: _np.ndarray, F: _np.ndarray, origin: _np.ndarray = _np.zeros(3)) -> _np.ndarray: """ Nodal positions. @@ -406,7 +413,8 @@ def coordinates_node(size,F,origin=_np.zeros(3)): return coordinates0_node(F.shape[:3],size,origin) + displacement_node(size,F) -def cellsSizeOrigin_coordinates0_node(coordinates0,ordered=True): +def cellsSizeOrigin_coordinates0_node(coordinates0: _np.ndarray, + ordered: bool = True) -> Tuple[_np.ndarray,_np.ndarray,_np.ndarray]: """ Return grid 'DNA', i.e. cells, size, and origin from 1D array of nodal positions. @@ -441,13 +449,13 @@ def cellsSizeOrigin_coordinates0_node(coordinates0,ordered=True): raise ValueError('Regular cell spacing violated.') if ordered and not _np.allclose(coordinates0.reshape(tuple(cells+1)+(3,),order='F'), - coordinates0_node(cells,size,origin),atol=atol): + coordinates0_node(list(cells),size,origin),atol=atol): raise ValueError('Input data is not ordered (x fast, z slow).') return (cells,size,origin) -def point_to_node(cell_data): +def point_to_node(cell_data: _np.ndarray) -> _np.ndarray: """ Interpolate periodic point data to nodal data. @@ -469,7 +477,7 @@ def point_to_node(cell_data): return _np.pad(n,((0,1),(0,1),(0,1))+((0,0),)*len(cell_data.shape[3:]),mode='wrap') -def node_to_point(node_data): +def node_to_point(node_data: _np.ndarray) -> _np.ndarray: """ Interpolate periodic nodal data to point data. @@ -491,7 +499,7 @@ def node_to_point(node_data): return c[1:,1:,1:] -def coordinates0_valid(coordinates0): +def coordinates0_valid(coordinates0: _np.ndarray) -> bool: """ Check whether coordinates form a regular grid. @@ -513,7 +521,7 @@ def coordinates0_valid(coordinates0): return False -def regrid(size,F,cells): +def regrid(size: _np.ndarray, F: _np.ndarray, cells: Union[_np.ndarray,Sequence[int]]) -> _np.ndarray: """ Return mapping from coordinates in deformed configuration to a regular grid. diff --git a/python/damask/seeds.py b/python/damask/seeds.py index 85b28850b..1d7dfc58e 100644 --- a/python/damask/seeds.py +++ b/python/damask/seeds.py @@ -1,7 +1,7 @@ """Functionality for generation of seed points for Voronoi or Laguerre tessellation.""" from scipy import spatial as _spatial -from typing import Sequence +from typing import Sequence,Tuple import numpy as _np @@ -97,7 +97,7 @@ def from_Poisson_disc(size: _np.ndarray, N_seeds: int, N_candidates: int, distan def from_grid(grid, selection: Sequence[int] = None, - invert: bool = False, average: bool = False, periodic: bool = True) -> tuple[_np.ndarray, _np.ndarray]: + invert: bool = False, average: bool = False, periodic: bool = True) -> Tuple[_np.ndarray, _np.ndarray]: """ Create seeds from grid description. From 59a6dc365277d9eb3b997c5cd43f15bf18034ba4 Mon Sep 17 00:00:00 2001 From: Philip Eisenlohr Date: Tue, 2 Nov 2021 14:40:09 -0400 Subject: [PATCH 09/10] run mypy on all python/damask --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index e064cb0af..14783263d 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -95,7 +95,7 @@ mypy: stage: python script: - cd $DAMASKROOT/python - - mypy damask/tensor.py damask/mechanics.py damask/seeds.py + - mypy damask/*.py except: - master - release From f0c587d4aa468446201cdf850735a98728547332 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Wed, 3 Nov 2021 07:53:38 +0100 Subject: [PATCH 10/10] polishing oder of imports is build-in, 3rd party, internal --- .gitlab-ci.yml | 2 +- python/damask/grid_filters.py | 4 +++- python/damask/seeds.py | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 14783263d..165cdc68f 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -95,7 +95,7 @@ mypy: stage: python script: - cd $DAMASKROOT/python - - mypy damask/*.py + - mypy -m damask except: - master - release diff --git a/python/damask/grid_filters.py b/python/damask/grid_filters.py index cb6388f41..42b5a16c4 100644 --- a/python/damask/grid_filters.py +++ b/python/damask/grid_filters.py @@ -11,11 +11,13 @@ the following operations are required for tensorial data: - D1 = D3.reshape(cells+(-1,)).reshape(-1,9,order='F') """ -from scipy import spatial as _spatial + from typing import Sequence, Tuple, Union +from scipy import spatial as _spatial import numpy as _np + def _ks(size: _np.ndarray, cells: Union[_np.ndarray,Sequence[int]], first_order: bool = False) -> _np.ndarray: """ Get wave numbers operator. diff --git a/python/damask/seeds.py b/python/damask/seeds.py index 1d7dfc58e..4d5a8c624 100644 --- a/python/damask/seeds.py +++ b/python/damask/seeds.py @@ -1,8 +1,8 @@ """Functionality for generation of seed points for Voronoi or Laguerre tessellation.""" -from scipy import spatial as _spatial from typing import Sequence,Tuple +from scipy import spatial as _spatial import numpy as _np from . import util as _util