2020-06-27 23:13:35 +05:30
|
|
|
import os
|
2020-06-28 14:50:09 +05:30
|
|
|
import filecmp
|
|
|
|
import time
|
2020-06-27 23:13:35 +05:30
|
|
|
|
2020-06-27 19:31:16 +05:30
|
|
|
import numpy as np
|
2020-06-27 23:13:35 +05:30
|
|
|
import pytest
|
2020-08-04 23:44:04 +05:30
|
|
|
from PIL import Image
|
|
|
|
from PIL import ImageChops
|
2020-06-27 19:31:16 +05:30
|
|
|
|
|
|
|
from damask import Colormap
|
|
|
|
|
2020-06-28 14:50:09 +05:30
|
|
|
@pytest.fixture
|
2023-04-24 10:41:14 +05:30
|
|
|
def res_path(res_path_base):
|
|
|
|
"""Directory containing testing resources."""
|
|
|
|
return res_path_base/'Colormap'
|
2020-06-28 14:50:09 +05:30
|
|
|
|
2020-06-27 19:31:16 +05:30
|
|
|
class TestColormap:
|
|
|
|
|
2020-08-24 04:01:38 +05:30
|
|
|
@pytest.fixture(autouse=True)
|
2020-11-15 16:19:52 +05:30
|
|
|
def _patch_execution_stamp(self, patch_execution_stamp):
|
2020-08-25 00:20:40 +05:30
|
|
|
print('patched damask.util.execution_stamp')
|
2020-06-27 19:31:16 +05:30
|
|
|
|
2020-11-15 16:30:26 +05:30
|
|
|
def test_repr(self,patch_plt_show):
|
|
|
|
print(Colormap.from_predefined('stress'))
|
|
|
|
|
2020-06-27 19:31:16 +05:30
|
|
|
def test_conversion(self):
|
|
|
|
specials = np.array([[0.,0.,0.],
|
|
|
|
[1.,0.,0.],
|
|
|
|
[0.,1.,0.],
|
|
|
|
[0.,0.,1.],
|
|
|
|
[1.,1.,0.],
|
|
|
|
[0.,1.,1.],
|
|
|
|
[1.,0.,1.],
|
|
|
|
[1.,1.,1.]
|
|
|
|
])
|
2020-06-27 23:13:35 +05:30
|
|
|
rgbs = np.vstack((specials,np.random.rand(100,3)))
|
2020-06-27 19:31:16 +05:30
|
|
|
for rgb in rgbs:
|
|
|
|
print('rgb',rgb)
|
|
|
|
|
|
|
|
# rgb2hsv2rgb
|
2020-06-27 22:25:27 +05:30
|
|
|
hsv = Colormap._rgb2hsv(rgb)
|
|
|
|
print('hsv',hsv)
|
|
|
|
assert np.allclose(Colormap._hsv2rgb(hsv),rgb)
|
2020-06-27 19:31:16 +05:30
|
|
|
|
|
|
|
# rgb2hsl2rgb
|
2020-06-27 22:25:27 +05:30
|
|
|
hsl = Colormap._rgb2hsl(rgb)
|
|
|
|
print('hsl',hsl)
|
|
|
|
assert np.allclose(Colormap._hsl2rgb(hsl),rgb)
|
2020-06-27 19:31:16 +05:30
|
|
|
|
|
|
|
# rgb2xyz2rgb
|
|
|
|
xyz = Colormap._rgb2xyz(rgb)
|
|
|
|
print('xyz',xyz)
|
|
|
|
assert np.allclose(Colormap._xyz2rgb(xyz),rgb,atol=1.e-6,rtol=0)
|
|
|
|
|
|
|
|
# xyz2lab2xyz
|
|
|
|
lab = Colormap._xyz2lab(xyz)
|
|
|
|
print('lab',lab)
|
|
|
|
assert np.allclose(Colormap._lab2xyz(lab),xyz)
|
|
|
|
|
|
|
|
# lab2msh2lab
|
2020-06-27 22:25:27 +05:30
|
|
|
msh = Colormap._lab2msh(lab)
|
|
|
|
print('msh',msh)
|
|
|
|
assert np.allclose(Colormap._msh2lab(msh),lab)
|
|
|
|
|
|
|
|
# lab2rgb2lab
|
|
|
|
assert np.allclose(Colormap._rgb2lab(Colormap._lab2rgb(lab)),lab,atol=1.e-6,rtol=0)
|
|
|
|
|
|
|
|
# rgb2msh2rgb
|
|
|
|
assert np.allclose(Colormap._msh2rgb(Colormap._rgb2msh(rgb)),rgb,atol=1.e-6,rtol=0)
|
|
|
|
|
|
|
|
# hsv2msh
|
|
|
|
assert np.allclose(Colormap._hsv2msh(hsv),msh,atol=1.e-6,rtol=0)
|
|
|
|
|
|
|
|
# hsl2msh
|
|
|
|
assert np.allclose(Colormap._hsv2msh(hsv),msh,atol=1.e-6,rtol=0)
|
|
|
|
|
|
|
|
# xyz2msh
|
|
|
|
assert np.allclose(Colormap._xyz2msh(xyz),msh,atol=1.e-6,rtol=0)
|
2020-06-27 23:13:35 +05:30
|
|
|
|
2021-11-25 23:44:34 +05:30
|
|
|
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)
|
2022-01-15 17:52:15 +05:30
|
|
|
assert not Colormap.from_predefined('strain',N=128) == 1
|
2021-11-25 23:44:34 +05:30
|
|
|
|
2021-11-23 22:29:56 +05:30
|
|
|
@pytest.mark.parametrize('low,high',[((0,0,0),(1,1,1)),
|
2021-11-24 00:12:10 +05:30
|
|
|
([0,0,0],[1,1,1])])
|
2021-11-23 22:29:56 +05:30
|
|
|
def test_from_range_types(self,low,high):
|
2021-11-25 23:44:34 +05:30
|
|
|
assert Colormap.from_range(low,high) == Colormap.from_range(np.array(low),np.array(high))
|
2020-06-27 23:13:35 +05:30
|
|
|
|
2020-09-15 10:28:06 +05:30
|
|
|
@pytest.mark.parametrize('format',['ASCII','paraview','GOM','gmsh'])
|
2020-06-27 23:13:35 +05:30
|
|
|
@pytest.mark.parametrize('model',['rgb','hsv','hsl','xyz','lab','msh'])
|
2020-09-29 22:55:50 +05:30
|
|
|
def test_from_range(self,model,format,tmp_path):
|
2020-06-27 23:13:35 +05:30
|
|
|
N = np.random.randint(2,256)
|
2020-09-15 10:28:06 +05:30
|
|
|
c = Colormap.from_range(np.random.rand(3),np.random.rand(3),model=model,N=N) # noqa
|
2020-09-29 22:55:50 +05:30
|
|
|
eval(f'c.save_{format}(tmp_path/"color_out")')
|
2020-06-27 23:13:35 +05:30
|
|
|
|
2020-09-15 10:28:06 +05:30
|
|
|
@pytest.mark.parametrize('format',['ASCII','paraview','GOM','gmsh'])
|
2020-06-27 23:13:35 +05:30
|
|
|
@pytest.mark.parametrize('name',['strain','gnuplot','Greys','PRGn','viridis'])
|
2020-09-29 22:55:50 +05:30
|
|
|
def test_from_predefined(self,name,format,tmp_path):
|
2020-06-27 23:13:35 +05:30
|
|
|
N = np.random.randint(2,256)
|
2020-09-15 10:28:06 +05:30
|
|
|
c = Colormap.from_predefined(name,N) # noqa
|
2020-09-29 22:55:50 +05:30
|
|
|
os.chdir(tmp_path)
|
2020-09-15 10:28:06 +05:30
|
|
|
eval(f'c.save_{format}()')
|
2020-06-27 23:13:35 +05:30
|
|
|
|
|
|
|
@pytest.mark.parametrize('format,name',[('ASCII','test.txt'),
|
|
|
|
('paraview','test.json'),
|
|
|
|
('GOM','test.legend'),
|
2020-09-15 10:28:06 +05:30
|
|
|
('gmsh','test.msh')
|
2020-06-27 23:13:35 +05:30
|
|
|
])
|
2020-09-29 22:55:50 +05:30
|
|
|
def test_write_filehandle(self,format,name,tmp_path):
|
2020-09-15 10:28:06 +05:30
|
|
|
c = Colormap.from_predefined('Dark2') # noqa
|
2020-09-29 22:55:50 +05:30
|
|
|
fname = tmp_path/name
|
2020-09-15 10:28:06 +05:30
|
|
|
with open(fname,'w') as f: # noqa
|
|
|
|
eval(f'c.save_{format}(f)')
|
2020-06-28 00:56:25 +05:30
|
|
|
for i in range(10):
|
|
|
|
if fname.exists(): return
|
|
|
|
time.sleep(.5)
|
|
|
|
assert False
|
2020-06-27 23:13:35 +05:30
|
|
|
|
|
|
|
@pytest.mark.parametrize('model',['rgb','hsv','hsl','lab','invalid'])
|
|
|
|
def test_invalid_color(self,model):
|
|
|
|
with pytest.raises(ValueError):
|
2020-06-30 07:53:24 +05:30
|
|
|
c = Colormap.from_range(-2.+np.random.rand(3),np.random.rand(3),N=10,model=model) # noqa
|
2020-06-27 23:13:35 +05:30
|
|
|
|
|
|
|
def test_reversed(self):
|
|
|
|
c_1 = Colormap.from_predefined('stress')
|
|
|
|
c_2 = c_1.reversed()
|
|
|
|
assert (not np.allclose(c_1.colors,c_2.colors)) and \
|
2020-09-15 10:28:06 +05:30
|
|
|
np.allclose(c_1.colors,c_2.reversed().colors)
|
2020-06-27 23:13:35 +05:30
|
|
|
|
2020-07-02 19:56:28 +05:30
|
|
|
def test_invert(self):
|
|
|
|
c_1 = Colormap.from_predefined('strain')
|
|
|
|
c_2 = ~c_1
|
2020-09-15 10:28:06 +05:30
|
|
|
assert (not np.allclose(c_1.colors, c_2.colors)) and \
|
|
|
|
np.allclose(c_1.colors,(~c_2).colors)
|
2020-07-02 19:56:28 +05:30
|
|
|
|
|
|
|
def test_add(self):
|
|
|
|
c = Colormap.from_predefined('jet')
|
|
|
|
c += c
|
2020-07-02 20:07:57 +05:30
|
|
|
assert (np.allclose(c.colors[:len(c.colors)//2],c.colors[len(c.colors)//2:]))
|
2020-07-02 19:56:28 +05:30
|
|
|
|
2022-01-24 16:23:33 +05:30
|
|
|
def test_mul(self):
|
|
|
|
c = o = Colormap.from_predefined('jet')
|
|
|
|
o *= 2
|
|
|
|
assert c+c == o
|
|
|
|
|
2021-12-14 05:37:18 +05:30
|
|
|
@pytest.mark.parametrize('N,cmap,at,result',[
|
|
|
|
(8,'gray',0.5,[0.5,0.5,0.5]),
|
|
|
|
(17,'gray',0.5,[0.5,0.5,0.5]),
|
|
|
|
(17,'gray',[0.5,0.75],[[0.5,0.5,0.5],[0.75,0.75,0.75]]),
|
2021-12-10 23:22:44 +05:30
|
|
|
])
|
2021-12-14 05:37:18 +05:30
|
|
|
def test_at_value(self, N, cmap, at, result):
|
|
|
|
assert np.allclose(Colormap.from_predefined(cmap,N=N).at(at)[...,:3],
|
2021-12-10 23:22:44 +05:30
|
|
|
result,
|
|
|
|
rtol=0.005)
|
|
|
|
|
2020-08-05 00:04:40 +05:30
|
|
|
@pytest.mark.parametrize('bounds',[None,[2,10]])
|
2023-04-24 10:41:14 +05:30
|
|
|
def test_shade(self,res_path,update,bounds):
|
2020-08-04 23:44:04 +05:30
|
|
|
data = np.add(*np.indices((10, 11)))
|
2020-08-05 00:04:40 +05:30
|
|
|
img_current = Colormap.from_predefined('orientation').shade(data,bounds=bounds)
|
2020-08-04 23:44:04 +05:30
|
|
|
if update:
|
2023-04-24 10:41:14 +05:30
|
|
|
img_current.save(res_path/f'shade_{bounds}.png')
|
2020-08-04 23:44:04 +05:30
|
|
|
else:
|
2023-04-24 10:41:14 +05:30
|
|
|
img_reference = Image.open(res_path/f'shade_{bounds}.png')
|
2020-08-04 23:44:04 +05:30
|
|
|
diff = ImageChops.difference(img_reference.convert('RGB'),img_current.convert('RGB'))
|
|
|
|
assert not diff.getbbox()
|
|
|
|
|
2020-11-16 20:53:01 +05:30
|
|
|
def test_predefined(self):
|
|
|
|
assert (isinstance(Colormap.predefined,dict))
|
2020-06-27 23:13:35 +05:30
|
|
|
|
2020-06-28 14:50:09 +05:30
|
|
|
@pytest.mark.parametrize('format,ext',[('ASCII','.txt'),
|
|
|
|
('paraview','.json'),
|
|
|
|
('GOM','.legend'),
|
2020-09-15 10:28:06 +05:30
|
|
|
('gmsh','.msh')
|
2020-06-28 14:50:09 +05:30
|
|
|
])
|
2023-04-24 10:41:14 +05:30
|
|
|
def test_compare_reference(self,format,ext,tmp_path,res_path,update):
|
2020-06-28 14:50:09 +05:30
|
|
|
name = 'binary'
|
2020-09-15 10:28:06 +05:30
|
|
|
c = Colormap.from_predefined(name) # noqa
|
2020-06-28 14:50:09 +05:30
|
|
|
if update:
|
2023-04-24 10:41:14 +05:30
|
|
|
os.chdir(res_path)
|
2020-09-15 10:28:06 +05:30
|
|
|
eval(f'c.save_{format}()')
|
2020-06-28 14:50:09 +05:30
|
|
|
else:
|
2020-09-29 22:55:50 +05:30
|
|
|
os.chdir(tmp_path)
|
2020-09-15 10:28:06 +05:30
|
|
|
eval(f'c.save_{format}()')
|
2020-06-28 14:50:09 +05:30
|
|
|
time.sleep(.5)
|
2023-04-24 10:41:14 +05:30
|
|
|
assert filecmp.cmp(tmp_path/(name+ext),res_path/(name+ext))
|