test __eq__; polish help strings

This commit is contained in:
Philip Eisenlohr 2021-11-25 13:14:34 -05:00
parent 3bf10127b8
commit bd908dc425
2 changed files with 25 additions and 20 deletions

View File

@ -80,7 +80,7 @@ class Colormap(mpl.colors.ListedColormap):
"""
Create a perceptually uniform colormap between given (inclusive) bounds.
Colors are internally stored as R(ed) G(green) B(lue) values.
Colors are internally stored as RGB (Red Green Blue) values.
The colormap can be used in matplotlib/seaborn or exported to
file for external use.
@ -154,8 +154,8 @@ class Colormap(mpl.colors.ListedColormap):
"""
Select from a set of predefined colormaps.
Predefined colormaps include native matplotlib colormaps
and common DAMASK colormaps.
Predefined colormaps (Colormap.predefined) include
native matplotlib colormaps and common DAMASK colormaps.
Parameters
----------
@ -202,7 +202,7 @@ class Colormap(mpl.colors.ListedColormap):
field : numpy.array of shape (:,:)
Data to be shaded.
bounds : iterable of float (2), optional
Value range (low,high) to shade with colormap.
Value range (low,high) spanned by colormap.
gap : field.dtype, optional
Transparent value. NaN will always be rendered transparent.
@ -241,8 +241,8 @@ class Colormap(mpl.colors.ListedColormap):
Parameters
----------
name : str, optional
Name for the reversed colormap.
A name of None will be replaced by the name of the parent colormap + "_r".
Name of the reversed colormap.
If None, parent colormap name + "_r".
Returns
-------
@ -259,26 +259,28 @@ class Colormap(mpl.colors.ListedColormap):
return Colormap(np.array(rev.colors),rev.name[:-4] if rev.name.endswith('_r_r') else rev.name)
def _get_file_handle(self, fname: Union[TextIO, str, Path, None], ext: str) -> TextIO:
def _get_file_handle(self,
fname: Union[TextIO, str, Path, None] = None,
suffix: str = None) -> TextIO:
"""
Provide file handle.
Provide filehandle.
Parameters
----------
fname : file, str, pathlib.Path, or None
Filename or filehandle.
If None, built from colormap name and ext.
ext: str
Extension of the filename.
If None, colormap name + suffix.
suffix: str, optional
Extension to use for colormap filename.
Returns
-------
f : file object
File handle with write access.
Filehandle with write access.
"""
if fname is None:
return open(self.name.replace(' ','_')+(ext if ext.startswith('.') else '.'+ext), 'w', newline='\n')
return open(self.name.replace(' ','_')+suffix, 'w', newline='\n')
elif isinstance(fname, (str, Path)):
return open(fname, 'w', newline='\n')
else:
@ -308,7 +310,7 @@ class Colormap(mpl.colors.ListedColormap):
'RGBPoints':colors
}]
json.dump(out,self._get_file_handle(fname,'json'),indent=4)
json.dump(out,self._get_file_handle(fname,'.json'),indent=4)
def save_ASCII(self, fname: Union[TextIO, str, Path] = None):
@ -324,7 +326,7 @@ class Colormap(mpl.colors.ListedColormap):
"""
labels = {'RGBA':4} if self.colors.shape[1] == 4 else {'RGB': 3}
t = Table(self.colors,labels,f'Creator: {util.execution_stamp("Colormap")}')
t.save(self._get_file_handle(fname,'txt'))
t.save(self._get_file_handle(fname,'.txt'))
def save_GOM(self, fname: Union[TextIO, str, Path] = None):
@ -345,7 +347,7 @@ class Colormap(mpl.colors.ListedColormap):
+ ' '.join([f' 0 {c[0]} {c[1]} {c[2]} 255 1' for c in reversed((self.colors*255).astype(int))]) \
+ '\n'
self._get_file_handle(fname,'legend').write(GOM_str)
self._get_file_handle(fname,'.legend').write(GOM_str)
def save_gmsh(self, fname: Union[TextIO, str, Path] = None):
@ -363,7 +365,7 @@ class Colormap(mpl.colors.ListedColormap):
gmsh_str = 'View.ColorTable = {\n' \
+'\n'.join([f'{c[0]},{c[1]},{c[2]},' for c in self.colors[:,:3]*255]) \
+'\n}\n'
self._get_file_handle(fname,'msh').write(gmsh_str)
self._get_file_handle(fname,'.msh').write(gmsh_str)
@staticmethod

View File

@ -77,12 +77,15 @@ class TestColormap:
# xyz2msh
assert np.allclose(Colormap._xyz2msh(xyz),msh,atol=1.e-6,rtol=0)
def test_eq(self):
assert Colormap.from_predefined('strain') == Colormap.from_predefined('strain')
assert Colormap.from_predefined('strain') != Colormap.from_predefined('stress')
assert Colormap.from_predefined('strain',N=128) != Colormap.from_predefined('strain',N=64)
@pytest.mark.parametrize('low,high',[((0,0,0),(1,1,1)),
([0,0,0],[1,1,1])])
def test_from_range_types(self,low,high):
a = Colormap.from_range(low,high)
b = Colormap.from_range(np.array(low),np.array(high))
assert np.all(a.colors == b.colors)
assert Colormap.from_range(low,high) == Colormap.from_range(np.array(low),np.array(high))
@pytest.mark.parametrize('format',['ASCII','paraview','GOM','gmsh'])
@pytest.mark.parametrize('model',['rgb','hsv','hsl','xyz','lab','msh'])