some suggestions from prospector/pylint

This commit is contained in:
Martin Diehl 2022-01-29 22:38:17 +01:00
parent ce4591fa29
commit df96110733
5 changed files with 46 additions and 46 deletions

View File

@ -267,7 +267,7 @@ class Colormap(mpl.colors.ListedColormap):
(bounds[0],bounds[1]) (bounds[0],bounds[1])
if abs(delta := r-l) * 1e8 <= (avg := 0.5*abs(r+l)): # delta is similar to numerical noise if abs(delta := r-l) * 1e8 <= (avg := 0.5*abs(r+l)): # delta is similar to numerical noise
l,r = l-0.5*avg*np.sign(delta),r+0.5*avg*np.sign(delta), # extend range to have actual data centered within l,r = (l-0.5*avg*np.sign(delta),r+0.5*avg*np.sign(delta)) # extend range to have actual data centered within
return Image.fromarray( return Image.fromarray(
(np.dstack(( (np.dstack((
@ -301,7 +301,7 @@ class Colormap(mpl.colors.ListedColormap):
>>> damask.Colormap.from_predefined('stress').reversed() >>> damask.Colormap.from_predefined('stress').reversed()
""" """
rev = super(Colormap,self).reversed(name) rev = super().reversed(name)
return Colormap(np.array(rev.colors),rev.name[:-4] if rev.name.endswith('_r_r') else rev.name) return Colormap(np.array(rev.colors),rev.name[:-4] if rev.name.endswith('_r_r') else rev.name)
@ -436,11 +436,11 @@ class Colormap(mpl.colors.ListedColormap):
def adjust_hue(msh_sat, msh_unsat): def adjust_hue(msh_sat, msh_unsat):
"""If saturation of one of the two colors is much less than the other, hue of the less.""" """If saturation of one of the two colors is much less than the other, hue of the less."""
if msh_sat[0] >= msh_unsat[0]: if msh_sat[0] >= msh_unsat[0]:
return msh_sat[2] return msh_sat[2]
else:
hSpin = msh_sat[1]/np.sin(msh_sat[1])*np.sqrt(msh_unsat[0]**2.0-msh_sat[0]**2)/msh_sat[0] hSpin = msh_sat[1]/np.sin(msh_sat[1])*np.sqrt(msh_unsat[0]**2.0-msh_sat[0]**2)/msh_sat[0]
if msh_sat[2] < - np.pi/3.0: hSpin *= -1.0 if msh_sat[2] < - np.pi/3.0: hSpin *= -1.0
return msh_sat[2] + hSpin return msh_sat[2] + hSpin
lo = np.array(low) lo = np.array(low)
hi = np.array(high) hi = np.array(high)
@ -453,9 +453,9 @@ class Colormap(mpl.colors.ListedColormap):
else: else:
lo = np.array([M_mid,0.0,0.0]) lo = np.array([M_mid,0.0,0.0])
frac = 2.0*frac - 1.0 frac = 2.0*frac - 1.0
if lo[1] < 0.05 and hi[1] > 0.05: if lo[1] < 0.05 < hi[1]:
lo[2] = adjust_hue(hi,lo) lo[2] = adjust_hue(hi,lo)
elif lo[1] > 0.05 and hi[1] < 0.05: elif hi[1] < 0.05 < lo[1]:
hi[2] = adjust_hue(lo,hi) hi[2] = adjust_hue(lo,hi)
return (1.0 - frac) * lo + frac * hi return (1.0 - frac) * lo + frac * hi

View File

@ -33,7 +33,7 @@ class Grid:
material: np.ndarray, material: np.ndarray,
size: FloatSequence, size: FloatSequence,
origin: FloatSequence = np.zeros(3), origin: FloatSequence = np.zeros(3),
comments: Union[str, Sequence[str]] = []): comments: Union[str, Sequence[str]] = None):
""" """
New geometry definition for grid solvers. New geometry definition for grid solvers.
@ -53,7 +53,7 @@ class Grid:
self.material = material self.material = material
self.size = size # type: ignore self.size = size # type: ignore
self.origin = origin # type: ignore self.origin = origin # type: ignore
self.comments = comments # type: ignore self.comments = [] if comments is None else comments # type: ignore
def __repr__(self) -> str: def __repr__(self) -> str:
@ -106,14 +106,14 @@ class Grid:
material: np.ndarray): material: np.ndarray):
if len(material.shape) != 3: if len(material.shape) != 3:
raise ValueError(f'invalid material shape {material.shape}') raise ValueError(f'invalid material shape {material.shape}')
elif material.dtype not in np.sctypes['float'] and material.dtype not in np.sctypes['int']: if material.dtype not in np.sctypes['float'] and material.dtype not in np.sctypes['int']:
raise TypeError(f'invalid material data type {material.dtype}') raise TypeError(f'invalid material data type {material.dtype}')
else:
self._material = np.copy(material)
if self.material.dtype in np.sctypes['float'] and \ self._material = np.copy(material)
np.all(self.material == self.material.astype(int).astype(float)):
self._material = self.material.astype(int) if self.material.dtype in np.sctypes['float'] and \
np.all(self.material == self.material.astype(int).astype(float)):
self._material = self.material.astype(int)
@property @property
@ -126,8 +126,8 @@ class Grid:
size: FloatSequence): size: FloatSequence):
if len(size) != 3 or any(np.array(size) < 0): if len(size) != 3 or any(np.array(size) < 0):
raise ValueError(f'invalid size {size}') raise ValueError(f'invalid size {size}')
else:
self._size = np.array(size) self._size = np.array(size)
@property @property
def origin(self) -> np.ndarray: def origin(self) -> np.ndarray:
@ -139,8 +139,8 @@ class Grid:
origin: FloatSequence): origin: FloatSequence):
if len(origin) != 3: if len(origin) != 3:
raise ValueError(f'invalid origin {origin}') raise ValueError(f'invalid origin {origin}')
else:
self._origin = np.array(origin) self._origin = np.array(origin)
@property @property
def comments(self) -> List[str]: def comments(self) -> List[str]:

View File

@ -526,10 +526,10 @@ class Table:
""" """
if self.shapes != other.shapes or not self.data.columns.equals(other.data.columns): if self.shapes != other.shapes or not self.data.columns.equals(other.data.columns):
raise KeyError('Labels or shapes or order do not match') raise KeyError('Labels or shapes or order do not match')
else:
dup = self.copy() dup = self.copy()
dup.data = dup.data.append(other.data,ignore_index=True) dup.data = dup.data.append(other.data,ignore_index=True)
return dup return dup
def join(self, def join(self,
@ -552,12 +552,12 @@ class Table:
""" """
if set(self.shapes) & set(other.shapes) or self.data.shape[0] != other.data.shape[0]: if set(self.shapes) & set(other.shapes) or self.data.shape[0] != other.data.shape[0]:
raise KeyError('Duplicated keys or row count mismatch') raise KeyError('Duplicated keys or row count mismatch')
else:
dup = self.copy() dup = self.copy()
dup.data = dup.data.join(other.data) dup.data = dup.data.join(other.data)
for key in other.shapes: for key in other.shapes:
dup.shapes[key] = other.shapes[key] dup.shapes[key] = other.shapes[key]
return dup return dup
def save(self, def save(self,

View File

@ -64,7 +64,7 @@ def eigenvectors(T_sym: _np.ndarray,
associated eigenvalues. associated eigenvalues.
""" """
(u,v) = _np.linalg.eigh(symmetric(T_sym)) _,v = _np.linalg.eigh(symmetric(T_sym))
if RHS: v[_np.linalg.det(v) < 0.0,:,2] *= -1.0 if RHS: v[_np.linalg.det(v) < 0.0,:,2] *= -1.0
return v return v

View File

@ -7,7 +7,7 @@ import subprocess
import shlex import shlex
import re import re
import fractions import fractions
import collections.abc as abc from collections import abc
from functools import reduce from functools import reduce
from typing import Union, Tuple, Iterable, Callable, Dict, List, Any, Literal, Optional from typing import Union, Tuple, Iterable, Callable, Dict, List, Any, Literal, Optional
from pathlib import Path from pathlib import Path
@ -77,7 +77,7 @@ def srepr(msg,
hasattr(msg, '__iter__'))): hasattr(msg, '__iter__'))):
return glue.join(str(x) for x in msg) return glue.join(str(x) for x in msg)
else: else:
return msg if isinstance(msg,str) else repr(msg) return msg if isinstance(msg,str) else repr(msg)
def emph(msg) -> str: def emph(msg) -> str:
@ -230,15 +230,15 @@ def show_progress(iterable: Iterable,
""" """
if isinstance(iterable,abc.Sequence): if isinstance(iterable,abc.Sequence):
if N_iter is None: if N_iter is None:
N = len(iterable) N = len(iterable)
else: else:
raise ValueError('N_iter given for sequence') raise ValueError('N_iter given for sequence')
else: else:
if N_iter is None: if N_iter is None:
raise ValueError('N_iter not given') raise ValueError('N_iter not given')
else:
N = N_iter N = N_iter
if N <= 1: if N <= 1:
for item in iterable: for item in iterable:
@ -454,7 +454,7 @@ def shapeshifter(fro: Tuple[int, ...],
""" """
if not len(fro) and not len(to): return () if len(fro) == 0 and len(to) == 0: return ()
beg = dict(left ='(^.*\\b)', beg = dict(left ='(^.*\\b)',
right='(^.*?\\b)') right='(^.*?\\b)')
@ -462,8 +462,8 @@ def shapeshifter(fro: Tuple[int, ...],
right='(.*?\\b)') right='(.*?\\b)')
end = dict(left ='(.*?$)', end = dict(left ='(.*?$)',
right='(.*$)') right='(.*$)')
fro = (1,) if not len(fro) else fro fro = (1,) if len(fro) == 0 else fro
to = (1,) if not len(to) else to to = (1,) if len(to) == 0 else to
try: try:
match = re.match(beg[mode] match = re.match(beg[mode]
+f',{sep[mode]}'.join(map(lambda x: f'{x}' +f',{sep[mode]}'.join(map(lambda x: f'{x}'