diff --git a/python/damask/_colormap.py b/python/damask/_colormap.py index 8a31ceb3c..8bcb076d4 100644 --- a/python/damask/_colormap.py +++ b/python/damask/_colormap.py @@ -8,7 +8,6 @@ if os.name == 'posix' and 'DISPLAY' not in os.environ: mpl.use('Agg') import matplotlib.pyplot as plt from matplotlib import cm - from PIL import Image import damask @@ -169,11 +168,11 @@ class Colormap(mpl.colors.ListedColormap): Parameters ---------- - field : numpy 2D array + field : np.array of shape(:,:) Data to be shaded. - bounds : array, optional + bounds : iterable of len(2), optional Lower and upper bound of value range. - gap : scalar, optional + gap : field.dtype, optional Transparent value. NaN will always be rendered transparent. Returns @@ -184,16 +183,17 @@ class Colormap(mpl.colors.ListedColormap): """ N = len(self.colors) mask = np.logical_not(np.isnan(field) if gap is None else \ - np.logical_or (np.isnan(field), field == gap)) # mask gap and NaN (if gap present) + np.logical_or (np.isnan(field), field == gap)) # mask gap and NaN (if gap present) if bounds is None: - bounds = [field[mask].min(), - field[mask].max()] - hi,lo = max(bounds),min(bounds) + hi,lo = field[mask].min(),field[mask].max() + else: + hi,lo = bounds[::-1] + delta,avg = hi-lo,0.5*(hi+lo) - if delta * 1e8 <= avg: # delta around numerical noise - hi,lo = hi+0.5*avg,lo-0.5*avg # extend range to have actual data centered within + if delta * 1e8 <= avg: # delta around numerical noise + hi,lo = hi+0.5*avg,lo-0.5*avg # extend range to have actual data centered within return Image.fromarray((np.dstack((self.colors[(np.clip((field-lo)/(hi-lo),0.0,1.0)*(N-1)).astype(np.uint8),:3], mask.astype(float)))*255).astype(np.uint8), 'RGBA') diff --git a/python/tests/reference/Colormap/shade.png b/python/tests/reference/Colormap/shade.png new file mode 100644 index 000000000..a9e5eacea Binary files /dev/null and b/python/tests/reference/Colormap/shade.png differ diff --git a/python/tests/test_Colormap.py b/python/tests/test_Colormap.py index fbe65e2bd..8eebf63ee 100644 --- a/python/tests/test_Colormap.py +++ b/python/tests/test_Colormap.py @@ -4,6 +4,8 @@ import time import numpy as np import pytest +from PIL import Image +from PIL import ImageChops import damask from damask import Colormap @@ -129,6 +131,16 @@ class TestColormap: c += c assert (np.allclose(c.colors[:len(c.colors)//2],c.colors[len(c.colors)//2:])) + def test_shade(self,reference_dir,update): + data = np.add(*np.indices((10, 11))) + img_current = Colormap.from_predefined('orientation').shade(data) + if update: + img_current.save(reference_dir/'shade.png') + else: + img_reference = Image.open(reference_dir/'shade.png') + diff = ImageChops.difference(img_reference.convert('RGB'),img_current.convert('RGB')) + assert not diff.getbbox() + def test_list(self): Colormap.list_predefined()