polishing + simple test

autoscaling seems to be broken
This commit is contained in:
Martin Diehl 2020-08-04 20:14:04 +02:00
parent 84551c9d1e
commit e07c00a592
3 changed files with 22 additions and 10 deletions

View File

@ -8,7 +8,6 @@ if os.name == 'posix' and 'DISPLAY' not in os.environ:
mpl.use('Agg') mpl.use('Agg')
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
from matplotlib import cm from matplotlib import cm
from PIL import Image from PIL import Image
import damask import damask
@ -169,11 +168,11 @@ class Colormap(mpl.colors.ListedColormap):
Parameters Parameters
---------- ----------
field : numpy 2D array field : np.array of shape(:,:)
Data to be shaded. Data to be shaded.
bounds : array, optional bounds : iterable of len(2), optional
Lower and upper bound of value range. Lower and upper bound of value range.
gap : scalar, optional gap : field.dtype, optional
Transparent value. NaN will always be rendered transparent. Transparent value. NaN will always be rendered transparent.
Returns Returns
@ -184,16 +183,17 @@ class Colormap(mpl.colors.ListedColormap):
""" """
N = len(self.colors) N = len(self.colors)
mask = np.logical_not(np.isnan(field) if gap is None else \ 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: if bounds is None:
bounds = [field[mask].min(), hi,lo = field[mask].min(),field[mask].max()
field[mask].max()] else:
hi,lo = max(bounds),min(bounds) hi,lo = bounds[::-1]
delta,avg = hi-lo,0.5*(hi+lo) delta,avg = hi-lo,0.5*(hi+lo)
if delta * 1e8 <= avg: # delta around numerical noise 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 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], 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') mask.astype(float)))*255).astype(np.uint8), 'RGBA')

Binary file not shown.

After

Width:  |  Height:  |  Size: 143 B

View File

@ -4,6 +4,8 @@ import time
import numpy as np import numpy as np
import pytest import pytest
from PIL import Image
from PIL import ImageChops
import damask import damask
from damask import Colormap from damask import Colormap
@ -129,6 +131,16 @@ class TestColormap:
c += c c += c
assert (np.allclose(c.colors[:len(c.colors)//2],c.colors[len(c.colors)//2:])) 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): def test_list(self):
Colormap.list_predefined() Colormap.list_predefined()